2024-11-15 15:06:55 +00:00
|
|
|
from common import * # isort:skip
|
2020-08-03 15:28:20 +00:00
|
|
|
|
2021-03-30 08:57:07 +00:00
|
|
|
import storage.cache
|
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 AuthorizeCoinJoin, GetOwnershipProof, SignTx
|
2024-11-15 15:06:55 +00:00
|
|
|
from trezor.wire import context
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
from apps.bitcoin.authorization import CoinJoinAuthorization
|
2023-06-28 10:58:54 +00:00
|
|
|
from apps.common import coins
|
2024-11-15 15:06:55 +00:00
|
|
|
from trezor.wire.codec.codec_context import CodecContext
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
_ROUND_ID_LEN = 32
|
|
|
|
|
2024-11-15 15:06:55 +00:00
|
|
|
if utils.USE_THP:
|
|
|
|
import thp_common
|
|
|
|
else:
|
|
|
|
import storage.cache_codec
|
|
|
|
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
class TestAuthorization(unittest.TestCase):
|
2024-11-15 15:06:55 +00:00
|
|
|
if utils.USE_THP:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
if __debug__:
|
|
|
|
thp_common.suppres_debug_log()
|
|
|
|
thp_common.prepare_context()
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
context.CURRENT_CONTEXT = CodecContext(None, bytearray(64))
|
|
|
|
super().__init__()
|
2020-08-03 15:28:20 +00:00
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
coin = coins.by_name("Bitcoin")
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.msg_auth = AuthorizeCoinJoin(
|
|
|
|
coordinator="www.example.com",
|
2022-03-15 18:29:39 +00:00
|
|
|
max_rounds=3,
|
|
|
|
max_coordinator_fee_rate=int(0.3 * 10**8),
|
|
|
|
max_fee_per_kvbyte=7000,
|
2020-08-03 15:28:20 +00:00
|
|
|
address_n=[H_(84), H_(0), H_(0)],
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
|
|
|
)
|
|
|
|
|
2021-03-30 08:57:07 +00:00
|
|
|
self.authorization = CoinJoinAuthorization(self.msg_auth)
|
2024-11-15 15:06:55 +00:00
|
|
|
if not utils.USE_THP:
|
|
|
|
storage.cache_codec.start_session()
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
def test_ownership_proof_account_depth_mismatch(self):
|
|
|
|
# Account depth mismatch.
|
|
|
|
msg = GetOwnershipProof(
|
|
|
|
address_n=[H_(84), H_(0), H_(0), 1],
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
|
|
|
user_confirmation=True,
|
2023-06-28 10:46:29 +00:00
|
|
|
commitment_data=b"\x0fwww.example.com"
|
|
|
|
+ int.to_bytes(1, _ROUND_ID_LEN, "big"),
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertFalse(self.authorization.check_get_ownership_proof(msg))
|
|
|
|
|
|
|
|
def test_ownership_proof_account_path_mismatch(self):
|
|
|
|
# Account path mismatch.
|
|
|
|
msg = GetOwnershipProof(
|
|
|
|
address_n=[H_(49), H_(0), H_(0), 1, 2],
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
|
|
|
user_confirmation=True,
|
2023-06-28 10:46:29 +00:00
|
|
|
commitment_data=b"\x0fwww.example.com"
|
|
|
|
+ int.to_bytes(1, _ROUND_ID_LEN, "big"),
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertFalse(self.authorization.check_get_ownership_proof(msg))
|
|
|
|
|
|
|
|
def test_ownership_proof_coordinator_mismatch(self):
|
|
|
|
# Coordinator name mismatch.
|
|
|
|
msg = GetOwnershipProof(
|
|
|
|
address_n=[H_(84), H_(0), H_(0), 1, 2],
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
|
|
|
user_confirmation=True,
|
2023-06-28 10:46:29 +00:00
|
|
|
commitment_data=b"\x0fwww.example.org"
|
|
|
|
+ int.to_bytes(1, _ROUND_ID_LEN, "big"),
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertFalse(self.authorization.check_get_ownership_proof(msg))
|
|
|
|
|
2022-02-10 15:13:25 +00:00
|
|
|
def test_ownership_proof_wrong_coordinator_length(self):
|
2020-08-03 15:28:20 +00:00
|
|
|
msg = GetOwnershipProof(
|
|
|
|
address_n=[H_(84), H_(0), H_(0), 1, 2],
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
|
|
|
user_confirmation=True,
|
2023-06-28 10:46:29 +00:00
|
|
|
commitment_data=b"\x0ewww.example.com"
|
|
|
|
+ int.to_bytes(1, _ROUND_ID_LEN - 1, "big"),
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertFalse(self.authorization.check_get_ownership_proof(msg))
|
|
|
|
|
|
|
|
msg = GetOwnershipProof(
|
|
|
|
address_n=[H_(84), H_(0), H_(0), 1, 2],
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
|
|
|
user_confirmation=True,
|
2023-06-28 10:46:29 +00:00
|
|
|
commitment_data=b"\x10www.example.com"
|
|
|
|
+ int.to_bytes(1, _ROUND_ID_LEN + 1, "big"),
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertFalse(self.authorization.check_get_ownership_proof(msg))
|
|
|
|
|
|
|
|
def test_authorize_ownership_proof(self):
|
|
|
|
|
|
|
|
msg = GetOwnershipProof(
|
|
|
|
address_n=[H_(84), H_(0), H_(0), 1, 2],
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
script_type=InputScriptType.SPENDWITNESS,
|
|
|
|
user_confirmation=True,
|
2023-06-28 10:46:29 +00:00
|
|
|
commitment_data=b"\x0fwww.example.com"
|
|
|
|
+ int.to_bytes(1, _ROUND_ID_LEN, "big"),
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertTrue(self.authorization.check_get_ownership_proof(msg))
|
|
|
|
|
|
|
|
def test_approve_sign_tx(self):
|
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
msg = SignTx(
|
|
|
|
outputs_count=10,
|
|
|
|
inputs_count=21,
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
lock_time=0,
|
|
|
|
)
|
2020-08-03 15:28:20 +00:00
|
|
|
|
2022-03-15 18:29:39 +00:00
|
|
|
self.assertTrue(self.authorization.approve_sign_tx(msg))
|
|
|
|
self.assertTrue(self.authorization.approve_sign_tx(msg))
|
|
|
|
self.assertTrue(self.authorization.approve_sign_tx(msg))
|
|
|
|
self.assertFalse(self.authorization.approve_sign_tx(msg))
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
if __name__ == "__main__":
|
2020-08-03 15:28:20 +00:00
|
|
|
unittest.main()
|