Skip to content

Commit 023d0a2

Browse files
committed
feat: add Solana account form update
1 parent 398221b commit 023d0a2

File tree

7 files changed

+344
-38
lines changed

7 files changed

+344
-38
lines changed

test/e2e/flask/solana/switching-network-accounts.spec.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ describe('Switching between account from different networks', function (this: Su
3535
await headerNavbar.check_ifNetworkPickerClickable(true);
3636
await headerNavbar.check_currentSelectedNetwork('Localhost 8545');
3737
await headerNavbar.openAccountMenu();
38-
await accountListPage.addAccount({ accountType: ACCOUNT_TYPE.Solana });
38+
await accountListPage.addAccount({
39+
accountType: ACCOUNT_TYPE.Solana,
40+
accountName: 'Solana Account 2',
41+
});
3942
await headerNavbar.check_ifNetworkPickerClickable(true);
4043
await headerNavbar.check_currentSelectedNetwork('Solana Mainnet');
4144
await headerNavbar.check_accountLabel('Solana Account 2');

ui/components/multichain/account-list-menu/account-list-menu.tsx

+104-32
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import {
2222
KeyringAccountType,
2323
///: END:ONLY_INCLUDE_IF
2424
} from '@metamask/keyring-api';
25+
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
26+
import { CaipChainId } from '@metamask/utils';
27+
///: END:ONLY_INCLUDE_IF
2528
///: BEGIN:ONLY_INCLUDE_IF(build-flask)
2629
import {
2730
BITCOIN_WALLET_NAME,
@@ -77,10 +80,8 @@ import {
7780
getIsSolanaSupportEnabled,
7881
///: END:ONLY_INCLUDE_IF
7982
///: BEGIN:ONLY_INCLUDE_IF(multi-srp,solana)
80-
getMetaMaskHdKeyrings,
81-
///: END:ONLY_INCLUDE_IF
82-
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
8383
getHdKeyringOfSelectedAccountOrPrimaryKeyring,
84+
getMetaMaskHdKeyrings,
8485
///: END:ONLY_INCLUDE_IF
8586
} from '../../../selectors';
8687
import { setSelectedAccount } from '../../../store/actions';
@@ -131,6 +132,9 @@ import {
131132
AccountOverviewTabKey,
132133
} from '../../../../shared/constants/app-state';
133134
import { CreateEthAccount } from '../create-eth-account';
135+
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
136+
import { CreateSnapAccount } from '../create-snap-account';
137+
///: END:ONLY_INCLUDE_IF
134138
import { ImportAccount } from '../import-account';
135139
///: BEGIN:ONLY_INCLUDE_IF(solana)
136140
import {
@@ -159,6 +163,10 @@ const ACTION_MODES = {
159163
// Same but for testnet
160164
ADD_BITCOIN_TESTNET: 'add-bitcoin-testnet',
161165
///: END:ONLY_INCLUDE_IF
166+
///: BEGIN:ONLY_INCLUDE_IF(solana)
167+
// Displays the add account form controls (for solana account)
168+
ADD_SOLANA: 'add-solana',
169+
///: END:ONLY_INCLUDE_IF
162170
// Displays the import account form controls
163171
IMPORT: 'import',
164172
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
@@ -190,6 +198,10 @@ export const getActionTitle = (
190198
case ACTION_MODES.ADD_BITCOIN_TESTNET:
191199
return t('addAccount');
192200
///: END:ONLY_INCLUDE_IF
201+
///: BEGIN:ONLY_INCLUDE_IF(solana)
202+
case ACTION_MODES.ADD_SOLANA:
203+
return t('addAccount');
204+
///: END:ONLY_INCLUDE_IF
193205
case ACTION_MODES.IMPORT:
194206
return t('importPrivateKey');
195207
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
@@ -252,6 +264,11 @@ export const AccountListMenu = ({
252264
///: END:ONLY_INCLUDE_IF
253265
const [searchQuery, setSearchQuery] = useState('');
254266
const [actionMode, setActionMode] = useState(ACTION_MODES.LIST);
267+
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
268+
const [previousActionMode, setPreviousActionMode] = useState(
269+
ACTION_MODES.LIST,
270+
);
271+
///: END:ONLY_INCLUDE_IF(multi-srp)
255272
const hiddenAddresses = useSelector(getHiddenAccountsList);
256273
const updatedAccountsList = useSelector(getUpdatedAndSortedAccounts);
257274
const filteredUpdatedAccountList = useMemo(
@@ -267,6 +284,9 @@ export const AccountListMenu = ({
267284
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
268285
const addSnapAccountEnabled = useSelector(getIsAddSnapAccountEnabled);
269286
///: END:ONLY_INCLUDE_IF
287+
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
288+
const multiSrpEnabled = true;
289+
///: END:ONLY_INCLUDE_IF
270290
///: BEGIN:ONLY_INCLUDE_IF(build-flask)
271291
const isAddWatchEthereumAccountEnabled = useSelector(
272292
getIsWatchEthereumAccountEnabled,
@@ -363,7 +383,7 @@ export const AccountListMenu = ({
363383
onBack = () => setActionMode(ACTION_MODES.LIST);
364384
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
365385
} else if (actionMode === ACTION_MODES.SELECT_SRP) {
366-
onBack = () => setActionMode(ACTION_MODES.ADD);
386+
onBack = () => setActionMode(previousActionMode);
367387
///: END:ONLY_INCLUDE_IF
368388
} else {
369389
onBack = () => setActionMode(ACTION_MODES.MENU);
@@ -445,6 +465,55 @@ export const AccountListMenu = ({
445465
searchQuery,
446466
]);
447467

468+
const onActionComplete = useCallback(
469+
async (confirmed: boolean) => {
470+
if (confirmed) {
471+
onClose();
472+
} else {
473+
setActionMode(ACTION_MODES.LIST);
474+
}
475+
},
476+
[onClose, setActionMode],
477+
);
478+
479+
/**
480+
* Determines the client type and chain ID based on the action mode
481+
*
482+
* @param mode - The current action mode
483+
* @returns An object containing the client type and chain ID, or null values if not a snap account creation mode
484+
*/
485+
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
486+
const getSnapClientConfig = (
487+
mode: string,
488+
): { clientType: WalletClientType | null; chainId: CaipChainId | null } => {
489+
switch (mode) {
490+
case ACTION_MODES.ADD_BITCOIN:
491+
return {
492+
clientType: WalletClientType.Bitcoin,
493+
chainId: MultichainNetworks.BITCOIN,
494+
};
495+
case ACTION_MODES.ADD_BITCOIN_TESTNET:
496+
return {
497+
clientType: WalletClientType.Bitcoin,
498+
chainId: MultichainNetworks.BITCOIN_TESTNET,
499+
};
500+
case ACTION_MODES.ADD_SOLANA:
501+
return {
502+
clientType: WalletClientType.Solana,
503+
chainId: MultichainNetworks.SOLANA,
504+
};
505+
default:
506+
return {
507+
clientType: null,
508+
chainId: null,
509+
};
510+
}
511+
};
512+
513+
// Use the helper function to get client type and chain ID
514+
const { clientType, chainId } = getSnapClientConfig(actionMode);
515+
///: END:ONLY_INCLUDE_IF
516+
448517
return (
449518
<Modal isOpen onClose={onClose}>
450519
<ModalOverlay />
@@ -463,36 +532,43 @@ export const AccountListMenu = ({
463532
{actionMode === ACTION_MODES.ADD ? (
464533
<Box paddingLeft={4} paddingRight={4} paddingBottom={4}>
465534
<CreateEthAccount
466-
onActionComplete={(confirmed) => {
467-
if (confirmed) {
468-
onClose();
469-
} else {
470-
setActionMode(ACTION_MODES.LIST);
471-
}
472-
}}
535+
onActionComplete={onActionComplete}
473536
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
474537
selectedKeyringId={selectedKeyringId}
475-
onSelectSrp={() => setActionMode(ACTION_MODES.SELECT_SRP)}
538+
onSelectSrp={() => {
539+
setPreviousActionMode(ACTION_MODES.ADD);
540+
setActionMode(ACTION_MODES.SELECT_SRP);
541+
}}
476542
///: END:ONLY_INCLUDE_IF(multi-srp)
477543
/>
478544
</Box>
479545
) : null}
546+
{
547+
///: BEGIN:ONLY_INCLUDE_IF(multi-srp)
548+
clientType && chainId ? (
549+
<Box paddingLeft={4} paddingRight={4} paddingBottom={4}>
550+
<CreateSnapAccount
551+
onActionComplete={onActionComplete}
552+
selectedKeyringId={selectedKeyringId}
553+
onSelectSrp={() => {
554+
setPreviousActionMode(actionMode);
555+
setActionMode(ACTION_MODES.SELECT_SRP);
556+
}}
557+
clientType={clientType}
558+
chainId={chainId}
559+
/>
560+
</Box>
561+
) : null
562+
///: END:ONLY_INCLUDE_IF(multi-srp)
563+
}
480564
{actionMode === ACTION_MODES.IMPORT ? (
481565
<Box
482566
paddingLeft={4}
483567
paddingRight={4}
484568
paddingBottom={4}
485569
paddingTop={0}
486570
>
487-
<ImportAccount
488-
onActionComplete={(confirmed) => {
489-
if (confirmed) {
490-
onClose();
491-
} else {
492-
setActionMode(ACTION_MODES.LIST);
493-
}
494-
}}
495-
/>
571+
<ImportAccount onActionComplete={onActionComplete} />
496572
</Box>
497573
) : null}
498574
{
@@ -505,15 +581,7 @@ export const AccountListMenu = ({
505581
paddingTop={0}
506582
style={{ overflowY: 'scroll' }}
507583
>
508-
<ImportSrp
509-
onActionComplete={(confirmed: boolean) => {
510-
if (confirmed) {
511-
onClose();
512-
} else {
513-
setActionMode(ACTION_MODES.LIST);
514-
}
515-
}}
516-
/>
584+
<ImportSrp onActionComplete={onActionComplete} />
517585
</Box>
518586
)
519587
///: END:ONLY_INCLUDE_IF
@@ -524,7 +592,7 @@ export const AccountListMenu = ({
524592
<SrpList
525593
onActionComplete={(keyringId: string) => {
526594
setSelectedKeyringId(keyringId);
527-
setActionMode(ACTION_MODES.ADD);
595+
setActionMode(previousActionMode);
528596
}}
529597
/>
530598
)
@@ -582,12 +650,16 @@ export const AccountListMenu = ({
582650
},
583651
});
584652

653+
if (multiSrpEnabled) {
654+
return setActionMode(ACTION_MODES.ADD_SOLANA);
655+
}
656+
585657
// The account creation + renaming is handled by the
586658
// Snap account bridge, so we need to close the current
587659
// modal
588660
onClose();
589661

590-
await solanaWalletSnapClient.createAccount(
662+
return await solanaWalletSnapClient.createAccount(
591663
MultichainNetworks.SOLANA,
592664
primaryKeyring.metadata.id,
593665
);

0 commit comments

Comments
 (0)