|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | 3 | import base64 |
| 4 | +import json |
4 | 5 | import click |
5 | 6 | import functools |
6 | 7 | import logging |
|
10 | 11 | from jadepy.jade import JadeAPI |
11 | 12 |
|
12 | 13 |
|
| 14 | +def h2b(hexdata): |
| 15 | + if hexdata is None or isinstance(hexdata, (int, bool)): |
| 16 | + return hexdata |
| 17 | + if isinstance(hexdata, list): |
| 18 | + return list(map(h2b, hexdata)) |
| 19 | + if isinstance(hexdata, dict): |
| 20 | + return {k: h2b(v) for k, v in hexdata.items()} |
| 21 | + return bytes.fromhex(hexdata) |
| 22 | + |
| 23 | + |
| 24 | +def b2h_impl(obj, leaf_fn): |
| 25 | + if isinstance(obj, dict): |
| 26 | + return {k: b2h_impl(v, leaf_fn) for k, v in obj.items()} |
| 27 | + if isinstance(obj, list): |
| 28 | + return [b2h_impl(v, leaf_fn) for v in obj] |
| 29 | + if isinstance(obj, tuple): |
| 30 | + return tuple(b2h_impl(v, leaf_fn) for v in obj) |
| 31 | + return leaf_fn(obj) |
| 32 | + |
| 33 | + |
| 34 | +def b2h(result): |
| 35 | + return b2h_impl( |
| 36 | + result, |
| 37 | + lambda v: bytes(v).hex() if isinstance(v, (bytes, bytearray)) else v |
| 38 | + ) |
| 39 | + |
| 40 | + |
13 | 41 | class JadeClient: |
14 | 42 | def __init__(self, device='tcp:localhost:30121'): |
15 | 43 | self.device = device |
@@ -151,11 +179,16 @@ def sign_message(jade, path, message, network): |
151 | 179 |
|
152 | 180 | @cli.command() |
153 | 181 | @click.argument('tx') |
| 182 | +@click.argument('inputs') |
| 183 | +@click.argument('change') |
154 | 184 | @click.option('--network', default='testnet') |
155 | 185 | @with_jade_client |
156 | | -def sign_tx(jade, tx, network): |
157 | | - result = jade.sign_tx(network, tx) |
158 | | - click.echo(base64.b64encode(result)) |
| 186 | +def sign_tx(jade, tx, inputs, change, network): |
| 187 | + tx_bytes = bytes.fromhex(tx) |
| 188 | + inputs_obj = h2b(json.loads(inputs)) |
| 189 | + change_obj = h2b(json.loads(change)) |
| 190 | + result = jade.sign_tx(network, tx_bytes, inputs_obj, change_obj) |
| 191 | + click.echo(json.dumps(b2h(result))) |
159 | 192 |
|
160 | 193 |
|
161 | 194 | @cli.command() |
|
0 commit comments