Skip to content

Commit 8a42454

Browse files
committed
address requested changes
1 parent 82f0c38 commit 8a42454

File tree

13 files changed

+170
-57
lines changed

13 files changed

+170
-57
lines changed

packages/examples/packages/send-flow/src/components/SendForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const SendForm: SnapComponent<SendFormProps> = ({
4444
}) => (
4545
<Form name="sendForm">
4646
<Field label="From account">
47-
<AccountSelector name="accountSelector" switchSelectedAccount />
47+
<AccountSelector name="accountSelector" switchGlobalAccount />
4848
</Field>
4949
<Field label="Send amount" error={errors?.amount}>
5050
<Box>

packages/snaps-controllers/src/interface/SnapInterfaceController.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ describe('SnapInterfaceController', () => {
229229
<Field label="baz">
230230
<AccountSelector
231231
name="baz"
232-
switchSelectedAccount
233-
selectedAddress="eip155:0:0x1234567890123456789012345678901234567890"
232+
switchGlobalAccount
233+
value="eip155:0:0x1234567890123456789012345678901234567890"
234234
/>
235235
</Field>
236236
<Field label="foobar">

packages/snaps-controllers/src/interface/utils.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,13 @@ describe('constructState', () => {
646646
<Box>
647647
<AssetSelector
648648
name="foo"
649+
<<<<<<< HEAD
649650
addresses={[
650651
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
651652
]}
653+
=======
654+
value="eip155:0:0x1234567890123456789012345678901234567890"
655+
>>>>>>> 82969fe5 (address requested changes)
652656
/>
653657
</Box>
654658
);
@@ -727,9 +731,13 @@ describe('constructState', () => {
727731
<Field label="foo">
728732
<AssetSelector
729733
name="foo"
734+
<<<<<<< HEAD
730735
addresses={[
731736
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
732737
]}
738+
=======
739+
value="eip155:0:0x1234567890123456789012345678901234567890"
740+
>>>>>>> 82969fe5 (address requested changes)
733741
/>
734742
</Field>
735743
</Form>
@@ -766,6 +774,7 @@ describe('constructState', () => {
766774

767775
const element = (
768776
<Box>
777+
<<<<<<< HEAD
769778
<Form name="form">
770779
<Field label="foo">
771780
<AssetSelector
@@ -777,6 +786,12 @@ describe('constructState', () => {
777786
/>
778787
</Field>
779788
</Form>
789+
=======
790+
<AccountSelector
791+
name="foo"
792+
value="bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6"
793+
/>
794+
>>>>>>> 82969fe5 (address requested changes)
780795
</Box>
781796
);
782797

@@ -829,6 +844,7 @@ describe('constructState', () => {
829844
});
830845
});
831846

847+
<<<<<<< HEAD
832848
it('sets the value to null if the account has no assets', () => {
833849
elementDataGetters.getAssetsState.mockReturnValue({
834850
assetsMetadata: {
@@ -845,14 +861,27 @@ describe('constructState', () => {
845861
elementDataGetters.getAccountByAddress.mockReturnValue({
846862
id: MOCK_ACCOUNT_ID,
847863
});
864+
=======
865+
it('switches the selected account if `switchGlobalAccount` is set', () => {
866+
getAccountByAddress.mockImplementation((address: string) => ({
867+
id: 'foo',
868+
address,
869+
scopes: ['eip155:0'],
870+
}));
871+
>>>>>>> 82969fe5 (address requested changes)
848872

849873
const element = (
850874
<Box>
851875
<AssetSelector
852876
name="foo"
877+
<<<<<<< HEAD
853878
addresses={[
854879
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
855880
]}
881+
=======
882+
value="eip155:0:0x1234567890123456789012345678901234567890"
883+
switchGlobalAccount
884+
>>>>>>> 82969fe5 (address requested changes)
856885
/>
857886
</Box>
858887
);

packages/snaps-controllers/src/interface/utils.ts

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type {
2727
import { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';
2828
import type { InternalAccount } from '@metamask/snaps-utils';
2929
import {
30-
createAddressList,
30+
createAccountList,
3131
getJsonSizeUnsafe,
3232
getJsxChildren,
3333
getJsxElementFromComponent,
@@ -101,9 +101,15 @@ type GetAccountByAddress = (
101101

102102
type SetSelectedAccount = (accountId: string) => void;
103103

104+
/**
105+
* A function to get the selected account in the client.
106+
*
107+
* @returns The selected account.
108+
*/
104109
type GetSelectedAccount = () => InternalAccount | undefined;
105110

106111
/**
112+
<<<<<<< HEAD
107113
* Data getters for elements.
108114
* This is used to get data from elements that is not directly accessible from the element itself.
109115
*
@@ -116,6 +122,23 @@ type ElementDataGetters = {
116122
getSelectedAccount: GetSelectedAccount;
117123
setSelectedAccount: SetSelectedAccount;
118124
};
125+
=======
126+
* A function to get an account by address.
127+
*
128+
* @param address - The address of the account.
129+
* @returns The account with the given address or undefined if none.
130+
*/
131+
type GetAccountByAddress = (
132+
address: CaipAccountAddress,
133+
) => InternalAccount | undefined;
134+
135+
/**
136+
* A function to set the selected account in the client.
137+
*
138+
* @param accountId - The ID of the account to set as selected.
139+
*/
140+
type SetSelectedAccount = (accountId: string) => void;
141+
>>>>>>> 82969fe5 (address requested changes)
119142

120143
/**
121144
* Get a JSX element from a component or JSX element. If the component is a
@@ -276,7 +299,7 @@ function constructComponentSpecificDefaultState(
276299

277300
const { id, address, scopes } = account;
278301

279-
const addresses = createAddressList(address, scopes);
302+
const addresses = createAccountList(address, scopes);
280303

281304
return { accountId: id, addresses };
282305
}
@@ -297,6 +320,7 @@ function constructComponentSpecificDefaultState(
297320
}
298321

299322
/**
323+
<<<<<<< HEAD
300324
* Get the state value for an asset selector.
301325
*
302326
* @param value - The asset selector value.
@@ -323,6 +347,41 @@ export function getAssetSelectorStateValue(
323347
name: asset.name,
324348
symbol: asset.symbol,
325349
};
350+
=======
351+
* Get the state value for an account selector.
352+
*
353+
* @param element - The account selector element.
354+
* @param getAccountByAddress - A function to get an account by address.
355+
* @param setSelectedAccount - A function to set the selected account in the client.
356+
* @returns The state value for the account selector.
357+
*/
358+
function getAccountSelectorStateValue(
359+
element: AccountSelectorElement,
360+
getAccountByAddress: GetAccountByAddress,
361+
setSelectedAccount: SetSelectedAccount,
362+
) {
363+
if (!element.props.value) {
364+
return undefined;
365+
}
366+
367+
const { address: parsedAddress } = parseCaipAccountId(element.props.value);
368+
369+
const account = getAccountByAddress(parsedAddress);
370+
371+
if (!account) {
372+
return undefined;
373+
}
374+
375+
if (element.props.switchGlobalAccount) {
376+
setSelectedAccount(account.id);
377+
}
378+
379+
const { id, address, scopes } = account;
380+
381+
const addresses = createAccountList(address, scopes);
382+
383+
return { accountId: id, addresses };
384+
>>>>>>> 82969fe5 (address requested changes)
326385
}
327386

328387
/**
@@ -359,6 +418,7 @@ function getComponentStateValue(
359418
case 'Checkbox':
360419
return element.props.checked;
361420

421+
<<<<<<< HEAD
362422
case 'AssetSelector':
363423
return getAssetSelectorStateValue(element.props.value, getAssetsState);
364424

@@ -392,6 +452,14 @@ function getComponentStateValue(
392452

393453
return { accountId: id, addresses };
394454
}
455+
=======
456+
case 'AccountSelector':
457+
return getAccountSelectorStateValue(
458+
element,
459+
getAccountByAddress,
460+
setSelectedAccount,
461+
);
462+
>>>>>>> 82969fe5 (address requested changes)
395463

396464
default:
397465
return element.props.value;

packages/snaps-controllers/src/test-utils/controller.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,18 +830,31 @@ export const getRestrictedSnapInterfaceControllerMessenger = (
830830
(address: string) => ({
831831
address,
832832
id: 'foo',
833+
<<<<<<< HEAD
833834
scopes: ['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'],
835+
=======
836+
scopes: ['eip155:0'],
837+
>>>>>>> 82969fe5 (address requested changes)
834838
}),
835839
);
836840

837841
messenger.registerActionHandler(
838842
'AccountsController:getSelectedMultichainAccount',
843+
<<<<<<< HEAD
839844
() =>
840845
({
841846
address: '0x1234567890123456789012345678901234567890',
842847
id: 'foo',
843848
scopes: ['eip155:0'],
844849
}) as unknown as InternalAccount,
850+
=======
851+
// @ts-expect-error partial mock
852+
() => ({
853+
address: '0x1234567890123456789012345678901234567890',
854+
id: 'foo',
855+
scopes: ['eip155:0'],
856+
}),
857+
>>>>>>> 82969fe5 (address requested changes)
845858
);
846859

847860
messenger.registerActionHandler(

packages/snaps-sdk/src/jsx/components/form/AccountSelector.test.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,32 @@ describe('AccountSelector', () => {
4848

4949
it('returns an account selector element with the switchGlobalAccount filter prop', () => {
5050
const result = (
51-
<AccountSelector name="account" switchSelectedAccount={true} />
51+
<AccountSelector name="account" switchGlobalAccount={true} />
5252
);
5353

5454
expect(result).toStrictEqual({
5555
type: 'AccountSelector',
5656
props: {
5757
name: 'account',
58-
switchSelectedAccount: true,
58+
switchGlobalAccount: true,
5959
},
6060
key: null,
6161
});
6262
});
6363

64-
it('returns an account selector element with a selectedAccount prop', () => {
64+
it('returns an account selector element with a value prop', () => {
6565
const result = (
6666
<AccountSelector
6767
name="account"
68-
selectedAddress="eip155:1:0x1234567890abcdef1234567890abcdef12345678"
68+
value="eip155:1:0x1234567890abcdef1234567890abcdef12345678"
6969
/>
7070
);
7171

7272
expect(result).toStrictEqual({
7373
type: 'AccountSelector',
7474
props: {
7575
name: 'account',
76-
selectedAddress: 'eip155:1:0x1234567890abcdef1234567890abcdef12345678',
76+
value: 'eip155:1:0x1234567890abcdef1234567890abcdef12345678',
7777
},
7878
key: null,
7979
});
@@ -85,8 +85,8 @@ describe('AccountSelector', () => {
8585
name="account"
8686
chainIds={['bip122:000000000019d6689c085ae165831e93']}
8787
hideExternalAccounts={true}
88-
switchSelectedAccount={true}
89-
selectedAddress="bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6"
88+
switchGlobalAccount={true}
89+
value="bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6"
9090
/>
9191
);
9292

@@ -96,8 +96,8 @@ describe('AccountSelector', () => {
9696
name: 'account',
9797
chainIds: ['bip122:000000000019d6689c085ae165831e93'],
9898
hideExternalAccounts: true,
99-
switchSelectedAccount: true,
100-
selectedAddress:
99+
switchGlobalAccount: true,
100+
value:
101101
'bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6',
102102
},
103103
key: null,

packages/snaps-sdk/src/jsx/components/form/AccountSelector.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import { createSnapComponent } from '../../component';
77
*
88
* @property name - The name of the account selector. This is used to identify the
99
* state in the form data.
10-
* @property hideExternalAccounts - Whether to hide accounts that doesn't belong to the snap.
10+
* @property hideExternalAccounts - Whether to hide accounts that don't belong to the snap.
1111
* @property chainIds - The chain IDs to filter the accounts to show.
12-
* @property switchSelectedAccount - Whether to switch the selected account in the client.
13-
* @property selectedAddress - The selected address.
12+
* @property switchGlobalAccount - Whether to switch the selected account in the client.
13+
* @property value - The selected address.
1414
*/
1515
export type AccountSelectorProps = {
1616
name: string;
1717
hideExternalAccounts?: boolean | undefined;
1818
chainIds?: CaipChainId[] | undefined;
19-
switchSelectedAccount?: boolean | undefined;
20-
selectedAddress?: CaipAccountId | undefined;
19+
switchGlobalAccount?: boolean | undefined;
20+
value?: CaipAccountId | undefined;
2121
};
2222

2323
const TYPE = 'AccountSelector';
@@ -30,8 +30,8 @@ const TYPE = 'AccountSelector';
3030
* state in the form data.
3131
* @param props.hideExternalAccounts - Whether to hide accounts that doesn't belong to the snap.
3232
* @param props.chainIds - The chain IDs to filter the accounts to show.
33-
* @param props.switchSelectedAccount - Whether to switch the selected account in the client.
34-
* @param props.selectedAddress - The selected address.
33+
* @param props.switchGlobalAccount - Whether to switch the selected account in the client.
34+
* @param props.value - The selected address.
3535
* @returns An account selector element.
3636
* @example
3737
* <AccountSelector name="account-selector" />
@@ -40,11 +40,11 @@ const TYPE = 'AccountSelector';
4040
* @example
4141
* <AccountSelector name="account-selector" chainIds={['eip155:1']} />
4242
* @example
43-
* <AccountSelector name="account-selector" switchSelectedAccount />
43+
* <AccountSelector name="account-selector" switchGlobalAccount />
4444
* @example
45-
* <AccountSelector name="account-selector" selectedAddress="eip155:1:0x1234..." />
45+
* <AccountSelector name="account-selector" value="eip155:1:0x1234..." />
4646
* @example
47-
* <AccountSelector name="account-selector" hideExternalAccounts chainIds={['eip155:1']} switchSelectedAccount selectedAddress="eip155:1:0x1234..." />
47+
* <AccountSelector name="account-selector" hideExternalAccounts chainIds={['eip155:1']} switchGlobalAccount value="eip155:1:0x1234..." />
4848
*/
4949
export const AccountSelector = createSnapComponent<
5050
AccountSelectorProps,

0 commit comments

Comments
 (0)