def test_prevouts(self):
     coin = coins.by_name(self.tx.coin_name)
     bip143 = Bitcoin(self.tx, None, coin, BasicApprover(self.tx, coin))
     bip143.hash143_add_input(self.inp1)
     bip143.hash143_add_input(self.inp2)
     prevouts_hash = get_tx_hash(bip143.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)
     bip143 = Bitcoin(self.tx, None, coin, BasicApprover(self.tx, coin))
     bip143.hash143_add_input(self.inp1)
     bip143.hash143_add_input(self.inp2)
     sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double)
     self.assertEqual(hexlify(sequence_hash), b'52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b')
 def test_bip143_prevouts(self):
     coin = coins.by_name(self.tx.coin_name)
     bip143 = Bitcoin(self.tx, None, coin)
     bip143.hash143_add_input(self.inp1)
     prevouts_hash = get_tx_hash(bip143.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)
     bip143 = Bitcoin(self.tx, None, coin)
     bip143.hash143_add_input(self.inp1)
     sequence_hash = get_tx_hash(bip143.h_sequence,
                                 double=coin.sign_hash_double)
     self.assertEqual(
         hexlify(sequence_hash),
         b'18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198'
     )
예제 #5
0
    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)
        bip143 = Bitcoin(self.tx, None, coin)

        for txo in [self.out1, self.out2]:
            txo_bin = TxOutputBinType()
            txo_bin.amount = txo.amount
            script_pubkey = output_derive_script(txo.address, coin)
            bip143.hash143_add_output(txo_bin, script_pubkey)

        outputs_hash = get_tx_hash(bip143.h_outputs, double=coin.sign_hash_double)
        self.assertEqual(hexlify(outputs_hash), b'de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83')
    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)
        bip143 = Bitcoin(self.tx, None, coin, BasicApprover(self.tx, coin))

        for txo in [self.out1, self.out2]:
            txo_bin = TxOutputBinType()
            txo_bin.amount = txo.amount
            script_pubkey = output_derive_script(txo.address, coin)
            bip143.hash143_add_output(txo_bin, script_pubkey)

        outputs_hash = get_tx_hash(bip143.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)
        bip143 = Bitcoin(self.tx, None, coin)
        bip143.hash143_add_input(self.inp1)
        bip143.hash143_add_input(self.inp2)

        for txo in [self.out1, self.out2]:
            txo_bin = TxOutputBinType()
            txo_bin.amount = txo.amount
            script_pubkey = output_derive_script(txo.address, coin)
            bip143.hash143_add_output(txo_bin, script_pubkey)

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

        # test data public key hash
        # only for input 2 - input 1 is not segwit
        result = bip143.hash143_preimage_hash(self.inp2, [node.public_key()],
                                              1)
        self.assertEqual(
            hexlify(result),
            b'2fa3f1351618b2532228d7182d3221d95c21fd3d496e7e22e9ded873cf022a8b'
        )
예제 #8
0
    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)
        bip143 = Bitcoin(self.tx, None, coin)
        bip143.hash143_add_input(self.inp1)
        for txo in [self.out1, self.out2]:
            txo_bin = TxOutputBinType()
            txo_bin.amount = txo.amount
            script_pubkey = output_derive_script(txo.address, coin)
            bip143.hash143_add_output(txo_bin, script_pubkey)

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

        # test data public key hash
        result = bip143.hash143_preimage_hash(self.inp1, [node.public_key()], 1)
        self.assertEqual(hexlify(result), b'6e28aca7041720995d4acf59bbda64eef5d6f23723d23f2e994757546674bbd9')
    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)
        bip143 = Bitcoin(self.tx, None, coin)
        bip143.hash143_add_input(self.inp1)
        for txo in [self.out1, self.out2]:
            txo_bin = TxOutputBinType()
            txo_bin.amount = txo.amount
            script_pubkey = output_derive_script(txo, coin)
            bip143.hash143_add_output(txo_bin, script_pubkey)

        # test data public key hash
        result = bip143.hash143_preimage_hash(
            self.inp1, unhexlify('79091972186c449eb1ded22b78e40d009bdf0089'))
        self.assertEqual(
            hexlify(result),
            b'64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6'
        )
    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)
        bip143 = Bitcoin(self.tx, None, coin)
        bip143.hash143_add_input(self.inp1)
        bip143.hash143_add_input(self.inp2)

        for txo in [self.out1, self.out2]:
            txo_bin = TxOutputBinType()
            txo_bin.amount = txo.amount
            script_pubkey = output_derive_script(txo, coin)
            bip143.hash143_add_output(txo_bin, script_pubkey)

        # test data public key hash
        # only for input 2 - input 1 is not segwit
        result = bip143.hash143_preimage_hash(
            self.inp2, unhexlify('1d0f172a0ecb48aee1be1f2687d2963ae33f71a1'))
        self.assertEqual(
            hexlify(result),
            b'c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670'
        )
예제 #11
0
    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), []))
예제 #12
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), []))