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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Version bytes for extended key payloads.
use core::fmt;
/// Extended key version bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Version {
/// Public key version bytes.
Public(u32),
/// Private key version bytes.
Private(u32),
}
impl Version {
/// Creates a public version from raw version bytes.
pub const fn public(value: u32) -> Self {
Self::Public(value)
}
/// Creates a private version from raw version bytes.
pub const fn private(value: u32) -> Self {
Self::Private(value)
}
/// Creates a public version from big-endian bytes.
pub const fn from_public_bytes(bytes: [u8; 4]) -> Self {
Self::Public(u32::from_be_bytes(bytes))
}
/// Creates a private version from big-endian bytes.
pub const fn from_private_bytes(bytes: [u8; 4]) -> Self {
Self::Private(u32::from_be_bytes(bytes))
}
/// Returns the raw version bytes.
pub const fn as_u32(self) -> u32 {
match self {
Version::Public(value) | Version::Private(value) => value,
}
}
/// Returns the big-endian version bytes.
pub const fn to_bytes(self) -> [u8; 4] {
self.as_u32().to_be_bytes()
}
/// Returns whether this version is public.
pub const fn is_public(self) -> bool {
matches!(self, Version::Public(_))
}
/// Returns whether this version is private.
pub const fn is_private(self) -> bool {
matches!(self, Version::Private(_))
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{:08X}", self.as_u32())
}
}
impl Version {
// ========================================================================
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
// https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
// https://github.com/bitcoin/bips/blob/master/bip-0086.mediawiki
// ========================================================================
/// Bitcoin mainnet **P2PKH** or **P2SH** public key version.
pub const XPUB: Version = Version::Public(0x0488_B21E);
/// Bitcoin mainnet **P2PKH** or **P2SH** private key version.
pub const XPRV: Version = Version::Private(0x0488_ADE4);
/// Bitcoin testnet **P2PKH** or **P2SH** public key version.
pub const TPUB: Version = Version::Public(0x0435_87CF);
/// Bitcoin testnet **P2PKH** or **P2SH** private key version.
pub const TPRV: Version = Version::Private(0x0435_8394);
// ========================================================================
// ========================================================================
// https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki
// ========================================================================
/// Bitcoin mainnet **P2SH-P2WPKH** public key version.
pub const YPUB: Version = Version::Public(0x049D_7CB2);
/// Bitcoin mainnet **P2SH-P2WPKH** private key version.
pub const YPRV: Version = Version::Private(0x049D_7878);
/// Bitcoin mainnet multi-signature **P2SH-P2WSH** public key version.
pub const YPUB_SHWSH: Version = Version::Public(0x0295_B43F);
/// Bitcoin mainnet multi-signature **P2SH-P2WSH** private key version.
pub const YPRV_SHWSH: Version = Version::Private(0x0295_B005);
/// Bitcoin testnet **P2SH-P2WPKH** public key version.
pub const UPUB: Version = Version::Public(0x044A_5262);
/// Bitcoin testnet **P2SH-P2WPKH** private key version.
pub const UPRV: Version = Version::Private(0x044A_4E28);
/// Bitcoin testnet multi-signature **P2SH-P2WSH** public key version.
pub const UPUB_SHWSH: Version = Version::Public(0x0242_89EF);
/// Bitcoin testnet multi-signature **P2SH-P2WSH** private key version.
pub const UPRV_SHWSH: Version = Version::Private(0x0242_85B5);
// ========================================================================
// ========================================================================
// https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki
// ========================================================================
/// Bitcoin mainnet **P2WPKH** public key version.
pub const ZPUB: Version = Version::Public(0x04B2_4746);
/// Bitcoin mainnet **P2WPKH** private key version.
pub const ZPRV: Version = Version::Private(0x04B2_430C);
/// Bitcoin mainnet multi-signature **P2WSH** public key version.
pub const ZPUB_WSH: Version = Version::Public(0x02AA_7ED3);
/// Bitcoin mainnet multi-signature **P2WSH** private key version.
pub const ZPRV_WSH: Version = Version::Private(0x02AA_7A99);
/// Bitcoin testnet **P2WPKH** public key version.
pub const VPUB: Version = Version::Public(0x045F_1CF6);
/// Bitcoin testnet **P2WPKH** private key version.
pub const VPRV: Version = Version::Private(0x045F_18BC);
/// Bitcoin testnet multi-signature **P2WSH** public key version.
pub const VPUB_WSH: Version = Version::Public(0x0257_5483);
/// Bitcoin testnet multi-signature **P2WSH** private key version.
pub const VPRV_WSH: Version = Version::Private(0x0257_5048);
// ========================================================================
/// Returns the known version descriptor for this version, if any.
pub const fn into_known_version(self) -> Option<KnownVersion> {
Some(match self {
Self::XPUB => KnownVersion::Xpub,
Self::XPRV => KnownVersion::Xprv,
Self::TPUB => KnownVersion::Tpub,
Self::TPRV => KnownVersion::Tprv,
Self::YPUB => KnownVersion::Ypub,
Self::YPRV => KnownVersion::Yprv,
Self::YPUB_SHWSH => KnownVersion::YpubShWsh,
Self::YPRV_SHWSH => KnownVersion::YprvShWsh,
Self::UPUB => KnownVersion::Upub,
Self::UPRV => KnownVersion::Uprv,
Self::UPRV_SHWSH => KnownVersion::UprvShWsh,
Self::UPUB_SHWSH => KnownVersion::UpubShWsh,
Self::ZPUB => KnownVersion::Zpub,
Self::ZPRV => KnownVersion::Zprv,
Self::ZPUB_WSH => KnownVersion::ZpubWsh,
Self::ZPRV_WSH => KnownVersion::ZprvWsh,
Self::VPUB => KnownVersion::Vpub,
Self::VPRV => KnownVersion::Vprv,
Self::VPUB_WSH => KnownVersion::VpubWsh,
Self::VPRV_WSH => KnownVersion::VprvWsh,
_ => return None,
})
}
}
/// Standard extended key versions (BIP32 and common extensions).
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum KnownVersion {
/// Bitcoin mainnet **P2PKH** or **P2SH** public key version.
Xpub,
/// Bitcoin mainnet **P2PKH** or **P2SH** private key version.
Xprv,
/// Bitcoin testnet **P2PKH** or **P2SH** public key version.
Tpub,
/// Bitcoin testnet **P2PKH** or **P2SH** private key version.
Tprv,
/// Bitcoin mainnet **P2SH-P2WPKH** public key version.
Ypub,
/// Bitcoin mainnet **P2SH-P2WPKH** private key version.
Yprv,
/// Bitcoin mainnet multi-signature **P2SH-P2WSH** public key version.
YpubShWsh,
/// Bitcoin mainnet multi-signature **P2SH-P2WSH** private key version.
YprvShWsh,
/// Bitcoin testnet **P2SH-P2WPKH** public key version.
Upub,
/// Bitcoin testnet **P2SH-P2WPKH** private key version.
Uprv,
/// Bitcoin testnet multi-signature **P2SH-P2WSH** public key version.
UpubShWsh,
/// Bitcoin testnet multi-signature **P2SH-P2WSH** private key version.
UprvShWsh,
/// Bitcoin mainnet **P2WPKH** public key version.
Zpub,
/// Bitcoin mainnet **P2WPKH** private key version.
Zprv,
/// Bitcoin mainnet multi-signature **P2WSH** public key version.
ZpubWsh,
/// Bitcoin mainnet multi-signature **P2WSH** private key version.
ZprvWsh,
/// Bitcoin testnet **P2WPKH** public key version.
Vpub,
/// Bitcoin testnet **P2WPKH** private key version.
Vprv,
/// Bitcoin testnet multi-signature **P2WSH** public key version.
VpubWsh,
/// Bitcoin testnet multi-signature **P2WSH** private key version.
VprvWsh,
}
impl KnownVersion {
/// Returns the version bytes for this known version.
pub const fn into_version(self) -> Version {
match self {
Self::Xpub => Version::XPUB,
Self::Xprv => Version::XPRV,
Self::Tpub => Version::TPUB,
Self::Tprv => Version::TPRV,
Self::Ypub => Version::YPUB,
Self::Yprv => Version::YPRV,
Self::YpubShWsh => Version::YPUB_SHWSH,
Self::YprvShWsh => Version::YPRV_SHWSH,
Self::Upub => Version::UPUB,
Self::Uprv => Version::UPRV,
Self::UpubShWsh => Version::UPUB_SHWSH,
Self::UprvShWsh => Version::UPRV_SHWSH,
Self::Zpub => Version::ZPUB,
Self::Zprv => Version::ZPRV,
Self::ZpubWsh => Version::ZPUB_WSH,
Self::ZprvWsh => Version::ZPRV_WSH,
Self::Vpub => Version::VPUB,
Self::Vprv => Version::VPRV,
Self::VpubWsh => Version::VPUB_WSH,
Self::VprvWsh => Version::VPRV_WSH,
}
}
/// Returns the known version descriptor for raw version bytes, if any.
pub fn from_raw(raw: u32) -> Option<Self> {
Version::Public(raw)
.into_known_version()
.or_else(|| Version::Private(raw).into_known_version())
}
}
#[cfg(test)]
mod tests {
use super::{KnownVersion, Version};
const CASES: &[(KnownVersion, Version)] = &[
// ========================================================================
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
// https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
// https://github.com/bitcoin/bips/blob/master/bip-0086.mediawiki
// ========================================================================
(KnownVersion::Xpub, Version::XPUB),
(KnownVersion::Xprv, Version::XPRV),
(KnownVersion::Tpub, Version::TPUB),
(KnownVersion::Tprv, Version::TPRV),
// ========================================================================
// ========================================================================
// https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki
// ========================================================================
(KnownVersion::Ypub, Version::YPUB),
(KnownVersion::Yprv, Version::YPRV),
(KnownVersion::YpubShWsh, Version::YPUB_SHWSH),
(KnownVersion::YprvShWsh, Version::YPRV_SHWSH),
(KnownVersion::Upub, Version::UPUB),
(KnownVersion::Uprv, Version::UPRV),
(KnownVersion::UpubShWsh, Version::UPUB_SHWSH),
(KnownVersion::UprvShWsh, Version::UPRV_SHWSH),
// ========================================================================
// ========================================================================
// https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki
// ========================================================================
(KnownVersion::Zpub, Version::ZPUB),
(KnownVersion::Zprv, Version::ZPRV),
(KnownVersion::ZpubWsh, Version::ZPUB_WSH),
(KnownVersion::ZprvWsh, Version::ZPRV_WSH),
(KnownVersion::Vpub, Version::VPUB),
(KnownVersion::Vprv, Version::VPRV),
(KnownVersion::VpubWsh, Version::VPUB_WSH),
(KnownVersion::VprvWsh, Version::VPRV_WSH),
// ========================================================================
];
#[test]
fn known_version_roundtrip() {
for (known, version) in CASES {
assert_eq!(known.into_version(), *version);
assert_eq!(version.into_known_version(), Some(*known));
assert_eq!(KnownVersion::from_raw(version.as_u32()), Some(*known));
}
}
#[test]
fn from_raw_unknown_returns_none() {
assert_eq!(KnownVersion::from_raw(0xDEAD_BEEF), None);
}
}