Skip to content

fix: workaround for first party snap account name suggestion #31542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 4, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ jest.mock('../../../../app/scripts/lib/util', () => ({
jest.mock('../../../store/actions', () => {
return {
...jest.requireActual('../../../store/actions'),
getNextAvailableAccountName: () => mockNextAccountName,
generateNewHdKeyring: () => mockGenerateNewHdKeyring,
getNextAvailableAccountName: () => mockNextAccountName(),
generateNewHdKeyring: () => mockGenerateNewHdKeyring(),
};
});

Expand Down Expand Up @@ -586,6 +586,7 @@ describe('AccountListMenu', () => {
});

it('calls the bitcoin client to create an account', async () => {
mockNextAccountName.mockReturnValue('Snap Account 1');
const { getByText, getByTestId } = render();

const button = getByTestId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const CreateAccount: CreateAccountComponent = React.memo(
return (
<Box as="form" onSubmit={onSubmit}>
<FormTextField
data-testid="account-name-input"
ref={ref}
size={FormTextFieldSize.Lg}
gap={2}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import configureStore from '../../../store/store';
import mockState from '../../../../test/data/mock-state.json';
import { WalletClientType } from '../../../hooks/accounts/useMultichainWalletSnapClient';
import { createMockInternalAccount } from '../../../../test/jest/mocks';
import { MultichainNetworks } from '../../../../shared/constants/multichain/networks';
import { CreateSnapAccount } from './create-snap-account';

// Mock dependencies
jest.mock('../../../hooks/accounts/useMultichainWalletSnapClient', () => {
const mockCreateAccount = jest.fn().mockResolvedValue(true);
return {
...jest.requireActual(
'../../../hooks/accounts/useMultichainWalletSnapClient',
),
useMultichainWalletSnapClient: jest.fn().mockReturnValue({
createAccount: mockCreateAccount,
}),
Expand Down Expand Up @@ -145,6 +149,19 @@ describe('CreateSnapAccount', () => {
});
});

it('renders the suggested account name for a first party snap', async () => {
const { getByPlaceholderText } = render({
...defaultProps,
clientType: WalletClientType.Solana,
chainId: MultichainNetworks.SOLANA,
});

await waitFor(() => {
const nameSuggestion = getByPlaceholderText('Solana Account 2');
expect(nameSuggestion).toBeInTheDocument();
});
});

it('only calls createAccount once', async () => {
const { getByTestId } = render();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
WalletClientType,
useMultichainWalletSnapClient,
} from '../../../hooks/accounts/useMultichainWalletSnapClient';
import { MultichainNetworks } from '../../../../shared/constants/multichain/networks';

type CreateSnapAccountProps = {
/**
Expand Down Expand Up @@ -71,7 +72,32 @@ export const CreateSnapAccount = ({
);

const getNextAccountName = async () => {
return getNextAvailableAccountName(KeyringTypes.snap);
const defaultSnapAccountName = await getNextAvailableAccountName(
KeyringTypes.snap,
);

// FIXME: This is a temporary workaround to suggest a different account name for a first party snap.
const accountNumber = defaultSnapAccountName.trim().split(' ').pop();

switch (clientType) {
case WalletClientType.Bitcoin: {
if (chainId === MultichainNetworks.BITCOIN_TESTNET) {
return `Bitcoin Testnet Account ${accountNumber}`;
}
return `Bitcoin Account ${accountNumber}`;
}
case WalletClientType.Solana: {
if (chainId === MultichainNetworks.SOLANA_TESTNET) {
return `Solana Testnet Account ${accountNumber}`;
}
if (chainId === MultichainNetworks.SOLANA_DEVNET) {
return `Solana Devnet Account ${accountNumber}`;
}
return `Solana Account ${accountNumber}`;
}
default:
return defaultSnapAccountName;
}
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ exports[`create-named-snap-account confirmation matches snapshot 1`] = `
>
<div
class="mm-box mm-form-text-field mm-box--display-flex mm-box--gap-2 mm-box--flex-direction-column"
data-testid="account-name-input"
>
<label
class="mm-box mm-text mm-label mm-label--html-for mm-form-text-field__label mm-text--body-md mm-text--font-weight-medium mm-box--display-inline-flex mm-box--align-items-center mm-box--color-text-default"
Expand Down
Loading