Skip to main content

PublicKey

Struct PublicKey 

Source
pub struct PublicKey { /* private fields */ }
Expand description

A secp256k1 public key (a point on the curve).

Uses composition with Point, following Rust conventions. The TS SDK uses class inheritance (PublicKey extends Point); we mirror the public API names.

Implementations§

Source§

impl PublicKey

Source

pub fn from_point(point: Point) -> Self

Create a PublicKey from a Point.

Source

pub fn from_private_key(key: &PrivateKey) -> Self

Derive a public key from a private key.

Source

pub fn from_string(s: &str) -> Result<Self, PrimitivesError>

Parse a public key from a hex string (compressed or uncompressed DER).

Compressed: 33 bytes (66 hex chars), starts with 02 or 03 Uncompressed: 65 bytes (130 hex chars), starts with 04

Source

pub fn from_der_bytes(bytes: &[u8]) -> Result<Self, PrimitivesError>

Parse a public key from DER-encoded bytes.

Source

pub fn to_der(&self) -> Vec<u8>

Encode the public key in compressed DER format (33 bytes).

Format: prefix(1) || x(32) prefix = 0x02 if y is even, 0x03 if y is odd

Source

pub fn to_der_hex(&self) -> String

Encode the public key in compressed DER format as a hex string.

Source

pub fn to_der_uncompressed(&self) -> Vec<u8>

Encode the public key in uncompressed DER format (65 bytes).

Format: 0x04 || x(32) || y(32)

Source

pub fn to_hash(&self) -> Vec<u8>

Hash the compressed public key with hash160 (RIPEMD-160(SHA-256)).

Returns 20 bytes – the public key hash used in P2PKH addresses.

Source

pub fn to_address(&self, prefix: &[u8]) -> String

Derive a P2PKH Bitcoin address from this public key.

Format: Base58Check(prefix || hash160(compressed_der)) Default prefix [0x00] for mainnet.

Source

pub fn verify(&self, message: &[u8], signature: &Signature) -> bool

Verify a message signature using this public key.

The message is hashed with SHA-256 before verification.

Source

pub fn derive_shared_secret( &self, private_key: &PrivateKey, ) -> Result<Point, PrimitivesError>

Compute ECDH shared secret: private_key.bn * self.point.

Returns the resulting point on the curve.

Source

pub fn derive_child( &self, private_key: &PrivateKey, invoice_number: &str, ) -> Result<PublicKey, PrimitivesError>

Derive a child public key using Type-42 key derivation (BRC-42).

Computes: child_point = self.point + G * HMAC-SHA256(shared_secret_compressed, invoice_number) where shared_secret = private_key * self.

Source

pub fn point(&self) -> &Point

Access the underlying Point.

Trait Implementations§

Source§

impl Clone for PublicKey

Source§

fn clone(&self) -> PublicKey

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PublicKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for PublicKey

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.