refactor(core/cardano): introduce derive_public_key to simplify pub key derivation

pull/1582/head
gabrielkerekes 3 years ago committed by matejcik
parent a3d0016a2f
commit 6f59892824

@ -2,12 +2,10 @@ from trezor import wire
from trezor.crypto import base58, hashlib
from trezor.messages import CardanoAddressType
from apps.common.seed import remove_ed25519_prefix
from .byron_address import derive_byron_address, validate_byron_address
from .helpers import INVALID_ADDRESS, NETWORK_MISMATCH, bech32, network_ids
from .helpers.paths import SCHEMA_STAKING_ANY_ACCOUNT
from .helpers.utils import variable_length_encode
from .helpers.utils import derive_public_key, variable_length_encode
from .seed import is_byron_path, is_shelley_path
if False:
@ -146,8 +144,7 @@ def _get_address_network_id(address: bytes) -> int:
def get_public_key_hash(keychain: seed.Keychain, path: list[int]) -> bytes:
node = keychain.derive(path)
public_key = remove_ed25519_prefix(node.public_key())
public_key = derive_public_key(keychain, path)
return hashlib.blake2b(data=public_key, outlen=28).digest()

@ -4,11 +4,11 @@ from trezor.messages import CardanoAddressType
from apps.common import cbor
from ..common.seed import remove_ed25519_prefix
from .address import derive_address_bytes, derive_human_readable_address
from .helpers import INVALID_AUXILIARY_DATA, bech32
from .helpers.bech32 import HRP_JORMUN_PUBLIC_KEY
from .helpers.paths import SCHEMA_STAKING_ANY_ACCOUNT
from .helpers.utils import derive_public_key
from .layout import confirm_catalyst_registration, show_auxiliary_data_hash
if False:
@ -172,8 +172,9 @@ def _cborize_catalyst_registration(
protocol_magic: int,
network_id: int,
) -> CatalystRegistration:
staking_node = keychain.derive(catalyst_registration_parameters.staking_path)
staking_key = remove_ed25519_prefix(staking_node.public_key())
staking_key = derive_public_key(
keychain, catalyst_registration_parameters.staking_path
)
catalyst_registration_payload: CatalystRegistrationPayload = {
1: catalyst_registration_parameters.voting_public_key,

@ -2,12 +2,11 @@ from trezor import log
from trezor.crypto import crc, hashlib
from apps.common import cbor
from apps.common.seed import remove_ed25519_prefix
from .helpers import INVALID_ADDRESS, NETWORK_MISMATCH, protocol_magics
from .helpers.utils import derive_public_key
if False:
from trezor.crypto import bip32
from . import seed
PROTOCOL_MAGIC_KEY = 2
@ -30,11 +29,9 @@ def _encode_address_raw(address_data_encoded: bytes) -> bytes:
def derive_byron_address(
keychain: seed.Keychain, path: list, protocol_magic: int
) -> bytes:
node = keychain.derive(path)
address_attributes = get_address_attributes(protocol_magic)
address_root = _get_address_root(node, address_attributes)
address_root = _get_address_root(keychain, path, address_attributes)
address_type = 0
address_data = [address_root, address_attributes, address_type]
address_data_encoded = cbor.encode(address_data)
@ -119,6 +116,8 @@ def _address_hash(data: list) -> bytes:
return res
def _get_address_root(node: bip32.HDNode, address_attributes: dict) -> bytes:
extpubkey = remove_ed25519_prefix(node.public_key()) + node.chain_code()
def _get_address_root(
keychain: seed.Keychain, path: list[int], address_attributes: dict
) -> bytes:
extpubkey = derive_public_key(keychain, path, extended=True)
return _address_hash([0, [0, extpubkey], address_attributes])

@ -6,10 +6,10 @@ from trezor.messages.HDNodeType import HDNodeType
from trezor.ui.layouts import show_pubkey
from apps.common import paths
from apps.common.seed import remove_ed25519_prefix
from . import seed
from .helpers.paths import SCHEMA_PUBKEY
from .helpers.utils import derive_public_key
if False:
from trezor.messages.CardanoGetPublicKey import CardanoGetPublicKey
@ -44,7 +44,7 @@ def _get_public_key(
) -> CardanoPublicKey:
node = keychain.derive(derivation_path)
public_key = hexlify(remove_ed25519_prefix(node.public_key())).decode()
public_key = hexlify(derive_public_key(keychain, derivation_path)).decode()
chain_code = hexlify(node.chain_code()).decode()
xpub_key = public_key + chain_code
@ -53,7 +53,7 @@ def _get_public_key(
child_num=node.child_num(),
fingerprint=node.fingerprint(),
chain_code=node.chain_code(),
public_key=remove_ed25519_prefix(node.public_key()),
public_key=derive_public_key(keychain, derivation_path),
)
return CardanoPublicKey(node=node_type, xpub=xpub_key)

@ -1,9 +1,13 @@
from trezor.crypto import hashlib
from apps.cardano.helpers.paths import ACCOUNT_PATH_INDEX, unharden
from apps.common.seed import remove_ed25519_prefix
from . import bech32
if False:
from .. import seed
def variable_length_encode(number: int) -> bytes:
"""
@ -58,3 +62,11 @@ def format_asset_fingerprint(policy_id: bytes, asset_name_bytes: bytes) -> str:
).digest()
return bech32.encode("asset", fingerprint)
def derive_public_key(
keychain: seed.Keychain, path: list[int], extended: bool = False
) -> bytes:
node = keychain.derive(path)
public_key = remove_ed25519_prefix(node.public_key())
return public_key if not extended else public_key + node.chain_code()

@ -9,7 +9,6 @@ from trezor.messages.CardanoSignedTxChunkAck import CardanoSignedTxChunkAck
from apps.common import cbor, safety_checks
from apps.common.paths import validate_path
from apps.common.seed import remove_ed25519_prefix
from . import seed
from .address import (
@ -49,7 +48,7 @@ from .helpers.paths import (
SCHEMA_STAKING,
SCHEMA_STAKING_ANY_ACCOUNT,
)
from .helpers.utils import to_account_path
from .helpers.utils import derive_public_key, to_account_path
from .layout import (
confirm_certificate,
confirm_sending,
@ -510,7 +509,7 @@ def _cborize_shelley_witness(
signature = ed25519.sign_ext(
node.private_key(), node.private_key_ext(), tx_body_hash
)
public_key = remove_ed25519_prefix(node.public_key())
public_key = derive_public_key(keychain, path)
return public_key, signature
@ -532,7 +531,7 @@ def _cborize_byron_witnesses(
for path in paths:
node = keychain.derive(list(path))
public_key = remove_ed25519_prefix(node.public_key())
public_key = derive_public_key(keychain, list(path))
signature = ed25519.sign_ext(
node.private_key(), node.private_key_ext(), tx_body_hash
)

Loading…
Cancel
Save