|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from bip_utils import Bip32KeyIndex, Bip32Path, Bip32Utils |
| 4 | +from ledgerwallet.client import LedgerClient |
| 5 | + |
| 6 | +from starknet_py.constants import ( |
| 7 | + EIP_2645_PATH_LENGTH, |
| 8 | + EIP_2645_PURPOSE, |
| 9 | + PUBLIC_KEY_RESPONSE_LENGTH, |
| 10 | + SIGNATURE_RESPONSE_LENGTH, |
| 11 | + STARKNET_CLA, |
| 12 | + VERSION_RESPONSE_LENGTH, |
| 13 | +) |
| 14 | +from starknet_py.net.models import AccountTransaction |
| 15 | +from starknet_py.net.models.chains import ChainId |
| 16 | +from starknet_py.net.signer import BaseSigner |
| 17 | +from starknet_py.utils.typed_data import TypedData |
| 18 | + |
| 19 | + |
| 20 | +class LedgerStarknetApp: |
| 21 | + def __init__(self): |
| 22 | + self.client: LedgerClient = LedgerClient(cla=STARKNET_CLA) |
| 23 | + |
| 24 | + @property |
| 25 | + def version(self) -> str: |
| 26 | + """ |
| 27 | + Get the Ledger app version. |
| 28 | +
|
| 29 | + :return: Version string. |
| 30 | + """ |
| 31 | + response = self.client.apdu_exchange(ins=0) |
| 32 | + if len(response) != VERSION_RESPONSE_LENGTH: |
| 33 | + raise ValueError( |
| 34 | + f"Unexpected response length (expected: {VERSION_RESPONSE_LENGTH}, actual: {len(response)}" |
| 35 | + ) |
| 36 | + major, minor, patch = list(response) |
| 37 | + return f"{major}.{minor}.{patch}" |
| 38 | + |
| 39 | + def get_public_key( |
| 40 | + self, derivation_path: Bip32Path, device_confirmation: bool = False |
| 41 | + ) -> int: |
| 42 | + """ |
| 43 | + Get public key for the given derivation path. |
| 44 | +
|
| 45 | + :param derivation_path: Derivation path of the account. |
| 46 | + :param device_confirmation: Whether to display confirmation on the device for extra security. |
| 47 | + :return: Public key. |
| 48 | + """ |
| 49 | + |
| 50 | + data = _derivation_path_to_bytes(derivation_path) |
| 51 | + response = self.client.apdu_exchange( |
| 52 | + ins=1, |
| 53 | + data=data, |
| 54 | + p1=int(device_confirmation), |
| 55 | + p2=0, |
| 56 | + ) |
| 57 | + |
| 58 | + if len(response) != PUBLIC_KEY_RESPONSE_LENGTH: |
| 59 | + raise ValueError( |
| 60 | + f"Unexpected response length (expected: {PUBLIC_KEY_RESPONSE_LENGTH}, actual: {len(response)}" |
| 61 | + ) |
| 62 | + |
| 63 | + public_key = int.from_bytes(response[1:33], byteorder="big") |
| 64 | + return public_key |
| 65 | + |
| 66 | + def sign_hash(self, hash_val: int) -> List[int]: |
| 67 | + """ |
| 68 | + Request a signature for a raw hash with the given derivation path. |
| 69 | + Currently, the Ledger app only supports blind signing raw hashes. |
| 70 | +
|
| 71 | + :param hash_val: Hash to sign. |
| 72 | + :return: Signature as a list of two integers. |
| 73 | + """ |
| 74 | + |
| 75 | + # for some reason the Ledger app expects the data to be left shifted by 4 bits |
| 76 | + shifted_int = hash_val << 4 |
| 77 | + shifted_bytes = shifted_int.to_bytes(32, byteorder="big") |
| 78 | + |
| 79 | + response = self.client.apdu_exchange( |
| 80 | + ins=0x02, |
| 81 | + data=shifted_bytes, |
| 82 | + p1=0x01, |
| 83 | + p2=0x00, |
| 84 | + ) |
| 85 | + |
| 86 | + if ( |
| 87 | + len(response) != SIGNATURE_RESPONSE_LENGTH + 1 |
| 88 | + or response[0] != SIGNATURE_RESPONSE_LENGTH |
| 89 | + ): |
| 90 | + raise ValueError( |
| 91 | + f"Unexpected response length (expected: {SIGNATURE_RESPONSE_LENGTH}, actual: {len(response)}" |
| 92 | + ) |
| 93 | + |
| 94 | + r, s = int.from_bytes(response[1:33], byteorder="big"), int.from_bytes( |
| 95 | + response[33:65], byteorder="big" |
| 96 | + ) |
| 97 | + return [r, s] |
| 98 | + |
| 99 | + |
| 100 | +class LedgerSigner(BaseSigner): |
| 101 | + def __init__(self, derivation_path_str: str, chain_id: ChainId): |
| 102 | + """ |
| 103 | + :param derivation_path_str: Derivation path string of the account. |
| 104 | + :param chain_id: ChainId of the chain. |
| 105 | + """ |
| 106 | + |
| 107 | + self.app: LedgerStarknetApp = LedgerStarknetApp() |
| 108 | + self.derivation_path: Bip32Path = _parse_derivation_path_str( |
| 109 | + derivation_path_str |
| 110 | + ) |
| 111 | + self.chain_id: ChainId = chain_id |
| 112 | + |
| 113 | + @property |
| 114 | + def public_key(self) -> int: |
| 115 | + return self.app.get_public_key(derivation_path=self.derivation_path) |
| 116 | + |
| 117 | + def sign_transaction(self, transaction: AccountTransaction) -> List[int]: |
| 118 | + tx_hash = transaction.calculate_hash(self.chain_id) |
| 119 | + return self.app.sign_hash(hash_val=tx_hash) |
| 120 | + |
| 121 | + def sign_message(self, typed_data: TypedData, account_address: int) -> List[int]: |
| 122 | + msg_hash = typed_data.message_hash(account_address) |
| 123 | + return self.app.sign_hash(hash_val=msg_hash) |
| 124 | + |
| 125 | + |
| 126 | +def _parse_derivation_path_str(derivation_path_str: str) -> Bip32Path: |
| 127 | + """ |
| 128 | + Parse a derivation path string to a Bip32Path object. |
| 129 | +
|
| 130 | + :param derivation_path_str: Derivation path string. |
| 131 | + :return: Bip32Path object. |
| 132 | + """ |
| 133 | + if not derivation_path_str: |
| 134 | + raise ValueError("Empty derivation path") |
| 135 | + |
| 136 | + path_parts = derivation_path_str.lstrip("m/").split("/") |
| 137 | + path_elements = [ |
| 138 | + Bip32KeyIndex( |
| 139 | + Bip32Utils.HardenIndex(int(part[:-1])) if part.endswith("'") else int(part) |
| 140 | + ) |
| 141 | + for part in path_parts |
| 142 | + ] |
| 143 | + |
| 144 | + if len(path_elements) != EIP_2645_PATH_LENGTH: |
| 145 | + raise ValueError(f"Derivation path is not {EIP_2645_PATH_LENGTH}-level long") |
| 146 | + if path_elements[0] != EIP_2645_PURPOSE: |
| 147 | + raise ValueError("Derivation path is not prefixed with m/2645.") |
| 148 | + |
| 149 | + return Bip32Path(path_elements) |
| 150 | + |
| 151 | + |
| 152 | +def _derivation_path_to_bytes(derivation_path: Bip32Path) -> bytes: |
| 153 | + """ |
| 154 | + Convert a derivation path to a bytes object. |
| 155 | +
|
| 156 | + :param derivation_path: Derivation path. |
| 157 | + :return: Bytes object. |
| 158 | + """ |
| 159 | + return b"".join(index.ToBytes() for index in derivation_path) |
0 commit comments