Ejemplo n.º 1
0
 def test_multisig_one_at_a_time(self):
     M = 3
     N = 3
     keys = [Key(secret_exponent=i) for i in range(1, N + 2)]
     tx_in = TxIn.coinbase_tx_in(script=b'')
     script = ScriptMultisig(m=M, sec_keys=[key.sec()
                                            for key in keys[:N]]).script()
     tx_out = TxOut(1000000, script)
     tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out])
     tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(),
                              [keys[-1].address()])
     ids = [
         "403e5bfc59e097bb197bf77a692d158dd3a4f7affb4a1fa41072dafe7bec7058",
         "5931d9995e83721243dca24772d7012afcd4378996a8b953c458175f15a544db",
         "9bb4421088190bbbb5b42a9eaa9baed7ec7574a407c25f71992ba56ca43d9c44",
         "03a1dc2a63f93a5cf5a7cb668658eb3fc2eda88c06dc287b85ba3e6aff751771"
     ]
     for i in range(1, N + 1):
         self.assertEqual(tx2.bad_signature_count(), 1)
         self.assertEqual(tx2.id(), ids[i - 1])
         hash160_lookup = build_hash160_lookup(key.secret_exponent()
                                               for key in keys[i - 1:i])
         tx2.sign(hash160_lookup=hash160_lookup)
         self.assertEqual(tx2.id(), ids[i])
     self.assertEqual(tx2.bad_signature_count(), 0)
Ejemplo n.º 2
0
def tx_from_json_dict(r):
    version = r.get("version")
    lock_time = r.get("locktime")
    txs_in = []
    for vin in r.get("vin"):
        if "coinbase" in vin:
            previous_hash = b'\0' * 32
            script = h2b(vin.get("coinbase"))
            previous_index = 4294967295
        else:
            previous_hash = h2b_rev(vin.get("txid"))
            scriptSig = vin.get("scriptSig")
            if "hex" in scriptSig:
                script = h2b(scriptSig.get("hex"))
            else:
                script = tools.compile(scriptSig.get("asm"))
            previous_index = vin.get("vout")
        sequence = vin.get("sequence")
        txs_in.append(TxIn(previous_hash, previous_index, script, sequence))
    txs_out = []
    for vout in r.get("vout"):
        coin_value = btc_to_satoshi(decimal.Decimal(vout.get("value")))
        script = tools.compile(vout.get("scriptPubKey").get("asm"))
        txs_out.append(TxOut(coin_value, script))
    tx = Tx(version, txs_in, txs_out, lock_time)
    bh = r.get("blockhash")
    if bh:
        bh = h2b_rev(bh)
    tx.confirmation_block_hash = bh
    return tx
Ejemplo n.º 3
0
 def test_issue_224(self):
     RAWTX = (
         "010000000002145fea0b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739"
         "c6303788ac0000000000000000346a32544553540000000a0000000000000001000000"
         "0005f5e1000000000000000000000000000bebc2000032000000000000271000000000"
     )
     Tx.from_hex(RAWTX)
Ejemplo n.º 4
0
 def check_bip143_tx(self, tx_u_hex, tx_s_hex, txs_out_value_scripthex_pair,
                     tx_in_count, tx_out_count, version, lock_time):
     tx_u = Tx.from_hex(tx_u_hex)
     tx_s = Tx.from_hex(tx_s_hex)
     txs_out = [
         TxOut(int(coin_value * 1e8), h2b(script_hex))
         for coin_value, script_hex in txs_out_value_scripthex_pair
     ]
     for tx in (tx_u, tx_s):
         self.assertEqual(len(tx.txs_in), tx_in_count)
         self.assertEqual(len(tx.txs_out), tx_out_count)
         self.assertEqual(tx.version, version)
         self.assertEqual(tx.lock_time, lock_time)
         tx.set_unspents(txs_out)
     self.check_unsigned(tx_u)
     self.check_signed(tx_s)
     tx_hex = tx_u.as_hex()
     self.assertEqual(tx_hex, tx_u_hex)
     tx_hex = tx_s.as_hex()
     self.assertEqual(tx_hex, tx_s_hex)
     tx_u_prime = self.unsigned_copy(tx_s)
     tx_hex = tx_u_prime.as_hex()
     self.assertEqual(tx_hex, tx_u_hex)
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_s_hex))), tx_s.w_id())
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.w_id())
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.id())
     return tx_u, tx_s
Ejemplo n.º 5
0
def parse_tx(arg, parser, tx_db, network):
    # hex transaction id
    tx = None

    if TX_ID_RE.match(arg):
        if tx_db is None:
            tx_db = create_tx_db(network)
        tx = tx_db.get(h2b_rev(arg))
        if not tx:
            parser.error("can't find Tx with id %s" % arg)
        return tx, tx_db

    # hex transaction data
    try:
        return Tx.from_hex(arg), tx_db
    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)
                tx.parse_unspents(f)
        except Exception:
            pass

    return tx, tx_db
 def multisig_M_of_N_individually(self, M, N):
     keys = [Key(secret_exponent=i) for i in range(1, N + 2)]
     tx_in = TxIn.coinbase_tx_in(script=b'')
     script = ScriptMultisig(m=M, sec_keys=[key.sec()
                                            for key in keys[:N]]).script()
     tx_out = TxOut(1000000, script)
     tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out])
     for partial_key_list in itertools.permutations(keys[:N], M):
         tx2 = create_tx(tx1.tx_outs_as_spendable(), [keys[-1].address()])
         for key in partial_key_list:
             self.assertEqual(tx2.bad_signature_count(), 1)
             hash160_lookup = build_hash160_lookup([key.secret_exponent()])
             tx2.sign(hash160_lookup=hash160_lookup)
         self.assertEqual(tx2.bad_signature_count(), 0)
Ejemplo n.º 7
0
def main():
    if len(sys.argv) != 2:
        print("usage: %s address" % sys.argv[0])
        sys.exit(-1)

    # validate the address
    address = sys.argv[1]
    assert is_address_valid(address)

    print("creating coinbase transaction to %s" % address)

    tx_in = TxIn.coinbase_tx_in(script=b'')
    tx_out = TxOut(50*1e8, standard_tx_out_script(address))
    tx = Tx(1, [tx_in], [tx_out])
    print("Here is the tx as hex:\n%s" % tx.as_hex())
Ejemplo n.º 8
0
 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 = (
         "522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c168851165521"
         "02b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452ae"
     )
     p2sh_lookup = build_p2sh_lookup([h2b(raw_script)])
     partially_signed_raw_tx = (
         "010000000196238f11a5fd3ceef4efd5a186a7e6b9217d900418e72aca917cd6a6e634"
         "e74100000000910047304402201b41b471d9dd93cf97eed7cfc39a5767a546f6bfbf3e"
         "0c91ff9ad23ab9770f1f02205ce565666271d055be1f25a7e52e34cbf659f6c70770ff"
         "59bd783a6fcd1be3dd0147522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e4"
         "2cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff5"
         "0511830328c1b479986452aeffffffff01a0bb0d00000000001976a9143b3beefd6f78"
         "02fa8706983a76a51467bfa36f8b88ac00000000")
     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")
Ejemplo n.º 9
0
def standard_tx(coins_from, coins_to):
    txs_in = []
    unspents = []
    for h, idx, tx_out in coins_from:
        txs_in.append(TxIn(h, idx))
        unspents.append(tx_out)

    txs_out = []
    for coin_value, bitcoin_address in coins_to:
        txs_out.append(
            TxOut(coin_value, standard_tx_out_script(bitcoin_address)))

    version, lock_time = 1, 0
    tx = Tx(version, txs_in, txs_out, lock_time)
    tx.set_unspents(unspents)
    return tx
Ejemplo n.º 10
0
    def test_signature_hash(self):
        compressed = False
        exponent_2 = int(
            "137f3276686959c82b454eea6eefc9ab1b9e45bd4636fb9320262e114e321da1",
            16)
        bitcoin_address_2 = public_pair_to_bitcoin_address(
            exponent_2 * secp256k1_generator, compressed=compressed)
        exponent = wif_to_secret_exponent(
            "5JMys7YfK72cRVTrbwkq5paxU7vgkMypB55KyXEtN5uSnjV7K8Y")

        public_key_sec = public_pair_to_sec(exponent * secp256k1_generator,
                                            compressed=compressed)

        the_coinbase_tx = Tx.coinbase_tx(public_key_sec, int(50 * 1e8),
                                         COINBASE_BYTES_FROM_80971)
        coins_from = [(the_coinbase_tx.hash(), 0, the_coinbase_tx.txs_out[0])]
        coins_to = [(int(50 * 1e8), bitcoin_address_2)]
        unsigned_coinbase_spend_tx = standard_tx(coins_from, coins_to)

        tx_out_script_to_check = the_coinbase_tx.txs_out[0].script
        idx = 0
        actual_hash = unsigned_coinbase_spend_tx.signature_hash(
            tx_out_script_to_check, idx, hash_type=SIGHASH_ALL)
        self.assertEqual(
            actual_hash,
            29819170155392455064899446505816569230970401928540834591675173488544269166940
        )
Ejemplo n.º 11
0
 def multisig_M_of_N(self, M, N, unsigned_id, signed_id):
     keys = [Key(secret_exponent=i) for i in range(1, N + 2)]
     tx_in = TxIn.coinbase_tx_in(script=b'')
     script = ScriptMultisig(m=M, sec_keys=[key.sec()
                                            for key in keys[:N]]).script()
     tx_out = TxOut(1000000, script)
     tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out])
     tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(),
                              [keys[-1].address()])
     self.assertEqual(tx2.id(), unsigned_id)
     self.assertEqual(tx2.bad_signature_count(), 1)
     hash160_lookup = build_hash160_lookup(key.secret_exponent()
                                           for key in keys)
     tx2.sign(hash160_lookup=hash160_lookup)
     self.assertEqual(tx2.id(), signed_id)
     self.assertEqual(tx2.bad_signature_count(), 0)
Ejemplo n.º 12
0
 def test_blanked_hash(self):
     tx = Tx.from_hex(
         TX_E1A18B843FC420734DEEB68FF6DF041A2585E1A0D7DBF3B82AAB98291A6D9952_HEX
     )
     self.assertEqual(
         tx.id(),
         "e1a18b843fc420734deeb68ff6df041a2585e1a0d7dbf3b82aab98291a6d9952")
     self.assertEqual(
         b2h(tx.blanked_hash()),
         "909579526c4c2c441687c7478d3f96249724d2ff071d2272b44500d6cf70d5d6")
     tx.txs_in[0].script = b"foo"
     self.assertEqual(
         b2h(tx.blanked_hash()),
         "909579526c4c2c441687c7478d3f96249724d2ff071d2272b44500d6cf70d5d6")
     tx.txs_out[0].coin_value += 1
     self.assertEqual(
         b2h(tx.blanked_hash()),
         "10d4e87f7bf35f2949e7693e7a4a84189aad8631f0b2b0999e88f7261066cbe5")
     tx.txs_in[0].script = b"bar"
     self.assertEqual(
         b2h(tx.blanked_hash()),
         "10d4e87f7bf35f2949e7693e7a4a84189aad8631f0b2b0999e88f7261066cbe5")
     tx.txs_in[0].script = b""
     self.assertEqual(
         b2h(tx.hash()),
         "10d4e87f7bf35f2949e7693e7a4a84189aad8631f0b2b0999e88f7261066cbe5")
     tx.txs_in[0].script = b"foo"
     self.assertEqual(
         b2h(tx.hash()),
         "c91910058722f1c0f52fc5c734939053c9b87882a9c72b609f21632e0bd13751")
Ejemplo n.º 13
0
def main():
    if len(sys.argv) != 4:
        print("usage: %s tx-hex-file-path wif-file-path p2sh-file-path" %
              sys.argv[0])
        sys.exit(-1)

    # get the tx
    with open(sys.argv[1], "r") as f:
        tx_hex = f.readline().strip()
    tx = Tx.from_hex(tx_hex)

    # get the WIF
    with open(sys.argv[2], "r") as f:
        wif = f.readline().strip()
    assert is_wif_valid(wif)

    # create the p2sh_lookup
    with open(sys.argv[3], "r") as f:
        p2sh_script_hex = f.readline().strip()
    p2sh_script = h2b(p2sh_script_hex)

    # build a dictionary of script hashes to scripts
    p2sh_lookup = build_p2sh_lookup([p2sh_script])

    # sign the transaction with the given WIF
    sign_tx(tx, wifs=[wif], p2sh_lookup=p2sh_lookup)

    bad_signature_count = tx.bad_signature_count()
    print("tx %s now has %d bad signature(s)" % (tx.id(), bad_signature_count))

    include_unspents = (bad_signature_count > 0)
    print("Here is the tx as hex:\n%s" %
          tx.as_hex(include_unspents=include_unspents))
Ejemplo n.º 14
0
 def test_pay_to_script_file(self):
     the_dir = self.set_cache_dir()
     p2sh_file = tempfile.NamedTemporaryFile()
     p2sh_file.write(
         "52210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817982102c60"
         "47f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258"
         "c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae\n".encode("utf8"))
     p2sh_file.write(
         "53210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817982102c60"
         "47f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258"
         "c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae\n".encode("utf8"))
     p2sh_file.flush()
     tx_source_hex = (
         "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0"
         "0ffffffff0200f902950000000017a91415fc0754e73eb85d1cbce08786fadb7320ecb8dc8700f90295"
         "0000000017a914594f349df0bac3084ffea8a477bba5f03dcd45078700000000")
     self.launch_tool("tx -C %s" % tx_source_hex)
     tx_to_sign = (
         "01000000020a316ea8980ef9ba02f4e6637c88229bf059f39b06238d48d06a8e"
         "f672aea2bb0000000000ffffffff0a316ea8980ef9ba02f4e6637c88229bf059"
         "f39b06238d48d06a8ef672aea2bb0100000000ffffffff01f0ca052a01000000"
         "1976a914751e76e8199196d454941c45d1b3a323f1433bd688ac0000000000f9"
         "02950000000017a91415fc0754e73eb85d1cbce08786fadb7320ecb8dc8700f9"
         "02950000000017a914594f349df0bac3084ffea8a477bba5f03dcd450787")
     wifs = ' '.join(Key(_).wif() for _ in (1, 2, 3))
     signed = tempfile.mktemp(suffix=".hex")
     self.launch_tool("tx -a -P %s --db %s %s %s -o %s" % (
         p2sh_file.name, tx_source_hex, tx_to_sign, wifs, signed), env=dict(PYCOIN_CACHE_DIR=the_dir))
     tx = Tx.from_hex(open(signed).read())
     self.assertEqual(tx.id(), "9d991ddccf77e33cb4584e4fc061a36da0da43589232b2e78a1aa0748ac3254b")
Ejemplo n.º 15
0
 def test_sign_pay_to_script_multisig(self):
     M, N = 3, 3
     keys = [Key(secret_exponent=i) for i in range(1, N + 2)]
     tx_in = TxIn.coinbase_tx_in(script=b'')
     underlying_script = ScriptMultisig(
         m=M, sec_keys=[key.sec() for key in keys[:N]]).script()
     address = address_for_pay_to_script(underlying_script)
     self.assertEqual(address, "39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q")
     script = standard_tx_out_script(address)
     tx_out = TxOut(1000000, script)
     tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out])
     tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(), [address])
     hash160_lookup = build_hash160_lookup(key.secret_exponent()
                                           for key in keys[:N])
     p2sh_lookup = build_p2sh_lookup([underlying_script])
     tx2.sign(hash160_lookup=hash160_lookup, p2sh_lookup=p2sh_lookup)
     self.assertEqual(tx2.bad_signature_count(), 0)
Ejemplo n.º 16
0
def build_spending_tx(script_in_bin, credit_tx):
    txs_in = [TxIn(credit_tx.hash(), 0, script_in_bin, sequence=4294967295)]
    txs_out = [TxOut(credit_tx.txs_out[0].coin_value, b'')]
    spend_tx = Tx(1,
                  txs_in,
                  txs_out,
                  unspents=credit_tx.tx_outs_as_spendable())
    return spend_tx
Ejemplo n.º 17
0
 def test_cache_tx(self):
     the_dir = self.set_cache_dir()
     tx = Tx.from_hex(
         "01000000010000000000000000000000000000000000000000000000000000000000000000"
         "ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a"
         "2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781"
         "e62294721166bf621e73a82cbf2342c858eeac00000000")
     self.launch_tool("tx -C %s --db %s" % (tx.id(), tx.as_hex()), env=dict(PYCOIN_CACHE_DIR=the_dir))
     self.assertTrue(os.path.exists(os.path.join(the_dir, "txs", "%s_tx.bin" % tx.id())))
Ejemplo n.º 18
0
 def tx_for_tx_hash(self, tx_hash):
     """
     Get a Tx by its hash.
     """
     url = "%s/rawtx/%s" % (self.url, b2h_rev(tx_hash))
     d = urlopen(url).read()
     j = json.loads(d.decode("utf8"))
     tx = Tx.from_hex(j.get("rawtx", ""))
     if tx.hash() == tx_hash:
         return tx
Ejemplo n.º 19
0
def tx_to_b64(tx_hex):
    # use this to dump raw transactions in the data above
    import io
    tx = Tx.from_hex(tx_hex)
    f = io.BytesIO()
    tx.stream(f)
    d = f.getvalue()
    for idx in range(0, len(d), 45):
        print('"%s"' %
              binascii.b2a_base64(d[idx:idx + 45]).decode("utf8")[:-1])
Ejemplo n.º 20
0
 def test_coinbase_tx(self):
     tx = Tx.coinbase_tx(COINBASE_PUB_KEY_FROM_80971, int(50 * 1e8),
                         COINBASE_BYTES_FROM_80971)
     s = io.BytesIO()
     tx.stream(s)
     tx1 = s.getvalue()
     s = io.BytesIO()
     block_80971.txs[0].stream(s)
     tx2 = s.getvalue()
     self.assertEqual(tx1, tx2)
Ejemplo n.º 21
0
 def tx_for_tx_hash(self, tx_hash):
     """
     returns the pycoin.tx object for tx_hash
     """
     try:
         url_append = "?token=%s&includeHex=true" % self.api_key
         url = self.base_url("txs/%s%s" % (b2h_rev(tx_hash), url_append))
         result = json.loads(urlopen(url).read().decode("utf8"))
         tx = Tx.parse(io.BytesIO(h2b(result.get("hex"))))
         return tx
     except:
         raise Exception
Ejemplo n.º 22
0
 def test_tx_api(self):
     tx = Tx.from_hex(
         TX_E1A18B843FC420734DEEB68FF6DF041A2585E1A0D7DBF3B82AAB98291A6D9952_HEX
     )
     # this transaction is a pay-to-hash transaction
     self.assertEqual(
         tx.id(),
         "e1a18b843fc420734deeb68ff6df041a2585e1a0d7dbf3b82aab98291a6d9952")
     self.assertEqual(tx.txs_out[0].bitcoin_address(),
                      "19LemzJ3XPdUxp113uynqCAivDbXZBdBy3")
     self.assertEqual(tx.txs_out[1].bitcoin_address(),
                      "3KmkA7hvqG2wKkWUGz1BySioUywvcmdPLR")
Ejemplo n.º 23
0
    def test_p2sh_multisig_sequential_signing(self):
        raw_scripts = [
            h2b("52210234abcffd2e80ad01c2ec0276ad02682808169c6fafdd25ebfb60703df272b461"
                "2102e5baaafff8094e4d77ce8b009d5ebc3de9110085ebd3d96e50cc7ce70faf175221"
                "0316ee25e80eb6e6fc734d9c86fa580cbb9c4bfd94a19f0373a22353ececd4db6853ae"
                )
        ]
        txs_in = [
            TxIn(previous_hash=h2b(
                '43c95d14724437bccc102ccf86aba1ac02415524fd1aefa787db886bba52a10c'
            ),
                 previous_index=0)
        ]
        txs_out = [
            TxOut(10000,
                  standard_tx_out_script('3KeGeLFmsbmbVdeMLrWp7WYKcA3tdsB4AR'))
        ]
        spendable = {
            'script_hex':
            'a914c4ed4de526461e3efbb79c8b688a6f9282c0464687',
            'does_seem_spent':
            0,
            'block_index_spent':
            0,
            'coin_value':
            10000,
            'block_index_available':
            0,
            'tx_out_index':
            0,
            'tx_hash_hex':
            '0ca152ba6b88db87a7ef1afd24554102aca1ab86cf2c10ccbc374472145dc943'
        }

        tx__prototype = Tx(version=DEFAULT_VERSION,
                           txs_in=txs_in,
                           txs_out=txs_out,
                           unspents=[Spendable.from_dict(spendable)])
        key_1 = 'Kz6pytJCigYHeMsGLmfHQPJhN5og2wpeSVrU43xWwgHLCAvpsprh'
        key_2 = 'Kz7NHgX7MBySA3RSKj9GexUSN6NepEDoPNugSPr5absRDoKgn2dT'
        for ordered_keys in [(key_1, key_2), (key_2, key_1)]:
            tx = copy.deepcopy(tx__prototype)
            for key in ordered_keys:
                self.assertEqual(tx.bad_signature_count(), 1)
                tx.sign(LazySecretExponentDB([key], {}),
                        p2sh_lookup=build_p2sh_lookup(raw_scripts))
            self.assertEqual(tx.bad_signature_count(), 0)
Ejemplo n.º 24
0
 def get(self, key):
     for path in self.paths_for_hash(key):
         try:
             tx = Tx.parse(open(path, "rb"))
             if tx and tx.hash() == key:
                 return tx
         except IOError:
             pass
     for method in self.lookup_methods:
         try:
             tx = method(key)
             if tx and tx.hash() == key:
                 self.put(tx)
                 return tx
         except Exception:
             pass
     return None
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]

            flag_mask = 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, flag_mask, comments)
Ejemplo n.º 26
0
 def test_tx_with_gpg(self):
     # binary data with GPG-encrypted WIF KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn for secret exponent 1
     WIF_1_GPG = h2b(
         "8c0d040303026c3030b7518a94eb60c950bc87ab26f0604a37f247f74f88deda10b180bb807"
         "2879b728b8f056808baea0c8e511e7cf2eba77cce937d2f69a67a79e163bf70b57113d27cb6"
         "a1c2390a1e8069b447c34a7c9b5ba268c2beedd85b50")
     gpg_wif = tempfile.NamedTemporaryFile(suffix=".gpg")
     gpg_wif.write(WIF_1_GPG)
     gpg_wif.flush()
     output_file = tempfile.NamedTemporaryFile(suffix=".hex")
     self.launch_tool(
         args=["tx",
               "5564224b6c01dbc2bfad89bfd8038bc2f4ca6c55eb660511d7151d71e4b94b6d/0/"
               "210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac/5000000000",
               "1KissFDVu2wAYWPRm4UGh5ZCDU9sE9an8T", "-f", gpg_wif.name, "-g", "--batch --passphrase=foo",
               "-o", output_file.name])
     d = open(output_file.name).read()
     tx = Tx.from_hex(d)
     self.assertEqual(tx.id(), "c52b0c66cff6147b99acb29389343f6eae68c29faf2186fa8c1613d615b217e8")
Ejemplo n.º 27
0
def generate_tx(txs, spendables, payables, args):
    txs_in, txs_out, unspents = merge_txs(txs, spendables, payables)
    lock_time, version = calculate_lock_time_and_version(args, txs)
    if len(unspents) == len(txs_in):
        unspents = remove_indices(unspents, args.remove_tx_in)
    txs_in = remove_indices(txs_in, args.remove_tx_in)
    txs_out = remove_indices(txs_out, args.remove_tx_out)
    tx = Tx(txs_in=txs_in,
            txs_out=txs_out,
            lock_time=lock_time,
            version=version,
            unspents=unspents)
    fee = args.fee
    try:
        if len(payables) > 0:
            distribute_from_split_pool(tx, fee)
    except ValueError as ex:
        print("warning: %s" % ex.args[0], file=sys.stderr)
    return tx
Ejemplo n.º 28
0
 def test_nulldata(self):
     OP_RETURN = tools.compile("OP_RETURN")
     # note that because chr() is used samples with length > 255 will not work
     for sample in [
             b'test', b'me', b'a', b'39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q',
             b'', b'0' * 80
     ]:
         sample_script = OP_RETURN + tools.bin_script([sample])
         nd = ScriptNulldata(sample)
         self.assertEqual(nd.nulldata, sample)
         self.assertEqual(nd.script(), sample_script)
         nd2 = ScriptNulldata.from_script(sample_script)
         self.assertEqual(nd.nulldata, nd2.nulldata)
         out = TxOut(1, nd.script())
         # ensure we can create a tx
         Tx(0, [], [out])
         # convert between asm and back to ensure no bugs with compilation
         self.assertEqual(nd.script(),
                          tools.compile(tools.disassemble(nd.script())))
def main():
    if len(sys.argv) != 4:
        print("usage: %s incoming_tx_hex_filename tx_out_index new_address" %
              sys.argv[0])
        sys.exit(-1)

    with open(sys.argv[1], "r") as f:
        tx_hex = f.readline().strip()

    # get the spendable from the prior transaction
    tx = Tx.from_hex(tx_hex)
    tx_out_index = int(sys.argv[2])
    spendable = tx.tx_outs_as_spendable()[tx_out_index]

    # make sure the address is valid
    payable = sys.argv[3]
    assert is_address_valid(payable)

    # create the unsigned transaction
    tx = create_tx([spendable], [payable])

    print("here is the transaction: %s" % tx.as_hex(include_unspents=True))
Ejemplo n.º 30
0
 def test_validate_multisig(self):
     # this is a transaction in the block chain
     # the unspents are included too, so it can be validated
     f = io.BytesIO(
         h2b("01000000025718fb915fb8b3a802bb699ddf04dd91261ef6715f5f2820a2b1b9b7e38b"
             "4f27000000004a004830450221008c2107ed4e026ab4319a591e8d9ec37719cdea0539"
             "51c660566e3a3399428af502202ecd823d5f74a77cc2159d8af2d3ea5d36a702fef9a7"
             "edaaf562aef22ac35da401ffffffff038f52231b994efb980382e4d804efeadaee13cf"
             "e01abe0d969038ccb45ec17000000000490047304402200487cd787fde9b337ab87f9f"
             "e54b9fd46d5d1692aa58e97147a4fe757f6f944202203cbcfb9c0fc4e3c453938bbea9"
             "e5ae64030cf7a97fafaf460ea2cb54ed5651b501ffffffff0100093d00000000001976"
             "a9144dc39248253538b93d3a0eb122d16882b998145888ac0000000002000000000000"
             "004751210351efb6e91a31221652105d032a2508275f374cea63939ad72f1b1e02f477"
             "da782100f2b7816db49d55d24df7bdffdbc1e203b424e8cd39f5651ab938e5e4a19356"
             "9e52ae404b4c00000000004751210351efb6e91a31221652105d032a2508275f374cea"
             "63939ad72f1b1e02f477da7821004f0331742bbc917ba2056a3b8a857ea47ec088dd10"
             "475ea311302112c9d24a7152ae"))
     tx = Tx.parse(f)
     tx.parse_unspents(f)
     self.assertEqual(
         tx.id(),
         "70c4e749f2b8b907875d1483ae43e8a6790b0c8397bbb33682e3602617f9a77a")
     self.assertEqual(tx.bad_signature_count(), 0)