2016-12-08 15:14:47 +00:00
|
|
|
from common import *
|
|
|
|
|
|
|
|
from trezor.utils import chunks
|
2022-02-25 12:03:18 +00:00
|
|
|
from trezor.crypto import bip39
|
2021-03-23 12:35:27 +00:00
|
|
|
from trezor.messages import SignTx
|
|
|
|
from trezor.messages import TxAckInput
|
|
|
|
from trezor.messages import TxAckInputWrapper
|
|
|
|
from trezor.messages import TxInput
|
|
|
|
from trezor.messages import TxAckOutput
|
|
|
|
from trezor.messages import TxAckOutputWrapper
|
|
|
|
from trezor.messages import TxOutput
|
|
|
|
from trezor.messages import TxAckPrevMeta
|
|
|
|
from trezor.messages import PrevTx
|
|
|
|
from trezor.messages import TxAckPrevInput
|
|
|
|
from trezor.messages import TxAckPrevInputWrapper
|
|
|
|
from trezor.messages import PrevInput
|
|
|
|
from trezor.messages import TxAckPrevOutput
|
|
|
|
from trezor.messages import TxAckPrevOutputWrapper
|
|
|
|
from trezor.messages import PrevOutput
|
|
|
|
from trezor.messages import TxRequest
|
|
|
|
from trezor.enums.RequestType import TXINPUT, TXOUTPUT, TXMETA, TXFINISHED
|
|
|
|
from trezor.messages import TxRequestDetailsType
|
|
|
|
from trezor.messages import TxRequestSerializedType
|
|
|
|
from trezor.enums import AmountUnit
|
|
|
|
from trezor.enums import OutputScriptType
|
2016-12-08 15:14:47 +00:00
|
|
|
|
|
|
|
from apps.common import coins
|
2020-07-23 14:20:54 +00:00
|
|
|
from apps.common.keychain import Keychain
|
2022-09-21 07:56:28 +00:00
|
|
|
from apps.bitcoin.keychain import _get_schemas_for_coin
|
2020-05-15 18:35:09 +00:00
|
|
|
from apps.bitcoin.sign_tx import bitcoin, helpers
|
2020-04-16 12:31:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
EMPTY_SERIALIZED = TxRequestSerializedType(serialized_tx=bytearray())
|
2016-12-08 15:14:47 +00:00
|
|
|
|
2017-12-16 01:11:26 +00:00
|
|
|
|
2016-12-08 15:14:47 +00:00
|
|
|
class TestSignTx(unittest.TestCase):
|
|
|
|
# pylint: disable=C0301
|
|
|
|
|
|
|
|
def test_one_one_fee(self):
|
2022-02-25 12:03:18 +00:00
|
|
|
# input tx: 1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3
|
|
|
|
# input 0: 0.03801747 BTC
|
|
|
|
|
|
|
|
# output tx: https://btc1.trezor.io/tx/e590d5d76867e9f14466f715e26438845afff0ae082cda1f2f55aa8bf7f98140
|
|
|
|
|
|
|
|
coin_bitcoin = coins.by_name("Bitcoin")
|
|
|
|
|
|
|
|
ptx1 = PrevTx(
|
|
|
|
version=1, lock_time=0, inputs_count=2, outputs_count=1, extra_data_len=0
|
|
|
|
)
|
|
|
|
pinp1 = PrevInput(
|
|
|
|
script_sig=unhexlify(
|
|
|
|
"47304402202df6c8885489be0fd52ed1f82c20b4c2c03e479369ea8439ed6233a3bb349ddd02207f749a00dd98d1aca699178d8fa954dc45b9faff33d3c9f03b81fa766061f2d70121038b60c00f69d78f2c5df84a24b30894c3700e4d6176a1f440461c8c69bf1f4262"
|
|
|
|
),
|
|
|
|
prev_hash=unhexlify(
|
|
|
|
"468c0de3bf9dfcc4915354904108611db9547a16ec38a24bf6b240ccc946e0d3"
|
|
|
|
),
|
|
|
|
prev_index=0,
|
|
|
|
sequence=0xFFFF_FFFF,
|
|
|
|
)
|
|
|
|
pinp2 = PrevInput(
|
|
|
|
script_sig=unhexlify(
|
|
|
|
"483045022100b11b3804e9b5fbbad70532e550192b3bbdd65b51f30572638a5559a86600856702204b3bd50a847ddf66426c0f413b9e1a98388b0d36479e66ef37b2d53d9ad13d47012103d443712f673afafafc006ccb6e19220de7ae0c07c9832666103974293d04a93c"
|
|
|
|
),
|
|
|
|
prev_hash=unhexlify(
|
|
|
|
"ebb32bba661e3dccb216acf593de26223f5f2c17f6a75634f2a0b581c4855fa0"
|
|
|
|
),
|
|
|
|
prev_index=0,
|
|
|
|
sequence=0xFFFF_FFFF,
|
|
|
|
)
|
|
|
|
pout1 = PrevOutput(
|
|
|
|
script_pubkey=unhexlify(
|
|
|
|
"76a914be145c9a7c131ad0c6c9b86f83748d660b42e2f488ac"
|
|
|
|
),
|
|
|
|
amount=3_801_747,
|
|
|
|
)
|
|
|
|
|
|
|
|
inp1 = TxInput(
|
|
|
|
address_n=[44 | 0x80000000, 0 | 0x80000000, 0 | 0x80000000, 0, 10],
|
|
|
|
prev_hash=unhexlify(
|
|
|
|
"1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
|
|
|
|
),
|
|
|
|
prev_index=0,
|
|
|
|
amount=3_801_747,
|
|
|
|
multisig=None,
|
|
|
|
sequence=0xFFFF_FFFF,
|
|
|
|
)
|
|
|
|
out1 = TxOutput(
|
|
|
|
address="19WEjX2zgXdn6FCLmRAJ5Ty593GkJ77pNj",
|
|
|
|
amount=3_801_747 - 50_000,
|
|
|
|
script_type=OutputScriptType.PAYTOADDRESS,
|
|
|
|
multisig=None,
|
|
|
|
)
|
|
|
|
tx = SignTx(
|
|
|
|
coin_name=None, version=1, lock_time=0, inputs_count=1, outputs_count=1
|
|
|
|
)
|
2016-12-08 15:14:47 +00:00
|
|
|
|
2022-05-03 13:21:58 +00:00
|
|
|
# precomputed tx weight is 768
|
|
|
|
fee_rate = 50_000 / (768 / 4)
|
|
|
|
|
2016-12-08 15:14:47 +00:00
|
|
|
messages = [
|
|
|
|
None,
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXINPUT,
|
|
|
|
details=TxRequestDetailsType(request_index=0, tx_hash=None),
|
|
|
|
serialized=EMPTY_SERIALIZED,
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckInput(tx=TxAckInputWrapper(input=inp1)),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXOUTPUT,
|
|
|
|
details=TxRequestDetailsType(request_index=0, tx_hash=None),
|
|
|
|
serialized=EMPTY_SERIALIZED,
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
|
2023-09-14 10:24:29 +00:00
|
|
|
helpers.UiConfirmOutput(out1, coin_bitcoin, AmountUnit.BITCOIN, 0, False),
|
2016-12-08 15:14:47 +00:00
|
|
|
True,
|
2023-01-11 15:43:02 +00:00
|
|
|
helpers.UiConfirmTotal(3_801_747, 50_000, fee_rate, coin_bitcoin, AmountUnit.BITCOIN, inp1.address_n[:3]),
|
2016-12-08 15:14:47 +00:00
|
|
|
True,
|
|
|
|
# ButtonRequest(code=ButtonRequest_ConfirmOutput),
|
|
|
|
# ButtonRequest(code=ButtonRequest_SignTx),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXINPUT,
|
|
|
|
details=TxRequestDetailsType(request_index=0, tx_hash=None),
|
|
|
|
serialized=EMPTY_SERIALIZED,
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckInput(tx=TxAckInputWrapper(input=inp1)),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXMETA,
|
|
|
|
details=TxRequestDetailsType(
|
|
|
|
request_index=None,
|
|
|
|
tx_hash=unhexlify(
|
|
|
|
"1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
serialized=EMPTY_SERIALIZED,
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckPrevMeta(tx=ptx1),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXINPUT,
|
|
|
|
details=TxRequestDetailsType(
|
|
|
|
request_index=0,
|
|
|
|
tx_hash=unhexlify(
|
|
|
|
"1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
serialized=EMPTY_SERIALIZED,
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp1)),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXINPUT,
|
|
|
|
details=TxRequestDetailsType(
|
|
|
|
request_index=1,
|
|
|
|
tx_hash=unhexlify(
|
|
|
|
"1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
serialized=EMPTY_SERIALIZED,
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckPrevInput(tx=TxAckPrevInputWrapper(input=pinp2)),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXOUTPUT,
|
|
|
|
details=TxRequestDetailsType(
|
|
|
|
request_index=0,
|
|
|
|
tx_hash=unhexlify(
|
|
|
|
"1f326f65768d55ef146efbb345bd87abe84ac7185726d0457a026fc347a26ef3"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
serialized=EMPTY_SERIALIZED,
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckPrevOutput(tx=TxAckPrevOutputWrapper(output=pout1)),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXINPUT,
|
|
|
|
details=TxRequestDetailsType(request_index=0, tx_hash=None),
|
|
|
|
serialized=TxRequestSerializedType(
|
|
|
|
serialized_tx=unhexlify("0100000001")
|
|
|
|
),
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckInput(tx=TxAckInputWrapper(input=inp1)),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXOUTPUT,
|
|
|
|
details=TxRequestDetailsType(request_index=0, tx_hash=None),
|
|
|
|
serialized=EMPTY_SERIALIZED,
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXOUTPUT,
|
|
|
|
details=TxRequestDetailsType(request_index=0, tx_hash=None),
|
|
|
|
serialized=TxRequestSerializedType(
|
|
|
|
signature_index=0,
|
|
|
|
signature=unhexlify(
|
|
|
|
"3045022100bebe58e7eac8170334f987d0f67032a2eddd0e2f378e002502ec88a69460ea460220040a8effc41167f5d5a5ffc77f37317d8125c7cd05f92d142e4d977f2f132c94"
|
|
|
|
),
|
|
|
|
serialized_tx=unhexlify(
|
|
|
|
"f36ea247c36f027a45d0265718c74ae8ab87bd45b3fb6e14ef558d76656f321f000000006b483045022100bebe58e7eac8170334f987d0f67032a2eddd0e2f378e002502ec88a69460ea460220040a8effc41167f5d5a5ffc77f37317d8125c7cd05f92d142e4d977f2f132c940121038bac33bcdaeec5626e2f2c5680a9fdc5e551d4e1167f272825bea98e6158d4c8ffffffff01"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-09-16 12:51:19 +00:00
|
|
|
TxAckOutput(tx=TxAckOutputWrapper(output=out1)),
|
2022-02-25 12:03:18 +00:00
|
|
|
TxRequest(
|
|
|
|
request_type=TXFINISHED,
|
|
|
|
details=TxRequestDetailsType(),
|
|
|
|
serialized=TxRequestSerializedType(
|
|
|
|
signature_index=None,
|
|
|
|
signature=None,
|
|
|
|
serialized_tx=unhexlify(
|
|
|
|
"433f3900000000001976a9145d48886f4e2aad3164e55e23a7a41b921646a33488ac00000000"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-12-08 15:14:47 +00:00
|
|
|
]
|
|
|
|
|
2022-02-25 12:03:18 +00:00
|
|
|
seed = bip39.seed(
|
|
|
|
" ".join(["all"] * 12),
|
|
|
|
"",
|
|
|
|
)
|
2022-09-21 07:56:28 +00:00
|
|
|
ns = _get_schemas_for_coin(coin_bitcoin)
|
2020-07-23 14:20:54 +00:00
|
|
|
keychain = Keychain(seed, coin_bitcoin.curve_name, ns)
|
2021-01-30 00:49:18 +00:00
|
|
|
signer = bitcoin.Bitcoin(tx, keychain, coin_bitcoin, None).signer()
|
2018-07-02 12:29:58 +00:00
|
|
|
|
2016-12-08 15:14:47 +00:00
|
|
|
for request, response in chunks(messages, 2):
|
2018-07-02 12:29:58 +00:00
|
|
|
res = signer.send(request)
|
2020-09-16 12:51:19 +00:00
|
|
|
if isinstance(res, tuple):
|
|
|
|
_, res = res
|
2018-12-04 13:27:06 +00:00
|
|
|
self.assertEqual(res, response)
|
2018-07-02 12:29:58 +00:00
|
|
|
|
2016-12-08 15:14:47 +00:00
|
|
|
with self.assertRaises(StopIteration):
|
|
|
|
signer.send(None)
|
|
|
|
|
|
|
|
|
2022-02-25 12:03:18 +00:00
|
|
|
if __name__ == "__main__":
|
2016-12-08 15:14:47 +00:00
|
|
|
unittest.main()
|