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
use miniscript::{
bitcoin::{self, ScriptBuf},
descriptor::Wildcard,
Descriptor, DescriptorPublicKey, ForEachKey,
};
#[derive(Debug)]
pub enum Error {
NotMultiXpub,
WrongNetwork,
MultiPathCount,
MultiPath,
Wildcard,
}
/// A struct that manages Bitcoin address derivation from a descriptor.
///
/// It holds the main descriptor, the receiving descriptor, the change descriptor,
/// and the network type. It ensures that the descriptors are valid at creation.
#[derive(Debug, Clone)]
pub struct Derivator {
descriptor: Descriptor<DescriptorPublicKey>, // DesciptorPublicKey::MultiXpub
recv: Descriptor<DescriptorPublicKey>, // DescriptorPublicKey::Xpub
change: Descriptor<DescriptorPublicKey>, // DescriptorPublicKey::Xpub
network: bitcoin::Network,
}
impl Derivator {
/// Creates a new `Derivator` instance.
///
/// # Parameters
/// - `descriptor`: A `Descriptor<DescriptorPublicKey>` that must be a multi-signature
/// descriptor.
/// - `network`: The Bitcoin network type.
///
/// # Returns
/// - `Result<Self, Error>`: Returns an instance of `Derivator` if successful,
/// or an `Error` if the descriptor is not valid.
///
/// # Note: the descriptor is expected to have this properties:
/// - It must be of type [`DescriptorPublicKey::MultiXpub`]
/// - All keys must have a multipath of size 2, the first element being the receive index,
/// the second being the change index.
/// - Multipath elements must be of unhardened type.
/// - It must have an Unhardened wildcard.
/// - All key must be for the given network.
pub fn new(
descriptor: Descriptor<DescriptorPublicKey>,
network: bitcoin::Network,
) -> Result<Self, Error> {
let is_multi_xpub =
descriptor.for_each_key(|k| matches!(k, DescriptorPublicKey::MultiXPub(_)));
if !is_multi_xpub {
return Err(Error::NotMultiXpub);
}
let mut wrong_network = false;
let mut wrong_multipath = false;
let mut wrong_wildcard = false;
descriptor.for_each_key(|k| {
if let DescriptorPublicKey::MultiXPub(key) = k {
if key.xkey.network != network.into() {
wrong_network = true;
}
let paths = key.derivation_paths.paths();
for p in paths {
let v = p.to_u32_vec();
// expected 1 multipath + 1 wildcard
if v.len() != 1 {
wrong_multipath = true;
}
for child in v {
// if hardened derivation path
if child >= 0x80000000 {
wrong_multipath = true;
}
}
if key.wildcard != Wildcard::Unhardened {
wrong_wildcard = true;
}
}
}
true
});
if wrong_network {
return Err(Error::WrongNetwork);
}
if wrong_multipath {
return Err(Error::MultiPath);
}
if wrong_wildcard {
return Err(Error::Wildcard);
}
let single_descriptors = descriptor
.clone()
.into_single_descriptors()
.expect("multipath already sanitized");
if single_descriptors.len() != 2 {
return Err(Error::MultiPathCount);
}
let mut single_descriptors = single_descriptors.into_iter();
let recv = single_descriptors.next().expect("length checked");
let change = single_descriptors.next().expect("length checked");
Ok(Self {
descriptor,
recv,
change,
network,
})
}
/// Returns the main descriptor of the `Derivator`.
///
/// # Returns
/// - `Descriptor<DescriptorPublicKey>`: The main descriptor associated with this
/// `Derivator`.
pub fn descriptor(&self) -> Descriptor<DescriptorPublicKey> {
self.descriptor.clone()
}
/// Derives a receiving address at the specified index.
///
/// # Parameters
/// - `index`: The index at which to derive the receiving address.
///
/// # Returns
/// - `bitcoin::Address`: The derived receiving address.
pub fn receive_at(&self, index: u32) -> bitcoin::Address {
self.recv
.at_derivation_index(index)
.expect("wildcard checked")
.address(self.network)
.expect("valid")
}
/// Derives a change address at the specified index.
///
/// # Parameters
/// - `index`: The index at which to derive the change address.
///
/// # Returns
/// - `bitcoin::Address`: The derived change address.
pub fn change_at(&self, index: u32) -> bitcoin::Address {
self.change
.at_derivation_index(index)
.expect("wildcard checked")
.address(self.network)
.expect("valid")
}
/// Returns the script public key for the receiving address at the specified index.
///
/// # Parameters
/// - `index`: The index at which to derive the receiving address.
///
/// # Returns
/// - `ScriptBuf`: The script public key of the derived receiving address.
pub fn receive_spk_at(&self, index: u32) -> ScriptBuf {
self.receive_at(index).script_pubkey()
}
/// Returns the script public key for the change address at the specified index.
///
/// # Parameters
/// - `index`: The index at which to derive the change address.
///
/// # Returns
/// - `ScriptBuf`: The script public key of the derived change address.
pub fn change_spk_at(&self, index: u32) -> ScriptBuf {
self.change_at(index).script_pubkey()
}
}