# # build_psbt.py - create a PSBT from (unsigned) transaction and keystore data. # import io import struct from binascii import a2b_hex, b2a_hex from struct import pack, unpack from electrum.transaction import (Transaction, multisig_script, parse_redeemScript_multisig, NotRecognizedRedeemScript) from electrum.logging import get_logger from electrum.wallet import Standard_Wallet, Multisig_Wallet, Abstract_Wallet from electrum.keystore import xpubkey_to_pubkey, Xpub from electrum.util import bfh, bh2u from electrum.crypto import hash_160, sha256 from electrum.bitcoin import DecodeBase58Check from electrum.i18n import _ from .basic_psbt import ( PSBT_GLOBAL_UNSIGNED_TX, PSBT_GLOBAL_XPUB, PSBT_IN_NON_WITNESS_UTXO, PSBT_IN_WITNESS_UTXO, PSBT_IN_SIGHASH_TYPE, PSBT_IN_REDEEM_SCRIPT, PSBT_IN_WITNESS_SCRIPT, PSBT_IN_PARTIAL_SIG, PSBT_IN_BIP32_DERIVATION, PSBT_OUT_BIP32_DERIVATION, PSBT_OUT_REDEEM_SCRIPT, PSBT_OUT_WITNESS_SCRIPT) from .basic_psbt import BasicPSBT _logger = get_logger(__name__) def xfp2str(xfp): # Standardized way to show an xpub's fingerprint... it's a 4-byte string # and not really an integer. Used to show as '0x%08x' but that's wrong endian. return b2a_hex(pack(' fingerprint LE32 # a/b/c => ints # # 2) all used keys in transaction: # - for all inputs and outputs (when its change back) # - for all keystores, if multisig # subkeys = {} for ks in wallet.get_keystores(): # XFP + fixed prefix for this keystore ks_prefix = packed_xfp_path_for_keystore(ks) # all pubkeys needed for input signing for xpubkey, derivation in ks.get_tx_derivations(tx).items(): pubkey = xpubkey_to_pubkey(xpubkey) # assuming depth two, non-harded: change + index aa, bb = derivation assert 0 <= aa < 0x80000000 and 0 <= bb < 0x80000000 subkeys[bfh(pubkey)] = ks_prefix + pack(' Transaction: # Take a PSBT object and re-construct the Electrum transaction object. # - does not include signatures, see merge_sigs_from_psbt # - any PSBT in the group could be used for this purpose; all must share tx details tx = Transaction(first.txn.hex()) tx.deserialize(force_full_parse=True) # .. add back some data that's been preserved in the PSBT, but isn't part of # of the unsigned bitcoin txn tx.is_partial_originally = True for idx, inp in enumerate(tx.inputs()): scr = first.inputs[idx].redeem_script or first.inputs[idx].witness_script # XXX should use transaction.py parse_scriptSig() here! if scr: try: M, N, __, pubkeys, __ = parse_redeemScript_multisig(scr) except NotRecognizedRedeemScript: # limitation: we can only handle M-of-N multisig here raise ValueError("Cannot handle non M-of-N multisig input") inp['pubkeys'] = pubkeys inp['x_pubkeys'] = pubkeys inp['num_sig'] = M inp['type'] = 'p2wsh' if first.inputs[idx].witness_script else 'p2sh' # bugfix: transaction.py:parse_input() puts empty dict here, but need a list inp['signatures'] = [None] * N if 'prev_tx' not in inp: # fetch info about inputs' previous txn wallet.add_hw_info(tx) if 'value' not in inp: # we'll need to know the value of the outpts used as part # of the witness data, much later... inp['value'] = inp['prev_tx'].outputs()[inp['prevout_n']].value return tx def merge_sigs_from_psbt(tx: Transaction, psbt: BasicPSBT): # Take new signatures from PSBT, and merge into in-memory transaction object. # - "we trust everyone here" ... no validation/checks count = 0 for inp_idx, inp in enumerate(psbt.inputs): if not inp.part_sigs: continue scr = inp.redeem_script or inp.witness_script # need to map from pubkey to signing position in redeem script M, N, _, pubkeys, _ = parse_redeemScript_multisig(scr) #assert (M, N) == (wallet.m, wallet.n) for sig_pk in inp.part_sigs: pk_pos = pubkeys.index(sig_pk.hex()) tx.add_signature_to_txin(inp_idx, pk_pos, inp.part_sigs[sig_pk].hex()) count += 1 #print("#%d: sigs = %r" % (inp_idx, tx.inputs()[inp_idx]['signatures'])) # reset serialization of TX tx.raw = tx.serialize() tx.raw_psbt = None return count # EOF