Skip to content

Commit 0027ea9

Browse files
fixup! fixup! feat(cardano): add support for script addresses derivation
1 parent 248328f commit 0027ea9

File tree

6 files changed

+63
-73
lines changed

6 files changed

+63
-73
lines changed

core/src/all_modules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@
456456
import apps.cardano.helpers.account_path_check
457457
apps.cardano.helpers.bech32
458458
import apps.cardano.helpers.bech32
459-
apps.cardano.helpers.credential_params
460-
import apps.cardano.helpers.credential_params
459+
apps.cardano.helpers.credential
460+
import apps.cardano.helpers.credential
461461
apps.cardano.helpers.hash_builder_collection
462462
import apps.cardano.helpers.hash_builder_collection
463463
apps.cardano.helpers.network_ids

core/src/apps/cardano/get_address.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from . import seed
55
from .address import derive_human_readable_address, validate_address_parameters
6-
from .helpers.credential_params import CredentialParams, should_show_address_credentials
6+
from .helpers.credential import Credential, should_show_address_credentials
77
from .layout import show_cardano_address, show_credentials
88
from .sign_tx import validate_network_info
99

@@ -47,8 +47,8 @@ async def _display_address(
4747
if should_show_address_credentials(address_parameters):
4848
await show_credentials(
4949
ctx,
50-
CredentialParams.payment_params(address_parameters),
51-
CredentialParams.stake_params(address_parameters),
50+
Credential.payment_credential(address_parameters),
51+
Credential.stake_credential(address_parameters),
5252
)
5353

5454
await show_cardano_address(ctx, address_parameters, address, protocol_magic)

core/src/apps/cardano/helpers/credential_params.py renamed to core/src/apps/cardano/helpers/credential.py

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
from trezor.ui.layouts import PropertyType
1313

1414

15-
class CredentialParams:
15+
class Credential:
1616
"""
17-
Serves mainly as a wrapper object for credential parameters (so that they don't have to be
17+
Serves mainly as a wrapper object for credentials (so that they don't have to be
1818
passed into functions separately) which also determines all properties that should be shown
1919
as warnings.
2020
Also contains functions which simplify displaying the credential.
@@ -50,11 +50,11 @@ def __init__(
5050
self.pointer = pointer
5151

5252
@classmethod
53-
def payment_params(
53+
def payment_credential(
5454
cls, address_params: CardanoAddressParametersType
55-
) -> "CredentialParams":
55+
) -> "Credential":
5656
address_type = address_params.address_type
57-
credential_params = CredentialParams(
57+
credential = cls(
5858
"payment",
5959
address_type,
6060
address_params.address_n,
@@ -71,33 +71,33 @@ def payment_params(
7171
CardanoAddressType.BYRON,
7272
):
7373
if not SCHEMA_PAYMENT.match(address_params.address_n):
74-
credential_params.is_unusual_path = True
74+
credential.is_unusual_path = True
7575

7676
elif address_type in (
7777
CardanoAddressType.BASE_SCRIPT_KEY,
7878
CardanoAddressType.BASE_SCRIPT_SCRIPT,
7979
CardanoAddressType.POINTER_SCRIPT,
8080
CardanoAddressType.ENTERPRISE_SCRIPT,
8181
):
82-
credential_params.is_other_warning = True
82+
credential.is_other_warning = True
8383

8484
elif address_type in (
8585
CardanoAddressType.REWARD,
8686
CardanoAddressType.REWARD_SCRIPT,
8787
):
88-
credential_params.is_reward = True
88+
credential.is_reward = True
8989

9090
else:
91-
raise ValueError("Invalid address type")
91+
raise RuntimeError # we didn't cover all address types
9292

93-
return credential_params
93+
return credential
9494

9595
@classmethod
96-
def stake_params(
96+
def stake_credential(
9797
cls, address_params: CardanoAddressParametersType
98-
) -> "CredentialParams":
98+
) -> "Credential":
9999
address_type = address_params.address_type
100-
credential_params = CredentialParams(
100+
credential = cls(
101101
"stake",
102102
address_type,
103103
address_params.address_n_staking,
@@ -108,50 +108,50 @@ def stake_params(
108108

109109
if address_type == CardanoAddressType.BASE:
110110
if address_params.staking_key_hash:
111-
credential_params.is_other_warning = True
111+
credential.is_other_warning = True
112112
else:
113113
if not SCHEMA_STAKING.match(address_params.address_n_staking):
114-
credential_params.is_unusual_path = True
114+
credential.is_unusual_path = True
115115
if not _do_base_address_credentials_match(
116116
address_params.address_n,
117117
address_params.address_n_staking,
118118
):
119-
credential_params.is_mismatch = True
119+
credential.is_mismatch = True
120120

121121
elif address_type == CardanoAddressType.BASE_SCRIPT_KEY:
122122
if address_params.address_n_staking and not SCHEMA_STAKING.match(
123123
address_params.address_n_staking
124124
):
125-
credential_params.is_unusual_path = True
125+
credential.is_unusual_path = True
126126

127127
elif address_type in (
128128
CardanoAddressType.POINTER,
129129
CardanoAddressType.POINTER_SCRIPT,
130130
):
131-
credential_params.is_other_warning = True
131+
credential.is_other_warning = True
132132

133133
elif address_type == CardanoAddressType.REWARD:
134134
if not SCHEMA_STAKING.match(address_params.address_n_staking):
135-
credential_params.is_unusual_path = True
135+
credential.is_unusual_path = True
136136

137137
elif address_type in (
138138
CardanoAddressType.BASE_KEY_SCRIPT,
139139
CardanoAddressType.BASE_SCRIPT_SCRIPT,
140140
CardanoAddressType.REWARD_SCRIPT,
141141
):
142-
credential_params.is_other_warning = True
142+
credential.is_other_warning = True
143143

144144
elif address_type in (
145145
CardanoAddressType.ENTERPRISE,
146146
CardanoAddressType.ENTERPRISE_SCRIPT,
147147
CardanoAddressType.BYRON,
148148
):
149-
credential_params.is_no_staking = True
149+
credential.is_no_staking = True
150150

151151
else:
152-
raise ValueError("Invalid address type")
152+
raise RuntimeError # we didn't cover all address types
153153

154-
return credential_params
154+
return credential
155155

156156
def should_warn(self) -> bool:
157157
return any(
@@ -164,17 +164,8 @@ def should_warn(self) -> bool:
164164
)
165165
)
166166

167-
def get_credential(self) -> list[int] | bytes | CardanoBlockchainPointerType | None:
168-
if self.path:
169-
return self.path
170-
elif self.key_hash:
171-
return self.key_hash
172-
elif self.script_hash:
173-
return self.script_hash
174-
elif self.pointer:
175-
return self.pointer
176-
else:
177-
return None
167+
def is_set(self) -> bool:
168+
return any((self.path, self.key_hash, self.script_hash, self.pointer))
178169

179170
def get_title(self) -> str:
180171
if self.path:

core/src/apps/cardano/layout.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
)
4949

5050
from trezor.ui.layouts import PropertyType
51-
from .helpers.credential_params import CredentialParams
51+
from .helpers.credential import Credential
5252

5353

5454
ADDRESS_TYPE_NAMES = {
@@ -247,62 +247,61 @@ async def confirm_sending_token(
247247

248248
async def show_credentials(
249249
ctx: wire.Context,
250-
payment_credential_params: CredentialParams,
251-
stake_credential_params: CredentialParams,
250+
payment_credential: Credential,
251+
stake_credential: Credential,
252252
is_change_output: bool = False,
253253
) -> None:
254-
await _show_credential(ctx, payment_credential_params, is_change_output)
255-
await _show_credential(ctx, stake_credential_params, is_change_output)
254+
await _show_credential(ctx, payment_credential, is_change_output)
255+
await _show_credential(ctx, stake_credential, is_change_output)
256256

257257

258258
async def _show_credential(
259259
ctx: wire.Context,
260-
credential_params: CredentialParams,
260+
credential: Credential,
261261
is_change_output: bool = False,
262262
) -> None:
263263
if is_change_output:
264264
title = "Confirm transaction"
265265
else:
266-
title = "%s address" % ADDRESS_TYPE_NAMES[credential_params.address_type]
266+
title = "%s address" % ADDRESS_TYPE_NAMES[credential.address_type]
267267

268268
props: list[PropertyType] = []
269269

270-
credential = credential_params.get_credential()
271270
# Credential can be empty in case of enterprise address stake credential
272271
# and reward address payment credential. In that case we don't want to
273272
# show some of the "props".
274-
if credential:
273+
if credential.is_set():
275274
if is_change_output:
276275
address_usage = "Change address"
277276
else:
278277
address_usage = "Address"
279278

280-
credential_title = credential_params.get_title()
279+
credential_title = credential.get_title()
281280
props.append(
282281
(
283282
"%s %s credential is a %s:"
284-
% (address_usage, credential_params.type_name, credential_title),
283+
% (address_usage, credential.type_name, credential_title),
285284
None,
286285
)
287286
)
288-
props.extend(credential_params.format())
287+
props.extend(credential.format())
289288

290-
if credential_params.is_unusual_path:
289+
if credential.is_unusual_path:
291290
props.append((None, "Path is unusual."))
292-
if credential_params.is_mismatch:
291+
if credential.is_mismatch:
293292
props.append((None, "Credential doesn't match payment credential."))
294-
if credential_params.is_reward:
293+
if credential.is_reward:
295294
props.append(("Address is a reward address.", None))
296-
if credential_params.is_no_staking:
295+
if credential.is_no_staking:
297296
props.append(
298297
(
299298
"%s address - no staking rewards."
300-
% ADDRESS_TYPE_NAMES[credential_params.address_type],
299+
% ADDRESS_TYPE_NAMES[credential.address_type],
301300
None,
302301
)
303302
)
304303

305-
if credential_params.should_warn():
304+
if credential.should_warn():
306305
icon = ui.ICON_WRONG
307306
icon_color = ui.RED
308307
else:

core/src/apps/cardano/sign_tx.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
protocol_magics,
7171
)
7272
from .helpers.account_path_check import AccountPathChecker
73-
from .helpers.credential_params import CredentialParams, should_show_address_credentials
73+
from .helpers.credential import Credential, should_show_address_credentials
7474
from .helpers.hash_builder_collection import HashBuilderDict, HashBuilderList
7575
from .helpers.paths import (
7676
CERTIFICATE_PATH_NAME,
@@ -792,8 +792,8 @@ async def _show_output(
792792

793793
await show_credentials(
794794
ctx,
795-
CredentialParams.payment_params(address_parameters),
796-
CredentialParams.stake_params(address_parameters),
795+
Credential.payment_credential(address_parameters),
796+
Credential.stake_credential(address_parameters),
797797
is_change_output=True,
798798
)
799799

@@ -1044,8 +1044,8 @@ def _fail_if_strict_and_unusual(
10441044
if not safety_checks.is_strict():
10451045
return
10461046

1047-
if CredentialParams.payment_params(address_parameters).is_unusual_path:
1047+
if Credential.payment_credential(address_parameters).is_unusual_path:
10481048
raise wire.DataError("Invalid %s" % CHANGE_OUTPUT_PATH_NAME.lower())
10491049

1050-
if CredentialParams.stake_params(address_parameters).is_unusual_path:
1050+
if Credential.stake_credential(address_parameters).is_unusual_path:
10511051
raise wire.DataError("Invalid %s" % CHANGE_OUTPUT_STAKING_PATH_NAME.lower())

core/tests/test_apps.cardano.credential_params.py renamed to core/tests/test_apps.cardano.credential.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from common import *
22

3-
from apps.cardano.helpers.credential_params import CredentialParams
3+
from apps.cardano.helpers.credential import Credential
44
from apps.common.paths import HARDENED
55
from trezor.enums import CardanoAddressType
66
from trezor.messages import CardanoAddressParametersType, CardanoBlockchainPointerType
@@ -239,26 +239,26 @@ def _create_flags(
239239
]
240240

241241

242-
def _get_flags(credential_params: CredentialParams) -> tuple[bool, ...]:
242+
def _get_flags(credential: Credential) -> tuple[bool, ...]:
243243
return (
244-
credential_params.is_reward,
245-
credential_params.is_no_staking,
246-
credential_params.is_mismatch,
247-
credential_params.is_unusual_path,
248-
credential_params.is_other_warning,
244+
credential.is_reward,
245+
credential.is_no_staking,
246+
credential.is_mismatch,
247+
credential.is_unusual_path,
248+
credential.is_other_warning,
249249
)
250250

251251

252252
@unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin")
253-
class TestCardanoCredentialParams(unittest.TestCase):
254-
def test_credential_params_flags(self):
253+
class TestCardanoCredential(unittest.TestCase):
254+
def test_credential_flags(self):
255255
for (
256256
address_parameters,
257257
expected_payment_flags,
258258
expected_stake_flags,
259259
) in ADDRESS_PARAMETERS_CASES:
260-
payment_credential = CredentialParams.payment_params(address_parameters)
261-
stake_credential = CredentialParams.stake_params(address_parameters)
260+
payment_credential = Credential.payment_credential(address_parameters)
261+
stake_credential = Credential.stake_credential(address_parameters)
262262
self.assertEqual(_get_flags(payment_credential), expected_payment_flags)
263263
self.assertEqual(_get_flags(stake_credential), expected_stake_flags)
264264

0 commit comments

Comments
 (0)