pub struct Secp256k1PublicKey { /* private fields */ }Expand description
Wrapper for secp256k1 public keys.
Stores both compressed (33 bytes) and uncompressed (65 bytes) formats to avoid recomputation.
§Formats
- Compressed: 33 bytes, prefix
0x02or0x03+ 32-byte X coordinate - Uncompressed: 65 bytes, prefix
0x04+ 32-byte X + 32-byte Y
§Example
use txgate_crypto::keypair::{KeyPair, Secp256k1KeyPair};
let keypair = Secp256k1KeyPair::generate();
let pubkey = keypair.public_key();
// Get compressed format (33 bytes)
assert_eq!(pubkey.compressed().len(), 33);
// Get uncompressed format (65 bytes)
assert_eq!(pubkey.uncompressed().len(), 65);
// Get Ethereum address (20 bytes)
let address = pubkey.ethereum_address();
assert_eq!(address.len(), 20);Implementations§
Source§impl Secp256k1PublicKey
impl Secp256k1PublicKey
Sourcepub const fn compressed(&self) -> &[u8; 33]
pub const fn compressed(&self) -> &[u8; 33]
Get the compressed public key (33 bytes).
Format: prefix (1 byte) || X (32 bytes)
- Prefix is
0x02if Y is even,0x03if Y is odd
Sourcepub const fn uncompressed(&self) -> &[u8; 65]
pub const fn uncompressed(&self) -> &[u8; 65]
Get the uncompressed public key (65 bytes).
Format: 0x04 || X (32 bytes) || Y (32 bytes)
Sourcepub fn ethereum_address(&self) -> [u8; 20]
pub fn ethereum_address(&self) -> [u8; 20]
Derive the Ethereum address from this public key.
The Ethereum address is the last 20 bytes of the Keccak-256 hash
of the uncompressed public key (without the 0x04 prefix).
§Example
use txgate_crypto::keypair::{KeyPair, Secp256k1KeyPair};
let keypair = Secp256k1KeyPair::generate();
let address = keypair.public_key().ethereum_address();
assert_eq!(address.len(), 20);Trait Implementations§
Source§impl AsRef<[u8]> for Secp256k1PublicKey
impl AsRef<[u8]> for Secp256k1PublicKey
Source§impl Clone for Secp256k1PublicKey
impl Clone for Secp256k1PublicKey
Source§fn clone(&self) -> Secp256k1PublicKey
fn clone(&self) -> Secp256k1PublicKey
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for Secp256k1PublicKey
impl RefUnwindSafe for Secp256k1PublicKey
impl Send for Secp256k1PublicKey
impl Sync for Secp256k1PublicKey
impl Unpin for Secp256k1PublicKey
impl UnwindSafe for Secp256k1PublicKey
Blanket Implementations§
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
👎Deprecated: use
ToHexExt insteadEncode the hex strict representing
self into the result.
Lower case letters are used (e.g. f9b4ca).Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
👎Deprecated: use
ToHexExt insteadEncode the hex strict representing
self into the result.
Upper case letters are used (e.g. F9B4CA).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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
Encode the hex strict representing
self into the result. Lower case
letters are used (e.g. f9b4ca)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
Encode the hex strict representing
self into the result. Upper case
letters are used (e.g. F9B4CA)Source§impl<T> ToHexExt for T
impl<T> ToHexExt for T
Source§fn encode_hex(&self) -> String
fn encode_hex(&self) -> String
Encode the hex strict representing
self into the result.
Lower case letters are used (e.g. f9b4ca).Source§fn encode_hex_upper(&self) -> String
fn encode_hex_upper(&self) -> String
Encode the hex strict representing
self into the result.
Upper case letters are used (e.g. F9B4CA).Source§fn encode_hex_with_prefix(&self) -> String
fn encode_hex_with_prefix(&self) -> String
Encode the hex strict representing
self into the result with prefix 0x.
Lower case letters are used (e.g. 0xf9b4ca).Source§fn encode_hex_upper_with_prefix(&self) -> String
fn encode_hex_upper_with_prefix(&self) -> String
Encode the hex strict representing
self into the result with prefix 0X.
Upper case letters are used (e.g. 0xF9B4CA).