def mock_fork(self, bad_header): forkpoint = bad_header['block_height'] b = blockchain.Blockchain(config=self.config, forkpoint=forkpoint, parent=None, forkpoint_hash=bh2u(sha256(str(forkpoint))), prev_hash=bh2u(sha256(str(forkpoint - 1)))) return b
def _register_multisig_wallet(wallet, keystore, address): wallet_fingerprint_hash = sha256(wallet.get_fingerprint()) multisig_name = 'ele' + wallet_fingerprint_hash.hex()[:12] # Collect all the signer data in case we need to register the # multisig wallet on the Jade hw - NOTE: re-register is a no-op. signers = [] for kstore in wallet.get_keystores(): fingerprint = kstore.get_root_fingerprint() bip32_path_prefix = kstore.get_derivation_prefix() derivation_path = bip32.convert_bip32_path_to_list_of_uint32( bip32_path_prefix) # Jade only understands standard xtypes, so convert here node = bip32.BIP32Node.from_xkey(kstore.xpub) standard_xpub = node._replace(xtype='standard').to_xkey() signers.append({ 'fingerprint': bytes.fromhex(fingerprint), 'derivation': derivation_path, 'xpub': standard_xpub, 'path': [] }) # Check multisig is registered - re-registering is a no-op # NOTE: electrum multisigs appear to always be sorted-multisig txin_type = wallet.get_txin_type(address) keystore.register_multisig(multisig_name, txin_type, True, wallet.m, signers) # Return the name used to register the wallet return multisig_name
def update(self): current_schedule = read_schedule_file() _list = current_schedule.get('items', []) self.model().clear() self.update_headers(self.__class__.headers) for idx, cron_item in enumerate(_list): item = cron_item['invoice'] schedule = cron_item['schedule'] invoice_type = item['type'] if invoice_type == PR_TYPE_LN: key = item['rhash'] icon_name = 'lightning.png' elif invoice_type == PR_TYPE_ONCHAIN: key = bh2u(sha256(repr(item))[0:16]) icon_name = 'bitcoin.png' if item.get('bip70'): icon_name = 'seal.png' else: raise Exception('Unsupported type') address_str = item['outputs'][0][1] message = item['message'] amount = item['amount'] amount_str = self.parent.format_amount(amount, whitespaces=True) last_str = '' next_str = '' try: from croniter import croniter cron = '{minute} {hour} {day} {month} {week}'.format(**schedule) item = croniter(cron, datetime.now()) next_str = item.get_next(datetime).isoformat() except: pass labels = [address_str, amount_str, message, last_str, next_str, ] items = [QStandardItem(e) for e in labels] self.set_editability(items) items[self.Columns.ADDRESS].setIcon(read_QIcon(icon_name)) items[self.Columns.ADDRESS].setData(key, role=ROLE_REQUEST_ID) items[self.Columns.ADDRESS].setData(invoice_type, role=ROLE_REQUEST_TYPE) self.model().insertRow(idx, items) self.selectionModel().select(self.model().index(0,0), QItemSelectionModel.SelectCurrent) self.filter()
def mock_fork(self, bad_header): forkpoint = bad_header['block_height'] b = blockchain.Blockchain(config=self.config, forkpoint=forkpoint, parent=None, forkpoint_hash=bh2u(sha256(str(forkpoint))), prev_hash=bh2u(sha256(str(forkpoint-1)))) return b
def make_long_id(xpub_hot, xpub_cold): return sha256(''.join(sorted([xpub_hot, xpub_cold])))
def make_long_id(xpub_hot, xpub_cold): return sha256(''.join(sorted([xpub_hot, xpub_cold])))
def build_psbt(tx: Transaction, wallet: Abstract_Wallet): # Render a PSBT file, for possible upload to Coldcard. # # TODO this should be part of Wallet object, or maybe Transaction? if getattr(tx, 'raw_psbt', False): _logger.info('PSBT cache hit') return tx.raw_psbt 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 wallet.add_hw_info(tx) # wallet.add_hw_info installs this attr assert tx.output_info is not None, 'need data about outputs' # Build a map of all pubkeys needed as derivation from master XFP, in PSBT binary format # 1) binary version of the common subpath for all keys # m/ => 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('<II', aa, bb) # all keys related to change outputs for o in tx.outputs(): if o.address in tx.output_info: # this address "is_mine" but might not be change (if I send funds to myself) output_info = tx.output_info.get(o.address) if not output_info.is_change: continue chg_path = output_info.address_index assert chg_path[0] == 1 and len( chg_path) == 2, f"unexpected change path: {chg_path}" pubkey = ks.derive_pubkey(True, chg_path[1]) subkeys[bfh(pubkey)] = ks_prefix + pack('<II', *chg_path) for txin in inputs: assert txin['type'] != 'coinbase', _("Coinbase not supported") if txin['type'] in ['p2sh', 'p2wsh-p2sh', 'p2wsh']: assert type(wallet) is Multisig_Wallet # 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) if type(wallet) is Multisig_Wallet: # always put the xpubs into the PSBT, useful at least for checking for xp, ks in zip(wallet.get_master_public_keys(), wallet.get_keystores()): ks_prefix = packed_xfp_path_for_keystore(ks) write_kv(PSBT_GLOBAL_XPUB, ks_prefix, DecodeBase58Check(xp)) # 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] if type(wallet) is Multisig_Wallet: # always need a redeem script for multisig scr = Transaction.get_preimage_script(txin) if Transaction.is_segwit_input(txin): # needed for both p2wsh-p2sh and p2wsh write_kv(PSBT_IN_WITNESS_SCRIPT, bfh(scr)) else: write_kv(PSBT_IN_REDEEM_SCRIPT, bfh(scr)) sigs = txin.get('signatures') for pk_pos, (pubkey, x_pubkey) in enumerate(zip(pubkeys, x_pubkeys)): if pubkey in subkeys: # faster? case ... calculated above write_kv(PSBT_IN_BIP32_DERIVATION, subkeys[pubkey], pubkey) else: # when an input is partly signed, tx.get_tx_derivations() # doesn't include that keystore's value and yet we need it # because we need to show a correct keypath... assert x_pubkey[0:2] == 'ff', x_pubkey for ks in wallet.get_keystores(): d = ks.get_pubkey_derivation(x_pubkey) if d is not None: ks_path = packed_xfp_path_for_keystore(ks, d) write_kv(PSBT_IN_BIP32_DERIVATION, ks_path, pubkey) break else: raise AssertionError("no keystore for: %s" % x_pubkey) if txin['type'] == 'p2wpkh-p2sh': assert len( pubkeys) == 1, 'can be only one redeem script per input' pa = hash_160(pubkey) assert len(pa) == 20 write_kv(PSBT_IN_REDEEM_SCRIPT, b'\x00\x14' + pa) # optional? insert (partial) signatures that we already have if sigs and sigs[pk_pos]: write_kv(PSBT_IN_PARTIAL_SIG, bfh(sigs[pk_pos]), pubkey) 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 (if I send funds to myself) output_info = tx.output_info.get(o.address) if output_info.is_change: pubkeys = [bfh(i) for i in wallet.get_public_keys(o.address)] # Add redeem/witness script? if type(wallet) is Multisig_Wallet: # always need a redeem script for multisig cases scr = bfh( multisig_script([bh2u(i) for i in sorted(pubkeys)], wallet.m)) if output_info.script_type == 'p2wsh-p2sh': write_kv(PSBT_OUT_WITNESS_SCRIPT, scr) write_kv(PSBT_OUT_REDEEM_SCRIPT, b'\x00\x20' + sha256(scr)) elif output_info.script_type == 'p2wsh': write_kv(PSBT_OUT_WITNESS_SCRIPT, scr) elif output_info.script_type == 'p2sh': write_kv(PSBT_OUT_REDEEM_SCRIPT, scr) else: raise ValueError(output_info.script_type) elif output_info.script_type == 'p2wpkh-p2sh': # need a redeem script when P2SH is used to wrap p2wpkh assert len(pubkeys) == 1 pa = hash_160(pubkeys[0]) write_kv(PSBT_OUT_REDEEM_SCRIPT, b'\x00\x14' + pa) # Document change output's bip32 derivation(s) for pubkey in pubkeys: sk = subkeys[pubkey] write_kv(PSBT_OUT_BIP32_DERIVATION, sk, pubkey) out_fd.write(b'\x00') # capture for later use tx.raw_psbt = out_fd.getvalue() return tx.raw_psbt
wif = 'cNyQjVGD6ojbLFu1UCapLCM836kCrgMiC4qpVTV9CUx8kVc5kVGQ' txid = x('ef3fad03b7fd4fe42956e41fccb10ef1a95d98083d3b9246b6c17a88e51c8def') vout = 1 sats = 10_000 sequence = 0 # in retrospect "-3" in two's complement may be better address = 'tb1q5rn69avl3ganw3cmhz5ldcxpash2kusq7sncfl' sats_less_fees = sats - 500 locktime = 1602572140 # Build the Transaction Input _, privkey, compressed = deserialize_privkey(wif) pubkey = ECPrivkey(privkey).get_public_key_hex(compressed=compressed) expiry = b2x(lx(b2x(locktime))) witness_script = compile( [expiry, 'OP_CHECKLOCKTIMEVERIFY', 'OP_DROP', pubkey, 'OP_CHECKSIG']) script_hash = b2x(sha256(x(witness_script))) hodl_address = bech32_encode(script_hash) prevout = TxOutpoint(txid=txid, out_idx=vout) txin = PartialTxInput(prevout=prevout) txin._trusted_value_sats = sats txin.nsequence = sequence txin.script_sig = x(compile([])) # empty script (important!) txin.witness_script = x(witness_script) # Build the Transaction Output txout = PartialTxOutput.from_address_and_value(address, sats_less_fees) # Build and sign the transaction tx = PartialTransaction.from_io([txin], [txout], locktime=locktime) tx.version = 1 txin_index = 0