-
-
Notifications
You must be signed in to change notification settings - Fork 630
/
btc.py
341 lines (288 loc) · 10.1 KB
/
btc.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
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
# This file is part of the Trezor project.
#
# Copyright (C) 2012-2019 SatoshiLabs and contributors
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import base64
import json
import click
import construct as c
from .. import btc, messages, protobuf, tools
from . import ChoiceType, with_client
INPUT_SCRIPTS = {
"address": messages.InputScriptType.SPENDADDRESS,
"segwit": messages.InputScriptType.SPENDWITNESS,
"p2shsegwit": messages.InputScriptType.SPENDP2SHWITNESS,
"pkh": messages.InputScriptType.SPENDADDRESS,
"wpkh": messages.InputScriptType.SPENDWITNESS,
"sh-wpkh": messages.InputScriptType.SPENDP2SHWITNESS,
"tr": messages.InputScriptType.SPENDTAPROOT,
}
OUTPUT_SCRIPTS = {
"address": messages.OutputScriptType.PAYTOADDRESS,
"segwit": messages.OutputScriptType.PAYTOWITNESS,
"p2shsegwit": messages.OutputScriptType.PAYTOP2SHWITNESS,
"pkh": messages.OutputScriptType.PAYTOADDRESS,
"wpkh": messages.OutputScriptType.PAYTOWITNESS,
"sh-wpkh": messages.OutputScriptType.PAYTOP2SHWITNESS,
"tr": messages.OutputScriptType.PAYTOTAPROOT,
}
DEFAULT_COIN = "Bitcoin"
XpubStruct = c.Struct(
"version" / c.Int32ub,
"depth" / c.Int8ub,
"fingerprint" / c.Int32ub,
"child_num" / c.Int32ub,
"chain_code" / c.Bytes(32),
"key" / c.Bytes(33),
c.Terminated,
)
def xpub_deserialize(xpubstr):
xpub_bytes = tools.b58check_decode(xpubstr)
data = XpubStruct.parse(xpub_bytes)
if data.key[0] == 0:
private_key = data.key[1:]
public_key = None
else:
public_key = data.key
private_key = None
node = messages.HDNodeType(
depth=data.depth,
fingerprint=data.fingerprint,
child_num=data.child_num,
chain_code=data.chain_code,
public_key=public_key,
private_key=private_key,
)
return data.version, node
@click.group(name="btc")
def cli():
"""Bitcoin and Bitcoin-like coins commands."""
#
# Address functions
#
@cli.command()
@click.option("-c", "--coin")
@click.option("-n", "--address", required=True, help="BIP-32 path")
@click.option("-t", "--script-type", type=ChoiceType(INPUT_SCRIPTS), default="address")
@click.option("-d", "--show-display", is_flag=True)
@click.option("-x", "--multisig-xpub", multiple=True, help="XPUBs of multisig owners")
@click.option("-m", "--multisig-threshold", type=int, help="Number of signatures")
@click.option(
"-N",
"--multisig-suffix-length",
help="BIP-32 suffix length for multisig",
type=int,
default=2,
)
@with_client
def get_address(
client,
coin,
address,
script_type,
show_display,
multisig_xpub,
multisig_threshold,
multisig_suffix_length,
):
"""Get address for specified path.
To obtain a multisig address, provide XPUBs of all signers (including your own) in
the intended order. All XPUBs should be on the same level. By default, it is assumed
that the XPUBs are on the account level, and the last two components of --address
should be derived from all of them.
For BIP-45 multisig:
\b
$ trezorctl btc get-public-node -n m/45h/0
xpub0101
$ trezorctl btc get-address -n m/45h/0/0/7 -m 3 -x xpub0101 -x xpub0202 -x xpub0303
This assumes that the other signers also created xpubs at address "m/45h/i".
For all the signers, the final keys will be derived with the "/0/7" suffix.
You can specify a different suffix length by using the -N option. For example, to
use final xpubs, specify '-N 0'.
"""
coin = coin or DEFAULT_COIN
address_n = tools.parse_path(address)
if multisig_xpub:
if multisig_threshold is None:
raise click.ClickException("Please specify signature threshold")
multisig_suffix = address_n[-multisig_suffix_length:]
nodes = [xpub_deserialize(x)[1] for x in multisig_xpub]
multisig = messages.MultisigRedeemScriptType(
nodes=nodes, address_n=multisig_suffix, m=multisig_threshold
)
if script_type == messages.InputScriptType.SPENDADDRESS:
script_type = messages.InputScriptType.SPENDMULTISIG
else:
multisig = None
return btc.get_address(
client,
coin,
address_n,
show_display,
script_type=script_type,
multisig=multisig,
)
@cli.command()
@click.option("-c", "--coin")
@click.option("-n", "--address", required=True, help="BIP-32 path, e.g. m/44'/0'/0'")
@click.option("-e", "--curve")
@click.option("-t", "--script-type", type=ChoiceType(INPUT_SCRIPTS), default="address")
@click.option("-d", "--show-display", is_flag=True)
@with_client
def get_public_node(client, coin, address, curve, script_type, show_display):
"""Get public node of given path."""
coin = coin or DEFAULT_COIN
address_n = tools.parse_path(address)
result = btc.get_public_node(
client,
address_n,
ecdsa_curve_name=curve,
show_display=show_display,
coin_name=coin,
script_type=script_type,
)
return {
"node": {
"depth": result.node.depth,
"fingerprint": "%08x" % result.node.fingerprint,
"child_num": result.node.child_num,
"chain_code": result.node.chain_code.hex(),
"public_key": result.node.public_key.hex(),
},
"xpub": result.xpub,
}
def _append_descriptor_checksum(desc: str) -> str:
checksum = tools.descriptor_checksum(desc)
return f"{desc}#{checksum}"
def _get_descriptor(client, coin, account, script_type, show_display):
coin = coin or DEFAULT_COIN
if script_type == messages.InputScriptType.SPENDADDRESS:
acc_type = 44
fmt = "pkh({})"
elif script_type == messages.InputScriptType.SPENDP2SHWITNESS:
acc_type = 49
fmt = "sh(wpkh({}))"
elif script_type == messages.InputScriptType.SPENDWITNESS:
acc_type = 84
fmt = "wpkh({})"
elif script_type == messages.InputScriptType.SPENDTAPROOT:
acc_type = 86
fmt = "tr({})"
else:
raise ValueError("Unsupported account type")
if coin is None or coin == "Bitcoin":
coin_type = 0
elif coin == "Testnet" or coin == "Regtest":
coin_type = 1
else:
raise ValueError("Unsupported coin")
path = f"m/{acc_type}'/{coin_type}'/{account}'"
n = tools.parse_path(path)
pub = btc.get_public_node(
client,
n,
show_display=show_display,
coin_name=coin,
script_type=script_type,
ignore_xpub_magic=True,
)
fingerprint = pub.root_fingerprint if pub.root_fingerprint is not None else 0
descriptor = f"[{fingerprint:08x}{path[1:]}]{pub.xpub}/<0;1>/*"
return _append_descriptor_checksum(fmt.format(descriptor))
@cli.command()
@click.option("-c", "--coin")
@click.option(
"-n", "--account", required=True, type=int, help="account index (0 = first account)"
)
@click.option("-t", "--script-type", type=ChoiceType(INPUT_SCRIPTS), default="address")
@click.option("-d", "--show-display", is_flag=True)
@with_client
def get_descriptor(client, coin, account, script_type, show_display):
"""Get descriptor of given account."""
try:
return _get_descriptor(client, coin, account, script_type, show_display)
except ValueError as e:
raise click.ClickException(e.msg)
#
# Signing functions
#
@cli.command()
@click.option("-c", "--coin", is_flag=True, hidden=True, expose_value=False)
@click.argument("json_file", type=click.File())
@with_client
def sign_tx(client, json_file):
"""Sign transaction.
Transaction data must be provided in a JSON file. See `transaction-format.md` for
description. You can use `tools/build_tx.py` from the source distribution to build
the required JSON file interactively:
$ python3 tools/build_tx.py | trezorctl btc sign-tx -
"""
data = json.load(json_file)
coin = data.get("coin_name", DEFAULT_COIN)
details = data.get("details", {})
inputs = [
protobuf.dict_to_proto(messages.TxInputType, i) for i in data.get("inputs", ())
]
outputs = [
protobuf.dict_to_proto(messages.TxOutputType, output)
for output in data.get("outputs", ())
]
prev_txes = {
bytes.fromhex(txid): protobuf.dict_to_proto(messages.TransactionType, tx)
for txid, tx in data.get("prev_txes", {}).items()
}
_, serialized_tx = btc.sign_tx(
client,
coin,
inputs,
outputs,
prev_txes=prev_txes,
**details,
)
click.echo()
click.echo("Signed Transaction:")
click.echo(serialized_tx.hex())
#
# Message functions
#
@cli.command()
@click.option("-c", "--coin")
@click.option("-n", "--address", required=True, help="BIP-32 path")
@click.option("-t", "--script-type", type=ChoiceType(INPUT_SCRIPTS), default="address")
@click.argument("message")
@with_client
def sign_message(client, coin, address, message, script_type):
"""Sign message using address of given path."""
coin = coin or DEFAULT_COIN
address_n = tools.parse_path(address)
res = btc.sign_message(client, coin, address_n, message, script_type)
return {
"message": message,
"address": res.address,
"signature": base64.b64encode(res.signature).decode(),
}
@cli.command()
@click.option("-c", "--coin")
@click.argument("address")
@click.argument("signature")
@click.argument("message")
@with_client
def verify_message(client, coin, address, signature, message):
"""Verify message."""
signature = base64.b64decode(signature)
coin = coin or DEFAULT_COIN
return btc.verify_message(client, coin, address, signature, message)
#
# deprecated interactive signing
# ALL BELOW is legacy code and will be dropped