You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
4.5 KiB
100 lines
4.5 KiB
![]()
7 years ago
|
from common import *
|
||
|
|
||
![]()
4 years ago
|
from apps.bitcoin.scripts import output_derive_script
|
||
|
from apps.bitcoin.sign_tx.bitcoin import Bitcoin
|
||
|
from apps.bitcoin.writers import get_tx_hash
|
||
![]()
7 years ago
|
from apps.common import coins
|
||
|
from trezor.messages.SignTx import SignTx
|
||
|
from trezor.messages.TxInputType import TxInputType
|
||
|
from trezor.messages.TxOutputType import TxOutputType
|
||
![]()
4 years ago
|
from trezor.messages.TxOutputBinType import TxOutputBinType
|
||
![]()
4 years ago
|
from trezor.messages import InputScriptType
|
||
![]()
7 years ago
|
from trezor.messages import OutputScriptType
|
||
![]()
4 years ago
|
from trezor.crypto import bip39
|
||
![]()
7 years ago
|
|
||
|
|
||
![]()
7 years ago
|
class TestSegwitBip143NativeP2WPKH(unittest.TestCase):
|
||
![]()
7 years ago
|
# pylint: disable=C0301
|
||
|
|
||
![]()
7 years ago
|
tx = SignTx(coin_name='Bitcoin', version=1, lock_time=0x00000011, inputs_count=2, outputs_count=2)
|
||
![]()
7 years ago
|
inp1 = TxInputType(address_n=[0],
|
||
![]()
7 years ago
|
# Trezor expects hash in reversed format
|
||
![]()
7 years ago
|
prev_hash=unhexlify('9f96ade4b41d5433f4eda31e1738ec2b36f6e7d1420d94a6af99801a88f7f7ff'),
|
||
|
prev_index=0,
|
||
|
amount=625000000, # 6.25 btc
|
||
|
script_type=InputScriptType.SPENDWITNESS,
|
||
![]()
7 years ago
|
multisig=None,
|
||
![]()
7 years ago
|
sequence=0xffffffee)
|
||
|
inp2 = TxInputType(address_n=[1],
|
||
|
# Trezor expects hash in reversed format
|
||
|
prev_hash=unhexlify('8ac60eb9575db5b2d987e29f301b5b819ea83a5c6579d282d189cc04b8e151ef'),
|
||
![]()
7 years ago
|
prev_index=1,
|
||
![]()
7 years ago
|
multisig=None,
|
||
![]()
7 years ago
|
amount=600000000, # 6 btc
|
||
|
script_type=InputScriptType.SPENDWITNESS,
|
||
|
sequence=0xffffffff)
|
||
|
out1 = TxOutputType(address='1Cu32FVupVCgHkMMRJdYJugxwo2Aprgk7H', # derived
|
||
|
amount=0x0000000006b22c20,
|
||
![]()
7 years ago
|
script_type=OutputScriptType.PAYTOADDRESS,
|
||
![]()
7 years ago
|
multisig=None,
|
||
![]()
7 years ago
|
address_n=[])
|
||
![]()
7 years ago
|
out2 = TxOutputType(address='16TZ8J6Q5iZKBWizWzFAYnrsaox5Z5aBRV', # derived
|
||
|
amount=0x000000000d519390,
|
||
![]()
7 years ago
|
script_type=OutputScriptType.PAYTOADDRESS,
|
||
![]()
7 years ago
|
multisig=None,
|
||
![]()
7 years ago
|
address_n=[])
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
def test_prevouts(self):
|
||
![]()
6 years ago
|
coin = coins.by_name(self.tx.coin_name)
|
||
![]()
4 years ago
|
bip143 = Bitcoin(self.tx, None, coin)
|
||
|
bip143.hash143_add_input(self.inp1)
|
||
|
bip143.hash143_add_input(self.inp2)
|
||
![]()
4 years ago
|
prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double)
|
||
|
self.assertEqual(hexlify(prevouts_hash), b'96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37')
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
def test_sequence(self):
|
||
![]()
6 years ago
|
coin = coins.by_name(self.tx.coin_name)
|
||
![]()
4 years ago
|
bip143 = Bitcoin(self.tx, None, coin)
|
||
|
bip143.hash143_add_input(self.inp1)
|
||
|
bip143.hash143_add_input(self.inp2)
|
||
![]()
4 years ago
|
sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double)
|
||
|
self.assertEqual(hexlify(sequence_hash), b'52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b')
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
def test_outputs(self):
|
||
![]()
7 years ago
|
|
||
|
seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
|
||
|
coin = coins.by_name(self.tx.coin_name)
|
||
![]()
4 years ago
|
bip143 = Bitcoin(self.tx, None, coin)
|
||
![]()
7 years ago
|
|
||
|
for txo in [self.out1, self.out2]:
|
||
|
txo_bin = TxOutputBinType()
|
||
|
txo_bin.amount = txo.amount
|
||
![]()
4 years ago
|
script_pubkey = output_derive_script(txo, coin)
|
||
|
bip143.hash143_add_output(txo_bin, script_pubkey)
|
||
![]()
7 years ago
|
|
||
![]()
4 years ago
|
outputs_hash = get_tx_hash(bip143.h_outputs, double=coin.sign_hash_double)
|
||
|
self.assertEqual(hexlify(outputs_hash), b'863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5')
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
def test_preimage_testdata(self):
|
||
![]()
7 years ago
|
|
||
|
seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
|
||
|
coin = coins.by_name(self.tx.coin_name)
|
||
![]()
4 years ago
|
bip143 = Bitcoin(self.tx, None, coin)
|
||
|
bip143.hash143_add_input(self.inp1)
|
||
|
bip143.hash143_add_input(self.inp2)
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
for txo in [self.out1, self.out2]:
|
||
|
txo_bin = TxOutputBinType()
|
||
|
txo_bin.amount = txo.amount
|
||
![]()
4 years ago
|
script_pubkey = output_derive_script(txo, coin)
|
||
|
bip143.hash143_add_output(txo_bin, script_pubkey)
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
# test data public key hash
|
||
![]()
7 years ago
|
# only for input 2 - input 1 is not segwit
|
||
![]()
4 years ago
|
result = bip143.hash143_preimage_hash(self.inp2, unhexlify('1d0f172a0ecb48aee1be1f2687d2963ae33f71a1'))
|
||
![]()
7 years ago
|
self.assertEqual(hexlify(result), b'c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670')
|
||
![]()
7 years ago
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
unittest.main()
|