1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 04:18:10 +00:00

fix(python/build_tx): make build_tx work again after blockbook protection upgrades

Also improve script type selection.
This commit is contained in:
matejcik 2021-11-04 12:32:38 +01:00
parent 57fbf7aa61
commit 428caf3d35
2 changed files with 33 additions and 10 deletions

View File

@ -0,0 +1 @@
Updated `tools/build_tx.py` to work with Blockbook's API protections.

View File

@ -26,6 +26,16 @@ from trezorlib.cli import ChoiceType
from trezorlib.cli.btc import INPUT_SCRIPTS, OUTPUT_SCRIPTS from trezorlib.cli.btc import INPUT_SCRIPTS, OUTPUT_SCRIPTS
from trezorlib.protobuf import to_dict from trezorlib.protobuf import to_dict
SESSION = requests.Session()
SESSION.headers.update({"User-Agent": "trezorlib"})
BITCOIN_CORE_INPUT_TYPES = {
"scripthash": messages.InputScriptType.SPENDADDRESS,
"pubkeyhash": messages.InputScriptType.SPENDP2SHWITNESS,
"witness_v0_keyhash": messages.InputScriptType.SPENDWITNESS,
"witness_v1_taproot": messages.InputScriptType.SPENDP2SHWITNESS,
}
def echo(*args, **kwargs): def echo(*args, **kwargs):
return click.echo(*args, err=True, **kwargs) return click.echo(*args, err=True, **kwargs)
@ -65,11 +75,10 @@ def _get_inputs_interactive(blockbook_url):
if not prev: if not prev:
break break
prev_hash, prev_index = prev prev_hash, prev_index = prev
address_n = prompt("BIP-32 path to derive the key", type=tools.parse_path)
txhash = prev_hash.hex() txhash = prev_hash.hex()
tx_url = blockbook_url + txhash tx_url = blockbook_url + txhash
r = requests.get(tx_url) r = SESSION.get(tx_url)
if not r.ok: if not r.ok:
raise click.ClickException(f"Failed to fetch URL: {tx_url}") raise click.ClickException(f"Failed to fetch URL: {tx_url}")
@ -79,21 +88,34 @@ def _get_inputs_interactive(blockbook_url):
tx = btc.from_json(tx_json) tx = btc.from_json(tx_json)
txes[txhash] = tx txes[txhash] = tx
try:
from_address = tx_json["vout"][prev_index]["scriptPubKey"]["address"]
echo(f"From address: {from_address}")
except Exception:
pass
amount = tx.bin_outputs[prev_index].amount amount = tx.bin_outputs[prev_index].amount
echo(f"Input amount: {amount}") echo(f"Input amount: {amount}")
address_n = prompt("BIP-32 path to derive the key", type=tools.parse_path)
reported_type = tx_json["vout"][prev_index]["scriptPubKey"].get("type")
if reported_type in BITCOIN_CORE_INPUT_TYPES:
script_type = BITCOIN_CORE_INPUT_TYPES[reported_type]
click.echo(f"Script type: {script_type.name}")
else:
script_type = prompt(
"Input type",
type=ChoiceType(INPUT_SCRIPTS),
default=_default_script_type(address_n, INPUT_SCRIPTS),
)
if isinstance(script_type, str):
script_type = INPUT_SCRIPTS[script_type]
sequence = prompt( sequence = prompt(
"Sequence Number to use (RBF opt-in enabled by default)", "Sequence Number to use (RBF opt-in enabled by default)",
type=int, type=int,
default=0xFFFFFFFD, default=0xFFFFFFFD,
) )
script_type = prompt(
"Input type",
type=ChoiceType(INPUT_SCRIPTS),
default=_default_script_type(address_n, INPUT_SCRIPTS),
)
if isinstance(script_type, str):
script_type = INPUT_SCRIPTS[script_type]
new_input = messages.TxInputType( new_input = messages.TxInputType(
address_n=address_n, address_n=address_n,
@ -151,7 +173,7 @@ def sign_interactive():
coin = prompt("Coin name", default="Bitcoin") coin = prompt("Coin name", default="Bitcoin")
blockbook_host = prompt("Blockbook server", default="btc1.trezor.io") blockbook_host = prompt("Blockbook server", default="btc1.trezor.io")
if not requests.get(f"https://{blockbook_host}/api").ok: if not SESSION.get(f"https://{blockbook_host}/api/block/1").ok:
raise click.ClickException("Could not connect to blockbook") raise click.ClickException("Could not connect to blockbook")
blockbook_url = f"https://{blockbook_host}/api/tx-specific/" blockbook_url = f"https://{blockbook_host}/api/tx-specific/"