def test_prevouts(self):
     coin = coins.by_name(self.tx.coin_name)
     sig_hasher = BitcoinSigHasher()
     sig_hasher.add_input(self.inp1, b"")
     sig_hasher.add_input(self.inp2, b"")
     prevouts_hash = get_tx_hash(sig_hasher.h_prevouts, double=coin.sign_hash_double)
     self.assertEqual(hexlify(prevouts_hash), b'96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37')
 def test_sequence(self):
     coin = coins.by_name(self.tx.coin_name)
     sig_hasher = BitcoinSigHasher()
     sig_hasher.add_input(self.inp1, b"")
     sig_hasher.add_input(self.inp2, b"")
     sequence_hash = get_tx_hash(sig_hasher.h_sequences, double=coin.sign_hash_double)
     self.assertEqual(hexlify(sequence_hash), b'52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b')
 def test_bip143_sequence(self):
     coin = coins.by_name(self.tx.coin_name)
     sig_hasher = BitcoinSigHasher()
     sig_hasher.add_input(self.inp1, b"")
     sequence_hash = get_tx_hash(sig_hasher.h_sequences,
                                 double=coin.sign_hash_double)
     self.assertEqual(
         hexlify(sequence_hash),
         b'18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198'
     )
 def test_bip143_prevouts(self):
     coin = coins.by_name(self.tx.coin_name)
     sig_hasher = BitcoinSigHasher()
     sig_hasher.add_input(self.inp1, b"")
     prevouts_hash = get_tx_hash(sig_hasher.h_prevouts,
                                 double=coin.sign_hash_double)
     self.assertEqual(
         hexlify(prevouts_hash),
         b'b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a'
     )
    def test_outputs(self):

        seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
        coin = coins.by_name(self.tx.coin_name)
        sig_hasher = BitcoinSigHasher()

        for txo in [self.out1, self.out2]:
            script_pubkey = output_derive_script(txo.address, coin)
            txo_bin = PrevOutput(amount=txo.amount, script_pubkey=script_pubkey)
            sig_hasher.add_output(txo_bin, script_pubkey)

        outputs_hash = get_tx_hash(sig_hasher.h_outputs, double=coin.sign_hash_double)
        self.assertEqual(hexlify(outputs_hash), b'863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5')
    def test_bip143_outputs(self):
        seed = bip39.seed(
            'alcohol woman abuse must during monitor noble actual mixed trade anger aisle',
            '')
        coin = coins.by_name(self.tx.coin_name)
        sig_hasher = BitcoinSigHasher()

        for txo in [self.out1, self.out2]:
            script_pubkey = output_derive_script(txo.address, coin)
            txo_bin = PrevOutput(amount=txo.amount,
                                 script_pubkey=script_pubkey)
            sig_hasher.add_output(txo_bin, script_pubkey)

        outputs_hash = get_tx_hash(sig_hasher.h_outputs,
                                   double=coin.sign_hash_double)
        self.assertEqual(
            hexlify(outputs_hash),
            b'de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83'
        )
    def test_preimage_testdata(self):

        seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
        coin = coins.by_name(self.tx.coin_name)
        sig_hasher = BitcoinSigHasher()
        sig_hasher.add_input(self.inp1, b"")
        sig_hasher.add_input(self.inp2, b"")

        for txo in [self.out1, self.out2]:
            script_pubkey = output_derive_script(txo.address, coin)
            txo_bin = PrevOutput(amount=txo.amount, script_pubkey=script_pubkey)
            sig_hasher.add_output(txo_bin, script_pubkey)

        keychain = Keychain(seed, coin.curve_name, [AlwaysMatchingSchema])
        node = keychain.derive(self.inp2.address_n)

        # test data public key hash
        # only for input 2 - input 1 is not segwit
        result = sig_hasher.hash143(self.inp2, [node.public_key()], 1, self.tx, coin, SigHashType.SIGHASH_ALL)
        self.assertEqual(hexlify(result), b'2fa3f1351618b2532228d7182d3221d95c21fd3d496e7e22e9ded873cf022a8b')
    def test_bip341(self):
        for i, tx in enumerate(VECTORS):
            hasher = BitcoinSigHasher()

            for txi in tx["inputs"]:
                hasher.add_input(txi, txi.script_pubkey)

            self.assertEqual(get_tx_hash(hasher.h_amounts), tx["sha_amounts"], f"sha_amounts tx {i}")
            self.assertEqual(get_tx_hash(hasher.h_prevouts), tx["sha_prevouts"], f"sha_prevouts tx {i}")
            self.assertEqual(get_tx_hash(hasher.h_scriptpubkeys), tx["sha_scriptpubkeys"], f"sha_scriptpubkeys tx {i}")
            self.assertEqual(get_tx_hash(hasher.h_sequences), tx["sha_sequences"], f"sha_sequences tx {i}")

            for txo in tx["outputs"]:
                hasher.add_output(txo, txo.script_pubkey)

            self.assertEqual(get_tx_hash(hasher.h_outputs), tx["sha_outputs"], f"sha_outputs tx {i}")

            for sh in tx["signature_hashes"]:
                txi = tx["inputs"][sh["index"]]
                result = hasher.hash341(sh["index"], tx["sign_tx"], sh["hash_type"])
                self.assertEqual(result, sh["result"], f"signature_hash tx {i} input {sh['index']}")
    def test_bip143_preimage_testdata(self):
        seed = bip39.seed(
            'alcohol woman abuse must during monitor noble actual mixed trade anger aisle',
            '')
        coin = coins.by_name(self.tx.coin_name)
        sig_hasher = BitcoinSigHasher()
        sig_hasher.add_input(self.inp1, b"")
        for txo in [self.out1, self.out2]:
            script_pubkey = output_derive_script(txo.address, coin)
            txo_bin = PrevOutput(amount=txo.amount,
                                 script_pubkey=script_pubkey)
            sig_hasher.add_output(txo_bin, script_pubkey)

        keychain = Keychain(seed, coin.curve_name, [AlwaysMatchingSchema])
        node = keychain.derive(self.inp1.address_n)

        # test data public key hash
        result = sig_hasher.hash143(self.inp1, [node.public_key()], 1, self.tx,
                                    coin, SigHashType.SIGHASH_ALL)
        self.assertEqual(
            hexlify(result),
            b'6e28aca7041720995d4acf59bbda64eef5d6f23723d23f2e994757546674bbd9'
        )