-
Notifications
You must be signed in to change notification settings - Fork 26
/
descriptors.py
290 lines (240 loc) Β· 10.9 KB
/
descriptors.py
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
#!/usr/bin/env python3
from __future__ import annotations
from collections import OrderedDict, deque
from typing import List, Optional
from bip32 import BIP32, HARDENED_INDEX
from scripts import ScriptType
# m: master key
# a: account index
# i: address index
descriptors = {
"m/44'/0'/a'/0/i": [ScriptType.LEGACY], # BIP44, external
"m/44'/0'/a'/1/i": [ScriptType.LEGACY], # BIP44, change
"m/49'/0'/a'/0/i": [ScriptType.COMPAT], # BIP49, external
"m/49'/0'/a'/1/i": [ScriptType.COMPAT], # BIP49, change
"m/84'/0'/a'/0/i": [ScriptType.SEGWIT], # BIP84, external
"m/84'/0'/a'/1/i": [ScriptType.SEGWIT], # BIP84, change
"m/0'/0'/i'": [ScriptType.LEGACY, ScriptType.COMPAT, ScriptType.SEGWIT], # Bitcoin Core
"m/0'/0/i": [ScriptType.LEGACY, ScriptType.COMPAT, ScriptType.SEGWIT], # BRD/Hodl/Coin/Multibit external
"m/0'/1/i": [ScriptType.LEGACY, ScriptType.COMPAT, ScriptType.SEGWIT], # BRD/Hodl/Coin/Multibit change
"m/44'/0'/2147483647'/0/i": [ScriptType.LEGACY], # Samourai ricochet, BIP44, external
"m/44'/0'/2147483647'/1/i": [ScriptType.LEGACY], # Samourai ricochet, BIP44, change
"m/49'/0'/2147483647'/0/i": [ScriptType.COMPAT], # Samourai ricochet, BIP49, external
"m/49'/0'/2147483647'/1/i": [ScriptType.COMPAT], # Samourai ricochet, BIP49, change
"m/84'/0'/2147483647'/0/i": [ScriptType.SEGWIT], # Samourai ricochet, BIP84, external
"m/84'/0'/2147483647'/1/i": [ScriptType.SEGWIT], # Samourai ricochet, BIP84, change
"m/84'/0'/2147483646'/0/i": [ScriptType.SEGWIT], # Samourai post-mix, external
"m/84'/0'/2147483646'/1/i": [ScriptType.SEGWIT], # Samourai post-mix, change
"m/84'/0'/2147483645'/0/i": [ScriptType.SEGWIT], # Samourai pre-mix, external
"m/84'/0'/2147483645'/1/i": [ScriptType.SEGWIT], # Samourai pre-mix, change
"m/84'/0'/2147483644'/0/i": [ScriptType.SEGWIT], # Samourai bad-bank, external
"m/84'/0'/2147483644'/1/i": [ScriptType.SEGWIT], # Samourai bad-bank, change
"m/44'/0'/0'/0/i": [ScriptType.SEGWIT], # Copay segwit, external
"m/44'/0'/0'/1/i": [ScriptType.SEGWIT], # Copay segwit, change
}
class Path:
"""
Derivation path from a master key that may have a variable account number, and a variable index number.
"""
def __init__(self, path: str):
self.path = path
def has_variable_account(self) -> bool:
"""
Whether this path has the account level as a free variable.
"""
return self.path.find('a') >= 0
def has_variable_index(self) -> bool:
"""
Whether this path has the index level as a free variable.
"""
return self.path.find('i') >= 0
def to_list(self, index: int = None, account: int = None) -> List[int]:
"""
Transform this path into a list of valid derivation indexes.
"""
# replace the placeholders
path = self.path.replace('a', str(account)).replace('i', str(index))
parts = path.split('/')[1:]
# compute the derivation indexes
indexes = []
for part in parts:
if part.endswith("'"):
indexes.append(HARDENED_INDEX + int(part[:-1]))
else:
indexes.append(int(part))
return indexes
def with_account(self, account: int) -> Path:
"""
Get a new path with a fixed account.
"""
return Path(self.path.replace('a', str(account)))
def with_index(self, index: int) -> Path:
"""
Get a new path with a fixed index.
"""
return Path(self.path.replace('i', str(index)))
def __eq__(self, other):
if isinstance(other, Path):
return self.path == other.path
return NotImplemented
def __hash__(self):
return hash(self.path)
class Script:
"""
Data needed to spend a script.
"""
def __init__(self, program: bytes, index: int, account: int, descriptor: DescriptorScriptIterator):
self.program = program
self.index = index
self.account = account
self.descriptor = descriptor
def type(self) -> ScriptType:
return self.descriptor.script_type
def path_with_account(self) -> Path:
return self.descriptor.path.with_account(self.account)
def full_path(self) -> Path:
return self.path_with_account().with_index(self.index)
def set_as_used(self) -> None:
self.descriptor.found_used_script(self)
class DescriptorScriptIterator:
"""
Iterator that can traverse the all the possible scripts generated by a descriptor (ie. a path and script type pair).
"""
def __init__(self, path: Path, script_type: ScriptType, address_gap: int, account_gap: int):
self.path = path
self.script_type = script_type
self.address_gap = address_gap
self.account_gap = account_gap
self.index = 0
self.account = 0
self.max_index = address_gap if path.has_variable_index() else 0
self.max_account = account_gap if path.has_variable_account() else 0
self.used_accounts = set()
self.priority_pairs = OrderedDict()
self.total_scripts = (self.max_index + 1) * (self.max_account + 1)
def _script_at(self, master_key: BIP32, index: int, account: int) -> Script:
"""
Render the script at a specific index and account pair.
"""
pubkey = master_key.get_pubkey_from_path(self.path.to_list(index, account))
script = self.script_type.build_output_script(pubkey)
return Script(script, index, account, self)
def _next_pair(self) -> None:
"""
Compute the next pair of index and account positions that we should explore.
"""
# Since traversing the entire [0; MAX_INDEX] x [0; MAX_ACCOUNT] space of combinations might take a while, we
# walk the (index, account) grid in diagonal order. This order prioritizes the most probable combinations
# (ie. low index, low account), while letting us explore a large space in the long run.
#
# 0 1 2
# β β β
# (0,0) (1,0) (2,0) 3
# β β β β
# (0,1) (1,1) (2,1) 4
# β β β β
# (0,2) (1,2) (2,2) 5
# β β β β
# (0,3) (1,3) (2,3)
# β β β
if self.index == 0 or self.account == self.max_account:
# if we reached the border, start in the next diagonal
diagonal_total = self.index + self.account + 1
self.index = min(diagonal_total, self.max_index)
self.account = diagonal_total - self.index
else:
# go down the diagonal
self.index -= 1
self.account += 1
def next_script(self, master_key: BIP32) -> Optional[Script]:
"""
Fetch the next script for the current descriptor.
"""
# if there's any priority pairs, explore the first one
for account, indexes in list(self.priority_pairs.items()):
if indexes:
index = indexes.popleft()
return self._script_at(master_key, index, account)
else:
del self.priority_pairs[account]
# if we are off the grid, then we are done
if self.account > self.max_account:
return None
# generate the script for the current pair in the grid
response = self._script_at(master_key, self.index, self.account)
# and compute the next pair in the grid to explore
self._next_pair()
while self.account in self.used_accounts:
self._next_pair()
return response
def found_used_script(self, script: Script) -> None:
"""
Update the priority scripts to process, knowing that a particular script was used.
"""
# stop exploring the current account during the grid search
self.used_accounts.add(script.account)
if script.account not in self.priority_pairs:
self.priority_pairs[script.account] = deque()
# extend the priority list of pairs to explore with enough indexes so that the next address gap is covered
missing_indexes = self.priority_pairs[script.account]
last_index = missing_indexes[-1] if missing_indexes else script.index
new_indexes = range(last_index + 1, script.index + self.address_gap + 1)
missing_indexes.extend(new_indexes)
self.total_scripts += len([i for i in new_indexes if i > self.max_index])
# extend the priority list of pairs to explore with enough accounts so that the next account gap is covered
while self.max_account <= script.account + self.account_gap:
self.max_account += 1
self.total_scripts += self.max_index + 1
current_diagonal = self.index + self.account
missing_indexes = deque(range(current_diagonal - self.max_account))
self.priority_pairs[self.max_account] = missing_indexes
def has_priority_scripts(self) -> bool:
"""
Whether this descriptor should be prioritized because it's exploring a used account path.
"""
for account, indexes in self.priority_pairs.items():
if indexes:
return True
return False
class ScriptIterator:
"""
Iterator that can traverse all the possible scripts of all the possible descriptors.
"""
def __init__(self, master_key: BIP32, address_gap: int, account_gap: int):
self.master_key = master_key
self.index = 0
self.descriptors = []
self.last_descriptor = None
for path, types in descriptors.items():
for type in types:
self.descriptors.append(DescriptorScriptIterator(Path(path), type, address_gap, account_gap))
def _next_descriptor_script(self) -> Optional[Script]:
"""
Fetch the next script from the next descriptor.
"""
if self.last_descriptor and self.last_descriptor.has_priority_scripts():
iter = self.last_descriptor.next_script(self.master_key)
if iter:
return iter
self.last_descriptor = self.descriptors[self.index]
iter = self.last_descriptor.next_script(self.master_key)
self.index += 1
if self.index >= len(self.descriptors):
self.index = 0
return iter
def next_script(self) -> Optional[Script]:
"""
Fetch the next script, cycling the descriptors in order to explore all of them progressively.
"""
skipped = 0
while skipped < len(self.descriptors):
iter = self._next_descriptor_script()
if iter:
return iter
skipped += 1
return None
def total_scripts(self) -> int:
"""
Compute the total number of scripts that were or will be explored.
"""
return sum([d.total_scripts for d in self.descriptors])