Skip to content
42 changes: 10 additions & 32 deletions app/actions/multiSrp/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { KeyringTypes } from '@metamask/keyring-controller';
import ExtendedKeyringTypes from '../../constants/keyringTypes';
import Engine from '../../core/Engine';
import {
importNewSecretRecoveryPhrase,
createNewSecretRecoveryPhrase,
addNewHdAccount,
} from './';
import { importNewSecretRecoveryPhrase, addNewHdAccount } from './';
import { createMockInternalAccount } from '../../util/test/accountsControllerTestUtils';
import { TraceName, TraceOperation } from '../../util/trace';
import ReduxService from '../../core/redux/ReduxService';
Expand Down Expand Up @@ -57,6 +53,13 @@ const hdKeyring = {
},
};

const hdKeyringV2 = {
getAccounts: () => {
mockGetAccounts();
return [{ address: mockAddress }];
},
};

jest.mock('../../selectors/seedlessOnboardingController', () => ({
selectSeedlessOnboardingLoginFlow: (state: unknown) =>
mockSelectSeedlessOnboardingLoginFlow(state),
Expand Down Expand Up @@ -103,6 +106,8 @@ jest.mock('../../core/Engine', () => ({
getKeyringsByType: () => mockGetKeyringsByType(),
withKeyring: (_selector: unknown, operation: (args: unknown) => void) =>
operation({ keyring: hdKeyring, metadata: { id: '1234' } }),
withKeyringV2: (_selector: unknown, operation: (args: unknown) => void) =>
operation({ keyring: hdKeyringV2, metadata: { id: '1234' } }),
},
AccountsController: {
getNextAvailableAccountName: jest.fn().mockReturnValue('Snap Account 1'),
Expand Down Expand Up @@ -474,33 +479,6 @@ describe('MultiSRP Actions', () => {
});
});

describe('createNewSecretRecoveryPhrase', () => {
it('creates new SRP', async () => {
mockAddNewKeyring.mockResolvedValue({
getAccounts: () => Promise.resolve([mockAddress]),
});

await createNewSecretRecoveryPhrase();

expect(mockAddNewKeyring).toHaveBeenCalledWith(
KeyringTypes.hd,
undefined,
);
expect(mockSetSelectedAddress).toHaveBeenCalledWith(mockAddress);
});

it('Does not set selected address or gets accounts on errors', async () => {
mockAddNewKeyring.mockRejectedValue(new Error('Test error'));

await expect(
async () => await createNewSecretRecoveryPhrase(),
).rejects.toThrow('Test error');

expect(mockGetAccounts).not.toHaveBeenCalled();
expect(mockSetSelectedAddress).not.toHaveBeenCalled();
});
});

describe('addNewHdAccount', () => {
it('adds a new HD account, sets the selected address and returns the account', async () => {
mockAddAccounts.mockReturnValue([mockAddress]);
Expand Down
26 changes: 5 additions & 21 deletions app/actions/multiSrp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function importNewSecretRecoveryPhrase(
});
const entropySource = wallet.entropySource;

const [newAccountAddress] = await KeyringController.withKeyring(
const [newAccount] = await KeyringController.withKeyringV2(
{
id: entropySource,
},
Expand Down Expand Up @@ -80,7 +80,7 @@ export async function importNewSecretRecoveryPhrase(
} catch (error) {
await MultichainAccountService.removeMultichainAccountWallet(
entropySource,
newAccountAddress,
newAccount.address,
);

const errorMessage =
Expand Down Expand Up @@ -129,34 +129,18 @@ export async function importNewSecretRecoveryPhrase(
} finally {
// We trigger the callback with the results, even in case of error (0 discovered accounts)
await callback?.({
address: newAccountAddress,
address: newAccount.address,
discoveredAccountsCount,
error: capturedError,
});
}
})();

if (shouldSelectAccount) {
Engine.setSelectedAddress(newAccountAddress);
Engine.setSelectedAddress(newAccount.address);
}

return { address: newAccountAddress, discoveredAccountsCount };
}

export async function createNewSecretRecoveryPhrase() {
const { KeyringController } = Engine.context;
const newHdkeyring = await KeyringController.addNewKeyring(
ExtendedKeyringTypes.hd,
);

const [newAccountAddress] = await KeyringController.withKeyring(
{
id: newHdkeyring.id,
},
async ({ keyring }) => keyring.getAccounts(),
);

return Engine.setSelectedAddress(newAccountAddress);
return { address: newAccount.address, discoveredAccountsCount };
}

export async function addNewHdAccount(
Expand Down
18 changes: 17 additions & 1 deletion app/core/Authentication/Authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ const mockHdKeyring = {
getAccounts: jest.fn().mockResolvedValue([mockAddress]),
};

const mockHdKeyringV2 = {
getAccounts: jest.fn().mockResolvedValue([{ address: mockAddress }]),
};

jest.mock('../Engine', () => ({
resetState: jest.fn(),
controllerMessenger: {
Expand Down Expand Up @@ -1812,6 +1816,12 @@ describe('Authentication', () => {
async ({ id: _id }, callback) =>
await callback({ keyring: mockHdKeyring }),
),
withKeyringV2: jest
.fn()
.mockImplementation(
async ({ id: _id }, callback) =>
await callback({ keyring: mockHdKeyringV2 }),
),
state: {
keyrings: [createMockHdKeyringObject()],
},
Expand Down Expand Up @@ -2917,6 +2927,12 @@ describe('Authentication', () => {
async ({ id: _id }, callback) =>
await callback({ keyring: mockHdKeyring }),
),
withKeyringV2: jest
.fn()
.mockImplementation(
async ({ id: _id }, callback) =>
await callback({ keyring: mockHdKeyringV2 }),
),
state: {
keyrings: [createMockHdKeyringObject()],
},
Expand Down Expand Up @@ -3042,7 +3058,7 @@ describe('Authentication', () => {
// Arrange
const mnemonic = 'test mnemonic phrase for wallet';
const error = new Error('Failed to get accounts');
mockHdKeyring.getAccounts.mockRejectedValue(error);
mockHdKeyringV2.getAccounts.mockRejectedValue(error);
Engine.context.MultichainAccountService.createMultichainAccountWallet.mockResolvedValue(
mockMultichainAccountWallet,
);
Expand Down
4 changes: 2 additions & 2 deletions app/core/Authentication/Authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ class AuthenticationService {
);
const entropySource = wallet.entropySource;

const [newAccountAddress] = await KeyringController.withKeyring(
const [newAccount] = await KeyringController.withKeyringV2(
{ id: entropySource },
async ({ keyring }) => keyring.getAccounts(),
);
Expand All @@ -1137,7 +1137,7 @@ class AuthenticationService {
// handle seedless controller import error by reverting keyring controller mnemonic import
await MultichainAccountService.removeMultichainAccountWallet(
entropySource,
newAccountAddress,
newAccount.address,
);
throw error;
}
Expand Down
Loading