2017-10-24 14:48:07 +00:00
|
|
|
from common import *
|
|
|
|
|
2020-05-15 18:35:09 +00:00
|
|
|
from apps.bitcoin.scripts import output_derive_script
|
2020-07-17 15:39:43 +00:00
|
|
|
from apps.bitcoin.sign_tx.approvers import BasicApprover
|
2020-05-15 18:35:09 +00:00
|
|
|
from apps.bitcoin.sign_tx.bitcoin import Bitcoin
|
|
|
|
from apps.bitcoin.writers import get_tx_hash
|
2017-10-24 14:48:07 +00:00
|
|
|
from apps.common import coins
|
2020-07-23 14:20:54 +00:00
|
|
|
from apps.common.keychain import Keychain
|
2017-10-24 14:48:07 +00:00
|
|
|
from trezor.messages.SignTx import SignTx
|
2020-09-16 12:51:19 +00:00
|
|
|
from trezor.messages.TxAckInputType import TxAckInputType
|
|
|
|
from trezor.messages.TxAckOutputType import TxAckOutputType
|
|
|
|
from trezor.messages.TxAckPrevOutputType import TxAckPrevOutputType
|
2020-04-16 12:31:15 +00:00
|
|
|
from trezor.messages import InputScriptType
|
2017-10-24 14:48:07 +00:00
|
|
|
from trezor.messages import OutputScriptType
|
2020-04-24 18:31:00 +00:00
|
|
|
from trezor.crypto import bip39
|
2017-10-24 14:48:07 +00:00
|
|
|
|
|
|
|
|
2017-11-09 14:37:22 +00:00
|
|
|
class TestSegwitBip143NativeP2WPKH(unittest.TestCase):
|
2017-10-24 14:48:07 +00:00
|
|
|
# pylint: disable=C0301
|
|
|
|
|
2017-11-09 14:37:22 +00:00
|
|
|
tx = SignTx(coin_name='Bitcoin', version=1, lock_time=0x00000011, inputs_count=2, outputs_count=2)
|
2020-09-16 12:51:19 +00:00
|
|
|
inp1 = TxAckInputType(address_n=[0],
|
2017-10-30 10:42:22 +00:00
|
|
|
# Trezor expects hash in reversed format
|
2017-11-09 14:37:22 +00:00
|
|
|
prev_hash=unhexlify('9f96ade4b41d5433f4eda31e1738ec2b36f6e7d1420d94a6af99801a88f7f7ff'),
|
|
|
|
prev_index=0,
|
|
|
|
amount=625000000, # 6.25 btc
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
2018-02-12 11:40:18 +00:00
|
|
|
multisig=None,
|
2017-11-09 14:37:22 +00:00
|
|
|
sequence=0xffffffee)
|
2020-09-16 12:51:19 +00:00
|
|
|
inp2 = TxAckInputType(address_n=[1],
|
2017-11-09 14:37:22 +00:00
|
|
|
# Trezor expects hash in reversed format
|
|
|
|
prev_hash=unhexlify('8ac60eb9575db5b2d987e29f301b5b819ea83a5c6579d282d189cc04b8e151ef'),
|
2017-10-24 14:48:07 +00:00
|
|
|
prev_index=1,
|
2018-02-12 11:40:18 +00:00
|
|
|
multisig=None,
|
2017-11-09 14:37:22 +00:00
|
|
|
amount=600000000, # 6 btc
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
|
|
|
sequence=0xffffffff)
|
2020-09-16 12:51:19 +00:00
|
|
|
out1 = TxAckOutputType(address='1Cu32FVupVCgHkMMRJdYJugxwo2Aprgk7H', # derived
|
2017-11-09 14:37:22 +00:00
|
|
|
amount=0x0000000006b22c20,
|
2017-10-30 10:42:22 +00:00
|
|
|
script_type=OutputScriptType.PAYTOADDRESS,
|
2018-02-12 11:40:18 +00:00
|
|
|
multisig=None,
|
2018-02-26 13:17:39 +00:00
|
|
|
address_n=[])
|
2020-09-16 12:51:19 +00:00
|
|
|
out2 = TxAckOutputType(address='16TZ8J6Q5iZKBWizWzFAYnrsaox5Z5aBRV', # derived
|
2017-11-09 14:37:22 +00:00
|
|
|
amount=0x000000000d519390,
|
2017-10-30 10:42:22 +00:00
|
|
|
script_type=OutputScriptType.PAYTOADDRESS,
|
2018-02-12 11:40:18 +00:00
|
|
|
multisig=None,
|
2018-02-26 13:17:39 +00:00
|
|
|
address_n=[])
|
2017-10-24 14:48:07 +00:00
|
|
|
|
2017-11-09 14:37:22 +00:00
|
|
|
def test_prevouts(self):
|
2018-07-02 07:42:11 +00:00
|
|
|
coin = coins.by_name(self.tx.coin_name)
|
2020-07-17 15:39:43 +00:00
|
|
|
bip143 = Bitcoin(self.tx, None, coin, BasicApprover(self.tx, coin))
|
2020-04-24 18:31:00 +00:00
|
|
|
bip143.hash143_add_input(self.inp1)
|
|
|
|
bip143.hash143_add_input(self.inp2)
|
2020-04-27 13:00:22 +00:00
|
|
|
prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double)
|
|
|
|
self.assertEqual(hexlify(prevouts_hash), b'96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37')
|
2017-10-24 14:48:07 +00:00
|
|
|
|
2017-11-09 14:37:22 +00:00
|
|
|
def test_sequence(self):
|
2018-07-02 07:42:11 +00:00
|
|
|
coin = coins.by_name(self.tx.coin_name)
|
2020-07-17 15:39:43 +00:00
|
|
|
bip143 = Bitcoin(self.tx, None, coin, BasicApprover(self.tx, coin))
|
2020-04-24 18:31:00 +00:00
|
|
|
bip143.hash143_add_input(self.inp1)
|
|
|
|
bip143.hash143_add_input(self.inp2)
|
2020-04-27 13:00:22 +00:00
|
|
|
sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double)
|
|
|
|
self.assertEqual(hexlify(sequence_hash), b'52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b')
|
2017-10-24 14:48:07 +00:00
|
|
|
|
2017-11-09 14:37:22 +00:00
|
|
|
def test_outputs(self):
|
2017-10-24 14:48:07 +00:00
|
|
|
|
|
|
|
seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
|
|
|
|
coin = coins.by_name(self.tx.coin_name)
|
2020-07-17 15:39:43 +00:00
|
|
|
bip143 = Bitcoin(self.tx, None, coin, BasicApprover(self.tx, coin))
|
2017-10-24 14:48:07 +00:00
|
|
|
|
|
|
|
for txo in [self.out1, self.out2]:
|
2020-05-25 20:31:13 +00:00
|
|
|
script_pubkey = output_derive_script(txo.address, coin)
|
2020-09-16 12:51:19 +00:00
|
|
|
txo_bin = TxAckPrevOutputType(amount=txo.amount, script_pubkey=script_pubkey)
|
2020-05-05 11:55:52 +00:00
|
|
|
bip143.hash143_add_output(txo_bin, script_pubkey)
|
2017-10-24 14:48:07 +00:00
|
|
|
|
2020-04-27 13:00:22 +00:00
|
|
|
outputs_hash = get_tx_hash(bip143.h_outputs, double=coin.sign_hash_double)
|
|
|
|
self.assertEqual(hexlify(outputs_hash), b'863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5')
|
2017-10-24 14:48:07 +00:00
|
|
|
|
2017-11-09 14:37:22 +00:00
|
|
|
def test_preimage_testdata(self):
|
2017-10-24 14:48:07 +00:00
|
|
|
|
|
|
|
seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
|
|
|
|
coin = coins.by_name(self.tx.coin_name)
|
2020-07-17 15:39:43 +00:00
|
|
|
bip143 = Bitcoin(self.tx, None, coin, BasicApprover(self.tx, coin))
|
2020-04-24 18:31:00 +00:00
|
|
|
bip143.hash143_add_input(self.inp1)
|
|
|
|
bip143.hash143_add_input(self.inp2)
|
2017-11-09 14:37:22 +00:00
|
|
|
|
2017-10-24 14:48:07 +00:00
|
|
|
for txo in [self.out1, self.out2]:
|
2020-05-25 20:31:13 +00:00
|
|
|
script_pubkey = output_derive_script(txo.address, coin)
|
2020-09-16 12:51:19 +00:00
|
|
|
txo_bin = TxAckPrevOutputType(amount=txo.amount, script_pubkey=script_pubkey)
|
2020-05-05 11:55:52 +00:00
|
|
|
bip143.hash143_add_output(txo_bin, script_pubkey)
|
2017-10-24 14:48:07 +00:00
|
|
|
|
2020-07-23 14:20:54 +00:00
|
|
|
keychain = Keychain(seed, coin.curve_name, [[]])
|
2020-06-20 15:28:21 +00:00
|
|
|
node = keychain.derive(self.inp2.address_n)
|
|
|
|
|
2017-10-30 10:42:22 +00:00
|
|
|
# test data public key hash
|
2017-11-09 14:37:22 +00:00
|
|
|
# only for input 2 - input 1 is not segwit
|
2020-06-20 15:28:21 +00:00
|
|
|
result = bip143.hash143_preimage_hash(self.inp2, [node.public_key()], 1)
|
|
|
|
self.assertEqual(hexlify(result), b'2fa3f1351618b2532228d7182d3221d95c21fd3d496e7e22e9ded873cf022a8b')
|
2017-10-24 14:48:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|