pub struct PublicKey(/* private fields */);
Expand description
Represents a secp256k1 public key.
const PRIVATE_KEY: [u8; 32] = hex!("e17fe86ce6e99f4806715b0c9412f8dad89334bf07f72d5834207a9d8f19d7f8");
// compressed public keys start with `0x02` or `0x03``, depending on sign of y-component
const COMPRESSED: [u8; 33] = hex!("021464586aeaea0eb5736884ca1bf42d165fc8e2243b1d917130fb9e321d7a93b8");
// full public key without prefix
const UNCOMPRESSED_PLAIN: [u8; 64] = hex!("1464586aeaea0eb5736884ca1bf42d165fc8e2243b1d917130fb9e321d7a93b8fb0699d4f177f9c84712f6d7c5f6b7f4f6916116047fa25c79ef806fc6c9523e");
// uncompressed public keys use `0x04` prefix
const UNCOMPRESSED: [u8; 65] = hex!("041464586aeaea0eb5736884ca1bf42d165fc8e2243b1d917130fb9e321d7a93b8fb0699d4f177f9c84712f6d7c5f6b7f4f6916116047fa25c79ef806fc6c9523e");
let from_privkey = PublicKey::from_privkey(&PRIVATE_KEY).unwrap();
let from_compressed = PublicKey::try_from(COMPRESSED.as_ref()).unwrap();
let from_uncompressed_plain = PublicKey::try_from(UNCOMPRESSED_PLAIN.as_ref()).unwrap();
let from_uncompressed = PublicKey::try_from(UNCOMPRESSED.as_ref()).unwrap();
assert_eq!(from_privkey, from_uncompressed);
assert_eq!(from_compressed, from_uncompressed_plain);
assert_eq!(from_uncompressed_plain, from_uncompressed);
// also works from a signed Ethereum transaction
const TX_HASH: [u8; 32] = hex!("eff80b9f035b1d369c6a60f362ac7c8b8c3b61b76d151d1be535145ccaa3e83e");
const R: [u8; 32] = hex!("c8048d137fbb10ddffa1e4ba5141c300fcd19e4fb7d0a4354ca62a7694e46f9b");
const S: [u8; 32] = hex!("5bb43e23d8b430f17ba3649e38b5a94d02815f0bcfaaf171800c52d4794c3136");
const V: u8 = 1u8;
let mut r_and_s = Vec::<u8>::with_capacity(64);
r_and_s.extend_from_slice(&R);
r_and_s.extend_from_slice(&S);
let sig = Signature::new(&r_and_s, V);
let from_transaction_signature = PublicKey::from_signature_hash(&TX_HASH, &sig).unwrap();
assert_eq!(from_uncompressed, from_transaction_signature);
Implementations§
Source§impl PublicKey
impl PublicKey
Sourcepub const SIZE_COMPRESSED: usize = 33usize
pub const SIZE_COMPRESSED: usize = 33usize
Size of the compressed public key in bytes
pub const SIZE_UNCOMPRESSED_PLAIN: usize = 64usize
Sourcepub const SIZE_UNCOMPRESSED: usize = 65usize
pub const SIZE_UNCOMPRESSED: usize = 65usize
Size of the uncompressed public key in bytes
pub fn from_privkey(private_key: &[u8]) -> Result<PublicKey>
pub fn from_signature(msg: &[u8], signature: &Signature) -> Result<PublicKey>
pub fn from_signature_hash( hash: &[u8], signature: &Signature, ) -> Result<PublicKey>
Sourcepub fn combine(summands: &[&PublicKey]) -> PublicKey
pub fn combine(summands: &[&PublicKey]) -> PublicKey
Sums all given public keys together, creating a new public key. Panics if reaches infinity (EC identity point), which is an invalid public key.
Sourcepub fn tweak_add(key: &PublicKey, tweak: &[u8]) -> PublicKey
pub fn tweak_add(key: &PublicKey, tweak: &[u8]) -> PublicKey
Adds the given public key with tweak
times secp256k1 generator, producing a new public key.
Panics if reaches infinity (EC identity point), which is an invalid public key.
Sourcepub fn random() -> Self
pub fn random() -> Self
Generates a new random public key. Because the corresponding private key is discarded, this might be useful only for testing purposes.
Sourcepub fn to_address(&self) -> Address
pub fn to_address(&self) -> Address
Converts the public key to an Ethereum address
Trait Implementations§
Source§impl From<&PublicKey> for CurvePoint
impl From<&PublicKey> for CurvePoint
Source§impl From<PublicKey> for CompressedPublicKey
impl From<PublicKey> for CompressedPublicKey
Source§impl From<PublicKey> for CurvePoint
impl From<PublicKey> for CurvePoint
Source§impl TryFrom<CurvePoint> for PublicKey
impl TryFrom<CurvePoint> for PublicKey
Source§type Error = CryptoError
type Error = CryptoError
The type returned in the event of a conversion error.
impl Eq for PublicKey
impl StructuralPartialEq for PublicKey
Auto Trait Implementations§
impl !Freeze for PublicKey
impl RefUnwindSafe for PublicKey
impl Send for PublicKey
impl Sync for PublicKey
impl Unpin for PublicKey
impl UnwindSafe for PublicKey
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more