Skip to content

Commit 0e5165f

Browse files
committed
refactor: indexFromAddress and pathFromAddress
1 parent f88fa39 commit 0e5165f

3 files changed

Lines changed: 35 additions & 24 deletions

File tree

packages/keyring-eth-qr/jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ module.exports = merge(baseConfig, {
2020
// An object that configures minimum threshold enforcement for coverage results
2121
coverageThreshold: {
2222
global: {
23-
branches: 91.66,
23+
branches: 96.66,
2424
functions: 100,
25-
lines: 94.97,
26-
statements: 95.07,
25+
lines: 98.99,
26+
statements: 99.01,
2727
},
2828
},
2929
});

packages/keyring-eth-qr/src/device.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,20 @@ export class Device {
274274
return path;
275275
}
276276

277-
const index = this.indexFromAddress(normalizedAddress);
277+
let index = this.#pairedDevice.indexes[normalizedAddress];
278+
if (index === undefined) {
279+
for (let i = 0; i < MAX_INDEX; i++) {
280+
const derivedAddress = this.addressFromIndex(i);
281+
if (derivedAddress === address) {
282+
index = i;
283+
break;
284+
}
285+
}
286+
if (index === undefined) {
287+
throw new Error(`Unknown address ${normalizedAddress}`);
288+
}
289+
}
290+
278291
return `${this.#pairedDevice.hdPath}/${this.#pairedDevice.childrenPath
279292
.replace('*', index.toString())
280293
.replace(/\*/gu, '0')}`;
@@ -292,28 +305,14 @@ export class Device {
292305
return Number(cachedIndex);
293306
}
294307

295-
if (this.#pairedDevice.keyringMode === DeviceMode.ACCOUNT) {
296-
const path = this.#pairedDevice.paths[address];
297-
if (path === undefined) {
298-
throw new Error(`Unknown address`);
299-
}
300-
301-
const index = path.split('/').pop();
302-
if (index === undefined) {
303-
throw new Error(`Invalid path for address ${address}`);
304-
}
305-
306-
return Number(index);
308+
const index = this.pathFromAddress(address).split('/').pop();
309+
if (index === undefined) {
310+
throw new Error(`Invalid path for address ${address}`);
307311
}
308312

309-
for (let i = 0; i < MAX_INDEX; i++) {
310-
const derivedAddress = this.addressFromIndex(i);
311-
if (derivedAddress === address) {
312-
return i;
313-
}
314-
}
313+
this.#pairedDevice.indexes[address] = Number(index);
315314

316-
throw new Error(`Address ${address} not found`);
315+
return Number(index);
317316
}
318317

319318
/**

packages/keyring-eth-qr/src/qr-keyring.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CryptoAccount, ETHSignature } from '@keystonehq/bc-ur-registry-eth';
22
import { MetaMaskKeyring as KeystoneKeyring } from '@keystonehq/metamask-airgapped-keyring';
3-
import type { Hex } from '@metamask/utils';
3+
import { add0x, type Hex } from '@metamask/utils';
44
import * as uuid from 'uuid';
55

66
import type { QrKeyringBridge } from '.';
@@ -718,6 +718,18 @@ describe('QrKeyring', () => {
718718
expect(signedTx.nonce).toBe(LEGACY_TRANSACTION.nonce);
719719
});
720720

721+
it('throws an error if the address is not found in the keyring', async () => {
722+
const keyring = new QrKeyring({
723+
bridge: getMockBridge(),
724+
ur,
725+
});
726+
const unknownAddress = add0x('0'.repeat(40));
727+
728+
await expect(
729+
keyring.signTransaction(unknownAddress, TRANSACTION),
730+
).rejects.toThrow(`Unknown address ${unknownAddress}`);
731+
});
732+
721733
it('throws an error if the signature scan `requestId` is wrong', async () => {
722734
const keyring = new QrKeyring({
723735
bridge: getMockBridge(),

0 commit comments

Comments
 (0)