2021-01-21 19:51:16 +00:00
|
|
|
# This file is part of the Trezor project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2020 SatoshiLabs and contributors
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2024-03-11 15:20:08 +00:00
|
|
|
from trezorlib import btc, messages, misc, models
|
2022-01-31 12:25:30 +00:00
|
|
|
from trezorlib.debuglink import TrezorClientDebugLink as Client
|
2021-01-21 19:51:16 +00:00
|
|
|
from trezorlib.exceptions import TrezorFailure
|
|
|
|
from trezorlib.tools import parse_path
|
|
|
|
|
2023-05-04 12:18:55 +00:00
|
|
|
from ...input_flows import InputFlowPaymentRequestDetails
|
2021-01-21 19:51:16 +00:00
|
|
|
from .payment_req import CoinPurchaseMemo, RefundMemo, TextMemo, make_payment_request
|
2022-01-24 20:23:58 +00:00
|
|
|
from .signtx import forge_prevtx
|
2021-01-21 19:51:16 +00:00
|
|
|
|
2022-01-24 20:23:58 +00:00
|
|
|
# address at seed "all all all..." path m/84h/1h/0h/0/0
|
|
|
|
INPUT_ADDRESS = "tb1qkvwu9g3k2pdxewfqr7syz89r3gj557l3uuf9r9"
|
|
|
|
PREV_HASH, PREV_TX = forge_prevtx([(INPUT_ADDRESS, 12_300_000)], network="testnet")
|
|
|
|
PREV_TXES = {PREV_HASH: PREV_TX}
|
2021-01-21 19:51:16 +00:00
|
|
|
|
|
|
|
|
2024-03-11 15:20:08 +00:00
|
|
|
pytestmark = [pytest.mark.skip_t1b1, pytest.mark.experimental]
|
2021-01-21 19:51:16 +00:00
|
|
|
|
|
|
|
|
2024-03-11 15:20:08 +00:00
|
|
|
def case(id, *args, altcoin: bool = False, skip_t2b1: bool = False):
|
2023-08-28 14:46:57 +00:00
|
|
|
marks = []
|
2021-01-21 19:51:16 +00:00
|
|
|
if altcoin:
|
2023-08-28 14:46:57 +00:00
|
|
|
marks.append(pytest.mark.altcoin)
|
2024-03-11 15:20:08 +00:00
|
|
|
if skip_t2b1:
|
|
|
|
marks.append(pytest.mark.skip_t2b1)
|
2021-01-21 19:51:16 +00:00
|
|
|
return pytest.param(*args, id=id, marks=marks)
|
|
|
|
|
|
|
|
|
|
|
|
inputs = [
|
|
|
|
messages.TxInputType(
|
2022-01-31 12:25:30 +00:00
|
|
|
address_n=parse_path("m/84h/1h/0h/0/0"),
|
|
|
|
amount=12_300_000,
|
2022-01-24 20:23:58 +00:00
|
|
|
prev_hash=PREV_HASH,
|
2021-01-21 19:51:16 +00:00
|
|
|
prev_index=0,
|
|
|
|
script_type=messages.InputScriptType.SPENDWITNESS,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
outputs = [
|
|
|
|
messages.TxOutputType(
|
|
|
|
address="2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp",
|
2022-01-31 12:25:30 +00:00
|
|
|
amount=5_000_000,
|
2021-01-21 19:51:16 +00:00
|
|
|
script_type=messages.OutputScriptType.PAYTOADDRESS,
|
|
|
|
),
|
|
|
|
messages.TxOutputType(
|
|
|
|
address="tb1q694ccp5qcc0udmfwgp692u2s2hjpq5h407urtu",
|
|
|
|
script_type=messages.OutputScriptType.PAYTOADDRESS,
|
2022-01-31 12:25:30 +00:00
|
|
|
amount=2_000_000,
|
2021-01-21 19:51:16 +00:00
|
|
|
),
|
|
|
|
messages.TxOutputType(
|
|
|
|
# tb1qkvwu9g3k2pdxewfqr7syz89r3gj557l3uuf9r9
|
2022-01-31 12:25:30 +00:00
|
|
|
address_n=parse_path("m/84h/1h/0h/0/0"),
|
|
|
|
amount=12_300_000 - 5_000_000 - 2_000_000 - 11_000,
|
2021-01-21 19:51:16 +00:00
|
|
|
script_type=messages.OutputScriptType.PAYTOWITNESS,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
memos1 = [
|
|
|
|
CoinPurchaseMemo(
|
|
|
|
amount="15.9636 DASH",
|
|
|
|
coin_name="Dash",
|
|
|
|
slip44=5,
|
2022-01-31 12:25:30 +00:00
|
|
|
address_n=parse_path("m/44h/5h/0h/1/0"),
|
2021-01-21 19:51:16 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
memos2 = [
|
|
|
|
CoinPurchaseMemo(
|
|
|
|
amount="3.1896 DASH",
|
|
|
|
coin_name="Dash",
|
|
|
|
slip44=5,
|
2022-01-31 12:25:30 +00:00
|
|
|
address_n=parse_path("m/44h/5h/0h/1/0"),
|
2021-01-21 19:51:16 +00:00
|
|
|
),
|
|
|
|
CoinPurchaseMemo(
|
|
|
|
amount="831.570802 GRS",
|
|
|
|
coin_name="Groestlcoin",
|
|
|
|
slip44=17,
|
2022-01-31 12:25:30 +00:00
|
|
|
address_n=parse_path("m/44h/17h/0h/0/3"),
|
2021-01-21 19:51:16 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
memos3 = [TextMemo("Invoice #87654321."), RefundMemo(parse_path("m/44h/1h/0h/0/1"))]
|
2021-01-21 19:51:16 +00:00
|
|
|
|
|
|
|
PaymentRequestParams = namedtuple(
|
|
|
|
"PaymentRequestParams", ["txo_indices", "memos", "get_nonce"]
|
|
|
|
)
|
|
|
|
|
2022-01-24 20:23:58 +00:00
|
|
|
SERIALIZED_TX = "01000000000101e29305e85821ea86f2bca1fcfe45e7cb0c8de87b612479ee641e0d3d12a723b20000000000ffffffff03404b4c000000000017a9147a55d61848e77ca266e79a39bfc85c580a6426c98780841e0000000000160014d16b8c0680c61fc6ed2e407455715055e41052f528b4500000000000160014b31dc2a236505a6cb9201fa0411ca38a254a7bf10247304402203ca28fc86a8947ccd11af2d80febfb592d3a29abcb0a8e0fc4924615a1307d89022051c1b41e0db900d90883c030da14f26a34b2bef6d2b289c5aac3097f3501005f012103adc58245cf28406af0ef5cc24b8afba7f1be6c72f279b642d85c48798685f86200000000"
|
2021-12-13 12:07:54 +00:00
|
|
|
|
2021-01-21 19:51:16 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"payment_request_params",
|
|
|
|
(
|
|
|
|
case(
|
2023-08-28 14:46:57 +00:00
|
|
|
"out0",
|
|
|
|
(PaymentRequestParams([0], memos1, get_nonce=True),),
|
|
|
|
altcoin=True,
|
2024-03-11 15:20:08 +00:00
|
|
|
skip_t2b1=True,
|
2021-01-21 19:51:16 +00:00
|
|
|
),
|
|
|
|
case(
|
2023-08-28 14:46:57 +00:00
|
|
|
"out1",
|
|
|
|
(PaymentRequestParams([1], memos2, get_nonce=True),),
|
|
|
|
altcoin=True,
|
2024-03-11 15:20:08 +00:00
|
|
|
skip_t2b1=True,
|
2021-01-21 19:51:16 +00:00
|
|
|
),
|
|
|
|
case("out2", (PaymentRequestParams([2], [], get_nonce=True),)),
|
|
|
|
case(
|
|
|
|
"out0+out1",
|
|
|
|
(
|
|
|
|
PaymentRequestParams([0], [], get_nonce=False),
|
|
|
|
PaymentRequestParams([1], [], get_nonce=True),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
case(
|
|
|
|
"out01",
|
|
|
|
(PaymentRequestParams([0, 1], memos3, get_nonce=True),),
|
|
|
|
),
|
|
|
|
case("out012", (PaymentRequestParams([0, 1, 2], [], get_nonce=True),)),
|
|
|
|
case("out12", (PaymentRequestParams([1, 2], [], get_nonce=True),)),
|
|
|
|
),
|
|
|
|
)
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_payment_request(client: Client, payment_request_params):
|
2021-01-21 19:51:16 +00:00
|
|
|
for txo in outputs:
|
|
|
|
txo.payment_req_index = None
|
|
|
|
|
|
|
|
payment_reqs = []
|
|
|
|
for i, params in enumerate(payment_request_params):
|
|
|
|
request_outputs = []
|
|
|
|
for txo_index in params.txo_indices:
|
|
|
|
outputs[txo_index].payment_req_index = i
|
|
|
|
request_outputs.append(outputs[txo_index])
|
|
|
|
nonce = misc.get_nonce(client) if params.get_nonce else None
|
|
|
|
payment_reqs.append(
|
|
|
|
make_payment_request(
|
|
|
|
client,
|
|
|
|
recipient_name="trezor.io",
|
|
|
|
outputs=request_outputs,
|
|
|
|
change_addresses=["tb1qkvwu9g3k2pdxewfqr7syz89r3gj557l3uuf9r9"],
|
|
|
|
memos=params.memos,
|
|
|
|
nonce=nonce,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
_, serialized_tx = btc.sign_tx(
|
|
|
|
client,
|
|
|
|
"Testnet",
|
|
|
|
inputs,
|
|
|
|
outputs,
|
2022-01-24 20:23:58 +00:00
|
|
|
prev_txes=PREV_TXES,
|
2021-01-21 19:51:16 +00:00
|
|
|
payment_reqs=payment_reqs,
|
|
|
|
)
|
|
|
|
|
2021-12-13 12:07:54 +00:00
|
|
|
assert serialized_tx.hex() == SERIALIZED_TX
|
2021-01-21 19:51:16 +00:00
|
|
|
|
|
|
|
# Ensure that the nonce has been invalidated.
|
|
|
|
with pytest.raises(TrezorFailure, match="Invalid nonce in payment request"):
|
|
|
|
btc.sign_tx(
|
|
|
|
client,
|
|
|
|
"Testnet",
|
|
|
|
inputs,
|
|
|
|
outputs,
|
2022-01-24 20:23:58 +00:00
|
|
|
prev_txes=PREV_TXES,
|
2021-01-21 19:51:16 +00:00
|
|
|
payment_reqs=payment_reqs,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_payment_request_details(client: Client):
|
2024-03-11 15:20:08 +00:00
|
|
|
if client.model is models.T2B1:
|
|
|
|
pytest.skip("Details not implemented on T2B1")
|
2023-05-12 09:19:35 +00:00
|
|
|
|
2021-12-13 12:07:54 +00:00
|
|
|
# Test that payment request details are shown when requested.
|
|
|
|
outputs[0].payment_req_index = 0
|
|
|
|
outputs[1].payment_req_index = 0
|
|
|
|
outputs[2].payment_req_index = None
|
|
|
|
nonce = misc.get_nonce(client)
|
|
|
|
payment_reqs = [
|
|
|
|
make_payment_request(
|
|
|
|
client,
|
|
|
|
recipient_name="trezor.io",
|
|
|
|
outputs=outputs[:2],
|
|
|
|
memos=[TextMemo("Invoice #87654321.")],
|
|
|
|
nonce=nonce,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
with client:
|
2023-05-04 12:18:55 +00:00
|
|
|
IF = InputFlowPaymentRequestDetails(client, outputs)
|
|
|
|
client.set_input_flow(IF.get())
|
2021-12-13 12:07:54 +00:00
|
|
|
|
|
|
|
_, serialized_tx = btc.sign_tx(
|
|
|
|
client,
|
|
|
|
"Testnet",
|
|
|
|
inputs,
|
|
|
|
outputs,
|
2022-01-24 20:23:58 +00:00
|
|
|
prev_txes=PREV_TXES,
|
2021-12-13 12:07:54 +00:00
|
|
|
payment_reqs=payment_reqs,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert serialized_tx.hex() == SERIALIZED_TX
|
|
|
|
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_payment_req_wrong_amount(client: Client):
|
2021-01-21 19:51:16 +00:00
|
|
|
# Test wrong total amount in payment request.
|
|
|
|
outputs[0].payment_req_index = 0
|
|
|
|
outputs[1].payment_req_index = 0
|
|
|
|
outputs[2].payment_req_index = None
|
|
|
|
payment_req = make_payment_request(
|
|
|
|
client,
|
|
|
|
recipient_name="trezor.io",
|
|
|
|
outputs=outputs[:2],
|
|
|
|
nonce=misc.get_nonce(client),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Decrease the total amount of the payment request.
|
|
|
|
payment_req.amount -= 1
|
|
|
|
|
|
|
|
with pytest.raises(TrezorFailure, match="Invalid amount in payment request"):
|
|
|
|
btc.sign_tx(
|
|
|
|
client,
|
|
|
|
"Testnet",
|
|
|
|
inputs,
|
|
|
|
outputs,
|
2022-01-24 20:23:58 +00:00
|
|
|
prev_txes=PREV_TXES,
|
2021-01-21 19:51:16 +00:00
|
|
|
payment_reqs=[payment_req],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_payment_req_wrong_mac_refund(client: Client):
|
2021-01-21 19:51:16 +00:00
|
|
|
# Test wrong MAC in payment request memo.
|
2022-01-31 12:25:30 +00:00
|
|
|
memo = RefundMemo(parse_path("m/44h/1h/0h/1/0"))
|
2021-01-21 19:51:16 +00:00
|
|
|
outputs[0].payment_req_index = 0
|
|
|
|
outputs[1].payment_req_index = 0
|
|
|
|
outputs[2].payment_req_index = None
|
|
|
|
payment_req = make_payment_request(
|
|
|
|
client,
|
|
|
|
recipient_name="trezor.io",
|
|
|
|
outputs=outputs[:2],
|
|
|
|
memos=[memo],
|
|
|
|
nonce=misc.get_nonce(client),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Corrupt the MAC value.
|
|
|
|
mac = bytearray(payment_req.memos[0].refund_memo.mac)
|
|
|
|
mac[0] ^= 1
|
|
|
|
payment_req.memos[0].refund_memo.mac = mac
|
|
|
|
|
|
|
|
with pytest.raises(TrezorFailure, match="Invalid address MAC"):
|
|
|
|
btc.sign_tx(
|
|
|
|
client,
|
|
|
|
"Testnet",
|
|
|
|
inputs,
|
|
|
|
outputs,
|
2022-01-24 20:23:58 +00:00
|
|
|
prev_txes=PREV_TXES,
|
2021-01-21 19:51:16 +00:00
|
|
|
payment_reqs=[payment_req],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.altcoin
|
2024-03-11 15:20:08 +00:00
|
|
|
@pytest.mark.skip_t2b1
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_payment_req_wrong_mac_purchase(client: Client):
|
2021-01-21 19:51:16 +00:00
|
|
|
# Test wrong MAC in payment request memo.
|
|
|
|
memo = CoinPurchaseMemo(
|
|
|
|
amount="22.34904 DASH",
|
|
|
|
coin_name="Dash",
|
|
|
|
slip44=5,
|
2022-01-31 12:25:30 +00:00
|
|
|
address_n=parse_path("m/44h/5h/0h/1/0"),
|
2021-01-21 19:51:16 +00:00
|
|
|
)
|
|
|
|
outputs[0].payment_req_index = 0
|
|
|
|
outputs[1].payment_req_index = 0
|
|
|
|
outputs[2].payment_req_index = None
|
|
|
|
payment_req = make_payment_request(
|
|
|
|
client,
|
|
|
|
recipient_name="trezor.io",
|
|
|
|
outputs=outputs[:2],
|
|
|
|
memos=[memo],
|
|
|
|
nonce=misc.get_nonce(client),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Corrupt the MAC value.
|
|
|
|
mac = bytearray(payment_req.memos[0].coin_purchase_memo.mac)
|
|
|
|
mac[0] ^= 1
|
|
|
|
payment_req.memos[0].coin_purchase_memo.mac = mac
|
|
|
|
|
|
|
|
with pytest.raises(TrezorFailure, match="Invalid address MAC"):
|
|
|
|
btc.sign_tx(
|
|
|
|
client,
|
|
|
|
"Testnet",
|
|
|
|
inputs,
|
|
|
|
outputs,
|
2022-01-24 20:23:58 +00:00
|
|
|
prev_txes=PREV_TXES,
|
2021-01-21 19:51:16 +00:00
|
|
|
payment_reqs=[payment_req],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_payment_req_wrong_output(client: Client):
|
2021-01-21 19:51:16 +00:00
|
|
|
# Test wrong output in payment request.
|
|
|
|
outputs[0].payment_req_index = 0
|
|
|
|
outputs[1].payment_req_index = 0
|
|
|
|
outputs[2].payment_req_index = None
|
|
|
|
payment_req = make_payment_request(
|
|
|
|
client,
|
|
|
|
recipient_name="trezor.io",
|
|
|
|
outputs=outputs[:2],
|
|
|
|
nonce=misc.get_nonce(client),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Use a different address in the second output.
|
|
|
|
fake_outputs = [
|
|
|
|
outputs[0],
|
|
|
|
messages.TxOutputType(
|
|
|
|
address="tb1qnspxpr2xj9s2jt6qlhuvdnxw6q55jvygcf89r2",
|
|
|
|
script_type=outputs[1].script_type,
|
|
|
|
amount=outputs[1].amount,
|
|
|
|
payment_req_index=outputs[1].payment_req_index,
|
|
|
|
),
|
|
|
|
outputs[2],
|
|
|
|
]
|
|
|
|
|
|
|
|
with pytest.raises(TrezorFailure, match="Invalid signature in payment request"):
|
|
|
|
btc.sign_tx(
|
|
|
|
client,
|
|
|
|
"Testnet",
|
|
|
|
inputs,
|
|
|
|
fake_outputs,
|
2022-01-24 20:23:58 +00:00
|
|
|
prev_txes=PREV_TXES,
|
2021-01-21 19:51:16 +00:00
|
|
|
payment_reqs=[payment_req],
|
|
|
|
)
|