-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
xpub.rs
628 lines (535 loc) · 18.9 KB
/
xpub.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// Modern, minimalistic & standard-compliant cold wallet library.
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2020-2023 by
// Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// Copyright (C) 2020-2023 LNP/BP Standards Association. All rights reserved.
// Copyright (C) 2020-2023 Dr Maxim Orlovsky. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::borrow::Borrow;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
use amplify::{hex, ByteArray, Bytes20, Bytes32, Bytes4, Wrapper};
use bc::{secp256k1, CompressedPk, InvalidPubkey, LegacyPk, XOnlyPk};
use hashes::{hash160, sha512, Hash, HashEngine, Hmac, HmacEngine};
use crate::secp256k1::SECP256K1;
use crate::{
base58, DerivationIndex, DerivationParseError, DerivationPath, DerivationSeg, HardenedIndex,
Idx, IdxBase, IndexParseError, Keychain, NormalIndex, SegParseError, Terminal,
};
pub const XPUB_MAINNET_MAGIC: [u8; 4] = [0x04u8, 0x88, 0xB2, 0x1E];
pub const XPUB_TESTNET_MAGIC: [u8; 4] = [0x04u8, 0x35, 0x87, 0xCF];
#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum XpubDecodeError {
/// wrong length of extended pubkey data ({0}).
WrongExtendedKeyLength(usize),
/// provided key is not a standard BIP-32 extended pubkey
UnknownKeyType([u8; 4]),
/// extended pubkey contains {0}
#[from]
#[from(bc::secp256k1::Error)]
InvalidPubkey(InvalidPubkey<33>),
}
#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
pub enum XpubParseError {
/// wrong Base58 encoding of extended pubkey data - {0}
#[display(doc_comments)]
#[from]
Base58(base58::Error),
#[display(inner)]
#[from]
Decode(XpubDecodeError),
#[display(inner)]
#[from]
DerivationPath(DerivationParseError),
/// invalid master key fingerprint - {0}
#[from]
InvalidMasterFp(hex::Error),
/// invalid terminal derivation format.
InvalidTerminal,
/// invalid keychain segment - {0}
#[from]
InvalidKeychain(SegParseError),
/// invalid index value in terminal derivation segment.
#[from]
InvalidIndex(IndexParseError),
/// no xpub key origin information.
NoOrigin,
/// no extended public key.
NoXpub,
/// xpub network and origin mismatch.
NetworkMismatch,
/// xpub depth and origin mismatch.
DepthMismatch,
/// xpub parent not matches the provided origin information.
ParentMismatch,
}
impl From<OriginParseError> for XpubParseError {
fn from(err: OriginParseError) -> Self {
match err {
OriginParseError::DerivationPath(e) => XpubParseError::DerivationPath(e),
OriginParseError::InvalidMasterFp(e) => XpubParseError::InvalidMasterFp(e),
}
}
}
/// BIP32 chain code used for hierarchical derivation
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]
#[wrapper(Deref, RangeOps)]
pub struct ChainCode(Bytes32);
impl AsRef<[u8]> for ChainCode {
fn as_ref(&self) -> &[u8] { self.0.as_ref() }
}
impl From<[u8; 32]> for ChainCode {
fn from(value: [u8; 32]) -> Self { Self(value.into()) }
}
impl From<ChainCode> for [u8; 32] {
fn from(value: ChainCode) -> Self { value.0.into_inner() }
}
/// Deterministic part of the extended public key.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct XpubCore {
/// Public key
pub public_key: CompressedPk,
/// BIP32 chain code used for hierarchical derivation
pub chain_code: ChainCode,
}
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Default, Debug, Display, From)]
#[wrapper(RangeOps, Hex, FromStr)]
#[display(LowerHex)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct XpubFp(
#[from]
#[from([u8; 4])]
Bytes4,
);
impl AsRef<[u8]> for XpubFp {
fn as_ref(&self) -> &[u8] { self.0.as_ref() }
}
impl From<XpubFp> for [u8; 4] {
fn from(value: XpubFp) -> Self { value.0.into_inner() }
}
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Default, Debug, Display, From)]
#[wrapper(RangeOps, Hex, FromStr)]
#[display(LowerHex)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct XpubId(
#[from]
#[from([u8; 20])]
Bytes20,
);
impl AsRef<[u8]> for XpubId {
fn as_ref(&self) -> &[u8] { self.0.as_ref() }
}
impl From<XpubId> for [u8; 20] {
fn from(value: XpubId) -> Self { value.0.into_inner() }
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct XpubMeta {
pub depth: u8,
pub parent_fp: XpubFp,
pub child_number: DerivationIndex,
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct Xpub {
testnet: bool,
meta: XpubMeta,
core: XpubCore,
}
impl Xpub {
pub fn decode(data: impl Borrow<[u8]>) -> Result<Xpub, XpubDecodeError> {
let data = data.borrow();
if data.len() != 78 {
return Err(XpubDecodeError::WrongExtendedKeyLength(data.len()));
}
let testnet = match &data[0..4] {
magic if magic == XPUB_MAINNET_MAGIC => false,
magic if magic == XPUB_TESTNET_MAGIC => true,
unknown => {
let mut magic = [0u8; 4];
magic.copy_from_slice(unknown);
return Err(XpubDecodeError::UnknownKeyType(magic));
}
};
let depth = data[4];
let mut parent_fp = [0u8; 4];
parent_fp.copy_from_slice(&data[5..9]);
let mut child_number = [0u8; 4];
child_number.copy_from_slice(&data[9..13]);
let child_number = u32::from_be_bytes(child_number);
let mut chain_code = [0u8; 32];
chain_code.copy_from_slice(&data[13..45]);
let public_key = CompressedPk::from_bytes(&data[45..78])?;
Ok(Xpub {
testnet,
meta: XpubMeta {
depth,
parent_fp: parent_fp.into(),
child_number: child_number.into(),
},
core: XpubCore {
public_key,
chain_code: chain_code.into(),
},
})
}
pub fn encode(&self) -> [u8; 78] {
let mut ret = [0; 78];
ret[0..4].copy_from_slice(&match self.testnet {
false => XPUB_MAINNET_MAGIC,
true => XPUB_TESTNET_MAGIC,
});
ret[4] = self.meta.depth;
ret[5..9].copy_from_slice(self.meta.parent_fp.as_ref());
ret[9..13].copy_from_slice(&self.meta.child_number.index().to_be_bytes());
ret[13..45].copy_from_slice(self.core.chain_code.as_ref());
ret[45..78].copy_from_slice(&self.core.public_key.serialize());
ret
}
/// Returns the HASH160 of the chaincode
pub fn identifier(&self) -> XpubId {
let hash = hash160::Hash::hash(&self.core.public_key.serialize());
XpubId::from_byte_array(*hash.as_byte_array())
}
pub fn fingerprint(&self) -> XpubFp {
let mut bytes = [0u8; 4];
bytes.copy_from_slice(&self.identifier()[..4]);
XpubFp::from_byte_array(bytes)
}
/// Constructs ECDSA public key valid in legacy context (compressed by default).
pub fn to_legacy_pub(&self) -> LegacyPk { LegacyPk::compressed(*self.core.public_key) }
/// Constructs ECDSA public key.
pub fn to_compr_pub(&self) -> CompressedPk { self.core.public_key }
/// Constructs BIP340 public key matching internal public key representation.
pub fn to_xonly_pub(&self) -> XOnlyPk { XOnlyPk::from(self.core.public_key) }
/// Attempts to derive an extended public key from a path.
///
/// The `path` argument can be any type implementing `AsRef<ChildNumber>`, such as
/// `DerivationPath`, for instance.
pub fn derive_pub(&self, path: impl AsRef<[NormalIndex]>) -> Self {
let mut pk = *self;
for cnum in path.as_ref() {
pk = pk.ckd_pub(*cnum)
}
pk
}
/// Compute the scalar tweak added to this key to get a child key
pub fn ckd_pub_tweak(&self, child_no: NormalIndex) -> (secp256k1::Scalar, ChainCode) {
let mut hmac_engine: HmacEngine<sha512::Hash> =
HmacEngine::new(self.core.chain_code.as_ref());
hmac_engine.input(&self.core.public_key.serialize());
hmac_engine.input(&child_no.to_be_bytes());
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
let private_key = secp256k1::SecretKey::from_slice(&hmac_result[..32])
.expect("negligible probability")
.into();
let mut bytes = [0u8; 32];
bytes.copy_from_slice(&hmac_result[32..]);
let chain_code = ChainCode::from_byte_array(bytes);
(private_key, chain_code)
}
/// Public->Public child key derivation
pub fn ckd_pub(&self, child_no: NormalIndex) -> Xpub {
let (scalar, chain_code) = self.ckd_pub_tweak(child_no);
let tweaked =
self.core.public_key.add_exp_tweak(SECP256K1, &scalar).expect("negligible probability");
let meta = XpubMeta {
depth: self.meta.depth + 1,
parent_fp: self.fingerprint(),
child_number: child_no.into(),
};
let core = XpubCore {
public_key: tweaked.into(),
chain_code,
};
Xpub {
testnet: self.testnet,
meta,
core,
}
}
}
impl Display for Xpub {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
base58::encode_check_to_fmt(f, &self.encode())
}
}
impl FromStr for Xpub {
type Err = XpubParseError;
fn from_str(inp: &str) -> Result<Xpub, XpubParseError> {
let data = base58::decode_check(inp)?;
Ok(Xpub::decode(data)?)
}
}
#[derive(Getters, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[display("{master_fp}{derivation}", alt = "{master_fp}{derivation:#}")]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct XpubOrigin {
#[getter(as_copy)]
master_fp: XpubFp,
derivation: DerivationPath<HardenedIndex>,
}
impl XpubOrigin {
pub fn new(master_fp: XpubFp, derivation: DerivationPath<HardenedIndex>) -> Self {
XpubOrigin {
master_fp,
derivation,
}
}
}
impl FromStr for XpubOrigin {
type Err = OriginParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (master_fp, path) = match s.split_once('/') {
None => (XpubFp::default(), ""),
Some(("00000000", p)) | Some(("m", p)) => (XpubFp::default(), p),
Some((fp, p)) => (XpubFp::from_str(fp)?, p),
};
Ok(XpubOrigin {
master_fp,
derivation: DerivationPath::from_str(path)?,
})
}
}
#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum OriginParseError {
/// invalid derivation path - {0}
#[from]
DerivationPath(DerivationParseError),
/// invalid master key fingerprint - {0}
#[from]
InvalidMasterFp(hex::Error),
}
#[derive(Getters, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[display("{master_fp}{derivation}", alt = "{master_fp}{derivation:#}")]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct KeyOrigin {
#[getter(as_copy)]
master_fp: XpubFp,
derivation: DerivationPath,
}
impl FromStr for KeyOrigin {
type Err = XpubParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (master_fp, path) = match s.split_once('/') {
None => (XpubFp::default(), ""),
Some(("00000000", p)) | Some(("m", p)) => (XpubFp::default(), p),
Some((fp, p)) => (XpubFp::from_str(fp)?, p),
};
Ok(KeyOrigin {
master_fp,
derivation: DerivationPath::from_str(path)?,
})
}
}
impl KeyOrigin {
pub fn new(master_fp: XpubFp, derivation: DerivationPath) -> Self {
KeyOrigin {
master_fp,
derivation,
}
}
pub fn with(xpub_origin: XpubOrigin, terminal: Terminal) -> Self {
let mut derivation = DerivationPath::new();
derivation.extend(xpub_origin.derivation().iter().copied().map(DerivationIndex::from));
derivation.push(terminal.keychain.into());
derivation.push(DerivationIndex::Normal(terminal.index));
KeyOrigin {
master_fp: xpub_origin.master_fp(),
derivation,
}
}
}
#[derive(Getters, Clone, Eq, PartialEq, Hash, Debug)]
pub struct XpubSpec {
origin: XpubOrigin,
xpub: Xpub,
}
impl XpubSpec {
pub fn new(xpub: Xpub, origin: XpubOrigin) -> Self { XpubSpec { xpub, origin } }
}
impl Display for XpubSpec {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("[")?;
Display::fmt(&self.origin, f)?;
f.write_str("]")?;
write!(f, "{}/", self.xpub)
}
}
impl FromStr for XpubSpec {
type Err = XpubParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with('[') {
return Err(XpubParseError::NoOrigin);
}
let (origin, xpub) =
s.trim_start_matches('[').split_once(']').ok_or(XpubParseError::NoOrigin)?;
let origin = XpubOrigin::from_str(origin)?;
let xpub = Xpub::from_str(xpub)?;
if origin.derivation.len() != xpub.meta.depth as usize {
return Err(XpubParseError::DepthMismatch);
}
if !origin.derivation.is_empty() {
let network = if xpub.testnet { HardenedIndex::ONE } else { HardenedIndex::ZERO };
if origin.derivation.get(1) != Some(&network) {
return Err(XpubParseError::NetworkMismatch);
}
if origin.derivation.last().copied().map(DerivationIndex::Hardened)
!= Some(xpub.meta.child_number)
{
return Err(XpubParseError::ParentMismatch);
}
}
Ok(XpubSpec { origin, xpub })
}
}
#[derive(Getters, Clone, Eq, PartialEq, Hash, Debug)]
pub struct XpubDerivable {
spec: XpubSpec,
variant: Option<NormalIndex>,
pub(crate) keychains: DerivationSeg<Keychain>,
}
impl XpubDerivable {
pub fn xpub(&self) -> Xpub { self.spec.xpub }
pub fn origin(&self) -> &XpubOrigin { &self.spec.origin }
}
impl Display for XpubDerivable {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self.spec, f)?;
if let Some(variant) = self.variant {
write!(f, "{variant}/")?;
}
Display::fmt(&self.keychains, f)?;
f.write_str("/*")
}
}
impl FromStr for XpubDerivable {
type Err = XpubParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with('[') {
return Err(XpubParseError::NoOrigin);
}
let (origin, remains) =
s.trim_start_matches('[').split_once(']').ok_or(XpubParseError::NoOrigin)?;
let origin = XpubOrigin::from_str(origin)?;
let mut segs = remains.split('/');
let Some(xpub) = segs.next() else {
return Err(XpubParseError::NoXpub);
};
let xpub = Xpub::from_str(xpub)?;
let (variant, keychains) = match (segs.next(), segs.next(), segs.next(), segs.next()) {
(Some(var), Some(keychains), Some("*"), None) => {
(Some(var.parse()?), keychains.parse()?)
}
(Some(keychains), Some("*"), None, None) => (None, keychains.parse()?),
_ => return Err(XpubParseError::InvalidTerminal),
};
Ok(XpubDerivable {
spec: XpubSpec::new(xpub, origin),
variant,
keychains,
})
}
}
#[cfg(feature = "serde")]
mod _serde {
use serde_crate::{de, Deserialize, Deserializer, Serialize, Serializer};
use super::*;
impl Serialize for Xpub {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
if serializer.is_human_readable() {
serializer.serialize_str(&self.to_string())
} else {
serializer.serialize_bytes(&self.encode())
}
}
}
impl<'de> Deserialize<'de> for Xpub {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
if deserializer.is_human_readable() {
let s = String::deserialize(deserializer)?;
Xpub::from_str(&s).map_err(|err| {
de::Error::custom(format!("invalid xpub string representation; {err}"))
})
} else {
let v = Vec::<u8>::deserialize(deserializer)?;
Xpub::decode(v)
.map_err(|err| de::Error::custom(format!("invalid xpub bytes; {err}")))
}
}
}
impl Serialize for XpubSpec {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for XpubSpec {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
let s = String::deserialize(deserializer)?;
XpubSpec::from_str(&s).map_err(|err| {
de::Error::custom(format!(
"invalid xpub specification string representation; {err}"
))
})
}
}
impl Serialize for XpubDerivable {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for XpubDerivable {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
let s = String::deserialize(deserializer)?;
XpubDerivable::from_str(&s).map_err(|err| {
de::Error::custom(format!("invalid xpub derivation string representation; {err}"))
})
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn display_from_str() {
let s = "[643a7adc/86h/1h/0h]tpubDCNiWHaiSkgnQjuhsg9kjwaUzaxQjUcmhagvYzqQ3TYJTgFGJstVaqnu4yhtFktBhCVFmBNLQ5sN53qKzZbMksm3XEyGJsEhQPfVZdWmTE2/<0;1>/*";
let xpub = XpubDerivable::from_str(s).unwrap();
assert_eq!(s, xpub.to_string());
}
}