Struct PublicKey
pub struct PublicKey(/* private fields */);
Expand description
Public key - used to verify ECDSA signatures and to do Taproot tweaks.
§Serde support
Implements de/serialization with the serde
feature enabled. We treat the byte value as a tuple
of 33 u8
s for non-human-readable formats. This representation is optimal for for some formats
(e.g. bincode
) however other formats may be less optimal (e.g. cbor
).
§Examples
Basic usage:
use secp256k1::{SecretKey, Secp256k1, PublicKey};
let secp = Secp256k1::new();
let secret_key = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
Implementations§
§impl PublicKey
impl PublicKey
pub fn cmp_fast_unstable(&self, other: &PublicKey) -> Ordering
pub fn cmp_fast_unstable(&self, other: &PublicKey) -> Ordering
Like cmp::Cmp
but faster and with no guarantees across library versions.
The Cmp
implementation for FFI types is stable but slow because it first
serializes self
and other
before comparing them. This function provides a faster
comparison if you know that your types come from the same library version.
pub fn eq_fast_unstable(&self, other: &PublicKey) -> bool
pub fn eq_fast_unstable(&self, other: &PublicKey) -> bool
Like cmp::Eq
but faster and with no guarantees across library versions.
The Eq
implementation for FFI types is stable but slow because it first serializes
self
and other
before comparing them. This function provides a faster equality
check if you know that your types come from the same library version.
§impl PublicKey
impl PublicKey
pub fn as_ptr(&self) -> *const PublicKey
👎Deprecated since 0.25.0: Use Self::as_c_ptr if you need to access the FFI layer
pub fn as_ptr(&self) -> *const PublicKey
Obtains a raw const pointer suitable for use with FFI functions.
pub fn as_mut_ptr(&mut self) -> *mut PublicKey
👎Deprecated since 0.25.0: Use Self::as_mut_c_ptr if you need to access the FFI layer
pub fn as_mut_ptr(&mut self) -> *mut PublicKey
Obtains a raw mutable pointer suitable for use with FFI functions.
pub fn from_secret_key<C>(secp: &Secp256k1<C>, sk: &SecretKey) -> PublicKeywhere
C: Signing,
pub fn from_secret_key<C>(secp: &Secp256k1<C>, sk: &SecretKey) -> PublicKeywhere
C: Signing,
pub fn from_ellswift(ellswift: ElligatorSwift) -> PublicKey
pub fn from_ellswift(ellswift: ElligatorSwift) -> PublicKey
Creates a new public key from an ElligatorSwift
.
pub fn from_secret_key_global(sk: &SecretKey) -> PublicKey
pub fn from_secret_key_global(sk: &SecretKey) -> PublicKey
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error>
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error>
Creates a public key directly from a slice.
pub fn from_keypair(keypair: &Keypair) -> PublicKey
pub fn from_keypair(keypair: &Keypair) -> PublicKey
pub fn from_x_only_public_key(pk: XOnlyPublicKey, parity: Parity) -> PublicKey
pub fn from_x_only_public_key(pk: XOnlyPublicKey, parity: Parity) -> PublicKey
Creates a PublicKey
using the key material from pk
combined with the parity
.
pub fn serialize(&self) -> [u8; 33]
pub fn serialize(&self) -> [u8; 33]
Serializes the key as a byte-encoded pair of values. In compressed form the y-coordinate is represented by only a single bit, as x determines it up to one bit.
pub fn serialize_uncompressed(&self) -> [u8; 65]
pub fn serialize_uncompressed(&self) -> [u8; 65]
Serializes the key as a byte-encoded pair of values, in uncompressed form.
pub fn negate<C>(self, secp: &Secp256k1<C>) -> PublicKeywhere
C: Verification,
pub fn negate<C>(self, secp: &Secp256k1<C>) -> PublicKeywhere
C: Verification,
Negates the public key.
pub fn add_exp_tweak<C>(
self,
secp: &Secp256k1<C>,
tweak: &Scalar,
) -> Result<PublicKey, Error>where
C: Verification,
pub fn add_exp_tweak<C>(
self,
secp: &Secp256k1<C>,
tweak: &Scalar,
) -> Result<PublicKey, Error>where
C: Verification,
pub fn mul_tweak<C>(
self,
secp: &Secp256k1<C>,
other: &Scalar,
) -> Result<PublicKey, Error>where
C: Verification,
pub fn mul_tweak<C>(
self,
secp: &Secp256k1<C>,
other: &Scalar,
) -> Result<PublicKey, Error>where
C: Verification,
pub fn combine(&self, other: &PublicKey) -> Result<PublicKey, Error>
pub fn combine(&self, other: &PublicKey) -> Result<PublicKey, Error>
Adds a second key to this one, returning the sum.
§Errors
If the result would be the point at infinity, i.e. adding this point to its own negation.
§Examples
use secp256k1::{rand, Secp256k1};
let secp = Secp256k1::new();
let mut rng = rand::thread_rng();
let (_, pk1) = secp.generate_keypair(&mut rng);
let (_, pk2) = secp.generate_keypair(&mut rng);
let sum = pk1.combine(&pk2).expect("It's improbable to fail for 2 random public keys");
pub fn combine_keys(keys: &[&PublicKey]) -> Result<PublicKey, Error>
pub fn combine_keys(keys: &[&PublicKey]) -> Result<PublicKey, Error>
Adds the keys in the provided slice together, returning the sum.
§Errors
Errors under any of the following conditions:
- The result would be the point at infinity, i.e. adding a point to its own negation.
- The provided slice is empty.
- The number of elements in the provided slice is greater than
i32::MAX
.
§Examples
use secp256k1::{rand, Secp256k1, PublicKey};
let secp = Secp256k1::new();
let mut rng = rand::thread_rng();
let (_, pk1) = secp.generate_keypair(&mut rng);
let (_, pk2) = secp.generate_keypair(&mut rng);
let (_, pk3) = secp.generate_keypair(&mut rng);
let sum = PublicKey::combine_keys(&[&pk1, &pk2, &pk3]).expect("It's improbable to fail for 3 random public keys");
pub fn x_only_public_key(&self) -> (XOnlyPublicKey, Parity)
pub fn x_only_public_key(&self) -> (XOnlyPublicKey, Parity)
Returns the XOnlyPublicKey
(and it’s Parity
) for this PublicKey
.
Trait Implementations§
§impl CPtr for PublicKey
impl CPtr for PublicKey
This trait enables interaction with the FFI layer and even though it is part of the public API normal users should never need to directly interact with FFI types.
§impl Decodable for PublicKey
impl Decodable for PublicKey
§fn consensus_decode<D>(d: D) -> Result<PublicKey, Error>where
D: Read,
fn consensus_decode<D>(d: D) -> Result<PublicKey, Error>where
D: Read,
§impl<'de> Deserialize<'de> for PublicKey
impl<'de> Deserialize<'de> for PublicKey
§fn deserialize<D>(d: D) -> Result<PublicKey, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(d: D) -> Result<PublicKey, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl Encodable for PublicKey
impl Encodable for PublicKey
§fn consensus_encode<W>(&self, e: W) -> Result<usize, Error>where
W: Write,
fn consensus_encode<W>(&self, e: W) -> Result<usize, Error>where
W: Write,
Write
errors. Returns the number of bytes written on
success§impl From<PublicKey> for DelayedPaymentBasepoint
impl From<PublicKey> for DelayedPaymentBasepoint
§fn from(value: PublicKey) -> DelayedPaymentBasepoint
fn from(value: PublicKey) -> DelayedPaymentBasepoint
§impl From<PublicKey> for HtlcBasepoint
impl From<PublicKey> for HtlcBasepoint
§fn from(value: PublicKey) -> HtlcBasepoint
fn from(value: PublicKey) -> HtlcBasepoint
§impl From<PublicKey> for PublicKey
impl From<PublicKey> for PublicKey
Creates a new public key from a FFI public key.
Note, normal users should never need to interact directly with FFI types.
§impl From<PublicKey> for RevocationBasepoint
impl From<PublicKey> for RevocationBasepoint
§fn from(value: PublicKey) -> RevocationBasepoint
fn from(value: PublicKey) -> RevocationBasepoint
§impl From<PublicKey> for XOnlyPublicKey
impl From<PublicKey> for XOnlyPublicKey
§fn from(src: PublicKey) -> XOnlyPublicKey
fn from(src: PublicKey) -> XOnlyPublicKey
§impl MiniscriptKey for PublicKey
impl MiniscriptKey for PublicKey
§type Sha256 = Hash
type Sha256 = Hash
bitcoin::hashes::sha256::Hash
for this [MiniscriptKey
], used in the
sha256 fragment.§type Hash256 = Hash
type Hash256 = Hash
miniscript::hash256::Hash
] for this [MiniscriptKey
], used in the
hash256 fragment.§type Ripemd160 = Hash
type Ripemd160 = Hash
bitcoin::hashes::ripemd160::Hash
for this [MiniscriptKey
] type, used
in the ripemd160 fragment.§type Hash160 = Hash
type Hash160 = Hash
bitcoin::hashes::hash160::Hash
for this [MiniscriptKey
] type, used in
the hash160 fragment.§fn is_uncompressed(&self) -> bool
fn is_uncompressed(&self) -> bool
false
.§fn is_x_only_key(&self) -> bool
fn is_x_only_key(&self) -> bool
false
.§fn num_der_paths(&self) -> usize
fn num_der_paths(&self) -> usize
§impl Ord for PublicKey
impl Ord for PublicKey
§impl PartialOrd for PublicKey
impl PartialOrd for PublicKey
§impl Serialize for PublicKey
impl Serialize for PublicKey
§fn serialize<S>(
&self,
s: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
s: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
§impl ToPublicKey for PublicKey
impl ToPublicKey for PublicKey
§fn to_public_key(&self) -> PublicKey
fn to_public_key(&self) -> PublicKey
§fn to_sha256(hash: &Hash) -> Hash
fn to_sha256(hash: &Hash) -> Hash
MiniscriptKey::Sha256
] to sha256::Hash
§fn to_hash256(hash: &Hash) -> Hash
fn to_hash256(hash: &Hash) -> Hash
MiniscriptKey::Hash256
] to [hash256::Hash
]§fn to_ripemd160(hash: &Hash) -> Hash
fn to_ripemd160(hash: &Hash) -> Hash
MiniscriptKey::Ripemd160
] to ripemd160::Hash
§fn to_hash160(hash: &Hash) -> Hash
fn to_hash160(hash: &Hash) -> Hash
MiniscriptKey::Hash160
] to hash160::Hash
§fn to_x_only_pubkey(&self) -> XOnlyPublicKey
fn to_x_only_pubkey(&self) -> XOnlyPublicKey
§fn to_pubkeyhash(&self, sig_type: SigType) -> Hash
fn to_pubkeyhash(&self, sig_type: SigType) -> Hash
§impl Writeable for PublicKey
impl Writeable for PublicKey
impl Copy for PublicKey
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§
§impl<T> AnyEq for T
impl<T> AnyEq for T
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> FromStrKey for Twhere
T: MiniscriptKey + FromStr,
<T as MiniscriptKey>::Sha256: FromStr,
<T as MiniscriptKey>::Hash256: FromStr,
<T as MiniscriptKey>::Ripemd160: FromStr,
<T as MiniscriptKey>::Hash160: FromStr,
<T as FromStr>::Err: Debug + Display,
<<T as MiniscriptKey>::Sha256 as FromStr>::Err: Debug + Display,
<<T as MiniscriptKey>::Hash256 as FromStr>::Err: Debug + Display,
<<T as MiniscriptKey>::Ripemd160 as FromStr>::Err: Debug + Display,
<<T as MiniscriptKey>::Hash160 as FromStr>::Err: Debug + Display,
impl<T> FromStrKey for Twhere
T: MiniscriptKey + FromStr,
<T as MiniscriptKey>::Sha256: FromStr,
<T as MiniscriptKey>::Hash256: FromStr,
<T as MiniscriptKey>::Ripemd160: FromStr,
<T as MiniscriptKey>::Hash160: FromStr,
<T as FromStr>::Err: Debug + Display,
<<T as MiniscriptKey>::Sha256 as FromStr>::Err: Debug + Display,
<<T as MiniscriptKey>::Hash256 as FromStr>::Err: Debug + Display,
<<T as MiniscriptKey>::Ripemd160 as FromStr>::Err: Debug + Display,
<<T as MiniscriptKey>::Hash160 as FromStr>::Err: Debug + Display,
§type _Sha256FromStrErr = <<T as MiniscriptKey>::Sha256 as FromStr>::Err
type _Sha256FromStrErr = <<T as MiniscriptKey>::Sha256 as FromStr>::Err
§type _Hash256FromStrErr = <<T as MiniscriptKey>::Hash256 as FromStr>::Err
type _Hash256FromStrErr = <<T as MiniscriptKey>::Hash256 as FromStr>::Err
§type _Ripemd160 = <T as MiniscriptKey>::Ripemd160
type _Ripemd160 = <T as MiniscriptKey>::Ripemd160
§type _Ripemd160FromStrErr = <<T as MiniscriptKey>::Ripemd160 as FromStr>::Err
type _Ripemd160FromStrErr = <<T as MiniscriptKey>::Ripemd160 as FromStr>::Err
§type _Hash160FromStrErr = <<T as MiniscriptKey>::Hash160 as FromStr>::Err
type _Hash160FromStrErr = <<T as MiniscriptKey>::Hash160 as FromStr>::Err
§type _FromStrErr = <T as FromStr>::Err
type _FromStrErr = <T as FromStr>::Err
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request