def tx_inputs(self, tx: Transaction, *, for_sig=False, keystore: 'TrezorKeyStore' = None): inputs = [] for txin in tx.inputs(): if txin.is_coinbase_input(): txinputtype = TxInputType( prev_hash=b"\x00"*32, prev_index=0xffffffff, # signed int -1 ) else: txinputtype = TxInputType( prev_hash=txin.prevout.txid, prev_index=txin.prevout.out_idx, ) if for_sig: assert isinstance(tx, PartialTransaction) assert isinstance(txin, PartialTxInput) assert keystore if len(txin.pubkeys) > 1: xpubs_and_deriv_suffixes = get_xpubs_and_der_suffixes_from_txinout(tx, txin) txinputtype.multisig = self._make_multisig(txin.num_sig, xpubs_and_deriv_suffixes) txinputtype.script_type = self.get_trezor_input_script_type(txin.script_type) my_pubkey, full_path = keystore.find_my_pubkey_in_txinout(txin) if full_path: txinputtype.address_n = full_path txinputtype.amount = txin.value_sats() txinputtype.script_sig = txin.script_sig txinputtype.sequence = txin.nsequence inputs.append(txinputtype) return inputs
def tx_inputs(self, tx: Transaction, *, for_sig=False, keystore: 'TrezorKeyStore' = None): inputs = [] for txin in tx.inputs(): txinputtype = TxInputType() if txin.is_coinbase_input(): prev_hash = b"\x00" * 32 if txin.script_sig.startswith( b"\xc4") and txin.nsequence == 0xffffffff: prev_index = 1 else: prev_index = txin.nsequence else: if for_sig: assert isinstance(tx, PartialTransaction) assert isinstance(txin, PartialTxInput) assert keystore if len(txin.pubkeys) > 1: xpubs_and_deriv_suffixes = get_xpubs_and_der_suffixes_from_txinout( tx, txin) multisig = self._make_multisig( txin.num_sig, xpubs_and_deriv_suffixes) else: multisig = None script_type = self.get_trezor_input_script_type( txin.script_type) txinputtype = TxInputType(script_type=script_type, multisig=multisig) my_pubkey, full_path = keystore.find_my_pubkey_in_txinout( txin) if full_path: txinputtype.address_n = full_path prev_hash = txin.prevout.txid prev_index = txin.prevout.out_idx if txin.value_sats() is not None: txinputtype.amount = txin.value_sats() txinputtype.prev_hash = prev_hash txinputtype.prev_index = prev_index if txin.script_sig is not None: txinputtype.script_sig = txin.script_sig txinputtype.sequence = txin.nsequence inputs.append(txinputtype) return inputs
def tx_inputs(self, tx, xpub_path): inputs = [] for txin in tx.inputs: txinputtype = TxInputType() txinputtype.prev_hash = bytes(reversed(txin.prev_hash)) txinputtype.prev_index = txin.prev_idx txinputtype.sequence = txin.sequence txinputtype.amount = txin.value xpubs = [ x_pubkey.bip32_extended_key_and_path() for x_pubkey in txin.x_pubkeys ] txinputtype.multisig = self._make_multisig( txin.threshold, xpubs, txin.stripped_signatures_with_blanks()) txinputtype.script_type = self.get_trezor_input_script_type( txinputtype.multisig is not None) # find which key is mine for xpub, path in xpubs: if xpub in xpub_path: xpub_n = bip32_decompose_chain_string(xpub_path[xpub]) txinputtype.address_n = xpub_n + path break # if txin.script_sig: # txinputtype.script_sig = bytes(txin.script_sig) inputs.append(txinputtype) return inputs
def tx_inputs(self, tx, xpub_path, for_sig=False): inputs = [] for txin in tx.inputs(): txinputtype = TxInputType() if txin['type'] == 'coinbase': prev_hash = b"\x00" * 32 if txin['scriptSig'].startswith( 'c4') and txin['prevout_n'] == 0xffffffff: prev_index = 1 else: prev_index = txin['prevout_n'] else: if for_sig: x_pubkeys = txin['x_pubkeys'] xpubs = [parse_xpubkey(x) for x in x_pubkeys] multisig = self._make_multisig(txin.get('num_sig'), xpubs, txin.get('signatures')) script_type = self.get_trezor_input_script_type( txin['type']) txinputtype = TxInputType(script_type=script_type, multisig=multisig) # find which key is mine for xpub, deriv in xpubs: if xpub in xpub_path: xpub_n = parse_path(xpub_path[xpub]) txinputtype.address_n = xpub_n + deriv break prev_hash = bfh(txin['prevout_hash']) prev_index = txin['prevout_n'] if 'value' in txin: txinputtype.amount = txin['value'] txinputtype.prev_hash = prev_hash txinputtype.prev_index = prev_index if txin.get('scriptSig') is not None: script_sig = bfh(txin['scriptSig']) txinputtype.script_sig = script_sig txinputtype.sequence = txin.get('sequence', 0xffffffff - 1) inputs.append(txinputtype) return inputs
def utxo_to_input(utxo, script_type): return TxInputType( amount=int(utxo.value), address_n=utxo.address.path, script_type=script_type, prev_hash=bytes.fromhex(utxo.tx["txid"]), prev_index=utxo.vout, sequence=0xFFFF_FFFD, )
def tx_inputs(self, tx: Transaction, xpub_path: Optional[Dict[str, str]] = None, is_prev_tx: bool = False) -> List[TxInputType]: inputs = [] for txin in tx.inputs: txinputtype = TxInputType() # Trezor tx hashes are same byte order as the reversed hex tx id. txinputtype.prev_hash = bytes(reversed(txin.prev_hash)) txinputtype.prev_index = txin.prev_idx txinputtype.sequence = txin.sequence txinputtype.amount = txin.value if txin.script_sig: txinputtype.script_sig = bytes(txin.script_sig) if not is_prev_tx: assert xpub_path is not None, "no xpubs provided for hw signing operation" xpubs = [ x_pubkey.bip32_extended_key_and_path() for x_pubkey in txin.x_pubkeys ] txinputtype.multisig = self._make_multisig( txin.threshold, xpubs, txin.stripped_signatures_with_blanks()) txinputtype.script_type = self.get_trezor_input_script_type( txinputtype.multisig is not None) # find which key is mine for xpub, path in xpubs: if xpub in xpub_path: xpub_n = tuple( bip32_decompose_chain_string(xpub_path[xpub])) # Sequences cannot be added according to mypy, annoying.. txinputtype.address_n = xpub_n + path # type: ignore break inputs.append(txinputtype) return inputs