Skip to content

Commit

Permalink
fixup! feat(cardano): add key hash stake credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmisiak committed Nov 22, 2021
1 parent fae1459 commit 9c46518
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
2 changes: 2 additions & 0 deletions core/src/apps/cardano/helpers/bech32.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
HRP_SCRIPT_HASH = "script"
HRP_KEY_HASH = "addr_vkh"
HRP_SHARED_KEY_HASH = "addr_shared_vkh"
HRP_STAKE_KEY_HASH = "stake_vkh"
HRP_SHARED_STAKE_KEY_HASH = "stake_shared_vkh"


def encode(hrp: str, data: bytes) -> str:
Expand Down
36 changes: 23 additions & 13 deletions core/src/apps/cardano/helpers/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Credential:
"""

type_name: str
is_stake_credential: bool
address_type: CardanoAddressType
path: list[int]
key_hash: bytes | None
Expand All @@ -36,13 +37,15 @@ class Credential:
def __init__(
self,
type_name: str,
is_stake_credential: bool,
address_type: CardanoAddressType,
path: list[int],
key_hash: bytes | None,
script_hash: bytes | None,
pointer: CardanoBlockchainPointerType | None,
):
self.type_name = type_name
self.is_stake_credential = is_stake_credential
self.address_type = address_type
self.path = path
self.key_hash = key_hash
Expand All @@ -55,12 +58,13 @@ def payment_credential(
) -> "Credential":
address_type = address_params.address_type
credential = cls(
"payment",
address_type,
address_params.address_n,
None,
address_params.script_payment_hash,
None,
type_name="payment",
is_stake_credential=False,
address_type=address_type,
path=address_params.address_n,
key_hash=None,
script_hash=address_params.script_payment_hash,
pointer=None,
)

if address_type in (
Expand Down Expand Up @@ -98,12 +102,13 @@ def stake_credential(
) -> "Credential":
address_type = address_params.address_type
credential = cls(
"stake",
address_type,
address_params.address_n_staking,
address_params.staking_key_hash,
address_params.script_staking_hash,
address_params.certificate_pointer,
type_name="stake",
is_stake_credential=True,
address_type=address_type,
path=address_params.address_n_staking,
key_hash=address_params.staking_key_hash,
script_hash=address_params.script_staking_hash,
pointer=address_params.certificate_pointer,
)

if address_type == CardanoAddressType.BASE:
Expand Down Expand Up @@ -183,7 +188,12 @@ def format(self) -> list[PropertyType]:
if self.path:
return [(None, address_n_to_str(self.path))]
elif self.key_hash:
return [(None, format_key_hash(self.key_hash, False))]
formatted_key_hash = format_key_hash(
self.key_hash,
is_shared_key=False,
is_stake_key=self.is_stake_credential,
)
return [(None, formatted_key_hash)]
elif self.script_hash:
return [(None, format_script_hash(self.script_hash))]
elif self.pointer:
Expand Down
9 changes: 7 additions & 2 deletions core/src/apps/cardano/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ def format_script_hash(script_hash: bytes) -> str:
return bech32.encode(bech32.HRP_SCRIPT_HASH, script_hash)


def format_key_hash(key_hash: bytes, is_shared_key: bool) -> str:
hrp = bech32.HRP_SHARED_KEY_HASH if is_shared_key else bech32.HRP_KEY_HASH
def format_key_hash(key_hash: bytes, is_shared_key: bool, is_stake_key: bool) -> str:
hrp = {
(False, False): bech32.HRP_KEY_HASH,
(True, False): bech32.HRP_SHARED_KEY_HASH,
(False, True): bech32.HRP_STAKE_KEY_HASH,
(True, True): bech32.HRP_SHARED_STAKE_KEY_HASH,
}[(is_shared_key, is_stake_key)]
return bech32.encode(hrp, key_hash)


Expand Down
17 changes: 14 additions & 3 deletions core/src/apps/cardano/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ async def show_native_script(
if script.type == CardanoNativeScriptType.PUB_KEY:
assert script.key_hash is not None or script.key_path # validate_script
if script.key_hash:
props.append((None, format_key_hash(script.key_hash, True)))
formatted_key_hash = format_key_hash(
script.key_hash, is_shared_key=True, is_stake_key=False
)
props.append((None, formatted_key_hash))
elif script.key_path:
props.append((address_n_to_str(script.key_path), None))
elif script.type == CardanoNativeScriptType.N_OF_K:
Expand Down Expand Up @@ -629,7 +632,10 @@ def _format_stake_credential(
address_n_to_str(to_account_path(path)),
)
elif key_hash:
return ("for key hash:", format_key_hash(key_hash, False))
return (
"for key hash:",
format_key_hash(key_hash, is_shared_key=False, is_stake_key=True),
)
elif script_hash:
return ("for script:", format_script_hash(script_hash))
else:
Expand Down Expand Up @@ -731,7 +737,12 @@ async def confirm_required_signer(
required_signer.key_hash is not None or required_signer.key_path
) # _validate_required_signer
if required_signer.key_hash:
prop = ("Required signer", format_key_hash(required_signer.key_hash, False))
prop = (
"Required signer",
format_key_hash(
required_signer.key_hash, is_shared_key=False, is_stake_key=False
),
)
elif required_signer.key_path:
prop = ("Required signer", address_n_to_str(required_signer.key_path))

Expand Down

0 comments on commit 9c46518

Please sign in to comment.