mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-18 12:28:09 +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:
parent
57fbf7aa61
commit
428caf3d35
1
python/.changelog.d/1896.fixed
Normal file
1
python/.changelog.d/1896.fixed
Normal file
@ -0,0 +1 @@
|
|||||||
|
Updated `tools/build_tx.py` to work with Blockbook's API protections.
|
@ -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,14 +88,21 @@ 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}")
|
||||||
|
|
||||||
sequence = prompt(
|
address_n = prompt("BIP-32 path to derive the key", type=tools.parse_path)
|
||||||
"Sequence Number to use (RBF opt-in enabled by default)",
|
|
||||||
type=int,
|
reported_type = tx_json["vout"][prev_index]["scriptPubKey"].get("type")
|
||||||
default=0xFFFFFFFD,
|
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(
|
script_type = prompt(
|
||||||
"Input type",
|
"Input type",
|
||||||
type=ChoiceType(INPUT_SCRIPTS),
|
type=ChoiceType(INPUT_SCRIPTS),
|
||||||
@ -95,6 +111,12 @@ def _get_inputs_interactive(blockbook_url):
|
|||||||
if isinstance(script_type, str):
|
if isinstance(script_type, str):
|
||||||
script_type = INPUT_SCRIPTS[script_type]
|
script_type = INPUT_SCRIPTS[script_type]
|
||||||
|
|
||||||
|
sequence = prompt(
|
||||||
|
"Sequence Number to use (RBF opt-in enabled by default)",
|
||||||
|
type=int,
|
||||||
|
default=0xFFFFFFFD,
|
||||||
|
)
|
||||||
|
|
||||||
new_input = messages.TxInputType(
|
new_input = messages.TxInputType(
|
||||||
address_n=address_n,
|
address_n=address_n,
|
||||||
prev_hash=prev_hash,
|
prev_hash=prev_hash,
|
||||||
@ -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/"
|
||||||
|
Loading…
Reference in New Issue
Block a user