def load_tx(get_txs_func, rawtx): Tx.ALLOW_SEGWIT = False # FIXME remove on next pycoin version tx = Tx.from_hex(rawtx) unspent_info = {} # txid -> index for txin in tx.txs_in: unspent_info[b2h_rev(txin.previous_hash)] = txin.previous_index utxo_rawtxs = get_txs_func(list(unspent_info.keys())) for utxo_txid, utxo_rawtx in utxo_rawtxs.items(): utxo_tx = Tx.from_hex(utxo_rawtx) prev_index = unspent_info[utxo_txid] tx.unspents.append(utxo_tx.txs_out[prev_index]) return tx
def get_tx(self, tx_hash): url = "%s/rawtx/%s" % (self.base_url, b2h_rev(tx_hash)) result = json.loads(urlopen(url).read().decode("utf8")) tx = Tx.from_hex(result["rawtx"]) if tx.hash() == tx_hash: return tx return None
def sign_tx(certificate_metadata, last_input, allowable_wif_prefixes=None): """sign the transaction with private key""" with open(certificate_metadata.unsigned_tx_file_name, 'rb') as in_file: hextx = str(in_file.read(), 'utf-8') logging.info( 'Signing tx with private key for recipient id: %s ...', certificate_metadata.uid) tx = Tx.from_hex(hextx) if allowable_wif_prefixes: wif = wif_to_secret_exponent( helpers.import_key(), allowable_wif_prefixes) else: wif = wif_to_secret_exponent(helpers.import_key()) lookup = build_hash160_lookup([wif]) tx.set_unspents( [TxOut(coin_value=last_input.amount, script=last_input.script_pub_key)]) signed_tx = tx.sign(lookup) signed_hextx = signed_tx.as_hex() with open(certificate_metadata.unsent_tx_file_name, 'wb') as out_file: out_file.write(bytes(signed_hextx, 'utf-8')) logging.info('Finished signing transaction for certificate uid=%s', certificate_metadata.uid)
def get_tx(self, tx_hash): url = "%s/rawtx/%s" % (self.base_url, b2h_rev(tx_hash)) result = json.loads(urlopen(url).read().decode("utf8")) tx = Tx.from_hex(result["rawtx"]) if tx.hash() == tx_hash: return tx return None
def get_tx(self, tx_hash): URL = "%s/api/rawtx/%s" % (self.base_url, b2h_rev(tx_hash)) r = json.loads(urlopen(URL).read().decode("utf8")) tx = Tx.from_hex(r['rawtx']) if tx.hash() == tx_hash: return tx return None
def sign_tx(hextx, tx_input, allowable_wif_prefixes=None): """ Sign the transaction with private key :param hextx: :param tx_input: :param allowable_wif_prefixes: :return: """ logging.info('Signing tx with private key') tx = Tx.from_hex(hextx) if allowable_wif_prefixes: wif = wif_to_secret_exponent( helpers.import_key(), allowable_wif_prefixes) else: wif = wif_to_secret_exponent(helpers.import_key()) lookup = build_hash160_lookup([wif]) tx.set_unspents( [TxOut(coin_value=tx_input.amount, script=tx_input.script_pub_key)]) signed_tx = tx.sign(lookup) signed_hextx = signed_tx.as_hex() logging.info('Finished signing transaction') return signed_hextx
def sign_tx(certificate_metadata, last_input, allowable_wif_prefixes=None): """sign the transaction with private key""" with open(certificate_metadata.unsigned_tx_file_name, 'rb') as in_file: hextx = str(in_file.read(), 'utf-8') logging.info('Signing tx with private key for recipient id: %s ...', certificate_metadata.uid) tx = Tx.from_hex(hextx) if allowable_wif_prefixes: wif = wif_to_secret_exponent(helpers.import_key(), allowable_wif_prefixes) else: wif = wif_to_secret_exponent(helpers.import_key()) lookup = build_hash160_lookup([wif]) tx.set_unspents([ TxOut(coin_value=last_input.amount, script=last_input.script_pub_key) ]) signed_tx = tx.sign(lookup) signed_hextx = signed_tx.as_hex() with open(certificate_metadata.unsent_tx_file_name, 'wb') as out_file: out_file.write(bytes(signed_hextx, 'utf-8')) logging.info('Finished signing transaction for certificate uid=%s', certificate_metadata.uid)
def prepare_tx_for_signing(hex_tx, tx_inputs): logging.info('Preparing tx for signing') transaction = Tx.from_hex(hex_tx) unspents = [ TxOut(coin_value=tx_input.coin_value, script=tx_input.script) for tx_input in tx_inputs ] transaction.set_unspents(unspents) return transaction
def test_broadcast_tx_mainnet(self): """ Broadcasting tests fail because the transaction has already been pushed. So we're looking for a 'transaction already in blockchain' error """ try: tx = Tx.from_hex(MAINNET_TX) broadcast_tx(tx) self.assertTrue(False) except Exception as e: self.assertTrue('already in block chain', str(e.args[0])) return self.assertTrue(False)
def test_broadcast_tx_mainnet(self): """ Broadcasting tests fail because the transaction has already been pushed. So we're looking for a 'transaction already in blockchain' error """ try: tx = Tx.from_hex(MAINNET_TX) broadcast_tx(tx) self.assertTrue(False) except Exception as e: self.assertTrue('already in block chain', str(e.args[0])) return self.assertTrue(False)
def broadcastTransaction (self, transaction): t = Tx.from_hex (transaction) h = t.id () if not h in self.db['mempool']: mp = self.db['mempool'] mp[h] = t self.db['mempool'] = mp self.db.sync () self.mempooltimer = Timer (1.0, self.announceTransactions) self.mempooltimer.start () return h
def broadcastTransaction (self, transaction): t = Tx.from_hex (transaction) h = t.id () if not h in self.db['mempool']: mp = self.db['mempool'] mp[h] = t self.db['mempool'] = mp self.db.sync () self.mempooltimer = Timer (1.0, self.announceTransactions) self.mempooltimer.start () return h
def test_sign_bitcoind_partially_signed_2_of_2(self): # Finish signing a 2 of 2 transaction, that already has one signature signed by bitcoind # This tx can be found on testnet3 blockchain, txid: 9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2 raw_script = "522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452ae" p2sh_lookup = build_p2sh_lookup([h2b(raw_script)]) partially_signed_raw_tx = "010000000196238f11a5fd3ceef4efd5a186a7e6b9217d900418e72aca917cd6a6e634e74100000000910047304402201b41b471d9dd93cf97eed7cfc39a5767a546f6bfbf3e0c91ff9ad23ab9770f1f02205ce565666271d055be1f25a7e52e34cbf659f6c70770ff59bd783a6fcd1be3dd0147522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452aeffffffff01a0bb0d00000000001976a9143b3beefd6f7802fa8706983a76a51467bfa36f8b88ac00000000" tx = Tx.from_hex(partially_signed_raw_tx) tx_out = TxOut(1000000, h2b("a914a10dfa21ee8c33b028b92562f6fe04e60563d3c087")) tx.set_unspents([tx_out]) key = Key.from_text("cThRBRu2jAeshWL3sH3qbqdq9f4jDiDbd1SVz4qjTZD2xL1pdbsx") hash160_lookup = build_hash160_lookup([key.secret_exponent()]) self.assertEqual(tx.bad_signature_count(), 1) tx.sign(hash160_lookup=hash160_lookup, p2sh_lookup=p2sh_lookup) self.assertEqual(tx.bad_signature_count(), 0) self.assertEqual(tx.id(), "9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2")
def sign_tx(hex_tx, tx_input): """ Sign the transaction with private key :param hex_tx: :param tx_input: :return: """ logging.info('Signing tx with private key') transaction = Tx.from_hex(hex_tx) wif = wif_to_secret_exponent(helpers.import_key(), ALLOWABLE_WIF_PREFIXES) lookup = build_hash160_lookup([wif]) transaction.set_unspents([TxOut(coin_value=tx_input.coin_value, script=tx_input.script)]) signed_tx = transaction.sign(lookup) logging.info('Finished signing transaction') return signed_tx
def txs_from_json(path): """ Read tests from ./data/tx_??valid.json Format is an array of arrays Inner arrays are either [ "comment" ] or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...], serializedTransaction, verifyFlags] ... where all scripts are stringified scripts. verifyFlags is a comma separated list of script verification flags to apply, or "NONE" """ with open(path, 'r') as f: for tvec in json.load(f): if len(tvec) == 1: continue assert len(tvec) == 3 prevouts = tvec[0] for prevout in prevouts: assert len(prevout) == 3 tx_hex = tvec[1] flags = set() for flag in tvec[2].split(','): assert flag in FLAGS flags.add(flag) try: tx = Tx.from_hex(tx_hex) except: print("Cannot parse tx_hex: %s" % tx_hex) raise spendable_db = {} blank_spendable = Spendable(0, b'', b'\0' * 32, 0) for prevout in prevouts: spendable = Spendable(coin_value=1000000, script=compile_script(prevout[2]), tx_hash=h2b_rev(prevout[0]), tx_out_index=prevout[1]) spendable_db[(spendable.tx_hash, spendable.tx_out_index)] = spendable unspents = [ spendable_db.get((tx_in.previous_hash, tx_in.previous_index), blank_spendable) for tx_in in tx.txs_in ] tx.set_unspents(unspents) yield (tx, flags)
def test_compare_cost(self): """ Compare our size estimation with a known transaction. The transaction contains 1 input, 6 outputs, and 1 OP_RETURN Note that the estimation may be off +/- the number of inputs, which is why the estimate was off by 1 in this case. :return: """ tx = Tx.from_hex( '0100000001ae17c5db3174b46ae2bdc911c25df6bc3ce88092256b6f6e564989693ecf67fc030000006b483045022100b0cfd576dd30bbdf6fd11e0d6118c59b6c6f8e7bf6513d323c7f9f5f8296bef102200174a28e28c792425b71155df99ea6110cdb67d3567792f1696e61424c1f67400121037175dfbeecd8b5a54eb5ad9a696f15b7b39da2ea7d67b4cd7a3299bb95e28884ffffffff07be0a0000000000001976a91481c706f7e6b2d9546169c1e76f50a3ee18e1e1d788acbe0a0000000000001976a914c2b9a62457e35bef48ef350a00622b1e63394d4588acbe0a0000000000001976a91481c706f7e6b2d9546169c1e76f50a3ee18e1e1d788acbe0a0000000000001976a914c2b9a62457e35bef48ef350a00622b1e63394d4588acbe0a0000000000001976a914cc0a909c4c83068be8b45d69b60a6f09c2be0fda88ac5627cb1d000000001976a9144103222e7c72b869c5e47bfe86702684531f2c9088ac0000000000000000226a206f308c70afcfcb0311ad0de989b80904fb54d9131fd3ab2187b89ca9601adab000000000') s = io.BytesIO() tx.stream(s) tx_byte_count = len(s.getvalue()) estimated_byte_count = tx_utils.calculate_raw_tx_size_with_op_return(num_inputs=1, num_outputs=6) self.assertEquals(estimated_byte_count, tx_byte_count + 1)
def test_compare_cost(self): """ Compare our size estimation with a known transaction. The transaction contains 1 input, 6 outputs, and 1 OP_RETURN Note that the estimation may be off +/- the number of inputs, which is why the estimate was off by 1 in this case. :return: """ tx = Tx.from_hex( '0100000001ae17c5db3174b46ae2bdc911c25df6bc3ce88092256b6f6e564989693ecf67fc030000006b483045022100b0cfd576dd30bbdf6fd11e0d6118c59b6c6f8e7bf6513d323c7f9f5f8296bef102200174a28e28c792425b71155df99ea6110cdb67d3567792f1696e61424c1f67400121037175dfbeecd8b5a54eb5ad9a696f15b7b39da2ea7d67b4cd7a3299bb95e28884ffffffff07be0a0000000000001976a91481c706f7e6b2d9546169c1e76f50a3ee18e1e1d788acbe0a0000000000001976a914c2b9a62457e35bef48ef350a00622b1e63394d4588acbe0a0000000000001976a91481c706f7e6b2d9546169c1e76f50a3ee18e1e1d788acbe0a0000000000001976a914c2b9a62457e35bef48ef350a00622b1e63394d4588acbe0a0000000000001976a914cc0a909c4c83068be8b45d69b60a6f09c2be0fda88ac5627cb1d000000001976a9144103222e7c72b869c5e47bfe86702684531f2c9088ac0000000000000000226a206f308c70afcfcb0311ad0de989b80904fb54d9131fd3ab2187b89ca9601adab000000000') s = io.BytesIO() tx.stream(s) tx_byte_count = len(s.getvalue()) estimated_byte_count = trx_utils.calculate_raw_tx_size_with_op_return(num_inputs=1, num_outputs=6) self.assertEquals(estimated_byte_count, tx_byte_count + 1)
def txs_from_json(path): """ Read tests from ./data/tx_??valid.json Format is an array of arrays Inner arrays are either [ "comment" ] or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...], serializedTransaction, verifyFlags] ... where all scripts are stringified scripts. verifyFlags is a comma separated list of script verification flags to apply, or "NONE" """ comments = None with open(path, 'r') as f: for tvec in json.load(f): if len(tvec) == 1: comments = tvec[0] continue assert len(tvec) == 3 prevouts = tvec[0] for prevout in prevouts: assert len(prevout) in (3, 4) tx_hex = tvec[1] flags = parse_flags(tvec[2]) try: tx = Tx.from_hex(tx_hex) except: print("Cannot parse tx_hex: %s" % tx_hex) raise spendable_db = {} blank_spendable = Spendable(0, b'', b'\0' * 32, 0) for prevout in prevouts: coin_value = 1000000 if len(prevout) == 4: coin_value = prevout[3] spendable = Spendable(coin_value=coin_value, script=compile(prevout[2]), tx_hash=h2b_rev(prevout[0]), tx_out_index=prevout[1]) spendable_db[(spendable.tx_hash, spendable.tx_out_index)] = spendable unspents = [spendable_db.get((tx_in.previous_hash, tx_in.previous_index), blank_spendable) for tx_in in tx.txs_in] tx.set_unspents(unspents) yield (tx, flags, comments)
def test_sign_bitcoind_partially_signed_2_of_2(self): # Finish signing a 2 of 2 transaction, that already has one signature signed by bitcoind # This tx can be found on testnet3 blockchain, txid: 9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2 raw_script = "522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452ae" p2sh_lookup = build_p2sh_lookup([h2b(raw_script)]) partially_signed_raw_tx = "010000000196238f11a5fd3ceef4efd5a186a7e6b9217d900418e72aca917cd6a6e634e74100000000910047304402201b41b471d9dd93cf97eed7cfc39a5767a546f6bfbf3e0c91ff9ad23ab9770f1f02205ce565666271d055be1f25a7e52e34cbf659f6c70770ff59bd783a6fcd1be3dd0147522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452aeffffffff01a0bb0d00000000001976a9143b3beefd6f7802fa8706983a76a51467bfa36f8b88ac00000000" tx = Tx.from_hex(partially_signed_raw_tx) tx_out = TxOut(1000000, h2b("a914a10dfa21ee8c33b028b92562f6fe04e60563d3c087")) tx.set_unspents([tx_out]) key = Key.from_text( "cThRBRu2jAeshWL3sH3qbqdq9f4jDiDbd1SVz4qjTZD2xL1pdbsx") hash160_lookup = build_hash160_lookup([key.secret_exponent()]) self.assertEqual(tx.bad_signature_count(), 1) tx.sign(hash160_lookup=hash160_lookup, p2sh_lookup=p2sh_lookup) self.assertEqual(tx.bad_signature_count(), 0) self.assertEqual( tx.id(), "9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2")
def get_spend_secret(payout_rawtx, commit_script_hex): """ Get spend secret for given payout transaction. Args: payout_rawtx (str): Payout rawtx hex. commit_script_hex (str): Hex encoded commit script. Return: str: Hex spend secret or None if not a payout for given commit script. """ validate_commit_script(commit_script_hex) tx = Tx.from_hex(payout_rawtx) spend_script_bin = tx.txs_in[0].script try: _validate_payout_scriptsig(b2h(spend_script_bin), commit_script_hex) except InvalidScript: return None opcode, data, disassembled = get_word(spend_script_bin, 3) opcode, spend_secret, disassembled = get_word(spend_script_bin, 1) return b2h(spend_secret)
def sign_tx(self, hex_tx, tx_input, netcode): """ Sign the transaction with private key :param hex_tx: :param tx_input: :param netcode: :return: """ logging.info('Signing tx with private key') self.secret_manager.start() wif = self.secret_manager.get_wif() transaction = Tx.from_hex(hex_tx) allowable_wif_prefixes = wif_prefix_for_netcode(netcode) se = wif_to_secret_exponent(wif, allowable_wif_prefixes) lookup = build_hash160_lookup([se]) transaction.set_unspents([TxOut(coin_value=tx_input.coin_value, script=tx_input.script)]) signed_tx = transaction.sign(lookup) self.secret_manager.stop() logging.info('Finished signing transaction') return signed_tx
def __init__(self, hex): self.tx = Tx.from_hex(hex) self.signatures = []
def __init__(self, hex): self.tx = Tx.from_hex(hex) self.signatures = []
def main(): parser = argparse.ArgumentParser( description="Manipulate bitcoin (or alt coin) transactions.", epilog=EPILOG) parser.add_argument('-t', "--transaction-version", type=int, help='Transaction version, either 1 (default) or 3 (not yet supported).') parser.add_argument('-l', "--lock-time", type=parse_locktime, help='Lock time; either a block' 'index, or a date/time (example: "2014-01-01T15:00:00"') parser.add_argument('-n', "--network", default="BTC", help='Define network code (M=Bitcoin mainnet, T=Bitcoin testnet).') parser.add_argument('-a', "--augment", action='store_true', help='augment tx by adding any missing spendable metadata by fetching' ' inputs from cache and/or web services') parser.add_argument('-s', "--verbose-signature", action='store_true', help='Display technical signature details.') parser.add_argument("-i", "--fetch-spendables", metavar="address", action="append", help='Add all unspent spendables for the given bitcoin address. This information' ' is fetched from web services.') parser.add_argument('-f', "--private-key-file", metavar="path-to-private-keys", action="append", help='file containing WIF or BIP0032 private keys. If file name ends with .gpg, ' '"gpg -d" will be invoked automatically. File is read one line at a time, and if ' 'the file contains only one WIF per line, it will also be scanned for a bitcoin ' 'address, and any addresses found will be assumed to be public keys for the given' ' private key.', type=argparse.FileType('r')) parser.add_argument('-g', "--gpg-argument", help='argument to pass to gpg (besides -d).', default='') parser.add_argument("--remove-tx-in", metavar="tx_in_index_to_delete", action="append", type=int, help='remove a tx_in') parser.add_argument("--remove-tx-out", metavar="tx_out_index_to_delete", action="append", type=int, help='remove a tx_out') parser.add_argument('-F', "--fee", help='fee, in satoshis, to pay on transaction, or ' '"standard" to auto-calculate. This is only useful if the "split pool" ' 'is used; otherwise, the fee is automatically set to the unclaimed funds.', default="standard", metavar="transaction-fee", type=parse_fee) parser.add_argument('-C', "--cache", help='force the resultant transaction into the transaction cache.' ' Mostly for testing.', action='store_true'), parser.add_argument('-u', "--show-unspents", action='store_true', help='show TxOut items for this transaction in Spendable form.') parser.add_argument('-b', "--bitcoind-url", help='URL to bitcoind instance to validate against (http://user:pass@host:port).') parser.add_argument('-o', "--output-file", metavar="path-to-output-file", type=argparse.FileType('wb'), help='file to write transaction to. This supresses most other output.') parser.add_argument('-d', "--disassemble", action='store_true', help='Disassemble scripts.') parser.add_argument("--trace", action='store_true', help='Trace scripts.') parser.add_argument('-p', "--pay-to-script", metavar="pay-to-script", action="append", help='a hex version of a script required for a pay-to-script input (a bitcoin address that starts with 3)') parser.add_argument('-P', "--pay-to-script-file", metavar="pay-to-script-file", nargs=1, type=argparse.FileType('r'), help='a file containing hex scripts (one per line) corresponding to pay-to-script inputs') parser.add_argument("argument", nargs="+", help='generic argument: can be a hex transaction id ' '(exactly 64 characters) to be fetched from cache or a web service;' ' a transaction as a hex string; a path name to a transaction to be loaded;' ' a spendable 4-tuple of the form tx_id/tx_out_idx/script_hex/satoshi_count ' 'to be added to TxIn list; an address/satoshi_count to be added to the TxOut ' 'list; an address to be added to the TxOut list and placed in the "split' ' pool".') args = parser.parse_args() # defaults txs = [] spendables = [] payables = [] key_iters = [] TX_ID_RE = re.compile(r"^[0-9a-fA-F]{64}$") # there are a few warnings we might optionally print out, but only if # they are relevant. We don't want to print them out multiple times, so we # collect them here and print them at the end if they ever kick in. warning_tx_cache = None warning_tx_for_tx_hash = None warning_spendables = None if args.private_key_file: wif_re = re.compile(r"[1-9a-km-zA-LMNP-Z]{51,111}") # address_re = re.compile(r"[1-9a-kmnp-zA-KMNP-Z]{27-31}") for f in args.private_key_file: if f.name.endswith(".gpg"): gpg_args = ["gpg", "-d"] if args.gpg_argument: gpg_args.extend(args.gpg_argument.split()) gpg_args.append(f.name) popen = subprocess.Popen(gpg_args, stdout=subprocess.PIPE) f = popen.stdout for line in f.readlines(): # decode if isinstance(line, bytes): line = line.decode("utf8") # look for WIFs possible_keys = wif_re.findall(line) def make_key(x): try: return Key.from_text(x) except Exception: return None keys = [make_key(x) for x in possible_keys] for key in keys: if key: key_iters.append((k.wif() for k in key.subkeys(""))) # if len(keys) == 1 and key.hierarchical_wallet() is None: # # we have exactly 1 WIF. Let's look for an address # potential_addresses = address_re.findall(line) # update p2sh_lookup p2sh_lookup = {} if args.pay_to_script: for p2s in args.pay_to_script: try: script = h2b(p2s) p2sh_lookup[hash160(script)] = script except Exception: print("warning: error parsing pay-to-script value %s" % p2s) if args.pay_to_script_file: hex_re = re.compile(r"[0-9a-fA-F]+") for f in args.pay_to_script_file: count = 0 for l in f: try: m = hex_re.search(l) if m: p2s = m.group(0) script = h2b(p2s) p2sh_lookup[hash160(script)] = script count += 1 except Exception: print("warning: error parsing pay-to-script file %s" % f.name) if count == 0: print("warning: no scripts found in %s" % f.name) # we create the tx_db lazily tx_db = None for arg in args.argument: # hex transaction id if TX_ID_RE.match(arg): if tx_db is None: warning_tx_cache = message_about_tx_cache_env() warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network) tx_db = get_tx_db(args.network) tx = tx_db.get(h2b_rev(arg)) if not tx: for m in [warning_tx_cache, warning_tx_for_tx_hash, warning_spendables]: if m: print("warning: %s" % m, file=sys.stderr) parser.error("can't find Tx with id %s" % arg) txs.append(tx) continue # hex transaction data try: tx = Tx.from_hex(arg) txs.append(tx) continue except Exception: pass is_valid = is_address_valid(arg, allowable_netcodes=[args.network]) if is_valid: payables.append((arg, 0)) continue try: key = Key.from_text(arg) # TODO: check network if key.wif() is None: payables.append((key.address(), 0)) continue # TODO: support paths to subkeys key_iters.append((k.wif() for k in key.subkeys(""))) continue except Exception: pass if os.path.exists(arg): try: with open(arg, "rb") as f: if f.name.endswith("hex"): f = io.BytesIO(codecs.getreader("hex_codec")(f).read()) tx = Tx.parse(f) txs.append(tx) try: tx.parse_unspents(f) except Exception as ex: pass continue except Exception: pass parts = arg.split("/") if len(parts) == 4: # spendable try: spendables.append(Spendable.from_text(arg)) continue except Exception: pass if len(parts) == 2 and is_address_valid(parts[0], allowable_netcodes=[args.network]): try: payables.append(parts) continue except ValueError: pass parser.error("can't parse %s" % arg) if args.fetch_spendables: warning_spendables = message_about_spendables_for_address_env(args.network) for address in args.fetch_spendables: spendables.extend(spendables_for_address(address)) for tx in txs: if tx.missing_unspents() and args.augment: if tx_db is None: warning_tx_cache = message_about_tx_cache_env() warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network) tx_db = get_tx_db(args.network) tx.unspents_from_db(tx_db, ignore_missing=True) txs_in = [] txs_out = [] unspents = [] # we use a clever trick here to keep each tx_in corresponding with its tx_out for tx in txs: smaller = min(len(tx.txs_in), len(tx.txs_out)) txs_in.extend(tx.txs_in[:smaller]) txs_out.extend(tx.txs_out[:smaller]) unspents.extend(tx.unspents[:smaller]) for tx in txs: smaller = min(len(tx.txs_in), len(tx.txs_out)) txs_in.extend(tx.txs_in[smaller:]) txs_out.extend(tx.txs_out[smaller:]) unspents.extend(tx.unspents[smaller:]) for spendable in spendables: txs_in.append(spendable.tx_in()) unspents.append(spendable) for address, coin_value in payables: script = standard_tx_out_script(address) txs_out.append(TxOut(coin_value, script)) lock_time = args.lock_time version = args.transaction_version # if no lock_time is explicitly set, inherit from the first tx or use default if lock_time is None: if txs: lock_time = txs[0].lock_time else: lock_time = DEFAULT_LOCK_TIME # if no version is explicitly set, inherit from the first tx or use default if version is None: if txs: version = txs[0].version else: version = DEFAULT_VERSION if args.remove_tx_in: s = set(args.remove_tx_in) txs_in = [tx_in for idx, tx_in in enumerate(txs_in) if idx not in s] if args.remove_tx_out: s = set(args.remove_tx_out) txs_out = [tx_out for idx, tx_out in enumerate(txs_out) if idx not in s] tx = Tx(txs_in=txs_in, txs_out=txs_out, lock_time=lock_time, version=version, unspents=unspents) fee = args.fee try: distribute_from_split_pool(tx, fee) except ValueError as ex: print("warning: %s" % ex.args[0], file=sys.stderr) unsigned_before = tx.bad_signature_count() unsigned_after = unsigned_before if unsigned_before > 0 and key_iters: def wif_iter(iters): while len(iters) > 0: for idx, iter in enumerate(iters): try: wif = next(iter) yield wif except StopIteration: iters = iters[:idx] + iters[idx+1:] break print("signing...", file=sys.stderr) sign_tx(tx, wif_iter(key_iters), p2sh_lookup=p2sh_lookup) unsigned_after = tx.bad_signature_count() if unsigned_after > 0: print("warning: %d TxIn items still unsigned" % unsigned_after, file=sys.stderr) if len(tx.txs_in) == 0: print("warning: transaction has no inputs", file=sys.stderr) if len(tx.txs_out) == 0: print("warning: transaction has no outputs", file=sys.stderr) include_unspents = (unsigned_after > 0) tx_as_hex = tx.as_hex(include_unspents=include_unspents) if args.output_file: f = args.output_file if f.name.endswith(".hex"): f.write(tx_as_hex.encode("utf8")) else: tx.stream(f) if include_unspents: tx.stream_unspents(f) f.close() elif args.show_unspents: for spendable in tx.tx_outs_as_spendable(): print(spendable.as_text()) else: if not tx.missing_unspents(): check_fees(tx) dump_tx(tx, args.network, args.verbose_signature, args.disassemble, args.trace) if include_unspents: print("including unspents in hex dump since transaction not fully signed") print(tx_as_hex) if args.cache: if tx_db is None: warning_tx_cache = message_about_tx_cache_env() warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network) tx_db = get_tx_db(args.network) tx_db.put(tx) if args.bitcoind_url: if tx_db is None: warning_tx_cache = message_about_tx_cache_env() warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network) tx_db = get_tx_db(args.network) validate_bitcoind(tx, tx_db, args.bitcoind_url) if tx.missing_unspents(): print("\n** can't validate transaction as source transactions missing", file=sys.stderr) else: try: if tx_db is None: warning_tx_cache = message_about_tx_cache_env() warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network) tx_db = get_tx_db(args.network) tx.validate_unspents(tx_db) print('all incoming transaction values validated') except BadSpendableError as ex: print("\n**** ERROR: FEES INCORRECTLY STATED: %s" % ex.args[0], file=sys.stderr) except Exception as ex: print("\n*** can't validate source transactions as untampered: %s" % ex.args[0], file=sys.stderr) # print warnings for m in [warning_tx_cache, warning_tx_for_tx_hash, warning_spendables]: if m: print("warning: %s" % m, file=sys.stderr)
def parse_context(args, parser): # defaults txs = [] spendables = [] payables = [] key_iters = [] TX_ID_RE = re.compile(r"^[0-9a-fA-F]{64}$") # there are a few warnings we might optionally print out, but only if # they are relevant. We don't want to print them out multiple times, so we # collect them here and print them at the end if they ever kick in. warning_tx_cache = None warning_tx_for_tx_hash = None warning_spendables = None if args.private_key_file: wif_re = re.compile(r"[1-9a-km-zA-LMNP-Z]{51,111}") # address_re = re.compile(r"[1-9a-kmnp-zA-KMNP-Z]{27-31}") for f in args.private_key_file: if f.name.endswith(".gpg"): gpg_args = ["gpg", "-d"] if args.gpg_argument: gpg_args.extend(args.gpg_argument.split()) gpg_args.append(f.name) popen = subprocess.Popen(gpg_args, stdout=subprocess.PIPE) f = popen.stdout for line in f.readlines(): # decode if isinstance(line, bytes): line = line.decode("utf8") # look for WIFs possible_keys = wif_re.findall(line) def make_key(x): try: return Key.from_text(x) except Exception: return None keys = [make_key(x) for x in possible_keys] for key in keys: if key: key_iters.append((k.wif() for k in key.subkeys(""))) # if len(keys) == 1 and key.hierarchical_wallet() is None: # # we have exactly 1 WIF. Let's look for an address # potential_addresses = address_re.findall(line) # update p2sh_lookup p2sh_lookup = {} if args.pay_to_script: for p2s in args.pay_to_script: try: script = h2b(p2s) p2sh_lookup[hash160(script)] = script except Exception: print("warning: error parsing pay-to-script value %s" % p2s) if args.pay_to_script_file: hex_re = re.compile(r"[0-9a-fA-F]+") for f in args.pay_to_script_file: count = 0 for l in f: try: m = hex_re.search(l) if m: p2s = m.group(0) script = h2b(p2s) p2sh_lookup[hash160(script)] = script count += 1 except Exception: print("warning: error parsing pay-to-script file %s" % f.name) if count == 0: print("warning: no scripts found in %s" % f.name) # we create the tx_db lazily tx_db = None for arg in args.argument: # hex transaction id if TX_ID_RE.match(arg): if tx_db is None: warning_tx_cache = message_about_tx_cache_env() warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network) tx_db = get_tx_db(args.network) tx = tx_db.get(h2b_rev(arg)) if not tx: for m in [warning_tx_cache, warning_tx_for_tx_hash, warning_spendables]: if m: print("warning: %s" % m, file=sys.stderr) parser.error("can't find Tx with id %s" % arg) txs.append(tx) continue # hex transaction data try: tx = Tx.from_hex(arg) txs.append(tx) continue except Exception: pass is_valid = is_address_valid(arg, allowable_netcodes=[args.network]) if is_valid: payables.append((arg, 0)) continue try: key = Key.from_text(arg) # TODO: check network if key.wif() is None: payables.append((key.address(), 0)) continue # TODO: support paths to subkeys key_iters.append((k.wif() for k in key.subkeys(""))) continue except Exception: pass if os.path.exists(arg): try: with open(arg, "rb") as f: if f.name.endswith("hex"): f = io.BytesIO(codecs.getreader("hex_codec")(f).read()) tx = Tx.parse(f) txs.append(tx) try: tx.parse_unspents(f) except Exception as ex: pass continue except Exception: pass parts = arg.split("/") if len(parts) == 4: # spendable try: spendables.append(Spendable.from_text(arg)) continue except Exception: pass if len(parts) == 2 and is_address_valid(parts[0], allowable_netcodes=[args.network]): try: payables.append(parts) continue except ValueError: pass parser.error("can't parse %s" % arg) if args.fetch_spendables: warning_spendables = message_about_spendables_for_address_env(args.network) for address in args.fetch_spendables: spendables.extend(spendables_for_address(address)) for tx in txs: if tx.missing_unspents() and args.augment: if tx_db is None: warning_tx_cache = message_about_tx_cache_env() warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network) tx_db = get_tx_db(args.network) tx.unspents_from_db(tx_db, ignore_missing=True) return (txs, spendables, payables, key_iters, p2sh_lookup, tx_db, warning_tx_cache, warning_tx_for_tx_hash, warning_spendables)
def tx_for_tx_hash(self, tx_hash): raw_tx = self.connection.getrawtransaction(b2h_rev(tx_hash)) tx = Tx.from_hex(raw_tx) return tx
def tx_for_tx_hash(self, tx_hash): raw_tx = self.connection.getrawtransaction(b2h_rev(tx_hash)) tx = Tx.from_hex(raw_tx) return tx
def prepare_tx_for_signing(hex_tx, tx_inputs): logging.info('Preparing tx for signing') transaction = Tx.from_hex(hex_tx) unspents = [TxOut(coin_value=tx_input.coin_value, script=tx_input.script) for tx_input in tx_inputs] transaction.set_unspents(unspents) return transaction
def parse_context(args, parser): # defaults txs = [] spendables = [] payables = [] key_iters = [] TX_ID_RE = re.compile(r"^[0-9a-fA-F]{64}$") # there are a few warnings we might optionally print out, but only if # they are relevant. We don't want to print them out multiple times, so we # collect them here and print them at the end if they ever kick in. warning_tx_cache = None warning_tx_for_tx_hash = None warning_spendables = None if args.private_key_file: wif_re = re.compile(r"[1-9a-km-zA-LMNP-Z]{51,111}") # address_re = re.compile(r"[1-9a-kmnp-zA-KMNP-Z]{27-31}") for f in args.private_key_file: if f.name.endswith(".gpg"): gpg_args = ["gpg", "-d"] if args.gpg_argument: gpg_args.extend(args.gpg_argument.split()) gpg_args.append(f.name) popen = subprocess.Popen(gpg_args, stdout=subprocess.PIPE) f = popen.stdout for line in f.readlines(): # decode if isinstance(line, bytes): line = line.decode("utf8") # look for WIFs possible_keys = wif_re.findall(line) def make_key(x): try: return Key.from_text(x) except Exception: return None keys = [make_key(x) for x in possible_keys] for key in keys: if key: key_iters.append((k.wif() for k in key.subkeys(""))) # if len(keys) == 1 and key.hierarchical_wallet() is None: # # we have exactly 1 WIF. Let's look for an address # potential_addresses = address_re.findall(line) # update p2sh_lookup p2sh_lookup = {} if args.pay_to_script: for p2s in args.pay_to_script: try: script = h2b(p2s) p2sh_lookup[hash160(script)] = script except Exception: print("warning: error parsing pay-to-script value %s" % p2s) if args.pay_to_script_file: hex_re = re.compile(r"[0-9a-fA-F]+") for f in args.pay_to_script_file: count = 0 for l in f: try: m = hex_re.search(l) if m: p2s = m.group(0) script = h2b(p2s) p2sh_lookup[hash160(script)] = script count += 1 except Exception: print("warning: error parsing pay-to-script file %s" % f.name) if count == 0: print("warning: no scripts found in %s" % f.name) # we create the tx_db lazily tx_db = None for arg in args.argument: # hex transaction id if TX_ID_RE.match(arg): if tx_db is None: warning_tx_cache = message_about_tx_cache_env() warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env( args.network) tx_db = get_tx_db(args.network) tx = tx_db.get(h2b_rev(arg)) if not tx: for m in [ warning_tx_cache, warning_tx_for_tx_hash, warning_spendables ]: if m: print("warning: %s" % m, file=sys.stderr) parser.error("can't find Tx with id %s" % arg) txs.append(tx) continue # hex transaction data try: tx = Tx.from_hex(arg) txs.append(tx) continue except Exception: pass is_valid = is_address_valid(arg, allowable_netcodes=[args.network]) if is_valid: payables.append((arg, 0)) continue try: key = Key.from_text(arg) # TODO: check network if key.wif() is None: payables.append((key.address(), 0)) continue # TODO: support paths to subkeys key_iters.append((k.wif() for k in key.subkeys(""))) continue except Exception: pass if os.path.exists(arg): try: with open(arg, "rb") as f: if f.name.endswith("hex"): f = io.BytesIO(codecs.getreader("hex_codec")(f).read()) tx = Tx.parse(f) txs.append(tx) try: tx.parse_unspents(f) except Exception as ex: pass continue except Exception: pass parts = arg.split("/") if len(parts) == 4: # spendable try: spendables.append(Spendable.from_text(arg)) continue except Exception: pass if len(parts) == 2 and is_address_valid( parts[0], allowable_netcodes=[args.network]): try: payables.append(parts) continue except ValueError: pass parser.error("can't parse %s" % arg) if args.fetch_spendables: warning_spendables = message_about_spendables_for_address_env( args.network) for address in args.fetch_spendables: spendables.extend(spendables_for_address(address)) for tx in txs: if tx.missing_unspents() and args.augment: if tx_db is None: warning_tx_cache = message_about_tx_cache_env() warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env( args.network) tx_db = get_tx_db(args.network) tx.unspents_from_db(tx_db, ignore_missing=True) return (txs, spendables, payables, key_iters, p2sh_lookup, tx_db, warning_tx_cache, warning_tx_for_tx_hash, warning_spendables)
def gettxid(rawtx): Tx.ALLOW_SEGWIT = False # FIXME remove on next pycoin version return b2h_rev(Tx.from_hex(rawtx).hash())