mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-06-18 14:08:47 +00:00
trezorctl: Guess script type from BIP-32 in sign_tx
Also add change output to sign_tx
This commit is contained in:
parent
881015ae5f
commit
79da872316
33
trezorctl
33
trezorctl
@ -31,6 +31,7 @@ from trezorlib.client import TrezorClient, TrezorClientVerbose, CallException
|
|||||||
from trezorlib import messages as proto
|
from trezorlib import messages as proto
|
||||||
from trezorlib import protobuf
|
from trezorlib import protobuf
|
||||||
from trezorlib.coins import coins_txapi
|
from trezorlib.coins import coins_txapi
|
||||||
|
from trezorlib.ckd_public import PRIME_DERIVATION_FLAG
|
||||||
|
|
||||||
|
|
||||||
class ChoiceType(click.Choice):
|
class ChoiceType(click.Choice):
|
||||||
@ -54,6 +55,12 @@ CHOICE_INPUT_SCRIPT_TYPE = ChoiceType({
|
|||||||
'p2shsegwit': proto.InputScriptType.SPENDP2SHWITNESS,
|
'p2shsegwit': proto.InputScriptType.SPENDP2SHWITNESS,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
CHOICE_OUTPUT_SCRIPT_TYPE = ChoiceType({
|
||||||
|
'address': proto.OutputScriptType.PAYTOADDRESS,
|
||||||
|
'segwit': proto.OutputScriptType.PAYTOWITNESS,
|
||||||
|
'p2shsegwit': proto.OutputScriptType.PAYTOP2SHWITNESS,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def get_transport_class_by_name(name):
|
def get_transport_class_by_name(name):
|
||||||
|
|
||||||
@ -475,6 +482,16 @@ def sign_tx(connect, coin):
|
|||||||
|
|
||||||
client.set_tx_api(txapi)
|
client.set_tx_api(txapi)
|
||||||
|
|
||||||
|
def default_script_type(address_n):
|
||||||
|
script_type = 'address'
|
||||||
|
|
||||||
|
if address_n is None:
|
||||||
|
pass
|
||||||
|
elif address_n[0] == (49 | PRIME_DERIVATION_FLAG):
|
||||||
|
script_type = 'p2shsegwit'
|
||||||
|
|
||||||
|
return script_type
|
||||||
|
|
||||||
def outpoint(s):
|
def outpoint(s):
|
||||||
txid, vout = s.split(':')
|
txid, vout = s.split(':')
|
||||||
return binascii.unhexlify(txid), int(vout)
|
return binascii.unhexlify(txid), int(vout)
|
||||||
@ -488,7 +505,7 @@ def sign_tx(connect, coin):
|
|||||||
prev_hash, prev_index = prev
|
prev_hash, prev_index = prev
|
||||||
address_n = click.prompt('BIP-32 path to derive the key', type=client.expand_path)
|
address_n = click.prompt('BIP-32 path to derive the key', type=client.expand_path)
|
||||||
amount = click.prompt('Input amount (satoshis)', type=int, default=0)
|
amount = click.prompt('Input amount (satoshis)', type=int, default=0)
|
||||||
script_type = click.prompt('Input type', type=CHOICE_INPUT_SCRIPT_TYPE, default='address')
|
script_type = click.prompt('Input type', type=CHOICE_INPUT_SCRIPT_TYPE, default=default_script_type(address_n))
|
||||||
inputs.append(proto.TxInputType(
|
inputs.append(proto.TxInputType(
|
||||||
address_n=address_n,
|
address_n=address_n,
|
||||||
prev_hash=prev_hash,
|
prev_hash=prev_hash,
|
||||||
@ -500,14 +517,20 @@ def sign_tx(connect, coin):
|
|||||||
outputs = []
|
outputs = []
|
||||||
while True:
|
while True:
|
||||||
click.echo()
|
click.echo()
|
||||||
address = click.prompt('Pay to address', default='')
|
address = click.prompt('Output address (for non-change output)', default='')
|
||||||
if not address:
|
if address:
|
||||||
break
|
address_n = None
|
||||||
|
else:
|
||||||
|
address_n = click.prompt('BIP-32 path (for change output)', type=client.expand_path, default='')
|
||||||
|
if not address_n:
|
||||||
|
break
|
||||||
amount = click.prompt('Amount to spend (satoshis)', type=int)
|
amount = click.prompt('Amount to spend (satoshis)', type=int)
|
||||||
|
script_type = click.prompt('Output type', type=CHOICE_OUTPUT_SCRIPT_TYPE, default=default_script_type(address_n))
|
||||||
outputs.append(proto.TxOutputType(
|
outputs.append(proto.TxOutputType(
|
||||||
|
address_n=address_n,
|
||||||
address=address,
|
address=address,
|
||||||
amount=amount,
|
amount=amount,
|
||||||
script_type=proto.OutputScriptType.PAYTOADDRESS,
|
script_type=script_type,
|
||||||
))
|
))
|
||||||
|
|
||||||
(signatures, serialized_tx) = client.sign_tx(coin, inputs, outputs)
|
(signatures, serialized_tx) = client.sign_tx(coin, inputs, outputs)
|
||||||
|
Loading…
Reference in New Issue
Block a user