Skip to content

Commit

Permalink
Merge pull request #1254 from chainapsis/delivan/fix-starknet-ledger-…
Browse files Browse the repository at this point in the history
…issue-in-qa-2

[QA] Starknet Ledger app support - 2
  • Loading branch information
Thunnini authored Dec 3, 2024
2 parents 372b7f4 + 157c876 commit 52deeee
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 39 deletions.
7 changes: 4 additions & 3 deletions apps/extension/src/pages/sign/components/ledger-guide-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { FormattedMessage, useIntl } from "react-intl";
export const LedgerGuideBox: FunctionComponent<{
data: {
keyInsensitive: PlainObject;
isEthereum: boolean;
isEthereum?: boolean;
isStarknet?: boolean;
};
isLedgerInteracting: boolean;
ledgerInteractingError: Error | undefined;
Expand Down Expand Up @@ -172,7 +173,7 @@ export const LedgerGuideBox: FunctionComponent<{
app = "Secret";
}

if (data?.isEthereum) {
if (data.isEthereum) {
if (appData["Ethereum"]) {
app = "Ethereum";
} else {
Expand All @@ -193,7 +194,7 @@ export const LedgerGuideBox: FunctionComponent<{
}
}

if (appData["Starknet"]) {
if (data.isStarknet && appData["Starknet"]) {
app = "Starknet";
}

Expand Down
90 changes: 57 additions & 33 deletions apps/extension/src/pages/sign/utils/handle-starknet-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export const connectAndSignDeployAccountTxWithLedger = async (
transaction: DeployAccountSignerDetails;
signature: string[];
}> => {
await checkStarknetPubKey(expectedPubKey, options);

let transport: Transport;
try {
transport = options.useWebHID
Expand All @@ -79,8 +81,6 @@ export const connectAndSignDeployAccountTxWithLedger = async (
);
}

await checkStarknetPubKey(transport, expectedPubKey);

const nonce = 0; // DEPLOY_ACCOUNT transaction will have a nonce zero as it is the first transaction in the account
const contractAddress =
providedContractAddress ??
Expand Down Expand Up @@ -218,16 +218,14 @@ export const connectAndSignDeployAccountTxWithLedger = async (
};
});
} catch (e) {
await transport.close();

if (e.message?.includes("0x5515")) {
throw new KeplrError(
ErrModuleLedgerSign,
ErrCodeDeviceLocked,
"Device is locked"
);
} else {
throw e;
throw new KeplrError(ErrModuleLedgerSign, 9999, e.message);
}
} finally {
await transport.close();
Expand All @@ -240,21 +238,22 @@ export const connectAndSignInvokeTxWithLedger = async (
details: InvocationsSignerDetails,
options: LedgerOptions = { useWebHID: true }
): Promise<string[]> => {
await checkStarknetPubKey(expectedPubKey, options);

let transport: Transport;
try {
transport = options?.useWebHID
? await TransportWebHID.create()
: await TransportWebUSB.create();
} catch (e) {
console.error(e);
throw new KeplrError(
ErrModuleLedgerSign,
ErrFailedInit,
"Failed to init transport"
);
}

await checkStarknetPubKey(transport, expectedPubKey);

const txFields: TxFields | TxV1Fields = (() => {
switch (details.version) {
case "0x1":
Expand Down Expand Up @@ -301,16 +300,14 @@ export const connectAndSignInvokeTxWithLedger = async (
return formatStarknetSignature({ r, s });
});
} catch (e) {
await transport.close();

if (e.message?.includes("0x5515")) {
throw new KeplrError(
ErrModuleLedgerSign,
ErrCodeDeviceLocked,
"Device is locked"
);
} else {
throw e;
throw new KeplrError(ErrModuleLedgerSign, 9999, e.message);
}
} finally {
await transport.close();
Expand All @@ -323,21 +320,22 @@ export const connectAndSignMessageWithLedger = async (
signer: string,
options: LedgerOptions = { useWebHID: true }
): Promise<string[]> => {
await checkStarknetPubKey(expectedPubKey, options);

let transport: Transport;
try {
transport = options?.useWebHID
? await TransportWebHID.create()
: await TransportWebUSB.create();
} catch (e) {
console.error(e);
throw new KeplrError(
ErrModuleLedgerSign,
ErrFailedInit,
"Failed to init transport"
);
}

await checkStarknetPubKey(transport, expectedPubKey);

try {
const starknetApp = new StarknetClient(transport);
const res = await starknetApp.signMessage(
Expand All @@ -351,16 +349,14 @@ export const connectAndSignMessageWithLedger = async (
return formatStarknetSignature({ r, s });
});
} catch (e) {
await transport.close();

if (e.message?.includes("0x5515")) {
throw new KeplrError(
ErrModuleLedgerSign,
ErrCodeDeviceLocked,
"Device is locked"
);
} else {
throw e;
throw new KeplrError(ErrModuleLedgerSign, 9999, e.message);
}
} finally {
await transport.close();
Expand Down Expand Up @@ -410,31 +406,59 @@ function handleLedgerResponse<R>(
}

async function checkStarknetPubKey(
transport: Transport,
expectedPubKey: Uint8Array
expectedPubKey: Uint8Array,
options: LedgerOptions = { useWebHID: true }
) {
const starknetApp = new StarknetClient(transport);
let transport: Transport;
try {
transport = options?.useWebHID
? await TransportWebHID.create()
: await TransportWebUSB.create();
} catch (e) {
throw new KeplrError(
ErrModuleLedgerSign,
ErrFailedInit,
"Failed to init transport"
);
}

const res = await starknetApp.getPubKey(
STARKNET_LEDGER_DERIVATION_PATH,
false
);
try {
const starknetApp = new StarknetClient(transport);

handleLedgerResponse(res, () => {
const { publicKey } = res;
const res = await starknetApp.getPubKey(
STARKNET_LEDGER_DERIVATION_PATH,
false
);

if (
Buffer.from(new PubKeyStarknet(expectedPubKey).toBytes()).toString(
"hex"
) !== Buffer.from(new PubKeyStarknet(publicKey).toBytes()).toString("hex")
) {
transport.close();
return handleLedgerResponse(res, () => {
const { publicKey } = res;

if (
Buffer.from(new PubKeyStarknet(expectedPubKey).toBytes()).toString(
"hex"
) !==
Buffer.from(new PubKeyStarknet(publicKey).toBytes()).toString("hex")
) {
throw new KeplrError(
ErrModuleLedgerSign,
ErrPublicKeyUnmatched,
"Public key unmatched"
);
} else {
return publicKey;
}
});
} catch (e) {
if (e.message?.includes("0x5515")) {
throw new KeplrError(
ErrModuleLedgerSign,
ErrPublicKeyUnmatched,
"Public key unmatched"
ErrCodeDeviceLocked,
"Device is locked"
);
} else {
throw new KeplrError(ErrModuleLedgerSign, 9999, e.message);
}
});
} finally {
await transport.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export const AccountActivationModal: FunctionComponent<{
data={
data || {
keyInsensitive: keyRingStore.selectedKeyInfo!.insensitive,
isEthereum: false,
isStarknet: true,
}
}
isLedgerInteracting={isLedgerInteracting}
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/pages/starknet/sign/message/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export const SignStarknetMessageView: FunctionComponent<{
<LedgerGuideBox
data={{
keyInsensitive: interactionData.data.keyInsensitive,
isEthereum: false,
isStarknet: true,
}}
isLedgerInteracting={isLedgerInteracting}
ledgerInteractingError={ledgerInteractingError}
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/pages/starknet/sign/tx/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ export const SignStarknetTxView: FunctionComponent<{
<LedgerGuideBox
data={{
keyInsensitive: interactionData.data.keyInsensitive,
isEthereum: false,
isStarknet: true,
}}
isLedgerInteracting={isLedgerInteracting}
ledgerInteractingError={ledgerInteractingError}
Expand Down

0 comments on commit 52deeee

Please sign in to comment.