def is_any_tx_output_on_change_branch(tx: Transaction): if not tx.output_info: return False for o in tx.outputs(): info = tx.output_info.get(o.address) if info is not None: if info.address_index[0] == 1: return True return False
def is_any_tx_output_on_change_branch(tx: Transaction): if not tx.output_info: return False for o in tx.outputs(): info = tx.output_info.get(o.address) if info is not None: if info.address_index[0] == 1: return True return False
def build_psbt(self, tx: Transaction, wallet=None, xfp=None): # Render a PSBT file, for upload to Coldcard. # if xfp is None: # need fingerprint of MASTER xpub, not the derived key xfp = self.ckcc_xfp inputs = tx.inputs() if 'prev_tx' not in inputs[0]: # fetch info about inputs, if needed? # - needed during export PSBT flow, not normal online signing assert wallet, 'need wallet reference' wallet.add_hw_info(tx) # wallet.add_hw_info installs this attr assert tx.output_info is not None, 'need data about outputs' # Build map of pubkey needed as derivation from master, in PSBT binary format # 1) binary version of the common subpath for all keys # m/ => fingerprint LE32 # a/b/c => ints base_path = pack('<I', xfp) for x in self.get_derivation()[2:].split('/'): if x.endswith("'"): x = int(x[:-1]) | 0x80000000 else: x = int(x) base_path += pack('<I', x) # 2) all used keys in transaction subkeys = {} derivations = self.get_tx_derivations(tx) for xpubkey in derivations: pubkey = xpubkey_to_pubkey(xpubkey) # assuming depth two, non-harded: change + index aa, bb = derivations[xpubkey] assert 0 <= aa < 0x80000000 assert 0 <= bb < 0x80000000 subkeys[bfh(pubkey)] = base_path + pack('<II', aa, bb) for txin in inputs: if txin['type'] == 'coinbase': self.give_error("Coinbase not supported") if txin['type'] in ['p2sh', 'p2wsh-p2sh', 'p2wsh']: self.give_error('No support yet for inputs of type: ' + txin['type']) # Construct PSBT from start to finish. out_fd = io.BytesIO() out_fd.write(b'psbt\xff') def write_kv(ktype, val, key=b''): # serialize helper: write w/ size and key byte out_fd.write(my_var_int(1 + len(key))) out_fd.write(bytes([ktype]) + key) if isinstance(val, str): val = bfh(val) out_fd.write(my_var_int(len(val))) out_fd.write(val) # global section: just the unsigned txn class CustomTXSerialization(Transaction): @classmethod def input_script(cls, txin, estimate_size=False): return '' unsigned = bfh(CustomTXSerialization(tx.serialize()).serialize_to_network(witness=False)) write_kv(PSBT_GLOBAL_UNSIGNED_TX, unsigned) # end globals section out_fd.write(b'\x00') # inputs section for txin in inputs: if Transaction.is_segwit_input(txin): utxo = txin['prev_tx'].outputs()[txin['prevout_n']] spendable = txin['prev_tx'].serialize_output(utxo) write_kv(PSBT_IN_WITNESS_UTXO, spendable) else: write_kv(PSBT_IN_NON_WITNESS_UTXO, str(txin['prev_tx'])) pubkeys, x_pubkeys = tx.get_sorted_pubkeys(txin) pubkeys = [bfh(k) for k in pubkeys] for k in pubkeys: write_kv(PSBT_IN_BIP32_DERIVATION, subkeys[k], k) if txin['type'] == 'p2wpkh-p2sh': assert len(pubkeys) == 1, 'can be only one redeem script per input' pa = hash_160(k) assert len(pa) == 20 write_kv(PSBT_IN_REDEEM_SCRIPT, b'\x00\x14'+pa) out_fd.write(b'\x00') # outputs section for o in tx.outputs(): # can be empty, but must be present, and helpful to show change inputs # wallet.add_hw_info() adds some data about change outputs into tx.output_info if o.address in tx.output_info: # this address "is_mine" but might not be change (I like to sent to myself) output_info = tx.output_info.get(o.address) index, xpubs = output_info.address_index, output_info.sorted_xpubs if index[0] == 1 and len(index) == 2: # it is a change output (based on our standard derivation path) assert len(xpubs) == 1 # not expecting multisig xpubkey = xpubs[0] # document its bip32 derivation in output section aa, bb = index assert 0 <= aa < 0x80000000 assert 0 <= bb < 0x80000000 deriv = base_path + pack('<II', aa, bb) pubkey = bfh(self.get_pubkey_from_xpub(xpubkey, index)) write_kv(PSBT_OUT_BIP32_DERIVATION, deriv, pubkey) if output_info.script_type == 'p2wpkh-p2sh': pa = hash_160(pubkey) assert len(pa) == 20 write_kv(PSBT_OUT_REDEEM_SCRIPT, b'\x00\x14' + pa) out_fd.write(b'\x00') return out_fd.getvalue()