2024-11-15 12:55:14 +00:00
|
|
|
# flake8: noqa: F403,F405
|
2024-01-26 11:16:55 +00:00
|
|
|
from common import * # isort:skip
|
2020-03-16 15:16:10 +00:00
|
|
|
|
2021-03-23 12:35:27 +00:00
|
|
|
from trezor.enums import InputScriptType
|
2023-06-28 10:58:54 +00:00
|
|
|
from trezor.messages import TxInput
|
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,
|
2023-06-28 10:46:29 +00:00
|
|
|
sequence=0xFFFFFFFF,
|
2020-03-16 15:16:10 +00:00
|
|
|
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
|
2023-06-28 10:46:29 +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,
|
2023-06-28 10:46:29 +00:00
|
|
|
sequence=0xFFFFFFFF,
|
|
|
|
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)
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(
|
|
|
|
len(b), 4 + 4 + 32 + 4 + 11 + 4 + 4 + 1 + 8 + 1 + 1 + 1 + 4 + 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)
|
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
|
2020-03-16 15:16:10 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|