def test_op_checklocktimeverify():
    prev_txn_hash = Hash('6eae1e03964799c4e29039db459ea4fad4df57c2b06f730b60032a48fb075620')
    txn_input = TransactionInput(prev_txn_hash, 0, Script(""), 1)

    addr = "1HJiL6AYYmtkbJzC9bxAorznWijwNK5Z8E"
    out_script_pub_key = Script.build_p2pkh(
        utils.address_to_key_hash(addr)[1])
    txn_output = TransactionOutput(9000, out_script_pub_key)

    # Create the txn
    txn = Transaction(Transaction.DEFAULT_TRANSACTION_VERSION,
                      [txn_input],
                      [txn_output],
                      367987)

    # This is one more (367988) so it should fail
    s = Script("0x749d05 OP_CHECKLOCKTIMEVERIFY")

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=out_script_pub_key)
    si.run_script(s)

    assert not si.valid

    # This is negative, so it should fail
    s = Script("0xfff74d05 OP_CHECKLOCKTIMEVERIFY")

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=out_script_pub_key)
    si.run_script(s)

    assert not si.valid

    # This is one less (367986) so it should pass
    s = Script("0x729d05 OP_CHECKLOCKTIMEVERIFY")

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=out_script_pub_key)
    si.run_script(s)

    assert not si.stop

    # Now reformulate the txn so that the input is finalized
    txn_input.sequence_num = 0xffffffff
    si.run_script(s)

    assert not si.valid

    # The last check is if there are mismatching notions of locktime
    txn_input.sequence_num = 1
    txn.lock_time = 500000001
    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=out_script_pub_key)
    si.run_script(s)

    assert not si.valid
def test_op_checklocktimeverify():
    prev_txn_hash = Hash(
        '6eae1e03964799c4e29039db459ea4fad4df57c2b06f730b60032a48fb075620')
    txn_input = TransactionInput(prev_txn_hash, 0, Script(""), 1)

    addr = "1HJiL6AYYmtkbJzC9bxAorznWijwNK5Z8E"
    out_script_pub_key = Script.build_p2pkh(utils.address_to_key_hash(addr)[1])
    txn_output = TransactionOutput(9000, out_script_pub_key)

    # Create the txn
    txn = Transaction(Transaction.DEFAULT_TRANSACTION_VERSION, [txn_input],
                      [txn_output], 367987)

    # This is one more (367988) so it should fail
    s = Script("0x749d05 OP_CHECKLOCKTIMEVERIFY")

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=out_script_pub_key)
    si.run_script(s)

    assert not si.valid

    # This is negative, so it should fail
    s = Script("0xfff74d05 OP_CHECKLOCKTIMEVERIFY")

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=out_script_pub_key)
    si.run_script(s)

    assert not si.valid

    # This is one less (367986) so it should pass
    s = Script("0x729d05 OP_CHECKLOCKTIMEVERIFY")

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=out_script_pub_key)
    si.run_script(s)

    assert not si.stop

    # Now reformulate the txn so that the input is finalized
    txn_input.sequence_num = 0xffffffff
    si.run_script(s)

    assert not si.valid

    # The last check is if there are mismatching notions of locktime
    txn_input.sequence_num = 1
    txn.lock_time = 500000001
    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=out_script_pub_key)
    si.run_script(s)

    assert not si.valid
Beispiel #3
0
    def set_txn_side_effect_for_hd_discovery(self):
        # For each used account, there are at least 2 calls required:
        # 1 for the first DISCOVERY_INCREMENT payout addresses and 1
        # for the first DISCOVERY_INCREMENT change
        # addresses. Depending on the number of used addresses for the
        # account, this will change.

        effects = []

        n = self._num_used_accounts
        if n == 0:
            n = 1

        for acct_num in range(n):
            for change in [0, 1]:
                num_used = self._num_used_addresses[acct_num][change]
                r = math.ceil((num_used + HDAccount.GAP_LIMIT) /
                              self.address_increment)
                k = 'change_addresses' if change else 'payout_addresses'
                addr_list = self._acct_keys[acct_num][k]

                if change:
                    metadata = dict(block=234790 + r,
                                    block_hash=Hash("000000000000000007d57f03ebe36dbe4f87ab2f340e93b45999ab249b6dc0df"),
                                    confirmations=23890 - r)
                else:
                    metadata = dict(block=None,
                                    block_hash=None,
                                    confirmations=0)

                if r == 0:
                    r = 1
                for i in range(r):
                    start = i * self.address_increment
                    end = (i + 1) * self.address_increment
                    addr_range = range(start, end)

                    out = TransactionOutput(value=100000,
                                            script=Script.build_p2pkh(
                                                address_to_key_hash(
                                                    addr_list[i])[1]))
                    dummy_txn = Transaction(1,
                                            [],
                                            [out],
                                            0)

                    m = MockTxnDict(num_used=num_used,
                                    addr_range=addr_range,
                                    addr_list=addr_list,
                                    used_value=[dict(metadata=metadata,
                                                     transaction=dummy_txn)],
                                    unused_value=[])
                    effects.append(m)

        self.get_transactions.side_effect = effects

        return len(effects)
def test_op_checksig():
    # This is from the same example as in test_signing.py
    txn = Transaction.from_hex(
        "0100000001205607fb482a03600b736fb0c257dfd4faa49e45db3990e2c4994796031eae6e000000001976a914e9f061ff2c9885c7b137de35e416cbd5d3e1087c88acffffffff0128230000000000001976a914f1fd1dc65af03c30fe743ac63cef3a120ffab57d88ac00000000"
    )  # nopep8

    pub_key_hex = "0x04e674caf81eb3bb4a97f2acf81b54dc930d9db6a6805fd46ca74ac3ab212c0bbf62164a11e7edaf31fbf24a878087d925303079f2556664f3b32d125f2138cbef"  # nopep8
    sig_hex = "0x3045022100ed84be709227397fb1bc13b749f235e1f98f07ef8216f15da79e926b99d2bdeb02206ff39819d91bc81fecd74e59a721a38b00725389abb9cbecb42ad1c939fd826201"  # nopep8
    s = Script("%s %s OP_CHECKSIG" % (sig_hex, pub_key_hex))
    prev_script_pub_key = Script.build_p2pkh(
        utils.address_to_key_hash("1NKxQnbtKDdL6BY1UaKdrzCxQHfn3TQnqZ")[1])

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=prev_script_pub_key)
    si.run_script(s)

    assert len(si.stack) == 1
    assert si.stack[0] is True

    s = Script("%s %s OP_CHECKSIGVERIFY" % (sig_hex, pub_key_hex))

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=prev_script_pub_key)
    si.run_script(s)

    assert len(si.stack) == 0
    assert not si.stop

    # Try it once more with an incorrect sig
    pub_key_hex = "0x04e674caf81eb3bb4a97f2acf81b54dc930d9db6a6805fd46ca74ac3ab212c0bbf62164a11e7edaf31fbf24a878087d925303079f2556664f3b32d125f2138cbef"  # nopep8
    sig_hex = "0x3045022100ed84be709227397fc1bc13b749f235e1f98f07ef8216f15da79e926b99d2bdeb02206ff39819d91bc81fecd74e59a721a38b00725389abb9cbecb42ad1c939fd826201"  # nopep8
    s = Script("%s %s OP_CHECKSIG" % (sig_hex, pub_key_hex))

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=prev_script_pub_key)
    si.run_script(s)

    assert len(si.stack) == 1
    assert si.stack[0] is False

    s = Script("%s %s OP_CHECKSIGVERIFY" % (sig_hex, pub_key_hex))

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=prev_script_pub_key)
    si.run_script(s)

    assert len(si.stack) == 0
    assert si.stop
def test_op_checksig():
    # This is from the same example as in test_signing.py
    txn = Transaction.from_hex("0100000001205607fb482a03600b736fb0c257dfd4faa49e45db3990e2c4994796031eae6e000000001976a914e9f061ff2c9885c7b137de35e416cbd5d3e1087c88acffffffff0128230000000000001976a914f1fd1dc65af03c30fe743ac63cef3a120ffab57d88ac00000000")  # nopep8

    pub_key_hex = "0x04e674caf81eb3bb4a97f2acf81b54dc930d9db6a6805fd46ca74ac3ab212c0bbf62164a11e7edaf31fbf24a878087d925303079f2556664f3b32d125f2138cbef"  # nopep8
    sig_hex = "0x3045022100ed84be709227397fb1bc13b749f235e1f98f07ef8216f15da79e926b99d2bdeb02206ff39819d91bc81fecd74e59a721a38b00725389abb9cbecb42ad1c939fd826201"  # nopep8
    s = Script("%s %s OP_CHECKSIG" % (sig_hex, pub_key_hex))
    prev_script_pub_key = Script.build_p2pkh(utils.address_to_key_hash(
        "1NKxQnbtKDdL6BY1UaKdrzCxQHfn3TQnqZ")[1])

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=prev_script_pub_key)
    si.run_script(s)

    assert len(si.stack) == 1
    assert si.stack[0] is True

    s = Script("%s %s OP_CHECKSIGVERIFY" % (sig_hex, pub_key_hex))

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=prev_script_pub_key)
    si.run_script(s)

    assert len(si.stack) == 0
    assert not si.stop

    # Try it once more with an incorrect sig
    pub_key_hex = "0x04e674caf81eb3bb4a97f2acf81b54dc930d9db6a6805fd46ca74ac3ab212c0bbf62164a11e7edaf31fbf24a878087d925303079f2556664f3b32d125f2138cbef"  # nopep8
    sig_hex = "0x3045022100ed84be709227397fc1bc13b749f235e1f98f07ef8216f15da79e926b99d2bdeb02206ff39819d91bc81fecd74e59a721a38b00725389abb9cbecb42ad1c939fd826201"  # nopep8
    s = Script("%s %s OP_CHECKSIG" % (sig_hex, pub_key_hex))

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=prev_script_pub_key)
    si.run_script(s)

    assert len(si.stack) == 1
    assert si.stack[0] is False

    s = Script("%s %s OP_CHECKSIGVERIFY" % (sig_hex, pub_key_hex))

    si = ScriptInterpreter(txn=txn,
                           input_index=0,
                           sub_script=prev_script_pub_key)
    si.run_script(s)

    assert len(si.stack) == 0
    assert si.stop
def test_is_p2pkh():
    s = Script("OP_DUP OP_HASH160 0x68bf827a2fa3b31e53215e5dd19260d21fdf053e OP_EQUALVERIFY OP_CHECKSIG")
    assert s.is_p2pkh()
    assert not s.is_multisig_redeem()
    assert not s.is_p2pkh_sig()

    assert s.address()

    addresses = s.get_addresses()
    assert len(addresses) == 1
    assert addresses[0] == '1AYrhH1SAQqhT8w5NguBSogN63ajS6PxNL'

    s = Script.build_p2pkh(bytes.fromhex("68bf827a2fa3b31e53215e5dd19260d21fdf053e"))
    assert s.is_p2pkh()

    s = Script("OP_ADD OP_HASH160 0x68bf827a2fa3b31e53215e5dd19260d21fdf053e OP_EQUALVERIFY")
    assert not s.is_p2pkh()
    assert not s.is_multisig_redeem()