Skip to content

Commit

Permalink
Merge #602: trezor: Look for change keys based on output type
Browse files Browse the repository at this point in the history
1db8d0c trezor: Search for Taproot change specifically (Andrew Chow)

Pull request description:

  Search the correct derivation paths map depending on the type of script detected. For non-segwit and segwit v0. search `hd_keypaths`. For segwit v1, search `tap_bip32_paths`.

  Fixes #591

Top commit has no ACKs.

Tree-SHA512: b3def69b31a3b22b9217c43e07db5812648b35c6ccd38905fcfec95da3acd8a76765562967736f10688bdedc453e524116a4177c99bb80cc271f5dd9f1609523
  • Loading branch information
achow101 committed May 5, 2022
2 parents 72f108d + 1db8d0c commit ed62dda
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions hwilib/devices/trezor.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,38 +528,46 @@ def ignore_input() -> None:
out = psbt_out.get_txout()
txoutput = messages.TxOutputType(amount=out.nValue)
txoutput.script_type = messages.OutputScriptType.PAYTOADDRESS
if out.is_p2pkh():
wit, ver, prog = out.is_witness()
if wit:
txoutput.address = bech32.encode(bech32_hrp, ver, prog)
elif out.is_p2pkh():
txoutput.address = to_address(out.scriptPubKey[3:23], p2pkh_version)
elif out.is_p2sh():
txoutput.address = to_address(out.scriptPubKey[2:22], p2sh_version)
elif out.is_opreturn():
txoutput.script_type = messages.OutputScriptType.PAYTOOPRETURN
txoutput.op_return_data = out.scriptPubKey[2:]
else:
wit, ver, prog = out.is_witness()
if wit:
txoutput.address = bech32.encode(bech32_hrp, ver, prog)
else:
raise BadArgumentError("Output is not an address")
raise BadArgumentError("Output is not an address")

# Add the derivation path for change
for _, keypath in psbt_out.hd_keypaths.items():
if keypath.fingerprint != master_fp:
continue
wit, ver, prog = out.is_witness()
if out.is_p2pkh():
txoutput.address_n = keypath.path
txoutput.address = None
elif wit:
txoutput.script_type = messages.OutputScriptType.PAYTOWITNESS
txoutput.address_n = keypath.path
txoutput.address = None
elif out.is_p2sh() and psbt_out.redeem_script:
wit, ver, prog = CTxOut(0, psbt_out.redeem_script).is_witness()
if wit and len(prog) in [20, 32]:
txoutput.script_type = messages.OutputScriptType.PAYTOP2SHWITNESS
if not wit or (wit and ver == 0):
for _, keypath in psbt_out.hd_keypaths.items():
if keypath.fingerprint != master_fp:
continue
wit, ver, prog = out.is_witness()
if out.is_p2pkh():
txoutput.address_n = keypath.path
txoutput.address = None
elif wit:
txoutput.script_type = messages.OutputScriptType.PAYTOWITNESS
txoutput.address_n = keypath.path
txoutput.address = None
elif out.is_p2sh() and psbt_out.redeem_script:
wit, ver, prog = CTxOut(0, psbt_out.redeem_script).is_witness()
if wit and len(prog) in [20, 32]:
txoutput.script_type = messages.OutputScriptType.PAYTOP2SHWITNESS
txoutput.address_n = keypath.path
txoutput.address = None
elif wit and ver == 1:
for key, (leaf_hashes, origin) in psbt_out.tap_bip32_paths.items():
# TODO: Support script path change
if key == psbt_out.tap_internal_key and origin.fingerprint == master_fp:
txoutput.address_n = origin.path
txoutput.script_type = messages.OutputScriptType.PAYTOTAPROOT
txoutput.address = None
break

# add multisig info
if psbt_out.witness_script or psbt_out.redeem_script:
Expand Down

0 comments on commit ed62dda

Please sign in to comment.