Skip to content

Commit 3f31150

Browse files
committed
fix: address PR feedbacks
1 parent f98024f commit 3f31150

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

packages/keyring-eth-simple/src/simple-keyring-v2.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ describe('SimpleKeyringV2', () => {
319319
).rejects.toThrow('Invalid key');
320320
});
321321

322-
it('throws error when getAccounts returns fewer addresses after import', async () => {
322+
it('throws error when no new address is added after import', async () => {
323323
// Create first account
324324
await wrapper.createAccounts({
325325
type: 'private-key:import',
@@ -328,8 +328,14 @@ describe('SimpleKeyringV2', () => {
328328
privateKey: TEST_PRIVATE_KEY_1,
329329
});
330330

331-
// Mock getAccounts to return empty array (simulating the key didn't get added)
332-
jest.spyOn(inner, 'getAccounts').mockResolvedValueOnce([]);
331+
const existingAddresses = await inner.getAccounts();
332+
333+
// Mock getAccounts to return the same addresses before and after import
334+
// (simulating the key didn't get added)
335+
jest
336+
.spyOn(inner, 'getAccounts')
337+
.mockResolvedValueOnce(existingAddresses) // First call (before import)
338+
.mockResolvedValueOnce(existingAddresses); // Second call (after import) - same addresses
333339

334340
await expect(
335341
wrapper.createAccounts({

packages/keyring-eth-simple/src/simple-keyring-v2.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
PrivateKeyEncoding,
1515
} from '@metamask/keyring-api';
1616
import type { AccountId } from '@metamask/keyring-utils';
17-
import type { Hex, Json } from '@metamask/utils';
17+
import { add0x, type Hex, type Json } from '@metamask/utils';
1818
import { Mutex } from 'async-mutex';
1919

2020
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -37,7 +37,7 @@ const simpleKeyringV2Capabilities: KeyringCapabilities = {
3737
scopes: [EthScope.Eoa],
3838
privateKey: {
3939
importFormats: [
40-
{ encoding: PrivateKeyEncoding.Hexadecimal, type: 'eip155:eoa' },
40+
{ encoding: PrivateKeyEncoding.Hexadecimal, type: EthAccountType.Eoa },
4141
],
4242
exportFormats: [{ encoding: PrivateKeyEncoding.Hexadecimal }],
4343
},
@@ -152,20 +152,30 @@ export class SimpleKeyringV2
152152
);
153153
}
154154

155+
// Get current addresses before import
156+
const addressesBefore = await this.inner.getAccounts();
157+
const addressSetBefore = new Set(addressesBefore);
158+
155159
// Get current accounts to preserve them
156160
const currentAccounts = await this.inner.serialize();
157161

158162
// Import the new private key by deserializing with all accounts
159163
await this.inner.deserialize([...currentAccounts, privateKey]);
160164

161-
// Get the address of the newly added account
162-
const allAddresses = await this.inner.getAccounts();
163-
const newAddress = allAddresses[allAddresses.length - 1];
165+
// Get addresses after import and find the newly added one
166+
const addressesAfter = await this.inner.getAccounts();
167+
168+
// Find the new address by diffing the two sets
169+
const newAddresses = addressesAfter.filter(
170+
(addr) => !addressSetBefore.has(addr),
171+
);
164172

165-
if (!newAddress) {
173+
if (newAddresses.length !== 1 || !newAddresses[0]) {
166174
throw new Error('Failed to import private key');
167175
}
168176

177+
const newAddress = newAddresses[0];
178+
169179
// Create and return the new KeyringAccount
170180
const newAccount = this.#createKeyringAccount(newAddress);
171181
return [newAccount];
@@ -214,10 +224,12 @@ export class SimpleKeyringV2
214224
const privateKeyHex = await this.inner.exportAccount(
215225
this.toHexAddress(account.address),
216226
);
227+
// Sanitize private key format
228+
const privateKey = add0x(privateKeyHex);
217229

218230
const exported: ExportedAccount = {
219231
type: 'private-key',
220-
privateKey: `0x${privateKeyHex}`,
232+
privateKey,
221233
encoding: PrivateKeyEncoding.Hexadecimal,
222234
};
223235

0 commit comments

Comments
 (0)