-
Notifications
You must be signed in to change notification settings - Fork 68
/
public_key.rs
142 lines (119 loc) · 4.11 KB
/
public_key.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! Extended public keys
use crate::{
ChildNumber, Error, ExtendedKey, ExtendedKeyAttrs, ExtendedPrivateKey, HmacSha512,
KeyFingerprint, Prefix, PrivateKey, PublicKey, PublicKeyBytes, Result, KEY_SIZE,
};
use core::str::FromStr;
use hmac::Mac;
#[cfg(feature = "alloc")]
use alloc::string::{String, ToString};
/// Extended public secp256k1 ECDSA verification key.
#[cfg(feature = "secp256k1")]
#[cfg_attr(docsrs, doc(cfg(feature = "secp256k1")))]
pub type XPub = ExtendedPublicKey<k256::ecdsa::VerifyingKey>;
/// Extended public keys derived using BIP32.
///
/// Generic around a [`PublicKey`] type. When the `secp256k1` feature of this
/// crate is enabled, the [`XPub`] type provides a convenient alias for
/// extended ECDSA/secp256k1 public keys.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct ExtendedPublicKey<K: PublicKey> {
/// Derived public key
public_key: K,
/// Extended key attributes.
attrs: ExtendedKeyAttrs,
}
impl<K> ExtendedPublicKey<K>
where
K: PublicKey,
{
/// Obtain the non-extended public key value `K`.
pub fn public_key(&self) -> &K {
&self.public_key
}
/// Get attributes for this key such as depth, parent fingerprint,
/// child number, and chain code.
pub fn attrs(&self) -> &ExtendedKeyAttrs {
&self.attrs
}
/// Compute a 4-byte key fingerprint for this extended public key.
pub fn fingerprint(&self) -> KeyFingerprint {
self.public_key().fingerprint()
}
/// Derive a child key for a particular [`ChildNumber`].
pub fn derive_child(&self, child_number: ChildNumber) -> Result<Self> {
if child_number.is_hardened() {
// Cannot derive child public keys for hardened `ChildNumber`s
return Err(Error::ChildNumber);
}
let depth = self.attrs.depth.checked_add(1).ok_or(Error::Depth)?;
let mut hmac =
HmacSha512::new_from_slice(&self.attrs.chain_code).map_err(|_| Error::Crypto)?;
hmac.update(&self.public_key.to_bytes());
hmac.update(&child_number.to_bytes());
let result = hmac.finalize().into_bytes();
let (child_key, chain_code) = result.split_at(KEY_SIZE);
let public_key = self.public_key.derive_child(child_key.try_into()?)?;
let attrs = ExtendedKeyAttrs {
parent_fingerprint: self.public_key.fingerprint(),
child_number,
chain_code: chain_code.try_into()?,
depth,
};
Ok(ExtendedPublicKey { public_key, attrs })
}
/// Serialize the raw public key as a byte array (e.g. SEC1-encoded).
pub fn to_bytes(&self) -> PublicKeyBytes {
self.public_key.to_bytes()
}
/// Serialize this key as an [`ExtendedKey`].
pub fn to_extended_key(&self, prefix: Prefix) -> ExtendedKey {
ExtendedKey {
prefix,
attrs: self.attrs.clone(),
key_bytes: self.to_bytes(),
}
}
/// Serialize this key as a `String`.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn to_string(&self, prefix: Prefix) -> String {
self.to_extended_key(prefix).to_string()
}
}
impl<K> From<&ExtendedPrivateKey<K>> for ExtendedPublicKey<K::PublicKey>
where
K: PrivateKey,
{
fn from(xprv: &ExtendedPrivateKey<K>) -> ExtendedPublicKey<K::PublicKey> {
ExtendedPublicKey {
public_key: xprv.private_key().public_key(),
attrs: xprv.attrs().clone(),
}
}
}
impl<K> FromStr for ExtendedPublicKey<K>
where
K: PublicKey,
{
type Err = Error;
fn from_str(xpub: &str) -> Result<Self> {
ExtendedKey::from_str(xpub)?.try_into()
}
}
impl<K> TryFrom<ExtendedKey> for ExtendedPublicKey<K>
where
K: PublicKey,
{
type Error = Error;
fn try_from(extended_key: ExtendedKey) -> Result<ExtendedPublicKey<K>> {
if extended_key.prefix.is_public() {
Ok(ExtendedPublicKey {
public_key: PublicKey::from_bytes(extended_key.key_bytes)?,
attrs: extended_key.attrs.clone(),
})
} else {
Err(Error::Crypto)
}
}
}