def test_external_txweight(self):
        coin = coins.by_name('Testnet')

        inp1 = TxInput(
            amount=100000,
            prev_hash=unhexlify(
                'e5b7e21b5ba720e81efd6bfa9f854ababdcddc75a43bfa60bf0fe069cfd1bb8a'
            ),
            prev_index=0,
            script_type=InputScriptType.EXTERNAL,
            script_pubkey=unhexlify(
                '00149c02608d469160a92f40fdf8c6ccced029493088'),
            ownership_proof=unhexlify(
                '534c001900016b2055d8190244b2ed2d46513c40658a574d3bc2deb6969c0535bb818b44d2c40002483045022100d4ad0374c922848c71d913fba59c81b9075e0d33e884d953f0c4b4806b8ffd0c022024740e6717a2b6a5aa03148c3a28b02c713b4e30fc8aeae67fa69eb20e8ddcd9012103505f0d82bbdd251511591b34f36ad5eea37d3220c2b81a1189084431ddb3aa3d'
            ),
        )

        inp2 = TxInput(
            address_n=[84 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0],
            prev_hash=unhexlify(
                '70f9871eb03a38405cfd7a01e0e1448678132d815e2c9f552ad83ae23969509e'
            ),
            prev_index=0,
            amount=100000,
            script_type=InputScriptType.SPENDWITNESS,
        )

        inp3 = TxInput(
            # tb1qldlynaqp0hy4zc2aag3pkenzvxy65saesxw3wd
            # address_n=parse_path("m/84h/1h/0h/0/1"),
            prev_hash=unhexlify(
                '65b768dacccfb209eebd95a1fb80a04f1dd6a3abc6d7b41d5e9d9f91605b37d9'
            ),
            prev_index=0,
            amount=10000,
            script_type=InputScriptType.EXTERNAL,
            script_pubkey=unhexlify(
                '0014fb7e49f4017dc951615dea221b66626189aa43b9'),
            script_sig=bytes(0),
            witness=unhexlify(
                '024730440220432ac60461de52713ad543cbb1484f7eca1a72c615d539b3f42f5668da4501d2022063786a6d6940a5c1ed9c2d2fd02cef90b6c01ddd84829c946561e15be6c0aae1012103dcf3bc936ecb2ec57b8f468050abce8c8756e75fd74273c9977744b1a0be7d03'
            ),
        )

        out1 = TxOutput(
            address="tb1qnspxpr2xj9s2jt6qlhuvdnxw6q55jvygcf89r2",
            amount=100000 + 100000 + 10000,
            script_type=OutputScriptType.PAYTOWITNESS,
        )

        calculator = TxWeightCalculator()
        calculator.add_input(inp1)
        calculator.add_input(inp2)
        calculator.add_input(inp3)
        calculator.add_output(output_derive_script(out1.address, coin))

        self.assertEqual(calculator.get_total(), 4 * 164 + 325)
    def test_p2pkh_txweight(self):

        coin = coins.by_name('Bitcoin')
        seed = bip39.seed(' '.join(['all'] * 12), '')

        inp1 = TxInput(
            address_n=[0],  # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e
            # amount=390000,
            prev_hash=unhexlify(
                'd5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'
            ),
            prev_index=0,
            amount=None,
            script_type=InputScriptType.SPENDADDRESS,
            sequence=0xffff_ffff,
            multisig=None)
        out1 = TxOutput(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1',
                        amount=390000 - 10000,
                        script_type=OutputScriptType.PAYTOADDRESS,
                        address_n=[],
                        multisig=None)

        calculator = TxWeightCalculator(1, 1)
        calculator.add_input(inp1)
        calculator.add_output(output_derive_script(out1.address, coin))

        serialized_tx = '010000000182488650ef25a58fef6788bd71b8212038d7f2bbe4750bc7bcb44701e85ef6d5000000006b4830450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede7810121023230848585885f63803a0a8aecdd6538792d5c539215c91698e315bf0253b43dffffffff0160cc0500000000001976a914de9b2a8da088824e8fe51debea566617d851537888ac00000000'
        tx_weight = len(
            serialized_tx) / 2 * 4  # non-segwit tx's weight is simple length*4

        self.assertEqual(calculator.get_total(), tx_weight)
    def test_taproot_txweight(self):
        coin = coins.by_name('Testnet')

        inp1 = TxInput(
            address_n=[86 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
            amount=4600,
            prev_hash=unhexlify(
                '7956f1de3e7362b04115b64a31f0b6822c50dd6c08d78398f392a0ac3f0e357b'
            ),
            prev_index=1,
            script_type=InputScriptType.SPENDTAPROOT,
        )

        out1 = TxOutput(
            address=
            "tb1paxhjl357yzctuf3fe58fcdx6nul026hhh6kyldpfsf3tckj9a3wslqd7zd",
            amount=4450,
            script_type=OutputScriptType.PAYTOADDRESS,
        )

        calculator = TxWeightCalculator()
        calculator.add_input(inp1)
        calculator.add_output(output_derive_script(out1.address, coin))

        # 010000000001017b350e3faca092f39883d7086cdd502c82b6f0314ab61541b062733edef156790100000000ffffffff016211000000000000225120e9af2fc69e20b0be2629cd0e9c34da9f3ef56af7beac4fb4298262bc5a45ec5d0140493145b992dacbd7ea579a415efc2cba20c3bf0f7827d1bcf999109c0d11783fe96f91ddb04a889faa17ad21ecc5c81a578009744e95c7e721aff2a5c442916600000000
        self.assertEqual(calculator.get_total(), 4 * 94 + 68)
    def test_tx_input(self):
        inp = TxInput(
            address_n=[0],
            amount=390000,
            prev_hash=unhexlify(
                "d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882"
            ),
            prev_index=0,
            sequence=0xffffffff,
            script_sig=b"0123456789",
        )

        b = bytearray()
        writers.write_tx_input(b, inp, inp.script_sig)
        self.assertEqual(len(b), 32 + 4 + 1 + 10 + 4)

        for bad_prevhash in (b"", b"x", b"hello", b"x" * 33):
            inp.prev_hash = bad_prevhash
            self.assertRaises(AssertionError, writers.write_tx_input, b, inp, inp.script_sig)
    def test_tx_input_check(self):
        inp = TxInput(
            address_n=[0],
            amount=390000,
            prev_hash=unhexlify(
                "d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882"
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDWITNESS,
            sequence=0xffffffff,
            script_pubkey=unhexlify("76a91424a56db43cf6f2b02e838ea493f95d8d6047423188ac"),
            script_sig=b"0123456789",
        )

        b = bytearray()
        writers.write_tx_input_check(b, inp)
        self.assertEqual(len(b), 32 + 4 + 4 + 4 + 4 + 4 + 8 + 26)

        for bad_prevhash in (b"", b"x", b"hello", b"x" * 33):
            inp.prev_hash = bad_prevhash
            self.assertRaises(AssertionError, writers.write_tx_input_check, b, inp)
    def test_legacy_multisig_txweight(self):
        coin = coins.by_name('Bitcoin')
        seed = bip39.seed(' '.join(['all'] * 12), '')
        keychain = Keychain(seed, coin.curve_name, [AlwaysMatchingSchema])

        nodes = []
        for index in range(1, 4):
            node = keychain.derive([
                48 | 0x80000000, 0 | 0x80000000, index | 0x80000000,
                0 | 0x80000000
            ])
            nodes.append(
                HDNodeType(
                    depth=node.depth(),
                    child_num=node.child_num(),
                    fingerprint=node.fingerprint(),
                    chain_code=node.chain_code(),
                    public_key=node.public_key(),
                ))

        multisig = MultisigRedeemScriptType(nodes=nodes,
                                            address_n=[0, 0],
                                            signatures=[b"", b"", b""],
                                            m=2)

        inp1 = TxInput(
            address_n=[48 | 0x80000000, 0 | 0x80000000, 1 | 0x80000000, 0, 0],
            amount=100000,
            prev_hash=unhexlify(
                'c6091adf4c0c23982a35899a6e58ae11e703eacd7954f588ed4b9cdefc4dba52'
            ),
            prev_index=1,
            script_type=InputScriptType.SPENDMULTISIG,
            multisig=multisig,
        )

        out1 = TxOutput(
            address="12iyMbUb4R2K3gre4dHSrbu5azG5KaqVss",
            amount=100000,
            script_type=OutputScriptType.PAYTOADDRESS,
        )

        calculator = TxWeightCalculator()
        calculator.add_input(inp1)
        calculator.add_output(output_derive_script(out1.address, coin))

        # 010000000152ba4dfcde9c4bed88f55479cdea03e711ae586e9a89352a98230c4cdf1a09c601000000fdfe00004830450221009276eea820aa54a24bd9f1a056cb09a15f50c0816570a7c7878bd1c5ee7248540220677d200aec5e2f25bcf4000bdfab3faa9e1746d7f80c4ae4bfa1f5892eb5dcbf01483045022100c2a9fbfbff1be87036d8a6a22745512b158154f7f3d8f4cad4ba7ed130b37b83022058f5299b4c26222588dcc669399bd88b6f2bc6e04b48276373683853187a4fd6014c69522103dc0ff15b9c85c0d2c87099758bf47d36229c2514aeefcf8dea123f0f93c679762102bfe426e8671601ad46d54d09ee15aa035610d36d411961c87474908d403fbc122102a5d57129c6c96df663ad29492aa18605dad97231e043be8a92f9406073815c5d53aeffffffff01a0860100000000001976a91412e8391ad256dcdc023365978418d658dfecba1c88ac00000000
        self.assertEqual(calculator.get_total(), 4 * 341)
Example #7
0
    def test_coinjoin_input_account_path_mismatch(self):
        authorization = CoinJoinAuthorization(self.msg_auth)
        tx = SignTx(outputs_count=201,
                    inputs_count=100,
                    coin_name=self.coin.coin_name,
                    lock_time=0)
        approver = CoinJoinApprover(tx, self.coin, authorization)

        txi = TxInput(prev_hash=b"",
                      prev_index=0,
                      address_n=[H_(49), H_(0), H_(0), 0, 2],
                      amount=10000000,
                      script_type=InputScriptType.SPENDWITNESS)

        with self.assertRaises(wire.ProcessError):
            await_result(approver.add_internal_input(txi))
    def test_segwit_multisig_txweight(self):
        coin = coins.by_name('Testnet')
        seed = bip39.seed(' '.join(['all'] * 12), '')
        keychain = Keychain(seed, coin.curve_name, [AlwaysMatchingSchema])

        nodes = []
        for index in range(1, 4):
            node = keychain.derive(
                [49 | 0x80000000, 1 | 0x80000000, index | 0x80000000])
            nodes.append(
                HDNodeType(
                    depth=node.depth(),
                    child_num=node.child_num(),
                    fingerprint=node.fingerprint(),
                    chain_code=node.chain_code(),
                    public_key=node.public_key(),
                ))

        multisig = MultisigRedeemScriptType(nodes=nodes,
                                            address_n=[0, 0],
                                            signatures=[b"", b"", b""],
                                            m=2)

        inp1 = TxInput(
            address_n=[49 | 0x80000000, 1 | 0x80000000, 1 | 0x80000000, 0, 0],
            prev_hash=unhexlify(
                'c9348040bbc2024e12dcb4a0b4806b0398646b91acf314da028c3f03dd0179fc'
            ),
            prev_index=1,
            script_type=InputScriptType.SPENDP2SHWITNESS,
            multisig=multisig,
            amount=1610436,
        )

        out1 = TxOutput(
            address=
            "tb1qch62pf820spe9mlq49ns5uexfnl6jzcezp7d328fw58lj0rhlhasge9hzy",
            amount=1605000,
            script_type=OutputScriptType.PAYTOADDRESS,
        )

        calculator = TxWeightCalculator()
        calculator.add_input(inp1)
        calculator.add_output(output_derive_script(out1.address, coin))

        # 01000000000101be0210025c5be68a473f6a38bf53b53bc88d5c46567616026dc056e72b92319c01000000232200208d398cfb58a1d9cdb59ccbce81559c095e8c6f4a3e64966ca385078d9879f95effffffff01887d180000000000220020c5f4a0a4ea7c0392efe0a9670a73264cffa90b19107cd8a8e9750ff93c77fdfb0400483045022100dd6342c65197af27d7894d8b8b88b16b568ee3b5ebfdc55fdfb7caa9650e3b4c02200c7074a5bcb0068f63d9014c7cd2b0490aba75822d315d41aad444e9b86adf5201483045022100e7e6c2d21109512ba0609e93903e84bfb7731ac3962ee2c1cad54a7a30ff99a20220421497930226c39fc3834e8d6da3fc876516239518b0e82e2dc1e3c46271a17c01695221021630971f20fa349ba940a6ba3706884c41579cd760c89901374358db5dd545b92102f2ff4b353702d2bb03d4c494be19d77d0ab53d16161b53fbcaf1afeef4ad0cb52103e9b6b1c691a12ce448f1aedbbd588e064869c79fbd760eae3b8cd8a5f1a224db53ae00000000
        self.assertEqual(calculator.get_total(), 4 * 129 + 256)
Example #9
0
    def test_zip243(self):
        coin = coins.by_name("Zcash")

        for v in self.VECTORS:
            tx = SignTx(
                coin_name="Zcash",
                inputs_count=len(v["inputs"]),
                outputs_count=len(v["outputs"]),
                version=v["version"],
                lock_time=v["lock_time"],
                expiry=v["expiry"],
                version_group_id=v["version_group_id"],
                branch_id=v["branch_id"],
            )

            zip243 = Zip243SigHasher()

            for i in v["inputs"]:
                txi = TxInput(
                    amount=i["amount"],
                    prev_hash=unhexlify(i["prevout"][0]),
                    prev_index=i["prevout"][1],
                    script_type=i["script_type"],
                    sequence=i["sequence"],
                )
                zip243.add_input(txi, b"")

            for o in v["outputs"]:
                txo = PrevOutput(
                    amount=o["amount"],
                    script_pubkey=unhexlify(o["script_pubkey"]),
                )
                zip243.add_output(txo, txo.script_pubkey)

            self.assertEqual(hexlify(get_tx_hash(zip243.h_prevouts)),
                             v["prevouts_hash"])
            self.assertEqual(hexlify(get_tx_hash(zip243.h_sequence)),
                             v["sequence_hash"])
            self.assertEqual(hexlify(get_tx_hash(zip243.h_outputs)),
                             v["outputs_hash"])
            self.assertEqual(
                hexlify(
                    zip243.hash143(txi, [unhexlify(i["pubkey"])], 1, tx, coin,
                                   SigHashType.SIGHASH_ALL)),
                v["preimage_hash"])
    def test_native_p2wpkh_txweight(self):

        coin = coins.by_name('Testnet')
        seed = bip39.seed(' '.join(['all'] * 12), '')

        inp1 = TxInput(
            # 49'/1'/0'/0/0" - tb1qqzv60m9ajw8drqulta4ld4gfx0rdh82un5s65s
            address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0],
            amount=12300000,
            prev_hash=unhexlify(
                '09144602765ce3dd8f4329445b20e3684e948709c5cdcaf12da3bb079c99448a'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDWITNESS,
            sequence=0xffffffff,
            multisig=None,
        )
        out1 = TxOutput(
            address='2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp',
            amount=5000000,
            script_type=OutputScriptType.PAYTOADDRESS,
            address_n=[],
            multisig=None,
        )
        out2 = TxOutput(
            address='tb1q694ccp5qcc0udmfwgp692u2s2hjpq5h407urtu',
            script_type=OutputScriptType.PAYTOADDRESS,
            amount=12300000 - 11000 - 5000000,
            address_n=[],
            multisig=None,
        )

        calculator = TxWeightCalculator(1, 2)
        calculator.add_input(inp1)
        calculator.add_output(output_derive_script(out1.address, coin))
        calculator.add_output(output_derive_script(out2.address, coin))

        self.assertEqual(calculator.get_total(), 566)
    def test_p2wpkh_in_p2sh_txweight(self):

        coin = coins.by_name('Testnet')
        seed = bip39.seed(' '.join(['all'] * 12), '')

        inp1 = TxInput(
            # 49'/1'/0'/1/0" - 2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX
            address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
            amount=123456789,
            prev_hash=unhexlify(
                '20912f98ea3ed849042efed0fdac8cb4fc301961c5988cba56902d8ffb61c337'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDP2SHWITNESS,
            sequence=0xffffffff,
            multisig=None,
        )
        out1 = TxOutput(
            address='mhRx1CeVfaayqRwq5zgRQmD7W5aWBfD5mC',
            amount=12300000,
            script_type=OutputScriptType.PAYTOADDRESS,
            address_n=[],
            multisig=None,
        )
        out2 = TxOutput(
            address='2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX',
            script_type=OutputScriptType.PAYTOADDRESS,
            amount=123456789 - 11000 - 12300000,
            address_n=[],
            multisig=None,
        )

        calculator = TxWeightCalculator(1, 2)
        calculator.add_input(inp1)
        calculator.add_output(output_derive_script(out1.address, coin))
        calculator.add_output(output_derive_script(out2.address, coin))

        self.assertEqual(calculator.get_total(), 670)
    def test_purchase_ticket(self):
        inp1 = TxInput(
            address_n=[44 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0],
            prev_hash=unhexlify(
                "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
            ),
            prev_index=1,
            amount=200_000_000,
            multisig=None,
            sequence=0xFFFF_FFFF,
        )
        out1 = TxOutput(
            address="TscqTv1he8MZrV321SfRghw7LFBCJDKB3oz",
            amount=200_000_000 - 100_000,
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
        )
        out2 = TxOutput(
            address_n=[44 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0],
            amount=200_000_000,
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
        )
        out3 = TxOutput(
            address="TsR28UZRprhgQQhzWns2M6cAwchrNVvbYq2",
            amount=0,
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
        )
        tx = SignTx(
            coin_name="Decred Testnet",
            version=1,
            lock_time=0,
            inputs_count=1,
            outputs_count=3,
            decred_staking_ticket=True,
        )

        # precomputed tx weight is 1076
        fee_rate = 100_000 / (1076 / 4)

        messages = [
            None,
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify("0100000001")
                ),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify(
                        "1064cbf0f0889b919ce083c82065612710b1a6adcdb3f15d7ffc5e6de2cd8a4d0100000000ffffffff03"
                    )
                ),
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmDecredSSTXSubmission(
                out1, coin_decred, AmountUnit.BITCOIN
            ),
            True,
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=1, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify(
                        "603bea0b0000000000001aba76a914819d291a2f7fbf770e784bfd78b5ce92c58e95ea88ac"
                    )
                ),
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out2)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=2, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify(
                        "00000000000000000000206a1edc1a98d791735eb9a8715a2a219c23680edcedad00c2eb0b000000000058"
                    )
                ),
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out3)),
            helpers.UiConfirmTotal(
                200_000_000, 100_000, fee_rate, coin_decred, AmountUnit.BITCOIN
            ),
            True,
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify(
                        "000000000000000000001abd76a914000000000000000000000000000000000000000088ac0000000000000000"
                    )
                ),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXMETA,
                details=TxRequestDetailsType(
                    request_index=None,
                    tx_hash=unhexlify(
                        "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(
                    request_index=1,
                    tx_hash=unhexlify(
                        "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout2)),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(serialized_tx=unhexlify("01")),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXFINISHED,
                details=TxRequestDetailsType(),
                serialized=TxRequestSerializedType(
                    signature_index=0,
                    signature=unhexlify(
                        "3045022100b3a11ff4befcc035623de7665aaa76dacc9252e53aabf2a5d61238151e696532022004cbcc537c1d539e04c823140bac4524bdba09f528f5c4b76f3f1022b7dc0ad4"
                    ),
                    serialized_tx=unhexlify(
                        "00c2eb0b0000000000000000ffffffff6b483045022100b3a11ff4befcc035623de7665aaa76dacc9252e53aabf2a5d61238151e696532022004cbcc537c1d539e04c823140bac4524bdba09f528f5c4b76f3f1022b7dc0ad40121030e669acac1f280d1ddf441cd2ba5e97417bf2689e4bbec86df4f831bf9f7ffd0"
                    ),
                ),
            ),
        ]

        seed = bip39.seed(
            " ".join(["all"] * 12),
            "",
        )
        ns = get_schemas_for_coin(coin_decred)
        keychain = Keychain(seed, coin_decred.curve_name, ns)
        signer = decred.Decred(tx, keychain, coin_decred, None).signer()

        for request, response in chunks(messages, 2):
            res = signer.send(request)
            if isinstance(res, tuple):
                _, res = res

            self.assertEqual(res, response)

        with self.assertRaises(StopIteration):
            signer.send(None)
    def test_mixed_txweight(self):
        coin = coins.by_name('Testnet')

        inp1 = TxInput(
            address_n=[49 | 0x80000000, 1 | 0x80000000, 1 | 0x80000000, 0, 0],
            amount=20000,
            prev_hash=unhexlify(
                '8c3ea7a10ab6d289119b722ec8c27b70c17c722334ced31a0370d782e4b6775d'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDP2SHWITNESS,
        )
        inp2 = TxInput(
            address_n=[84 | 0x80000000, 1 | 0x80000000, 1 | 0x80000000, 0, 0],
            amount=15000,
            prev_hash=unhexlify(
                '7956f1de3e7362b04115b64a31f0b6822c50dd6c08d78398f392a0ac3f0e357b'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDWITNESS,
        )
        inp3 = TxInput(
            address_n=[86 | 0x80000000, 1 | 0x80000000, 1 | 0x80000000, 0, 0],
            amount=4450,
            prev_hash=unhexlify(
                '7956f1de3e7362b04115b64a31f0b6822c50dd6c08d78398f392a0ac3f0e357b'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDTAPROOT,
        )
        inp4 = TxInput(
            address_n=[44 | 0x80000000, 1 | 0x80000000, 1 | 0x80000000, 0, 0],
            amount=10000,
            prev_hash=unhexlify(
                '3ac32e90831d79385eee49d6030a2123cd9d009fe8ffc3d470af9a6a777a119b'
            ),
            prev_index=2,
            script_type=InputScriptType.SPENDADDRESS,
        )

        out1 = TxOutput(
            address="tb1q6xnnna3g7lk22h5tn8nlx2ezmndlvuk556w4w3",
            amount=25000,
            script_type=OutputScriptType.PAYTOWITNESS,
        )
        out2 = TxOutput(
            address="mfnMbVFC1rH4p9GNbjkMfrAjyKRLycFAzA",
            script_type=OutputScriptType.PAYTOADDRESS,
            amount=7000,
        )
        out3 = TxOutput(
            address="2MvAG8m2xSf83FgeR4ZpUtaubpLNjAMMoka",
            amount=6900,
            script_type=OutputScriptType.PAYTOP2SHWITNESS,
        )
        out4 = TxOutput(
            op_return_data=b"test of op_return data",
            amount=0,
            script_type=OutputScriptType.PAYTOOPRETURN,
        )
        out5 = TxOutput(
            address=
            "tb1ptgp9w0mm89ms43flw0gkrhyx75gyc6qjhtpf0jmt5sv0dufpnsrsyv9nsz",
            amount=10000,
            script_type=OutputScriptType.PAYTOTAPROOT,
        )

        calculator = TxWeightCalculator()
        calculator.add_input(inp1)
        calculator.add_input(inp2)
        calculator.add_input(inp3)
        calculator.add_input(inp4)
        calculator.add_output(output_derive_script(out1.address, coin))
        calculator.add_output(output_derive_script(out2.address, coin))
        calculator.add_output(output_derive_script(out3.address, coin))
        calculator.add_output(output_script_paytoopreturn(out4.op_return_data))
        calculator.add_output(output_derive_script(out5.address, coin))

        # 010000000001045d77b6e482d770031ad3ce3423727cc1707bc2c82e729b1189d2b60aa1a73e8c0000000017160014a33c6e24c99e108b97bc411e7e9ef31e9d5d6164ffffffff7b350e3faca092f39883d7086cdd502c82b6f0314ab61541b062733edef156790000000000ffffffff852e125137abca2dd7a42837dccfc34edc358c72eefd62978d6747d3be9315900000000000ffffffff9b117a776a9aaf70d4c3ffe89f009dcd23210a03d649ee5e38791d83902ec33a020000006b483045022100f6bd64136839b49822cf7e2050bc5c91346fc18b5cf97a945d4fd6c502f712d002207d1859e66d218f705b704f3cfca0c75410349bb1f50623f4fc2d09d5d8df0a3f012103bae960983f83e28fcb8f0e5f3dc1f1297b9f9636612fd0835b768e1b7275fb9dffffffff05a861000000000000160014d1a739f628f7eca55e8b99e7f32b22dcdbf672d4581b0000000000001976a91402e9b094fd98e2a26e805894eb78f7ff3fef199b88acf41a00000000000017a9141ff816cbeb74817050de585ceb2c772ebf71147a870000000000000000186a1674657374206f66206f705f72657475726e206461746110270000000000002251205a02573f7b39770ac53f73d161dc86f5104c6812bac297cb6ba418f6f1219c070247304402205fae7fa2b5141548593d5623ce5bd82ee18dfc751c243526039c91848efd603702200febfbe3467a68c599245ff89055514f26e146c79b58d932ced2325e6dad1b1a0121021630971f20fa349ba940a6ba3706884c41579cd760c89901374358db5dd545b90247304402201b21212100c84207697cebb852374669c382ed97cbd08afbbdfe1b302802161602206b32b2140d094cf5b7e758135961c95478c8e82fea0df30f56ccee284b79eaea012103f6b2377d52960a6094ec158cf19dcf9e33b3da4798c2302aa5806483ed4187ae01404a81e4b7f55d6d4a26923c5e2daf3cc86ed6030f83ea6e7bb16d7b81b988b34585be21a64ab45ddcc2fb9f17be2dfeff6b22cf943bc3fc8f125a7f463af428ed0000000000
        # The witness data is 283 bytes, but two of the DER signatures are one byte below the
        # average length, so the caculator should estimate 285 bytes of witness data.
        self.assertEqual(calculator.get_total(), 4 * 477 + 285)
Example #14
0
    def test_send_native_invalid_address(self):

        coin = coins.by_name('Testnet')
        seed = bip39.seed(' '.join(['all'] * 12), '')

        inp1 = TxInput(
            # 49'/1'/0'/0/0" - tb1qqzv60m9ajw8drqulta4ld4gfx0rdh82un5s65s
            address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0],
            amount=12300000,
            prev_hash=unhexlify(
                '09144602765ce3dd8f4329445b20e3684e948709c5cdcaf12da3bb079c99448a'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDWITNESS,
            sequence=0xffffffff,
            multisig=None,
        )
        ptx1 = PrevTx(version=1,
                      lock_time=0,
                      inputs_count=1,
                      outputs_count=2,
                      extra_data_len=0)
        pinp1 = PrevInput(
            script_sig=unhexlify(
                '160014d16b8c0680c61fc6ed2e407455715055e41052f5'),
            prev_hash=unhexlify(
                '20912f98ea3ed849042efed0fdac8cb4fc301961c5988cba56902d8ffb61c337'
            ),
            prev_index=0,
            sequence=4294967295)
        pout1 = PrevOutput(script_pubkey=unhexlify(
            '00140099a7ecbd938ed1839f5f6bf6d50933c6db9d5c'),
                           amount=12300000)
        pout2 = PrevOutput(script_pubkey=unhexlify(
            'a91458b53ea7f832e8f096e896b8713a8c6df0e892ca87'),
                           amount=111145789)

        out1 = TxOutput(
            address=
            'TB1Q694CCP5QCC0UDMFWGP692U2S2HJPQ5H407URTU',  # Error: should be lower case
            script_type=OutputScriptType.PAYTOADDRESS,
            amount=12300000 - 11000 - 5000000,
            address_n=[],
            multisig=None,
        )
        tx = SignTx(coin_name='Testnet',
                    version=1,
                    lock_time=0,
                    inputs_count=1,
                    outputs_count=1)

        messages = [
            None,

            # check fee
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            helpers.UiConfirmForeignAddress(address_n=inp1.address_n),
            True,
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            None
        ]

        ns = get_schemas_for_coin(coin)
        keychain = Keychain(seed, coin.curve_name, ns)
        signer = bitcoin.Bitcoin(tx, keychain, coin, None).signer()
        for request, expected_response in chunks(messages, 2):
            if expected_response is None:
                with self.assertRaises(wire.DataError):
                    signer.send(request)
            else:
                response = signer.send(request)
                if isinstance(response, tuple):
                    _, response = response
                self.assertEqual(response, expected_response)
Example #15
0
    def test_send_native_p2wpkh(self):

        coin = coins.by_name('Testnet')
        seed = bip39.seed(' '.join(['all'] * 12), '')

        inp1 = TxInput(
            # 49'/1'/0'/0/0" - tb1qqzv60m9ajw8drqulta4ld4gfx0rdh82un5s65s
            address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0],
            amount=12300000,
            prev_hash=unhexlify(
                '09144602765ce3dd8f4329445b20e3684e948709c5cdcaf12da3bb079c99448a'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDWITNESS,
            sequence=0xffffffff,
            multisig=None,
        )
        ptx1 = PrevTx(version=1,
                      lock_time=0,
                      inputs_count=1,
                      outputs_count=2,
                      extra_data_len=0)
        pinp1 = PrevInput(
            script_sig=unhexlify(
                '160014d16b8c0680c61fc6ed2e407455715055e41052f5'),
            prev_hash=unhexlify(
                '20912f98ea3ed849042efed0fdac8cb4fc301961c5988cba56902d8ffb61c337'
            ),
            prev_index=0,
            sequence=4294967295)
        pout1 = PrevOutput(script_pubkey=unhexlify(
            '00140099a7ecbd938ed1839f5f6bf6d50933c6db9d5c'),
                           amount=12300000)
        pout2 = PrevOutput(script_pubkey=unhexlify(
            'a91458b53ea7f832e8f096e896b8713a8c6df0e892ca87'),
                           amount=111145789)

        out1 = TxOutput(
            address='2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp',
            amount=5000000,
            script_type=OutputScriptType.PAYTOADDRESS,
            address_n=[],
            multisig=None,
        )
        out2 = TxOutput(
            address='tb1q694ccp5qcc0udmfwgp692u2s2hjpq5h407urtu',
            script_type=OutputScriptType.PAYTOADDRESS,
            amount=12300000 - 11000 - 5000000,
            address_n=[],
            multisig=None,
        )
        tx = SignTx(coin_name='Testnet',
                    version=1,
                    lock_time=0,
                    inputs_count=1,
                    outputs_count=2)

        messages = [
            None,

            # check fee
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            helpers.UiConfirmForeignAddress(address_n=inp1.address_n),
            True,
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmOutput(out1, coin, AmountUnit.BITCOIN),
            True,
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=1,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out2)),
            helpers.UiConfirmOutput(out2, coin, AmountUnit.BITCOIN),
            True,
            helpers.UiConfirmTotal(12300000, 11000, coin, AmountUnit.BITCOIN),
            True,

            # check prev tx
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(request_type=TXMETA,
                      details=TxRequestDetailsType(request_index=None,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=1,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout2)),

            # sign tx
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized header
                    serialized_tx=unhexlify('01000000000101'), )),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized inp1
                    serialized_tx=unhexlify(
                        '8a44999c07bba32df1cacdc50987944e68e3205b4429438fdde35c76024614090000000000ffffffff02'
                    ), )),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=1, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized out1
                    serialized_tx=unhexlify(
                        '404b4c000000000017a9147a55d61848e77ca266e79a39bfc85c580a6426c987'
                    ),
                    signature_index=None,
                    signature=None,
                )),
            TxAckOutput(tx=TxAckOutputWrapper(output=out2)),

            # segwit
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized out2
                    serialized_tx=unhexlify(
                        'a8386f0000000000160014d16b8c0680c61fc6ed2e407455715055e41052f5'
                    ),
                    signature_index=None,
                    signature=None,
                )),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXFINISHED,
                details=TxRequestDetailsType(),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify(
                        '02483045022100a7ca8f097525f9044e64376dc0a0f5d4aeb8d15d66808ba97979a0475b06b66502200597c8ebcef63e047f9aeef1a8001d3560470cf896c12f6990eec4faec599b950121033add1f0e8e3c3136f7428dd4a4de1057380bd311f5b0856e2269170b4ffa65bf00000000'
                    ),
                    signature_index=0,
                    signature=unhexlify(
                        '3045022100a7ca8f097525f9044e64376dc0a0f5d4aeb8d15d66808ba97979a0475b06b66502200597c8ebcef63e047f9aeef1a8001d3560470cf896c12f6990eec4faec599b95'
                    ),
                )),
        ]

        ns = get_schemas_for_coin(coin)
        keychain = Keychain(seed, coin.curve_name, ns)
        signer = bitcoin.Bitcoin(tx, keychain, coin, None).signer()
        for request, response in chunks(messages, 2):
            res = signer.send(request)
            if isinstance(res, tuple):
                _, res = res
            self.assertEqual(res, response)
        with self.assertRaises(StopIteration):
            signer.send(None)
Example #16
0
    def test_one_one_fee(self):
        # input tx: 1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3
        # input 0: 0.03801747 BTC

        # output tx: https://btc1.trezor.io/tx/e590d5d76867e9f14466f715e26438845afff0ae082cda1f2f55aa8bf7f98140

        coin_bitcoin = coins.by_name("Bitcoin")

        ptx1 = PrevTx(version=1,
                      lock_time=0,
                      inputs_count=2,
                      outputs_count=1,
                      extra_data_len=0)
        pinp1 = PrevInput(
            script_sig=unhexlify(
                "47304402202df6c8885489be0fd52ed1f82c20b4c2c03e479369ea8439ed6233a3bb349ddd02207f749a00dd98d1aca699178d8fa954dc45b9faff33d3c9f03b81fa766061f2d70121038b60c00f69d78f2c5df84a24b30894c3700e4d6176a1f440461c8c69bf1f4262"
            ),
            prev_hash=unhexlify(
                "468c0de3bf9dfcc4915354904108611db9547a16ec38a24bf6b240ccc946e0d3"
            ),
            prev_index=0,
            sequence=0xFFFF_FFFF,
        )
        pinp2 = PrevInput(
            script_sig=unhexlify(
                "483045022100b11b3804e9b5fbbad70532e550192b3bbdd65b51f30572638a5559a86600856702204b3bd50a847ddf66426c0f413b9e1a98388b0d36479e66ef37b2d53d9ad13d47012103d443712f673afafafc006ccb6e19220de7ae0c07c9832666103974293d04a93c"
            ),
            prev_hash=unhexlify(
                "ebb32bba661e3dccb216acf593de26223f5f2c17f6a75634f2a0b581c4855fa0"
            ),
            prev_index=0,
            sequence=0xFFFF_FFFF,
        )
        pout1 = PrevOutput(
            script_pubkey=unhexlify(
                "76a914be145c9a7c131ad0c6c9b86f83748d660b42e2f488ac"),
            amount=3_801_747,
        )

        inp1 = TxInput(
            address_n=[44 | 0x80000000, 0 | 0x80000000, 0 | 0x80000000, 0, 10],
            prev_hash=unhexlify(
                "1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
            ),
            prev_index=0,
            amount=3_801_747,
            multisig=None,
            sequence=0xFFFF_FFFF,
        )
        out1 = TxOutput(
            address="19WEjX2zgXdn6FCLmRAJ5Ty593GkJ77pNj",
            amount=3_801_747 - 50_000,
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
        )
        tx = SignTx(coin_name=None,
                    version=1,
                    lock_time=0,
                    inputs_count=1,
                    outputs_count=1)

        messages = [
            None,
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmOutput(out1, coin_bitcoin, AmountUnit.BITCOIN),
            True,
            helpers.UiConfirmTotal(3_801_747, 50_000, coin_bitcoin,
                                   AmountUnit.BITCOIN),
            True,
            # ButtonRequest(code=ButtonRequest_ConfirmOutput),
            # ButtonRequest(code=ButtonRequest_SignTx),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXMETA,
                details=TxRequestDetailsType(
                    request_index=None,
                    tx_hash=unhexlify(
                        "1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=1,
                    tx_hash=unhexlify(
                        "1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp2)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify("0100000001")),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    signature_index=0,
                    signature=unhexlify(
                        "3045022100bebe58e7eac8170334f987d0f67032a2eddd0e2f378e002502ec88a69460ea460220040a8effc41167f5d5a5ffc77f37317d8125c7cd05f92d142e4d977f2f132c94"
                    ),
                    serialized_tx=unhexlify(
                        "f36ea247c36f027a45d0265718c74ae8ab87bd45b3fb6e14ef558d76656f321f000000006b483045022100bebe58e7eac8170334f987d0f67032a2eddd0e2f378e002502ec88a69460ea460220040a8effc41167f5d5a5ffc77f37317d8125c7cd05f92d142e4d977f2f132c940121038bac33bcdaeec5626e2f2c5680a9fdc5e551d4e1167f272825bea98e6158d4c8ffffffff01"
                    ),
                ),
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            TxRequest(
                request_type=TXFINISHED,
                details=TxRequestDetailsType(),
                serialized=TxRequestSerializedType(
                    signature_index=None,
                    signature=None,
                    serialized_tx=unhexlify(
                        "433f3900000000001976a9145d48886f4e2aad3164e55e23a7a41b921646a33488ac00000000"
                    ),
                ),
            ),
        ]

        seed = bip39.seed(
            " ".join(["all"] * 12),
            "",
        )
        ns = get_schemas_for_coin(coin_bitcoin)
        keychain = Keychain(seed, coin_bitcoin.curve_name, ns)
        signer = bitcoin.Bitcoin(tx, keychain, coin_bitcoin, None).signer()

        for request, response in chunks(messages, 2):
            res = signer.send(request)
            if isinstance(res, tuple):
                _, res = res
            self.assertEqual(res, response)

        with self.assertRaises(StopIteration):
            signer.send(None)
    def test_send_p2wpkh_in_p2sh(self):

        coin = coins.by_name('Testnet')
        seed = bip39.seed(' '.join(['all'] * 12), '')

        inp1 = TxInput(
            # 49'/1'/0'/1/0" - 2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX
            address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
            amount=123456789,
            prev_hash=unhexlify(
                '20912f98ea3ed849042efed0fdac8cb4fc301961c5988cba56902d8ffb61c337'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDP2SHWITNESS,
            sequence=0xffffffff,
            multisig=None,
        )
        ptx1 = PrevTx(version=1,
                      lock_time=0,
                      inputs_count=1,
                      outputs_count=2,
                      extra_data_len=0)
        pinp1 = PrevInput(
            script_sig=unhexlify(
                '4730440220548e087d0426b20b8a571b03b9e05829f7558b80c53c12143e342f56ab29e51d02205b68cb7fb223981d4c999725ac1485a982c4259c4f50b8280f137878c232998a012102794a25b254a268e59a5869da57fbae2fadc6727cb3309321dab409b12b2fa17c'
            ),
            prev_hash=unhexlify(
                '802cabf0843b945eabe136d7fc7c89f41021658abf56cba000acbce88c41143a'
            ),
            prev_index=0,
            sequence=4294967295)
        pout1 = PrevOutput(script_pubkey=unhexlify(
            'a91458b53ea7f832e8f096e896b8713a8c6df0e892ca87'),
                           amount=123456789)
        pout2 = PrevOutput(script_pubkey=unhexlify(
            '76a914b84bacdcd8f4cc59274a5bfb73f804ca10f7fd1488ac'),
                           amount=865519308)

        out1 = TxOutput(
            address='mhRx1CeVfaayqRwq5zgRQmD7W5aWBfD5mC',
            amount=12300000,
            script_type=OutputScriptType.PAYTOADDRESS,
            address_n=[],
            multisig=None,
        )
        out2 = TxOutput(
            address='2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX',
            script_type=OutputScriptType.PAYTOADDRESS,
            amount=123456789 - 11000 - 12300000,
            address_n=[],
            multisig=None,
        )
        tx = SignTx(coin_name='Testnet',
                    version=1,
                    lock_time=0,
                    inputs_count=1,
                    outputs_count=2)

        messages = [
            None,

            # check fee
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmOutput(out1, coin, AmountUnit.BITCOIN),
            True,
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=1,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out2)),
            helpers.UiConfirmOutput(out2, coin, AmountUnit.BITCOIN),
            True,
            helpers.UiConfirmTotal(123445789 + 11000, 11000, coin,
                                   AmountUnit.BITCOIN),
            True,

            # check prev tx
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(request_type=TXMETA,
                      details=TxRequestDetailsType(request_index=None,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=1,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout2)),

            # sign tx
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized header
                    serialized_tx=unhexlify('01000000000101'), )),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized inp1
                    serialized_tx=unhexlify(
                        '37c361fb8f2d9056ba8c98c5611930fcb48cacfdd0fe2e0449d83eea982f91200000000017160014d16b8c0680c61fc6ed2e407455715055e41052f5ffffffff02'
                    ), )),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=1, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized out1
                    serialized_tx=unhexlify(
                        'e0aebb00000000001976a91414fdede0ddc3be652a0ce1afbc1b509a55b6b94888ac'
                    ),
                    signature_index=None,
                    signature=None,
                )),
            TxAckOutput(tx=TxAckOutputWrapper(output=out2)),

            # segwit
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized out2
                    serialized_tx=unhexlify(
                        '3df39f060000000017a91458b53ea7f832e8f096e896b8713a8c6df0e892ca87'
                    ),
                    signature_index=None,
                    signature=None,
                )),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXFINISHED,
                details=TxRequestDetailsType(),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify(
                        '02483045022100ccd253bfdf8a5593cd7b6701370c531199f0f05a418cd547dfc7da3f21515f0f02203fa08a0753688871c220648f9edadbdb98af42e5d8269364a326572cf703895b012103e7bfe10708f715e8538c92d46ca50db6f657bbc455b7494e6a0303ccdb868b7900000000'
                    ),
                    signature_index=0,
                    signature=unhexlify(
                        '3045022100ccd253bfdf8a5593cd7b6701370c531199f0f05a418cd547dfc7da3f21515f0f02203fa08a0753688871c220648f9edadbdb98af42e5d8269364a326572cf703895b'
                    ),
                )),
        ]

        ns = get_schemas_for_coin(coin)
        keychain = Keychain(seed, coin.curve_name, ns)
        signer = bitcoin.Bitcoin(tx, keychain, coin, None).signer()
        for request, expected_response in chunks(messages, 2):
            response = signer.send(request)
            if isinstance(response, tuple):
                _, response = response
            self.assertEqual(response, expected_response)
        with self.assertRaises(StopIteration):
            signer.send(None)
    def test_zcash_hasher(self):
        # this test vector was generated using
        # https://github.com/zcash-hackworks/zcash-test-vectors
        tx = SignTx(
            coin_name="Zcash",
            version=5,
            version_group_id=648488714,
            branch_id=928093729,
            lock_time=2591264634,
            expiry=36466477,
            inputs_count=3,
            outputs_count=3,
        )
        inputs = [
            TxInput(
                prev_hash=unhexlify(
                    "4f61d91843ccb386dd1c482169eef62efaaf9d9364b1666e4d4c299e04a852e1"
                ),
                prev_index=1569726664,
                multisig=None,
                amount=1249971475008092,
                script_type=InputScriptType.SPENDADDRESS,
                sequence=0x8849f2a3,
                script_pubkey=unhexlify(
                    "76a9149466817faf329208fc3c3ef42ce4513d22fc1f9b88ac"),
            ),
            TxInput(
                prev_hash=unhexlify(
                    "368e9c7e1fe01f6c54db9379a94c2941ef180c25b869bf8dcdb1cf014253b3c7"
                ),
                prev_index=2648876502,
                multisig=None,
                amount=1353789347081201,
                script_type=InputScriptType.SPENDADDRESS,
                sequence=0x8a37691c,
                script_pubkey=unhexlify(
                    "76a9142275979f97043edd9a6083ee27d136727ce5f42888ac"),
            ),
            TxInput(
                prev_hash=unhexlify(
                    "f5621d6ad566c13dce81632a9168694bb6bcec2f7bfac2626f9425e1640fe4f1"
                ),
                prev_index=492165032,
                multisig=None,
                amount=1672802384749611,
                script_type=InputScriptType.SPENDADDRESS,
                sequence=0x6a993d20,
                script_pubkey=unhexlify(
                    "76a914682c89bfc3940621bd4a4bfc349a79b46ce707e388ac"),
            ),
        ]
        outputs = [
            PrevOutput(
                amount=865034086766210,
                script_pubkey=unhexlify(
                    "76a9140d06a745f44ab023752cb5b406ed8985e18130ab88ac"),
            ),
            PrevOutput(
                amount=2088955338922857,
                script_pubkey=unhexlify(
                    "76a91463ccb8f676495c222f7fba1e31defa3d5a57efc288ac"),
            ),
            PrevOutput(
                amount=1760123755646275,
                script_pubkey=unhexlify(
                    "76a914fb1a38e01d94903d3c3e0ad3360c1d3710acd20b88ac"),
            ),
        ]
        pubkeys = [
            unhexlify(
                "02ed9c769c787fda78a7da13764707d14217e74e26428b47a2a8fe6d5a0bc46196"
            ),
            unhexlify(
                "0219ac5de9a45f76e7efede5259acd94bb047ab8e7cc60fe844cb32317072ebbf3"
            ),
            unhexlify(
                "02829099a7cf1f617c956c0222e7b77ae331813d6a736eab3c5f6344d961843d39"
            ),
        ]
        expected_txid = unhexlify(
            "c91d34ecc44484b07ee573f385d80e57e4e57571bb86aa6ec6c44d654123e4e9")
        expected_sighashes = [
            unhexlify(
                "4d82669c8c0e9b1f26d59bcb347212f2d044eeb839fce21e039d8bb082bbc343"
            ),
            unhexlify(
                "2e2a27d78d117e28760d3c972f9614547ec57688c970f06c19c515cded6b030c"
            ),
            unhexlify(
                "d0a92ffd4a4d262f5b84598bcfca741a42c17b8e9d26cf4fd87839df8f33e4ee"
            ),
        ]

        hasher = ZcashHasher(tx)
        for txi in inputs:
            hasher.add_input(txi, txi.script_pubkey)
        for txo in outputs:
            hasher.add_output(txo, txo.script_pubkey)

        # test ZcashSigHasher.txid_digest
        computed_txid = hasher.txid_digest()
        self.assertEqual(computed_txid, expected_txid)

        # test ZcashSigHasher.signature_digest
        for txi, expected_sighash, pk in zip(inputs, expected_sighashes,
                                             pubkeys):
            computed_sighash = hasher.signature_digest(txi, txi.script_pubkey)
            self.assertEqual(computed_sighash, expected_sighash)
    def test_under_threshold(self):
        coin_bitcoin = coins.by_name('Bitcoin')

        ptx1 = PrevTx(version=1,
                      lock_time=0,
                      inputs_count=2,
                      outputs_count=1,
                      extra_data_len=0)
        pinp1 = PrevInput(
            script_sig=unhexlify(
                '483045022072ba61305fe7cb542d142b8f3299a7b10f9ea61f6ffaab5dca8142601869d53c0221009a8027ed79eb3b9bc13577ac2853269323434558528c6b6a7e542be46e7e9a820141047a2d177c0f3626fc68c53610b0270fa6156181f46586c679ba6a88b34c6f4874686390b4d92e5769fbb89c8050b984f4ec0b257a0e5c4ff8bd3b035a51709503'
            ),
            prev_hash=unhexlify(
                'c16a03f1cf8f99f6b5297ab614586cacec784c2d259af245909dedb0e39eddcf'
            ),
            prev_index=1,
            sequence=0xffff_ffff)
        pinp2 = PrevInput(
            script_sig=unhexlify(
                '48304502200fd63adc8f6cb34359dc6cca9e5458d7ea50376cbd0a74514880735e6d1b8a4c0221008b6ead7fe5fbdab7319d6dfede3a0bc8e2a7c5b5a9301636d1de4aa31a3ee9b101410486ad608470d796236b003635718dfc07c0cac0cfc3bfc3079e4f491b0426f0676e6643a39198e8e7bdaffb94f4b49ea21baa107ec2e237368872836073668214'
            ),
            prev_hash=unhexlify(
                '1ae39a2f8d59670c8fc61179148a8e61e039d0d9e8ab08610cb69b4a19453eaf'
            ),
            prev_index=1,
            sequence=0xffff_ffff)
        pout1 = PrevOutput(script_pubkey=unhexlify(
            '76a91424a56db43cf6f2b02e838ea493f95d8d6047423188ac'),
                           amount=390000)

        inp1 = TxInput(
            address_n=[0],  # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e
            amount=390000,
            prev_hash=unhexlify(
                'd5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'
            ),
            prev_index=0,
            multisig=None,
            sequence=0xffff_ffff)
        out1 = TxOutput(
            address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1',
            amount=390000 -
            90000,  # fee increased to 90000, slightly less than the threshold
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
            address_n=[])
        tx = SignTx(coin_name=None,
                    version=1,
                    lock_time=0,
                    inputs_count=1,
                    outputs_count=1)

        # precomputed tx weight is 768
        fee_rate = 90000 / (768 / 4)

        messages = [
            None,
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            helpers.UiConfirmForeignAddress(address_n=inp1.address_n),
            True,
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmOutput(out1, coin_bitcoin, AmountUnit.BITCOIN),
            True,
            helpers.UiConfirmTotal(300000 + 90000, 90000, fee_rate,
                                   coin_bitcoin, AmountUnit.BITCOIN),
            True,
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXMETA,
                details=TxRequestDetailsType(
                    request_index=None,
                    tx_hash=unhexlify(
                        'd5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'
                    )),
                serialized=EMPTY_SERIALIZED),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        'd5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'
                    )),
                serialized=EMPTY_SERIALIZED),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=1,
                    tx_hash=unhexlify(
                        'd5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'
                    )),
                serialized=EMPTY_SERIALIZED),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp2)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        'd5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'
                    )),
                serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=TxRequestSerializedType(
                          serialized_tx=unhexlify('0100000001'))),
        ]

        seed = bip39.seed(
            'alcohol woman abuse must during monitor noble actual mixed trade anger aisle',
            '')

        keychain = Keychain(seed, coin_bitcoin.curve_name,
                            [AlwaysMatchingSchema])
        signer = bitcoin.Bitcoin(tx, keychain, coin_bitcoin, None).signer()
        for request, response in chunks(messages, 2):
            res = signer.send(request)
            if isinstance(res, tuple):
                _, res = res
            self.assertEqual(res, response)
    def test_one_one_fee(self):

        inp1 = TxInput(
            address_n=[44 | 0x80000000, 42 | 0x80000000, 0 | 0x80000000, 0, 0],
            prev_hash=unhexlify(
                "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
            ),
            prev_index=0,
            amount=390000,
            multisig=None,
            sequence=0xFFFF_FFFF,
        )
        out1 = TxOutput(
            address="DsaHnKa418BeeQmyhpQEGG4cxGAPrneydfv",
            amount=390000 - 10000,
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
        )
        tx = SignTx(coin_name="Decred",
                    version=1,
                    lock_time=0,
                    inputs_count=1,
                    outputs_count=1)

        messages = [
            None,
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify("0100000001")),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(serialized_tx=unhexlify(
                    "4ac247307a054c37525169a78d690a7a7f87d709bef7d722aae85584f59c8fdf0000000000ffffffff01"
                )),
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmOutput(out1, coin_decred, AmountUnit.BITCOIN),
            True,
            helpers.UiConfirmTotal(380000 + 10000, 10000, coin_decred,
                                   AmountUnit.BITCOIN),
            True,
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(serialized_tx=unhexlify(
                    "60cc05000000000000001976a914664b0cd46741a695a38f8ed37db2a20327471beb88ac0000000000000000"
                )),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXMETA,
                details=TxRequestDetailsType(
                    request_index=None,
                    tx_hash=unhexlify(
                        "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=1,
                    tx_hash=unhexlify(
                        "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp2)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify("01")),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXFINISHED,
                details=TxRequestDetailsType(request_index=None, tx_hash=None),
                serialized=TxRequestSerializedType(
                    signature_index=0,
                    signature=unhexlify(
                        "3044022078a5c388838796562eb9dad176b00e6d9425bc360083f633a14948685ca8a5ce02202a1b49cd44104a9d40aee8f988281a8aac94a497b5bc7337c77cc7ddbab16f23"
                    ),
                    serialized_tx=unhexlify(
                        "70f305000000000000000000ffffffff6a473044022078a5c388838796562eb9dad176b00e6d9425bc360083f633a14948685ca8a5ce02202a1b49cd44104a9d40aee8f988281a8aac94a497b5bc7337c77cc7ddbab16f23012103fc15aa2f684457332c0ef1fe44d908ab97208102a1792caa13bcc5e886c4b321"
                    ),
                ),
            ),
        ]

        seed = bip39.seed(
            "alcohol woman abuse must during monitor noble actual mixed trade anger aisle",
            "",
        )
        ns = get_schemas_for_coin(coin_decred)
        keychain = Keychain(seed, coin_decred.curve_name, ns)
        signer = decred.Decred(tx, keychain, coin_decred, None).signer()

        for request, response in chunks(messages, 2):
            res = signer.send(request)
            if isinstance(res, tuple):
                _, res = res

            self.assertEqual(res, response)

        with self.assertRaises(StopIteration):
            signer.send(None)
    def test_purchase_ticket(self):
        inp1 = TxInput(
            address_n=[44 | 0x80000000, 42 | 0x80000000, 0 | 0x80000000, 0, 0],
            prev_hash=unhexlify(
                "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
            ),
            prev_index=0,
            amount=390000,
            multisig=None,
            sequence=0xFFFF_FFFF,
        )
        out1 = TxOutput(
            address="DsaHnKa418BeeQmyhpQEGG4cxGAPrneydfv",
            amount=390000 - 10000,
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
        )
        out2 = TxOutput(
            address_n=[44 | 0x80000000, 42 | 0x80000000, 0 | 0x80000000, 0, 0],
            amount=390000,
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
        )
        out3 = TxOutput(
            address="DsQxuVRvS4eaJ42dhQEsCXauMWjvopWgrVg",
            amount=0,
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
        )
        tx = SignTx(coin_name="Decred",
                    version=1,
                    lock_time=0,
                    inputs_count=1,
                    outputs_count=3,
                    decred_staking_ticket=True)

        messages = [
            None,
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify("0100000001")),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(serialized_tx=unhexlify(
                    "4ac247307a054c37525169a78d690a7a7f87d709bef7d722aae85584f59c8fdf0000000000ffffffff03"
                )),
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmDecredSSTXSubmission(out1, coin_decred,
                                                  AmountUnit.BITCOIN),
            True,
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=1, tx_hash=None),
                serialized=TxRequestSerializedType(serialized_tx=unhexlify(
                    "60cc05000000000000001aba76a914664b0cd46741a695a38f8ed37db2a20327471beb88ac"
                )),
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out2)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=2, tx_hash=None),
                serialized=TxRequestSerializedType(serialized_tx=unhexlify(
                    "00000000000000000000206a1e762e46655536d93ad13f88a49bde9a2df45fe62e70f30500000000000058"
                )),
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out3)),
            helpers.UiConfirmTotal(380000 + 10000, 10000, coin_decred,
                                   AmountUnit.BITCOIN),
            True,
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(serialized_tx=unhexlify(
                    "000000000000000000001abd76a914000000000000000000000000000000000000000088ac0000000000000000"
                )),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXMETA,
                details=TxRequestDetailsType(
                    request_index=None,
                    tx_hash=unhexlify(
                        "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=1,
                    tx_hash=unhexlify(
                        "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp2)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "df8f9cf58455e8aa22d7f7be09d7877f7a0a698da7695152374c057a3047c24a"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify("01")),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXFINISHED,
                details=TxRequestDetailsType(),
                serialized=TxRequestSerializedType(
                    signature_index=0,
                    signature=unhexlify(
                        "3045022100d2a6baadc88ea67ec94a1f6dca70882e647e9af68d24e1bc72f9c27359e5e6ff02207b8a939e7cf82e79e2947e8fe59a14c11ee0b3a9cd1ff084d9bd54e23291b6be"
                    ),
                    serialized_tx=unhexlify(
                        "70f305000000000000000000ffffffff6b483045022100d2a6baadc88ea67ec94a1f6dca70882e647e9af68d24e1bc72f9c27359e5e6ff02207b8a939e7cf82e79e2947e8fe59a14c11ee0b3a9cd1ff084d9bd54e23291b6be012103fc15aa2f684457332c0ef1fe44d908ab97208102a1792caa13bcc5e886c4b321"
                    )),
            ),
        ]

        seed = bip39.seed(
            "alcohol woman abuse must during monitor noble actual mixed trade anger aisle",
            "",
        )
        ns = get_schemas_for_coin(coin_decred)
        keychain = Keychain(seed, coin_decred.curve_name, ns)
        signer = decred.Decred(tx, keychain, coin_decred, None).signer()

        for request, response in chunks(messages, 2):
            res = signer.send(request)
            if isinstance(res, tuple):
                _, res = res

            self.assertEqual(res, response)

        with self.assertRaises(StopIteration):
            signer.send(None)
    def test_one_one_fee(self):
        inp1 = TxInput(
            address_n=[44 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0],
            prev_hash=unhexlify(
                "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
            ),
            prev_index=1,
            amount=200_000_000,
            multisig=None,
            sequence=0xFFFF_FFFF,
        )
        out1 = TxOutput(
            address="TscqTv1he8MZrV321SfRghw7LFBCJDKB3oz",
            amount=200_000_000 - 100_000,
            script_type=OutputScriptType.PAYTOADDRESS,
            multisig=None,
        )
        tx = SignTx(
            coin_name="Decred Testnet",
            version=1,
            lock_time=0,
            inputs_count=1,
            outputs_count=1,
        )

        # precomputed tx weight is 768
        fee_rate = 100_000 / (768 / 4)

        messages = [
            None,
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify("0100000001")
                ),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify(
                        "1064cbf0f0889b919ce083c82065612710b1a6adcdb3f15d7ffc5e6de2cd8a4d0100000000ffffffff01"
                    )
                ),
            ),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmOutput(out1, coin_decred, AmountUnit.BITCOIN),
            True,
            helpers.UiConfirmTotal(
                200_000_000, 100_000, fee_rate, coin_decred, AmountUnit.BITCOIN
            ),
            True,
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify(
                        "603bea0b0000000000001976a914819d291a2f7fbf770e784bfd78b5ce92c58e95ea88ac0000000000000000"
                    )
                ),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXMETA,
                details=TxRequestDetailsType(
                    request_index=None,
                    tx_hash=unhexlify(
                        "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(
                    request_index=1,
                    tx_hash=unhexlify(
                        "4d8acde26d5efc7f5df1b3cdada6b11027616520c883e09c919b88f0f0cb6410"
                    ),
                ),
                serialized=EMPTY_SERIALIZED,
            ),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout2)),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(serialized_tx=unhexlify("01")),
            ),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXFINISHED,
                details=TxRequestDetailsType(request_index=None, tx_hash=None),
                serialized=TxRequestSerializedType(
                    signature_index=0,
                    signature=unhexlify(
                        "304402205ea5a0aec7e405eb3c792165f103f61f8ef862e76a2b0146bec1082b243cfbff022061e307113d389b969313bbee2c9a149fad4afdf715e8bd78df579438ef692814"
                    ),
                    serialized_tx=unhexlify(
                        "00c2eb0b0000000000000000ffffffff6a47304402205ea5a0aec7e405eb3c792165f103f61f8ef862e76a2b0146bec1082b243cfbff022061e307113d389b969313bbee2c9a149fad4afdf715e8bd78df579438ef6928140121030e669acac1f280d1ddf441cd2ba5e97417bf2689e4bbec86df4f831bf9f7ffd0"
                    ),
                ),
            ),
        ]

        seed = bip39.seed(
            " ".join(["all"] * 12),
            "",
        )
        ns = get_schemas_for_coin(coin_decred)
        keychain = Keychain(seed, coin_decred.curve_name, ns)
        signer = decred.Decred(tx, keychain, coin_decred, None).signer()

        for request, response in chunks(messages, 2):
            res = signer.send(request)
            if isinstance(res, tuple):
                _, res = res

            self.assertEqual(res, response)

        with self.assertRaises(StopIteration):
            signer.send(None)
Example #23
0
    def test_send_p2wpkh_in_p2sh(self):

        coin = coins.by_name('Groestlcoin Testnet')
        seed = bip39.seed(' '.join(['all'] * 12), '')

        inp1 = TxInput(
            # 49'/1'/0'/1/0" - 2N1LGaGg836mqSQqiuUBLfcyGBhyZYBtBZ7
            address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
            amount=123456789,
            prev_hash=unhexlify(
                '09a48bce2f9d5c6e4f0cb9ea1b32d0891855e8acfe5334f9ebd72b9ad2de60cf'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDP2SHWITNESS,
            sequence=0xfffffffe,
            multisig=None,
        )
        ptx1 = PrevTx(version=1,
                      lock_time=650749,
                      inputs_count=1,
                      outputs_count=2,
                      extra_data_len=0)
        pinp1 = PrevInput(
            script_sig=unhexlify(
                '47304402201f8f57f708144c3a11da322546cb37bd385aa825d940c37e8016f0efd6ec3e9402202a41bc02c29e4f3f13efd4bededbcd4308a6393279111d614ee1f7635cf3e65701210371546a36bdf6bc82087301b3f6e759736dc8790150673d2e7e2715d2ad72f3a4'
            ),
            prev_hash=unhexlify(
                '4f2f857f39ed1afe05542d058fb0be865a387446e32fc876d086203f483f61d1'
            ),
            prev_index=1,
            sequence=4294967294)
        pout1 = PrevOutput(script_pubkey=unhexlify(
            'a91458b53ea7f832e8f096e896b8713a8c6df0e892ca87'),
                           amount=123456789)
        pout2 = PrevOutput(script_pubkey=unhexlify(
            '76a91435528b20e9a793cf2c3a1cf9cff1f2127ad377da88ac'),
                           amount=9764242764)

        out1 = TxOutput(
            address='mvbu1Gdy8SUjTenqerxUaZyYjmvedc787y',
            amount=12300000,
            script_type=OutputScriptType.PAYTOADDRESS,
            address_n=[],
            multisig=None,
        )
        out2 = TxOutput(
            address='2N1LGaGg836mqSQqiuUBLfcyGBhyZYBtBZ7',
            script_type=OutputScriptType.PAYTOADDRESS,
            amount=123456789 - 11000 - 12300000,
            address_n=[],
            multisig=None,
        )
        tx = SignTx(coin_name='Groestlcoin Testnet',
                    version=1,
                    lock_time=650756,
                    inputs_count=1,
                    outputs_count=2)

        # precomputed tx weight is 670
        fee_rate = 11000 / (670 / 4)

        messages = [
            None,

            # check fee
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmOutput(out1, coin, AmountUnit.BITCOIN),
            True,
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=1,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out2)),
            helpers.UiConfirmOutput(out2, coin, AmountUnit.BITCOIN),
            True,
            helpers.UiConfirmNonDefaultLocktime(tx.lock_time,
                                                lock_time_disabled=False),
            True,
            helpers.UiConfirmTotal(123445789 + 11000, 11000, fee_rate, coin,
                                   AmountUnit.BITCOIN),
            True,

            # check prev tx
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(request_type=TXMETA,
                      details=TxRequestDetailsType(request_index=None,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=1,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout2)),

            # sign tx
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized header
                    serialized_tx=unhexlify('01000000000101'), )),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized inp1
                    serialized_tx=unhexlify(
                        'cf60ded29a2bd7ebf93453feace8551889d0321beab90c4f6e5c9d2fce8ba4090000000017160014d16b8c0680c61fc6ed2e407455715055e41052f5feffffff02'
                    ), )),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=1, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized out1
                    serialized_tx=unhexlify(
                        'e0aebb00000000001976a914a579388225827d9f2fe9014add644487808c695d88ac'
                    ),
                    signature_index=None,
                    signature=None,
                )),
            TxAckOutput(tx=TxAckOutputWrapper(output=out2)),

            # segwit
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized out2
                    serialized_tx=unhexlify(
                        '3df39f060000000017a91458b53ea7f832e8f096e896b8713a8c6df0e892ca87'
                    ),
                    signature_index=None,
                    signature=None,
                )),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXFINISHED,
                details=TxRequestDetailsType(),
                serialized=TxRequestSerializedType(
                    serialized_tx=unhexlify(
                        '02483045022100b7ce2972bcbc3a661fe320ba901e680913b2753fcb47055c9c6ba632fc4acf81022001c3cfd6c2fe92eb60f5176ce0f43707114dd7223da19c56f2df89c13c2fef80012103e7bfe10708f715e8538c92d46ca50db6f657bbc455b7494e6a0303ccdb868b7904ee0900'
                    ),
                    signature_index=0,
                    signature=unhexlify(
                        '3045022100b7ce2972bcbc3a661fe320ba901e680913b2753fcb47055c9c6ba632fc4acf81022001c3cfd6c2fe92eb60f5176ce0f43707114dd7223da19c56f2df89c13c2fef80'
                    ),
                )),
        ]

        ns = get_schemas_for_coin(coin)
        keychain = Keychain(seed, coin.curve_name, ns)
        signer = bitcoinlike.Bitcoinlike(tx, keychain, coin, None).signer()
        for request, expected_response in chunks(messages, 2):
            response = signer.send(request)
            if isinstance(response, tuple):
                _, response = response
            self.assertEqual(response, expected_response)
        with self.assertRaises(StopIteration):
            signer.send(None)
    def test_coinjoin_lots_of_inputs(self):
        denomination = 10000000

        # Other's inputs.
        inputs = [
            TxInput(
                prev_hash=b"",
                prev_index=0,
                amount=denomination + 1000000 * (i + 1),
                script_type=InputScriptType.EXTERNAL,
                sequence=0xffffffff,
            ) for i in range(99)
        ]

        # Our input.
        inputs.insert(
            30,
            TxInput(
                prev_hash=b"",
                prev_index=0,
                address_n=[H_(84), H_(0), H_(0), 0, 1],
                amount=denomination + 1000000,
                script_type=InputScriptType.SPENDWITNESS,
                sequence=0xffffffff,
            ))

        # Other's CoinJoined outputs.
        outputs = [
            TxOutput(
                amount=denomination,
                script_type=OutputScriptType.PAYTOWITNESS,
            ) for i in range(99)
        ]

        # Our CoinJoined output.
        outputs.insert(
            40,
            TxOutput(
                address_n=[H_(84), H_(0), H_(0), 0, 2],
                amount=denomination,
                script_type=OutputScriptType.PAYTOWITNESS,
            ))

        coordinator_fee = int(self.fee_per_anonymity_percent / 100 *
                              len(outputs) * denomination)
        fees = coordinator_fee + 10000
        total_coordinator_fee = coordinator_fee * len(outputs)

        # Other's change-outputs.
        outputs.extend(
            TxOutput(
                amount=1000000 * (i + 1) - fees,
                script_type=OutputScriptType.PAYTOWITNESS,
            ) for i in range(99))

        # Our change-output.
        outputs.append(
            TxOutput(
                address_n=[H_(84), H_(0), H_(0), 1, 1],
                amount=1000000 - fees,
                script_type=OutputScriptType.PAYTOWITNESS,
            ))

        # Coordinator's output.
        outputs.append(
            TxOutput(
                amount=total_coordinator_fee,
                script_type=OutputScriptType.PAYTOWITNESS,
            ))

        authorization = CoinJoinAuthorization(self.msg_auth)
        tx = SignTx(outputs_count=len(outputs),
                    inputs_count=len(inputs),
                    coin_name=self.coin.coin_name,
                    lock_time=0)
        approver = CoinJoinApprover(tx, self.coin, authorization)
        signer = Bitcoin(tx, None, self.coin, approver)

        for txi in inputs:
            if txi.script_type == InputScriptType.EXTERNAL:
                approver.add_external_input(txi)
            else:
                await_result(approver.add_internal_input(txi))

        for txo in outputs:
            if txo.address_n:
                approver.add_change_output(txo, script_pubkey=bytes(22))
            else:
                await_result(
                    approver.add_external_output(txo, script_pubkey=bytes(22)))

        await_result(approver.approve_tx(TxInfo(signer, tx), []))
    def test_send_p2wpkh_in_p2sh_attack_amount(self):

        coin = coins.by_name('Testnet')
        seed = bip39.seed(' '.join(['all'] * 12), '')

        inp1 = TxInput(
            # 49'/1'/0'/1/0" - 2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX
            address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
            amount=10,
            prev_hash=unhexlify(
                '20912f98ea3ed849042efed0fdac8cb4fc301961c5988cba56902d8ffb61c337'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDP2SHWITNESS,
            sequence=0xffffffff,
            multisig=None,
        )
        ptx1 = PrevTx(version=1,
                      lock_time=0,
                      inputs_count=1,
                      outputs_count=2,
                      extra_data_len=0)
        pinp1 = PrevInput(
            script_sig=unhexlify(
                '4730440220548e087d0426b20b8a571b03b9e05829f7558b80c53c12143e342f56ab29e51d02205b68cb7fb223981d4c999725ac1485a982c4259c4f50b8280f137878c232998a012102794a25b254a268e59a5869da57fbae2fadc6727cb3309321dab409b12b2fa17c'
            ),
            prev_hash=unhexlify(
                '802cabf0843b945eabe136d7fc7c89f41021658abf56cba000acbce88c41143a'
            ),
            prev_index=0,
            sequence=4294967295)
        pout1 = PrevOutput(script_pubkey=unhexlify(
            'a91458b53ea7f832e8f096e896b8713a8c6df0e892ca87'),
                           amount=123456789)
        pout2 = PrevOutput(script_pubkey=unhexlify(
            '76a914b84bacdcd8f4cc59274a5bfb73f804ca10f7fd1488ac'),
                           amount=865519308)

        inpattack = TxInput(
            # 49'/1'/0'/1/0" - 2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX
            address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
            amount=9,  # modified!
            prev_hash=unhexlify(
                '20912f98ea3ed849042efed0fdac8cb4fc301961c5988cba56902d8ffb61c337'
            ),
            prev_index=0,
            script_type=InputScriptType.SPENDP2SHWITNESS,
            sequence=0xffffffff,
            multisig=None,
        )
        out1 = TxOutput(
            address='mhRx1CeVfaayqRwq5zgRQmD7W5aWBfD5mC',
            amount=8,
            script_type=OutputScriptType.PAYTOADDRESS,
            address_n=[],
            multisig=None,
        )
        out2 = TxOutput(
            address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
            script_type=OutputScriptType.PAYTOP2SHWITNESS,
            amount=1,
            address=None,
            multisig=None,
        )
        tx = SignTx(coin_name='Testnet',
                    version=1,
                    lock_time=0,
                    inputs_count=1,
                    outputs_count=2)

        messages = [
            None,

            # check fee
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inpattack)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmOutput(out1, coin, AmountUnit.BITCOIN),
            True,
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=1,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out2)),
            helpers.UiConfirmTotal(9 - 1, 9 - 8 - 1, coin, AmountUnit.BITCOIN),
            True,

            # check prev tx
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(request_type=TXMETA,
                      details=TxRequestDetailsType(request_index=None,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=1,
                                                   tx_hash=inp1.prev_hash),
                      serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout2)),

            # sign tx
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    # returned serialized header
                    serialized_tx=unhexlify('01000000000101'), )),
        ]

        ns = get_schemas_for_coin(coin)
        keychain = Keychain(seed, coin.curve_name, ns)
        signer = bitcoin.Bitcoin(tx, keychain, coin, None).signer()
        i = 0
        messages_count = int(len(messages) / 2)
        for request, expected_response in chunks(messages, 2):
            if i == messages_count - 1:  # last message should throw wire.Error
                self.assertRaises(wire.DataError, signer.send, request)
            else:
                response = signer.send(request)
                if isinstance(response, tuple):
                    _, response = response
                self.assertEqual(response, expected_response)
            i += 1
        with self.assertRaises(StopIteration):
            signer.send(None)
from trezor.messages import SignTx
from trezor.messages import TxInput
from trezor.messages import TxOutput
from trezor.messages import PrevOutput
from trezor.enums import InputScriptType
from trezor.enums import OutputScriptType


VECTORS = [
    {  # https://github.com/bitcoin/bips/pull/1225/commits/f7af1f73b287c14cf2f63afcb8d199feaf6ab5e1
        "sign_tx": SignTx(coin_name='Bitcoin', version=2, lock_time=0x1dcd6500, inputs_count=9, outputs_count=2),
        "inputs": [
            TxInput(
                amount=420000000,
                prev_hash=unhexlify('9c4e333b5f116359b5f5578fe4a74c6f58b3bab9d28149a583da86f6bf0ce27d'),
                prev_index=1,
                script_pubkey=unhexlify('512053a1f6e454df1aa2776a2814a721372d6258050de330b3c6d10ee8f4e0dda343'),
                script_type=InputScriptType.SPENDTAPROOT,
                sequence=0x00000000,
            ),
            TxInput(
                amount=462000000,
                prev_hash=unhexlify('99ddaf6d9b75447d5127e17312f6def68acba2d4f464d0e2ac93137bb5cab7d7'),
                prev_index=0,
                script_pubkey=unhexlify('5120147c9c57132f6e7ecddba9800bb0c4449251c92a1e60371ee77557b6620f3ea3'),
                script_type=InputScriptType.SPENDTAPROOT,
                sequence=0xffffffff,
            ),
            TxInput(
                amount=294000000,
                prev_hash=unhexlify('4218a419542757d960174457dc82e06b3613ac8ed2c528926833433883f5e1f8'),
                prev_index=0,
    def test_one_one_fee(self):
        # http://groestlsight.groestlcoin.org/tx/f56521b17b828897f72b30dd21b0192fd942342e89acbb06abf1d446282c30f5
        # ptx1: http://groestlsight.groestlcoin.org/api/tx/cb74c8478c5814742c87cffdb4a21231869888f8042fb07a90e015a9db1f9d4a

        coin = coins.by_name('Groestlcoin')

        ptx1 = PrevTx(version=1,
                      lock_time=2160993,
                      inputs_count=1,
                      outputs_count=1,
                      extra_data_len=0)
        pinp1 = PrevInput(
            script_sig=unhexlify(
                '48304502210096a287593b1212a188e778596eb8ecd4cc169b93a4d115226460d8e3deae431c02206c78ec09b3df977f04a6df5eb53181165c4ea5a0b35f826551349130f879d6b8012102cf5126ff54e38a80a919579d7091cafe24840eab1d30fe2b4d59bdd9d267cad8'
            ),
            prev_hash=unhexlify(
                '7dc74a738c50c2ae1228ce9890841e5355fd6d7f2c1367e0a74403ab60db3224'
            ),
            prev_index=0,
            sequence=4294967294)
        pout1 = PrevOutput(script_pubkey=unhexlify(
            '76a914172b4e06e9b7881a48d2ee8062b495d0b2517fe888ac'),
                           amount=210016)

        inp1 = TxInput(
            address_n=[44 | 0x80000000, 17 | 0x80000000, 0 | 0x80000000, 0,
                       2],  #  FXHDsC5ZqWQHkDmShzgRVZ1MatpWhwxTAA
            prev_hash=unhexlify(
                'cb74c8478c5814742c87cffdb4a21231869888f8042fb07a90e015a9db1f9d4a'
            ),
            prev_index=0,
            amount=210016)
        out1 = TxOutput(address='FtM4zAn9aVYgHgxmamWBgWPyZsb6RhvkA9',
                        amount=210016 - 192,
                        script_type=OutputScriptType.PAYTOADDRESS,
                        address_n=[])
        tx = SignTx(coin_name='Groestlcoin',
                    version=1,
                    lock_time=0,
                    inputs_count=1,
                    outputs_count=1)

        messages = [
            None,
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            helpers.UiConfirmOutput(out1, coin, AmountUnit.BITCOIN),
            True,
            helpers.UiConfirmTotal(210016, 192, coin, AmountUnit.BITCOIN),
            True,
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(
                request_type=TXMETA,
                details=TxRequestDetailsType(
                    request_index=None,
                    tx_hash=unhexlify(
                        'cb74c8478c5814742c87cffdb4a21231869888f8042fb07a90e015a9db1f9d4a'
                    )),
                serialized=EMPTY_SERIALIZED),
            TxAckPrevMeta(tx=ptx1),
            TxRequest(
                request_type=TXINPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        'cb74c8478c5814742c87cffdb4a21231869888f8042fb07a90e015a9db1f9d4a'
                    )),
                serialized=EMPTY_SERIALIZED),
            TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(
                    request_index=0,
                    tx_hash=unhexlify(
                        'cb74c8478c5814742c87cffdb4a21231869888f8042fb07a90e015a9db1f9d4a'
                    )),
                serialized=EMPTY_SERIALIZED),
            TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
            # ButtonRequest(code=ButtonRequest_ConfirmOutput),
            # ButtonRequest(code=ButtonRequest_SignTx),
            TxRequest(request_type=TXINPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=TxRequestSerializedType(
                          signature_index=None,
                          signature=None,
                          serialized_tx=unhexlify('0100000001'))),
            TxAckInput(tx=TxAckInputWrapper(input=inp1)),
            TxRequest(request_type=TXOUTPUT,
                      details=TxRequestDetailsType(request_index=0,
                                                   tx_hash=None),
                      serialized=EMPTY_SERIALIZED),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            TxRequest(
                request_type=TXOUTPUT,
                details=TxRequestDetailsType(request_index=0, tx_hash=None),
                serialized=TxRequestSerializedType(
                    signature_index=0,
                    signature=unhexlify(
                        '304402201fb96d20d0778f54520ab59afe70d5fb20e500ecc9f02281cf57934e8029e8e10220383d5a3e80f2e1eb92765b6da0f23d454aecbd8236f083d483e9a74302368761'
                    ),
                    serialized_tx=unhexlify(
                        '4a9d1fdba915e0907ab02f04f88898863112a2b4fdcf872c7414588c47c874cb000000006a47304402201fb96d20d0778f54520ab59afe70d5fb20e500ecc9f02281cf57934e8029e8e10220383d5a3e80f2e1eb92765b6da0f23d454aecbd8236f083d483e9a7430236876101210331693756f749180aeed0a65a0fab0625a2250bd9abca502282a4cf0723152e67ffffffff01'
                    ))),
            TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
            TxRequest(
                request_type=TXFINISHED,
                details=TxRequestDetailsType(),
                serialized=TxRequestSerializedType(
                    signature_index=None,
                    signature=None,
                    serialized_tx=unhexlify(
                        'a0330300000000001976a914fe40329c95c5598ac60752a5310b320cb52d18e688ac00000000'
                    ),
                )),
        ]

        seed = bip39.seed(' '.join(['all'] * 12), '')
        ns = get_schemas_for_coin(coin)
        keychain = Keychain(seed, coin.curve_name, ns)
        signer = bitcoinlike.Bitcoinlike(tx, keychain, coin, None).signer()
        for request, expected_response in chunks(messages, 2):
            response = signer.send(request)
            if isinstance(response, tuple):
                _, response = response
            self.assertEqual(response, expected_response)
        with self.assertRaises(StopIteration):
            signer.send(None)
Example #28
0
    def test_coinjoin_lots_of_inputs(self):
        denomination = 10000000
        coordinator_fee = int(self.max_fee_rate_percent / 100 * denomination)
        fees = coordinator_fee + 500

        # Other's inputs.
        inputs = [
            TxInput(
                prev_hash=b"",
                prev_index=0,
                amount=denomination,
                script_pubkey=bytes(22),
                script_type=InputScriptType.EXTERNAL,
                sequence=0xffffffff,
                witness="",
            ) for i in range(99)
        ]

        # Our input.
        inputs.insert(
            30,
            TxInput(
                prev_hash=b"",
                prev_index=0,
                address_n=[H_(84), H_(0), H_(0), 0, 1],
                amount=denomination,
                script_type=InputScriptType.SPENDWITNESS,
                sequence=0xffffffff,
            ))

        # Other's CoinJoined outputs.
        outputs = [
            TxOutput(
                address="",
                amount=denomination - fees,
                script_type=OutputScriptType.PAYTOWITNESS,
                payment_req_index=0,
            ) for i in range(99)
        ]

        # Our CoinJoined output.
        outputs.insert(
            40,
            TxOutput(
                address="",
                address_n=[H_(84), H_(0), H_(0), 0, 2],
                amount=denomination - fees,
                script_type=OutputScriptType.PAYTOWITNESS,
                payment_req_index=0,
            ))

        # Coordinator's output.
        outputs.append(
            TxOutput(
                address="",
                amount=coordinator_fee * len(outputs),
                script_type=OutputScriptType.PAYTOWITNESS,
                payment_req_index=0,
            ))

        authorization = CoinJoinAuthorization(self.msg_auth)
        tx = SignTx(outputs_count=len(outputs),
                    inputs_count=len(inputs),
                    coin_name=self.coin.coin_name,
                    lock_time=0)
        approver = CoinJoinApprover(tx, self.coin, authorization)
        signer = Bitcoin(tx, None, self.coin, approver)

        # Compute payment request signature.
        # Private key of m/0h for "all all ... all" seed.
        private_key = b'?S\ti\x8b\xc5o{,\xab\x03\x194\xea\xa8[_:\xeb\xdf\xce\xef\xe50\xf17D\x98`\xb9dj'
        h_pr = HashWriter(sha256())
        writers.write_bytes_fixed(h_pr, b"SL\x00\x24", 4)
        writers.write_bytes_prefixed(h_pr, b"")  # Empty nonce.
        writers.write_bytes_prefixed(h_pr, self.coordinator_name.encode())
        writers.write_compact_size(h_pr, 0)  # No memos.
        writers.write_uint32(h_pr, self.coin.slip44)
        h_outputs = HashWriter(sha256())
        for txo in outputs:
            writers.write_uint64(h_outputs, txo.amount)
            writers.write_bytes_prefixed(h_outputs, txo.address.encode())
        writers.write_bytes_fixed(h_pr, h_outputs.get_digest(), 32)
        signature = secp256k1.sign(private_key, h_pr.get_digest())

        tx_ack_payment_req = TxAckPaymentRequest(
            recipient_name=self.coordinator_name,
            signature=signature,
        )

        for txi in inputs:
            if txi.script_type == InputScriptType.EXTERNAL:
                approver.add_external_input(txi)
            else:
                await_result(approver.add_internal_input(txi))

        await_result(approver.add_payment_request(tx_ack_payment_req, None))
        for txo in outputs:
            if txo.address_n:
                approver.add_change_output(txo, script_pubkey=bytes(22))
            else:
                await_result(
                    approver.add_external_output(txo, script_pubkey=bytes(22)))

        await_result(approver.approve_tx(TxInfo(signer, tx), []))
class TestSegwitBip143NativeP2WPKH(unittest.TestCase):
    # pylint: disable=C0301

    tx = SignTx(coin_name='Bitcoin', version=1, lock_time=0x00000011, inputs_count=2, outputs_count=2)
    inp1 = TxInput(address_n=[0],
                       # Trezor expects hash in reversed format
                       prev_hash=unhexlify('9f96ade4b41d5433f4eda31e1738ec2b36f6e7d1420d94a6af99801a88f7f7ff'),
                       prev_index=0,
                       amount=625000000,  # 6.25 btc
                       script_type=InputScriptType.SPENDWITNESS,
                       multisig=None,
                       sequence=0xffffffee)
    inp2 = TxInput(address_n=[1],
                       # Trezor expects hash in reversed format
                       prev_hash=unhexlify('8ac60eb9575db5b2d987e29f301b5b819ea83a5c6579d282d189cc04b8e151ef'),
                       prev_index=1,
                       multisig=None,
                       amount=600000000,  # 6 btc
                       script_type=InputScriptType.SPENDWITNESS,
                       sequence=0xffffffff)
    out1 = TxOutput(address='1Cu32FVupVCgHkMMRJdYJugxwo2Aprgk7H',  # derived
                        amount=0x0000000006b22c20,
                        script_type=OutputScriptType.PAYTOADDRESS,
                        multisig=None,
                        address_n=[])
    out2 = TxOutput(address='16TZ8J6Q5iZKBWizWzFAYnrsaox5Z5aBRV',  # derived
                        amount=0x000000000d519390,
                        script_type=OutputScriptType.PAYTOADDRESS,
                        multisig=None,
                        address_n=[])

    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_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_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')
class TestSegwitBip143(unittest.TestCase):
    # pylint: disable=C0301

    tx = SignTx(coin_name='Bitcoin',
                version=1,
                lock_time=0x00000492,
                inputs_count=1,
                outputs_count=2)
    inp1 = TxInput(
        address_n=[0],
        # Trezor expects hash in reversed format
        prev_hash=unhexlify(
            '77541aeb3c4dac9260b68f74f44c973081a9d4cb2ebe8038b2d70faa201b6bdb'
        ),
        prev_index=1,
        multisig=None,
        amount=1000000000,  # 10 btc
        script_type=InputScriptType.SPENDP2SHWITNESS,  # TODO: is this correct?
        sequence=0xfffffffe)
    out1 = TxOutput(address='1Fyxts6r24DpEieygQiNnWxUdb18ANa5p7',
                    amount=0x000000000bebb4b8,
                    script_type=OutputScriptType.PAYTOADDRESS,
                    multisig=None,
                    address_n=[])
    out2 = TxOutput(address='1Q5YjKVj5yQWHBBsyEBamkfph3cA6G9KK8',
                    amount=0x000000002faf0800,
                    script_type=OutputScriptType.PAYTOADDRESS,
                    multisig=None,
                    address_n=[])

    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_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_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_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'
        )