2020-03-16 15:16:10 +00:00
|
|
|
from common import *
|
|
|
|
|
2021-03-23 12:35:27 +00:00
|
|
|
from trezor.messages import TxInput
|
|
|
|
from trezor.enums import InputScriptType
|
2020-03-16 15:16:10 +00:00
|
|
|
|
2020-05-15 18:35:09 +00:00
|
|
|
from apps.bitcoin import writers
|
2020-03-16 15:16:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestWriters(unittest.TestCase):
|
|
|
|
def test_tx_input(self):
|
2020-09-23 11:08:26 +00:00
|
|
|
inp = TxInput(
|
2020-03-16 15:16:10 +00:00
|
|
|
address_n=[0],
|
|
|
|
amount=390000,
|
|
|
|
prev_hash=unhexlify(
|
|
|
|
"d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882"
|
|
|
|
),
|
|
|
|
prev_index=0,
|
|
|
|
sequence=0xffffffff,
|
|
|
|
script_sig=b"0123456789",
|
|
|
|
)
|
|
|
|
|
|
|
|
b = bytearray()
|
2020-05-05 11:20:36 +00:00
|
|
|
writers.write_tx_input(b, inp, inp.script_sig)
|
2020-03-16 15:16:10 +00:00
|
|
|
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
|
2020-05-05 11:20:36 +00:00
|
|
|
self.assertRaises(AssertionError, writers.write_tx_input, b, inp, inp.script_sig)
|
2020-03-16 15:16:10 +00:00
|
|
|
|
|
|
|
def test_tx_input_check(self):
|
2020-09-23 11:08:26 +00:00
|
|
|
inp = TxInput(
|
2020-03-16 15:16:10 +00:00
|
|
|
address_n=[0],
|
|
|
|
amount=390000,
|
|
|
|
prev_hash=unhexlify(
|
|
|
|
"d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882"
|
|
|
|
),
|
|
|
|
prev_index=0,
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
|
|
|
sequence=0xffffffff,
|
2021-10-29 19:28:37 +00:00
|
|
|
script_pubkey=unhexlify("76a91424a56db43cf6f2b02e838ea493f95d8d6047423188ac"),
|
2020-03-16 15:16:10 +00:00
|
|
|
script_sig=b"0123456789",
|
|
|
|
)
|
|
|
|
|
|
|
|
b = bytearray()
|
|
|
|
writers.write_tx_input_check(b, inp)
|
2021-10-29 19:28:37 +00:00
|
|
|
self.assertEqual(len(b), 32 + 4 + 4 + 4 + 4 + 4 + 8 + 26)
|
2020-03-16 15:16:10 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|