Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5023,9 +5023,15 @@ export default class MetamaskController extends EventEmitter {
this.accountsController.setSelectedAccount(account.id);
}

await this._addAccountsWithBalance(id, shouldImportSolanaAccount);
const discoveredAccounts = await this._addAccountsWithBalance(
id,
shouldImportSolanaAccount,
);

return newAccountAddress;
return {
newAccountAddress,
discoveredAccounts,
};
} finally {
releaseLock();
}
Expand Down Expand Up @@ -5271,6 +5277,11 @@ export default class MetamaskController extends EventEmitter {
);
}

const discoveredAccounts = {
bitcoin: 0,
solana: 0,
};

///: BEGIN:ONLY_INCLUDE_IF(bitcoin)
const btcClient = await this._getMultichainWalletSnapClient(
BITCOIN_WALLET_SNAP_ID,
Expand All @@ -5281,6 +5292,8 @@ export default class MetamaskController extends EventEmitter {
btcScope,
);

discoveredAccounts.bitcoin = btcAccounts.length;

// If none accounts got discovered, we still create the first (default) one.
if (btcAccounts.length === 0) {
await this._addSnapAccount(entropySource, btcClient, {
Expand All @@ -5301,6 +5314,8 @@ export default class MetamaskController extends EventEmitter {
solScope,
);

discoveredAccounts.solana = solanaAccounts.length;

// If none accounts got discovered, we still create the first (default) one.
if (solanaAccounts.length === 0) {
await this._addSnapAccount(entropySource, solanaClient, {
Expand All @@ -5309,8 +5324,13 @@ export default class MetamaskController extends EventEmitter {
}
}
///: END:ONLY_INCLUDE_IF
return discoveredAccounts;
} catch (e) {
log.warn(`Failed to add accounts with balance. Error: ${e}`);
return {
bitcoin: 0,
solana: 0,
};
} finally {
await this.userStorageController.setHasAccountSyncingSyncedAtLeastOnce(
true,
Expand Down
33 changes: 24 additions & 9 deletions ui/pages/multi-srp/import-srp/import-srp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,35 @@ export const ImportSrp = () => {

const joinedSrp = secretRecoveryPhrase.join(' ');
if (joinedSrp) {
await dispatch(actions.importMnemonicToVault(joinedSrp));
const result = (await dispatch(
actions.importMnemonicToVault(joinedSrp),
)) as unknown as {
newAccountAddress: string;
discoveredAccounts: { bitcoin: number; solana: number };
};

const { discoveredAccounts } = result;

// Clear the secret recovery phrase after importing
setSecretRecoveryPhrase(Array(defaultNumberOfWords).fill(''));

// Track the event with the discovered accounts
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
trackEvent({
event: MetaMetricsEventName.ImportSecretRecoveryPhraseCompleted,
properties: {
// eslint-disable-next-line @typescript-eslint/naming-convention
hd_entropy_index: newHdEntropyIndex,
// eslint-disable-next-line @typescript-eslint/naming-convention
number_of_solana_accounts_discovered: discoveredAccounts?.solana,
// eslint-disable-next-line @typescript-eslint/naming-convention
number_of_bitcoin_accounts_discovered: discoveredAccounts?.bitcoin,
},
});
}

history.push(DEFAULT_ROUTE);
dispatch(setShowNewSrpAddedToast(true));
trackEvent({
event: MetaMetricsEventName.ImportSecretRecoveryPhraseCompleted,
properties: {
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
// eslint-disable-next-line @typescript-eslint/naming-convention
hd_entropy_index: newHdEntropyIndex,
},
});
}

const isValidSrp = useMemo(() => {
Expand Down
21 changes: 21 additions & 0 deletions ui/store/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3078,6 +3078,27 @@ describe('Actions', () => {
true,
);
});

it('returns discovered accounts from background', async () => {
const store = mockStore();
const mockResult = {
newAccountAddress: '9fE6zKgca6K2EEa3yjbcq7zGMusUNqSQeWQNL2YDZ2Yi',
discoveredAccounts: { bitcoin: 2, solana: 1 },
};

const importMnemonicToVaultStub = sinon.stub().resolves(mockResult);

background.getApi.returns({
importMnemonicToVault: importMnemonicToVaultStub,
});
setBackgroundConnection(background.getApi());

const result = await store.dispatch(
actions.importMnemonicToVault('mnemonic'),
);

expect(result).toStrictEqual(mockResult);
});
});

describe('getTokenStandardAndDetailsByChain', () => {
Expand Down
42 changes: 27 additions & 15 deletions ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,27 +490,39 @@ export function createNewVaultAndRestore(
};
}

export function importMnemonicToVault(
mnemonic: string,
): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31879
// eslint-disable-next-line @typescript-eslint/no-misused-promises
return (dispatch: MetaMaskReduxDispatch) => {
export function importMnemonicToVault(mnemonic: string): ThunkAction<
Promise<{
newAccountAddress: string;
discoveredAccounts: { bitcoin: number; solana: number };
}>,
MetaMaskReduxState,
unknown,
AnyAction
> {
return async (dispatch: MetaMaskReduxDispatch) => {
dispatch(showLoadingIndication());
log.debug(`background.importMnemonicToVault`);

return new Promise<void>((resolve, reject) => {
callBackgroundMethod('importMnemonicToVault', [mnemonic], (err) => {
if (err) {
reject(err);
return;
}
resolve();
});
return new Promise<{
newAccountAddress: string;
discoveredAccounts: { bitcoin: number; solana: number };
}>((resolve, reject) => {
callBackgroundMethod(
'importMnemonicToVault',
[mnemonic],
(err, result) => {
if (err) {
reject(err);
return;
}
resolve(result);
},
);
})
.then(async () => {
.then(async (result) => {
dispatch(hideLoadingIndication());
dispatch(setShowNewSrpAddedToast(true));
return result;
})
.catch((err) => {
dispatch(displayWarning(err));
Expand Down
Loading