Exemplo n.º 1
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 = BitcoinScriptTools.compile(scriptSig.get("asm"))
            previous_index = vin.get("vout")
        sequence = vin.get("sequence")
        txs_in.append(Tx.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 = BitcoinScriptTools.compile(vout.get("scriptPubKey").get("asm"))
        txs_out.append(Tx.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
Exemplo n.º 2
0
def make_script_test(script_in, script_out, flags_string, comment, expected,
                     coin_value, script_witness):
    script_in_bin = BitcoinScriptTools.compile(script_in)
    script_out_bin = BitcoinScriptTools.compile(script_out)
    script_witness_bin = [h2b(w) for w in script_witness]
    flags = parse_flags(flags_string)

    def f(self):
        try:
            credit_tx = build_credit_tx(script_out_bin, coin_value)
            spend_tx = build_spending_tx(script_in_bin, credit_tx)
            spend_tx.txs_in[0].witness = script_witness_bin
            msg = ''
            spend_tx.check_solution(tx_in_idx=0, flags=flags)
            r = 0
        except ScriptError as se:
            r = se.error_code()
            msg = se.args[0]
        except Exception:
            r = -1
        # for now, just deal with 0 versus nonzero
        expect_error = getattr(errno, expected)
        if r != expect_error:
            dump_failure_info(spend_tx, script_in, script_out, flags,
                              flags_string, expected, r, msg, comment)
        self.assertEqual(r, expect_error)

    return f
Exemplo n.º 3
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 = BitcoinScriptTools.compile(scriptSig.get("asm"))
            previous_index = vin.get("vout")
        sequence = vin.get("sequence")
        txs_in.append(Tx.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 = BitcoinScriptTools.compile(
            vout.get("scriptPubKey").get("asm"))
        txs_out.append(Tx.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
Exemplo n.º 4
0
 def test_nulldata_push(self):
     OP_RETURN = BitcoinScriptTools.compile("OP_RETURN")
     for sample in [b'test', b'me', b'a', b'39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q', b'', b'0'*80]:
         sample_push = BitcoinScriptTools.compile_push_data_list([sample])
         sample_script = OP_RETURN + sample_push
         sc = script_for_nulldata_push(sample)
         info = script_info_for_script(sc)
         self.assertEqual(info.get("data"), sample_push)
         self.assertEqual(sc, sample_script)
         out = Tx.TxOut(1, sc)
         # ensure we can create a tx
         Tx(0, [], [out])
         # convert between asm and back to ensure no bugs with compilation
         self.assertEqual(sc, BitcoinScriptTools.compile(BitcoinScriptTools.disassemble(sc)))
Exemplo n.º 5
0
 def test_nulldata_push(self):
     OP_RETURN = BitcoinScriptTools.compile("OP_RETURN")
     for sample in [b'test', b'me', b'a', b'39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q', b'', b'0'*80]:
         sample_push = BitcoinScriptTools.compile_push_data_list([sample])
         sample_script = OP_RETURN + sample_push
         sc = script_for_nulldata_push(sample)
         info = script_info_for_script(sc)
         self.assertEqual(info.get("data"), sample_push)
         self.assertEqual(sc, sample_script)
         out = Tx.TxOut(1, sc)
         # ensure we can create a tx
         Tx(0, [], [out])
         # convert between asm and back to ensure no bugs with compilation
         self.assertEqual(sc, BitcoinScriptTools.compile(BitcoinScriptTools.disassemble(sc)))
Exemplo n.º 6
0
 def test_p2multisig_wit(self):
     keys = [Key(i, generator=secp256k1_generator) for i in (1, 2, 3)]
     secs = [k.sec() for k in keys]
     underlying_script = script_for_multisig(2, secs)
     p2sh_script = BitcoinScriptTools.compile("OP_0 [%s]" % b2h(hashlib.sha256(underlying_script).digest()))
     script = script_for_address(address_for_p2s(p2sh_script))
     self.do_test_tx(script, p2sh_lookup=build_p2sh_lookup([underlying_script, p2sh_script]))
Exemplo n.º 7
0
 def test_p2sh_wit(self):
     keys = [Key(i, generator=secp256k1_generator) for i in (1, 2, 3)]
     secs = [k.sec() for k in keys]
     underlying_script = script_for_multisig(2, secs)
     script = BitcoinScriptTools.compile(
         "OP_0 [%s]" % b2h(hashlib.sha256(underlying_script).digest()))
     self.do_test_tx(script,
                     p2sh_lookup=build_p2sh_lookup([underlying_script]))
Exemplo n.º 8
0
 def test_nulldata(self):
     OP_RETURN = BitcoinScriptTools.compile("OP_RETURN")
     for sample in [b'test', b'me', b'a', b'39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q', b'', b'0'*80]:
         sample_script = OP_RETURN + sample
         sc = script_for_nulldata(sample)
         info = script_info_for_script(sc)
         self.assertEqual(info.get("data"), sample)
         self.assertEqual(sc, sample_script)
         out = Tx.TxOut(1, sc)
         # ensure we can create a tx
         Tx(0, [], [out])
Exemplo n.º 9
0
 def test_nulldata(self):
     OP_RETURN = BitcoinScriptTools.compile("OP_RETURN")
     for sample in [b'test', b'me', b'a', b'39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q', b'', b'0'*80]:
         sample_script = OP_RETURN + sample
         sc = script_for_nulldata(sample)
         info = script_info_for_script(sc)
         self.assertEqual(info.get("data"), sample)
         self.assertEqual(sc, sample_script)
         out = Tx.TxOut(1, sc)
         # ensure we can create a tx
         Tx(0, [], [out])
Exemplo n.º 10
0
Arquivo: Tx.py Projeto: thephez/pycoin
 def coinbase_tx(cls,
                 public_key_sec,
                 coin_value,
                 coinbase_bytes=b'',
                 version=1,
                 lock_time=0):
     """Create the special "first in block" transaction that includes the mining fees."""
     tx_in = cls.TxIn.coinbase_tx_in(script=coinbase_bytes)
     COINBASE_SCRIPT_OUT = "%s OP_CHECKSIG"
     script_text = COINBASE_SCRIPT_OUT % b2h(public_key_sec)
     script_bin = BitcoinScriptTools.compile(script_text)
     tx_out = cls.TxOut(coin_value, script_bin)
     return cls(version, [tx_in], [tx_out], lock_time)
Exemplo n.º 11
0
def dump_failure_info(spend_tx, script_in, script_out, flags, flags_string,
                      expected, actual, message, comment):
    # return
    print()
    print(flags_string)
    print("EXPECTED: %s" % expected)
    print("ACTUAL: %s" % actual)
    print("MESSAGE: %s" % message)
    print(comment)
    print(BitcoinScriptTools.disassemble(
        BitcoinScriptTools.compile(script_in)))
    print(
        BitcoinScriptTools.disassemble(BitcoinScriptTools.compile(script_out)))

    def tbf(*args):
        opcode, data, pc, vm = args
        stack = vm.stack
        altstack = vm.altstack
        opd = BitcoinScriptTools.disassemble_for_opcode_data(opcode, data)
        if len(altstack) == 0:
            altstack = ''
        print("%s %s\n  %3x  %s" % (stack, altstack, pc, opd))
        import pdb
        pdb.set_trace()

    print("test failed: '%s' '%s' : %s  %s" %
          (script_in, script_out, comment, flags_string))
    try:
        import pdb
        pdb.set_trace()
        spend_tx.check_solution(tx_in_idx=0, traceback_f=tbf, flags=flags)
    except Exception as ex:
        print(ex)
    try:
        spend_tx.check_solution(tx_in_idx=0, traceback_f=tbf, flags=flags)
    except Exception as ex:
        print(ex)
        import pdb
        pdb.set_trace()
Exemplo n.º 12
0
    def test_p2sh(self):
        keys = [Key(i, generator=secp256k1_generator) for i in (1, 2, 3)]
        secs = [k.sec() for k in keys]
        underlying_script = script_for_multisig(1, secs)
        script = script_for_address(address_for_p2s(underlying_script))
        self.do_test_tx(script, p2sh_lookup=build_p2sh_lookup([underlying_script]))

        underlying_script = BitcoinScriptTools.compile("OP_SWAP") + script_for_address(keys[0].address())
        script = script_for_address(address_for_p2s(underlying_script))
        self.do_test_tx(script, p2sh_lookup=build_p2sh_lookup([underlying_script]))

        underlying_script = script_for_p2pk(keys[2].sec())
        script = script_for_address(address_for_p2s(underlying_script))
        self.do_test_tx(script, p2sh_lookup=build_p2sh_lookup([underlying_script]))
Exemplo n.º 13
0
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 Exception:
                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=ScriptTools.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)
Exemplo n.º 14
0
    def test_p2sh(self):
        keys = [Key(i, generator=secp256k1_generator) for i in (1, 2, 3)]
        secs = [k.sec() for k in keys]
        underlying_script = script_for_multisig(1, secs)
        script = script_for_address(address_for_p2s(underlying_script))
        self.do_test_tx(script,
                        p2sh_lookup=build_p2sh_lookup([underlying_script]))

        underlying_script = BitcoinScriptTools.compile(
            "OP_SWAP") + script_for_address(keys[0].address())
        script = script_for_address(address_for_p2s(underlying_script))
        self.do_test_tx(script,
                        p2sh_lookup=build_p2sh_lookup([underlying_script]))

        underlying_script = script_for_p2pk(keys[2].sec())
        script = script_for_address(address_for_p2s(underlying_script))
        self.do_test_tx(script,
                        p2sh_lookup=build_p2sh_lookup([underlying_script]))
Exemplo n.º 15
0
    def test_validate_two_inputs(self):
        def tx_from_b64(h):
            d = binascii.a2b_base64(h.encode("utf8"))
            return Tx.from_bin(d)

        # tx_0 = c9989d984c97128b03b9f118481c631c584f7aa42b578dbea6194148701b053d
        # This is the one we're going to validate. It has inputs from
        #  tx_1 = b52201c2741d410b70688335afebba0d58f8675fa9b6c8c54becb0d7c0a75983
        # and tx_2 = 72151f65db1d8594df90778639a4c0c17c1e303af01de0d04af8fac13854bbfd
        TX_0_HEX = (
            "AQAAAAKDWafA17DsS8XItqlfZ/hYDbrrrzWDaHALQR10wgEitQAAAACLSDBFAiAnyvQ1P7"
            "b8+84JbBUbE1Xtgrd0KNpD4eyVTNU/burbtgIhAOS8T1TrhXkGXQTGbLSEJy5uvZMGEzOj"
            "ITxO+DrykiPlAUEE3yJcIB5OCpaDjrop+N3bm8h9PKw8bF/YB4v3yD+VeQf4fXdUZ9hJJS"
            "nFeJ+QeJrC7q3Y23QSYeYbW/AfA3D5G//////9u1Q4wfr4StDgHfA6MB58wcCkOYZ3kN+U"
            "hR3bZR8VcgAAAACLSDBFAiAN6ZQr+9HTgmF57EsPyXIhQ6J5M4lgwlj/tJTShZ+toQIhAL"
            "0U1i9yiCEm75uCEp8uRaySqS7P4x7A+L2Vr5kS+7ANAUEEkSqVI6gw1scM0GuJWgMh4jpW"
            "KJA0yOl03uQaV/jHURn+HswOIORzvsG9qQY1/9BZgDPaMuI5U5JlyA3WkhLxgf////8Ctk"
            "SUzxAAAAAZdqkULXTu3lp2t/wMSuvqbifOSj9/kvmIrAAoa+4AAAAAGXapFF3ySpVdjz9V"
            "8fRKvzDqXQRcmowSiKwAAAAA")
        TX_1_HEX = (
            "AQAAAAEL3YmFDcZpf4SH7uN1IBmMoBd4OhmTp4EAQ8A0ZQ3tiwAAAACKRzBEAiA4Fkl8lk"
            "JSeLtWHsp1j0h7y0KKFmqxhDR0CK0HnmZWBQIgDSTDenor3zbNqTs+FApeDl8DKCz1xGQC"
            "JQN0/sp00VABQQQzSNc33wdDXA/F9y9/hAR88q6Se6vRCHEC7dYgbIp1pgxqGzrWXQroGk"
            "QLhnAbn/fDhUoVbCgM/UHXYmjXlhdO/////wI3HGlfEQAAABl2qRRM+dhUVUjeAlb0jEsH"
            "JrFClGGSZ4isMAYVCgAAAAAZdqkUgnSLXoYTeOKFFRdtLYxWcGZ2Ht2IrAAAAAA=")
        TX_2_HEX = (
            "AQAAAAFDjBbw61AYUWMx+3moZ2vb9dvLKydOSFIwcfBTjG0QSgEAAACKRzBEAiA5WWKhR4"
            "8OI60ZDCXnOru/FH6NvuTGhRLggjbpJB2dhgIgKp0FFL0ClSCxxqGjYneDinvgROGSw6Dt"
            "Vtvflrhaom8BQQR50YjAg1e5qRkP4ER29ec5jKfzk3DHJhS7Si0sEbvNIJMfjjbZfZWtJi"
            "15wHZhuHh4e3G6SWMdJLHH5pgbseFh/////wLPE5deAAAAABl2qRSmRdbMvv5fEbgFD1Yk"
            "taBU9zQTW4iswJ7mBQAAAAAZdqkU4E5+Is4tr+8bPU6ELYHSvz/Ng0eIrAAAAAA=")
        tx_0 = tx_from_b64(TX_0_HEX)
        self.assertEqual(
            tx_0.id(),
            "c9989d984c97128b03b9f118481c631c584f7aa42b578dbea6194148701b053d")
        tx_1 = tx_from_b64(TX_1_HEX)
        self.assertEqual(
            tx_1.id(),
            "b52201c2741d410b70688335afebba0d58f8675fa9b6c8c54becb0d7c0a75983")
        tx_2 = tx_from_b64(TX_2_HEX)
        self.assertEqual(
            tx_2.id(),
            "72151f65db1d8594df90778639a4c0c17c1e303af01de0d04af8fac13854bbfd")

        TX_DB = {tx.hash(): tx for tx in [tx_0, tx_1, tx_2]}

        tx_to_validate = tx_0
        self.assertEqual(
            "OP_DUP OP_HASH160 [2d74eede5a76b7fc0c4aebea6e27ce4a3f7f92f9] OP_EQUALVERIFY OP_CHECKSIG",
            BitcoinScriptTools.disassemble(tx_to_validate.txs_out[0].script))
        self.assertEqual(
            tx_to_validate.id(),
            "c9989d984c97128b03b9f118481c631c584f7aa42b578dbea6194148701b053d")

        tx_to_validate.unspents_from_db(TX_DB)
        self.assertEqual(tx_to_validate.bad_signature_count(), 0)

        # now let's mess with signatures
        disassembly = BitcoinScriptTools.disassemble(
            tx_to_validate.txs_in[0].script)
        tx_to_validate.txs_in[0].script = BitcoinScriptTools.compile(
            disassembly)
        self.assertEqual(tx_to_validate.bad_signature_count(), 0)
        disassembly = disassembly.replace("353fb6fcfbce09", "353fb6fcfbce19")
        tx_to_validate.txs_in[0].script = BitcoinScriptTools.compile(
            disassembly)
        self.assertEqual(tx_to_validate.bad_signature_count(), 1)
        self.assertFalse(tx_to_validate.is_signature_ok(0))

        tx_to_validate = tx_from_b64(TX_0_HEX)
        tx_to_validate.unspents_from_db(TX_DB)
        self.assertEqual(tx_to_validate.bad_signature_count(), 0)
        disassembly = BitcoinScriptTools.disassemble(
            tx_to_validate.txs_in[1].script)
        disassembly = disassembly.replace("960c258ffb494d2859f",
                                          "960d258ffb494d2859f")
        tx_to_validate.txs_in[1].script = BitcoinScriptTools.compile(
            disassembly)
        self.assertEqual(tx_to_validate.bad_signature_count(), 1)
        self.assertFalse(tx_to_validate.is_signature_ok(1))

        # futz with signature on tx_1
        tx_to_validate = tx_from_b64(TX_0_HEX)
        original_tx_hash = tx_1.hash()
        disassembly = BitcoinScriptTools.disassemble(tx_1.txs_out[0].script)
        disassembly = disassembly.replace(
            "4cf9d8545548de0256f48c4b0726b14294619267",
            "4cf9d8545548de1256f48c4b0726b14294619267")
        tx_1.txs_out[0].script = BitcoinScriptTools.compile(disassembly)
        TX_DB[original_tx_hash] = tx_1
        tx_to_validate.unspents_from_db(TX_DB, ignore_missing=True)
        self.assertEqual(tx_to_validate.bad_signature_count(), 1)
        self.assertFalse(tx_to_validate.is_signature_ok(0, ))

        # fix it up again
        TX_DB[original_tx_hash] = tx_from_b64(TX_1_HEX)
        tx_to_validate.unspents_from_db(TX_DB)
        self.assertEqual(tx_to_validate.bad_signature_count(), 0)

        # futz with signature on tx_2
        tx_to_validate = tx_from_b64(TX_0_HEX)
        original_tx_hash = tx_2.hash()
        disassembly = BitcoinScriptTools.disassemble(tx_2.txs_out[0].script)
        disassembly = disassembly.replace(
            "a645d6ccbefe5f11b8050f5624b5a054f734135b",
            "a665d6ccbefe5f11b8050f5624b5a054f734135b")
        tx_2.txs_out[0].script = BitcoinScriptTools.compile(disassembly)
        TX_DB[original_tx_hash] = tx_2
        tx_to_validate.unspents_from_db(TX_DB, ignore_missing=True)
        self.assertEqual(tx_to_validate.bad_signature_count(), 1)
        self.assertFalse(tx_to_validate.is_signature_ok(1))

        # fix it up again
        TX_DB[original_tx_hash] = tx_from_b64(TX_2_HEX)
        tx_to_validate.unspents_from_db(TX_DB)
        self.assertEqual(tx_to_validate.bad_signature_count(), 0)
Exemplo n.º 16
0
 def test_if(self):
     script = BitcoinScriptTools.compile("IF 1 ELSE 0 ENDIF")
Exemplo n.º 17
0
 def test_nonstandard_p2pkh(self):
     key = Key(1, generator=secp256k1_generator)
     self.do_test_tx(
         BitcoinScriptTools.compile("OP_SWAP") +
         script_for_address(key.address()))
Exemplo n.º 18
0
 def test_if(self):
     script = BitcoinScriptTools.compile("IF 1 ELSE 0 ENDIF")
Exemplo n.º 19
0
 def test_nonstandard_p2pkh(self):
     key = Key(1, generator=secp256k1_generator)
     self.do_test_tx(BitcoinScriptTools.compile("OP_SWAP") + script_for_address(key.address()))
Exemplo n.º 20
0
 def test_p2pkh_wit(self):
     key = Key(1, generator=secp256k1_generator)
     script = BitcoinScriptTools.compile("OP_0 [%s]" % b2h(key.hash160()))
     self.do_test_tx(script)
Exemplo n.º 21
0
    def test_validate_block_data(self):
        # block 80971
        block_80971_id = '00000000001126456C67A1F5F0FF0268F53B4F22E0531DC70C7B69746AF69DAC'.lower(
        )
        block_80971_data = h2b(
            "01000000950A1631FB9FAC411DFB173487B9E18018B7C6F7147E78C062584100000000"
            "00A881352F97F14BF191B54915AE124E051B8FE6C3922C5082B34EAD503000FC34D891"
            "974CED66471B4016850A04010000000100000000000000000000000000000000000000"
            "00000000000000000000000000FFFFFFFF0804ED66471B02C301FFFFFFFF0100F2052A"
            "01000000434104CB6B6B4EADC96C7D08B21B29D0ADA5F29F9378978CABDB602B8B65DA"
            "08C8A93CAAB46F5ABD59889BAC704925942DD77A2116D10E0274CAD944C71D3D1A6705"
            "70AC0000000001000000018C55ED829F16A4E43902940D3D33005264606D5F7D555B5F"
            "67EE4C033390C2EB010000008A47304402202D1BF606648EDCDB124C1254930852D991"
            "88E1231715031CBEAEA80CCFD2B39A02201FA9D6EE7A1763580E342474FC1AEF59B046"
            "8F98479953437F525063E25675DE014104A01F763CFBF5E518C628939158AF3DC0CAAC"
            "35C4BA7BC1CE8B7E634E8CDC44E15F0296B250282BD649BAA8398D199F2424FCDCD88D"
            "3A9ED186E4FD3CB9BF57CFFFFFFFFF02404B4C00000000001976A9148156FF75BEF24B"
            "35ACCE3C05289A2411E1B0E57988AC00AA38DF010000001976A914BC7E692A5FFE95A5"
            "96712F5ED83393B3002E452E88AC0000000001000000019C97AFDF6C9A31FFA86D71EA"
            "79A079001E2B59EE408FD418498219400639AC0A010000008B4830450220363CFFAE09"
            "599397B21E6D8A8073FB1DFBE06B6ACDD0F2F7D3FEA86CA9C3F605022100FA255A6ED2"
            "3FD825C759EF1A885A31CAD0989606CA8A3A16657D50FE3CEF5828014104FF444BAC08"
            "308B9EC97F56A652AD8866E0BA804DA97868909999566CB377F4A2C8F1000E83B49686"
            "8F3A282E1A34DF78565B65C15C3FA21A0763FD81A3DFBBB6FFFFFFFF02C05EECDE0100"
            "00001976A914588554E6CC64E7343D77117DA7E01357A6111B7988AC404B4C00000000"
            "001976A914CA6EB218592F289999F13916EE32829AD587DBC588AC0000000001000000"
            "01BEF5C9225CB9FE3DEF929423FA36AAD9980B9D6F8F3070001ACF3A5FB389A69F0000"
            "00004A493046022100FB23B1E2F2FB8B96E04D220D385346290A9349F89BBBC5C225D5"
            "A56D931F8A8E022100F298EB28294B90C1BAF319DAB713E7CA721AAADD8FCC15F849DE"
            "7B0A6CF5412101FFFFFFFF0100F2052A010000001976A9146DDEA8071439951115469D"
            "0D2E2B80ECBCDD48DB88AC00000000")

        # block 80974
        block_80974_id = '0000000000089F7910F6755C10EA2795EC368A29B435D80770AD78493A6FECF1'.lower(
        )
        block_80974_data = h2b(
            "010000007480150B299A16BBCE5CCDB1D1BBC65CFC5893B01E6619107C552000000000"
            "007900A2B203D24C69710AB6A94BEB937E1B1ADD64C2327E268D8C3E5F8B41DBED8796"
            "974CED66471B204C324703010000000100000000000000000000000000000000000000"
            "00000000000000000000000000FFFFFFFF0804ED66471B024001FFFFFFFF0100F2052A"
            "010000004341045FEE68BAB9915C4EDCA4C680420ED28BBC369ED84D48AC178E1F5F7E"
            "EAC455BBE270DABA06802145854B5E29F0A7F816E2DF906E0FE4F6D5B4C9B92940E4F0"
            "EDAC000000000100000001F7B30415D1A7BF6DB91CB2A272767C6799D721A4178AA328"
            "E0D77C199CB3B57F010000008A4730440220556F61B84F16E637836D2E74B8CB784DE4"
            "0C28FE3EF93CCB7406504EE9C7CAA5022043BD4749D4F3F7F831AC696748AD8D8E79AE"
            "B4A1C539E742AA3256910FC88E170141049A414D94345712893A828DE57B4C2054E2F5"
            "96CDCA9D0B4451BA1CA5F8847830B9BE6E196450E6ABB21C540EA31BE310271AA00A49"
            "ED0BA930743D1ED465BAD0FFFFFFFF0200E1F505000000001976A914529A63393D63E9"
            "80ACE6FA885C5A89E4F27AA08988ACC0ADA41A000000001976A9145D17976537F30886"
            "5ED533CCCFDD76558CA3C8F088AC00000000010000000165148D894D3922EF5FFDA962"
            "BE26016635C933D470C8B0AB7618E869E3F70E3C000000008B48304502207F5779EBF4"
            "834FEAEFF4D250898324EB5C0833B16D7AF4C1CB0F66F50FCF6E85022100B78A65377F"
            "D018281E77285EFC31E5B9BA7CB7E20E015CF6B7FA3E4A466DD195014104072AD79E0A"
            "A38C05FA33DD185F84C17F611E58A8658CE996D8B04395B99C7BE36529CAB7606900A0"
            "CD5A7AEBC6B233EA8E0FE60943054C63620E05E5B85F0426FFFFFFFF02404B4C000000"
            "00001976A914D4CAA8447532CA8EE4C80A1AE1D230A01E22BFDB88AC8013A0DE010000"
            "001976A9149661A79AE1F6D487AF3420C13E649D6DF3747FC288AC00000000")

        block_80971 = Block.from_bin(block_80971_data)
        self.assertEqual(block_80971.id(), block_80971_id)
        block_80974 = Block.from_bin(block_80974_data)
        self.assertEqual(block_80974.id(), block_80974_id)

        tx_db = {tx.hash(): tx for tx in block_80971.txs}

        tx_to_validate = block_80974.txs[2]
        self.assertEqual(
            "OP_DUP OP_HASH160 [d4caa8447532ca8ee4c80a1ae1d230a01e22bfdb] OP_EQUALVERIFY OP_CHECKSIG",
            BitcoinScriptTools.disassemble(tx_to_validate.txs_out[0].script))
        self.assertEqual(
            tx_to_validate.id(),
            "7c4f5385050c18aa8df2ba50da566bbab68635999cc99b75124863da1594195b")

        tx_to_validate.unspents_from_db(tx_db)
        self.assertEqual(tx_to_validate.bad_signature_count(), 0)

        # now, let's corrupt the Tx and see what happens
        tx_out = tx_to_validate.txs_out[1]

        disassembly = BitcoinScriptTools.disassemble(tx_out.script)
        tx_out.script = BitcoinScriptTools.compile(disassembly)

        self.assertEqual(tx_to_validate.bad_signature_count(), 0)

        disassembly = disassembly.replace(
            "9661a79ae1f6d487af3420c13e649d6df3747fc2",
            "9661a79ae1f6d487af3420c13e649d6df3747fc3")

        tx_out.script = BitcoinScriptTools.compile(disassembly)

        self.assertEqual(tx_to_validate.bad_signature_count(), 1)
        self.assertFalse(tx_to_validate.is_signature_ok(0))
Exemplo n.º 22
0
    def _test_sighash_single(self, network):
        Key = network.ui._key_class
        k0 = Key(secret_exponent=PRIV_KEYS[0],
                 generator=secp256k1_generator,
                 is_compressed=True)
        k1 = Key(secret_exponent=PRIV_KEYS[1],
                 generator=secp256k1_generator,
                 is_compressed=True)
        k2 = Key(secret_exponent=PRIV_KEYS[2],
                 generator=secp256k1_generator,
                 is_compressed=True)
        k3 = Key(secret_exponent=PRIV_KEYS[3],
                 generator=secp256k1_generator,
                 is_compressed=True)
        k4 = Key(secret_exponent=PRIV_KEYS[4],
                 generator=secp256k1_generator,
                 is_compressed=True)
        k5 = Key(secret_exponent=PRIV_KEYS[5],
                 generator=secp256k1_generator,
                 is_compressed=True)

        # Fake a coinbase transaction
        coinbase_tx = Tx.coinbase_tx(k0.sec(), 500000000)
        coinbase_tx.txs_out.append(
            TxOut(1000000000,
                  BitcoinScriptTools.compile('%s OP_CHECKSIG' %
                                             b2h(k1.sec()))))
        coinbase_tx.txs_out.append(
            TxOut(1000000000,
                  BitcoinScriptTools.compile('%s OP_CHECKSIG' %
                                             b2h(k2.sec()))))

        self.assertEqual(
            '2acbe1006f7168bad538b477f7844e53de3a31ffddfcfc4c6625276dd714155a',
            b2h_rev(coinbase_tx.hash()))

        script_for_address = network.ui.script_for_address

        # Make the test transaction
        txs_in = [
            TxIn(coinbase_tx.hash(), 0),
            TxIn(coinbase_tx.hash(), 1),
            TxIn(coinbase_tx.hash(), 2),
        ]
        txs_out = [
            TxOut(900000000, script_for_address(k3.address())),
            TxOut(800000000, script_for_address(k4.address())),
            TxOut(800000000, script_for_address(k5.address())),
        ]
        tx = Tx(1, txs_in, txs_out)
        tx.set_unspents(coinbase_tx.txs_out)

        self.assertEqual(
            '791b98ef0a3ac87584fe273bc65abd89821569fd7c83538ac0625a8ca85ba587',
            b2h_rev(tx.hash()))

        sig_type = SIGHASH_SINGLE

        solution_checker = BitcoinSolutionChecker(tx)
        sig_hash = solution_checker._signature_hash(
            coinbase_tx.txs_out[0].script, 0, sig_type)
        self.assertEqual(
            0xcc52d785a3b4133504d1af9e60cd71ca422609cb41df3a08bbb466b2a98a885e,
            sig_hash)

        sig = sigmake(k0, sig_hash, sig_type)
        self.assertTrue(sigcheck(k0, sig_hash, sig[:-1]))

        tx.txs_in[0].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(0))

        sig_hash = solution_checker._signature_hash(
            coinbase_tx.txs_out[1].script, 1, sig_type)
        self.assertEqual(
            0x93bb883d70fccfba9b8aa2028567aca8357937c65af7f6f5ccc6993fd7735fb7,
            sig_hash)

        sig = sigmake(k1, sig_hash, sig_type)
        self.assertTrue(sigcheck(k1, sig_hash, sig[:-1]))

        tx.txs_in[1].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(1))

        sig_hash = solution_checker._signature_hash(
            coinbase_tx.txs_out[2].script, 2, sig_type)
        self.assertEqual(
            0x53ef7f67c3541bffcf4e0d06c003c6014e2aa1fb38ff33240b3e1c1f3f8e2a35,
            sig_hash)

        sig = sigmake(k2, sig_hash, sig_type)
        self.assertTrue(sigcheck(k2, sig_hash, sig[:-1]))

        tx.txs_in[2].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(2))

        sig_type = SIGHASH_SINGLE | SIGHASH_ANYONECANPAY

        sig_hash = solution_checker._signature_hash(
            coinbase_tx.txs_out[0].script, 0, sig_type)
        self.assertEqual(
            0x2003393d246a7f136692ce7ab819c6eadc54ffea38eb4377ac75d7d461144e75,
            sig_hash)

        sig = sigmake(k0, sig_hash, sig_type)
        self.assertTrue(sigcheck(k0, sig_hash, sig[:-1]))

        tx.txs_in[0].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(0))

        sig_hash = solution_checker._signature_hash(
            coinbase_tx.txs_out[1].script, 1, sig_type)
        self.assertEqual(
            0xe3f469ac88e9f35e8eff0bd8ad4ad3bf899c80eb7645947d60860de4a08a35df,
            sig_hash)

        sig = sigmake(k1, sig_hash, sig_type)
        self.assertTrue(sigcheck(k1, sig_hash, sig[:-1]))

        tx.txs_in[1].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(1))

        sig_hash = solution_checker._signature_hash(
            coinbase_tx.txs_out[2].script, 2, sig_type)
        self.assertEqual(
            0xbacd7c3ab79cad71807312677c1788ad9565bf3c00ab9a153d206494fb8b7e6a,
            sig_hash)

        sig = sigmake(k2, sig_hash, sig_type)
        self.assertTrue(sigcheck(k2, sig_hash, sig[:-1]))

        tx.txs_in[2].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(2))
Exemplo n.º 23
0
    def _test_sighash_single(self, network):
        Key = network.ui._key_class
        k0 = Key(secret_exponent=PRIV_KEYS[0], generator=secp256k1_generator, is_compressed=True)
        k1 = Key(secret_exponent=PRIV_KEYS[1], generator=secp256k1_generator, is_compressed=True)
        k2 = Key(secret_exponent=PRIV_KEYS[2], generator=secp256k1_generator, is_compressed=True)
        k3 = Key(secret_exponent=PRIV_KEYS[3], generator=secp256k1_generator, is_compressed=True)
        k4 = Key(secret_exponent=PRIV_KEYS[4], generator=secp256k1_generator, is_compressed=True)
        k5 = Key(secret_exponent=PRIV_KEYS[5], generator=secp256k1_generator, is_compressed=True)

        # Fake a coinbase transaction
        coinbase_tx = Tx.coinbase_tx(k0.sec(), 500000000)
        coinbase_tx.txs_out.append(TxOut(1000000000, BitcoinScriptTools.compile('%s OP_CHECKSIG' % b2h(k1.sec()))))
        coinbase_tx.txs_out.append(TxOut(1000000000, BitcoinScriptTools.compile('%s OP_CHECKSIG' % b2h(k2.sec()))))

        self.assertEqual('2acbe1006f7168bad538b477f7844e53de3a31ffddfcfc4c6625276dd714155a',
                         b2h_rev(coinbase_tx.hash()))

        script_for_address = network.ui.script_for_address

        # Make the test transaction
        txs_in = [
            TxIn(coinbase_tx.hash(), 0),
            TxIn(coinbase_tx.hash(), 1),
            TxIn(coinbase_tx.hash(), 2),
        ]
        txs_out = [
            TxOut(900000000, script_for_address(k3.address())),
            TxOut(800000000, script_for_address(k4.address())),
            TxOut(800000000, script_for_address(k5.address())),
        ]
        tx = Tx(1, txs_in, txs_out)
        tx.set_unspents(coinbase_tx.txs_out)

        self.assertEqual('791b98ef0a3ac87584fe273bc65abd89821569fd7c83538ac0625a8ca85ba587', b2h_rev(tx.hash()))

        sig_type = SIGHASH_SINGLE

        solution_checker = BitcoinSolutionChecker(tx)
        sig_hash = solution_checker._signature_hash(coinbase_tx.txs_out[0].script, 0, sig_type)
        self.assertEqual(0xcc52d785a3b4133504d1af9e60cd71ca422609cb41df3a08bbb466b2a98a885e, sig_hash)

        sig = sigmake(k0, sig_hash, sig_type)
        self.assertTrue(sigcheck(k0, sig_hash, sig[:-1]))

        tx.txs_in[0].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(0))

        sig_hash = solution_checker._signature_hash(coinbase_tx.txs_out[1].script, 1, sig_type)
        self.assertEqual(0x93bb883d70fccfba9b8aa2028567aca8357937c65af7f6f5ccc6993fd7735fb7, sig_hash)

        sig = sigmake(k1, sig_hash, sig_type)
        self.assertTrue(sigcheck(k1, sig_hash, sig[:-1]))

        tx.txs_in[1].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(1))

        sig_hash = solution_checker._signature_hash(coinbase_tx.txs_out[2].script, 2, sig_type)
        self.assertEqual(0x53ef7f67c3541bffcf4e0d06c003c6014e2aa1fb38ff33240b3e1c1f3f8e2a35, sig_hash)

        sig = sigmake(k2, sig_hash, sig_type)
        self.assertTrue(sigcheck(k2, sig_hash, sig[:-1]))

        tx.txs_in[2].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(2))

        sig_type = SIGHASH_SINGLE | SIGHASH_ANYONECANPAY

        sig_hash = solution_checker._signature_hash(coinbase_tx.txs_out[0].script, 0, sig_type)
        self.assertEqual(0x2003393d246a7f136692ce7ab819c6eadc54ffea38eb4377ac75d7d461144e75, sig_hash)

        sig = sigmake(k0, sig_hash, sig_type)
        self.assertTrue(sigcheck(k0, sig_hash, sig[:-1]))

        tx.txs_in[0].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(0))

        sig_hash = solution_checker._signature_hash(coinbase_tx.txs_out[1].script, 1, sig_type)
        self.assertEqual(0xe3f469ac88e9f35e8eff0bd8ad4ad3bf899c80eb7645947d60860de4a08a35df, sig_hash)

        sig = sigmake(k1, sig_hash, sig_type)
        self.assertTrue(sigcheck(k1, sig_hash, sig[:-1]))

        tx.txs_in[1].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(1))

        sig_hash = solution_checker._signature_hash(coinbase_tx.txs_out[2].script, 2, sig_type)
        self.assertEqual(0xbacd7c3ab79cad71807312677c1788ad9565bf3c00ab9a153d206494fb8b7e6a, sig_hash)

        sig = sigmake(k2, sig_hash, sig_type)
        self.assertTrue(sigcheck(k2, sig_hash, sig[:-1]))

        tx.txs_in[2].script = BitcoinScriptTools.compile(b2h(sig))
        self.assertTrue(tx.is_signature_ok(2))
Exemplo n.º 24
0
 def test_p2pkh_wit(self):
     key = Key(1, generator=secp256k1_generator)
     script = BitcoinScriptTools.compile("OP_0 [%s]" % b2h(key.hash160()))
     self.do_test_tx(script)