Skip to content

Commit 311858f

Browse files
committed
move function to snap-utils and add JSDocs
1 parent ee67e9e commit 311858f

File tree

8 files changed

+58
-48
lines changed

8 files changed

+58
-48
lines changed
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"branches": 93.4,
3-
"functions": 96.81,
4-
"lines": 98.18,
3+
"functions": 96.8,
4+
"lines": 98.17,
55
"statements": 97.91
66
}

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -457,21 +457,37 @@ export class SnapInterfaceController extends BaseController<
457457
);
458458
}
459459

460+
/**
461+
* Get an account by address.
462+
*
463+
* @param address - The address of the account.
464+
* @returns The account.
465+
*/
460466
#getAccountByAddress(address: string) {
461467
return this.messagingSystem.call(
462468
'AccountsController:getAccountByAddress',
463469
address,
464470
);
465471
}
466472

473+
/**
474+
* Get the selected account in the client.
475+
*
476+
* @returns The selected account.
477+
*/
467478
#getSelectedAccount() {
468479
return this.messagingSystem.call(
469480
'AccountsController:getSelectedMultichainAccount',
470481
);
471482
}
472483

484+
/**
485+
* Set the selected account in the client.
486+
*
487+
* @param accountId - The account id.
488+
*/
473489
#setSelectedAccount(accountId: string) {
474-
return this.messagingSystem.call(
490+
this.messagingSystem.call(
475491
'AccountsController:setSelectedAccount',
476492
accountId,
477493
);

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

+1-20
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ import {
1818
AccountSelector,
1919
} from '@metamask/snaps-sdk/jsx';
2020

21-
import {
22-
assertNameIsUnique,
23-
constructState,
24-
createAddressList,
25-
getJsxInterface,
26-
} from './utils';
21+
import { assertNameIsUnique, constructState, getJsxInterface } from './utils';
2722

2823
describe('getJsxInterface', () => {
2924
it('returns the JSX interface for a JSX element', () => {
@@ -69,20 +64,6 @@ describe('assertNameIsUnique', () => {
6964
});
7065
});
7166

72-
describe('createAddressList', () => {
73-
it('creates an address list from an account', () => {
74-
const result = createAddressList(
75-
'0x1234567890123456789012345678901234567890',
76-
['eip155:1', 'eip155:2'],
77-
);
78-
79-
expect(result).toStrictEqual([
80-
'eip155:1:0x1234567890123456789012345678901234567890',
81-
'eip155:2:0x1234567890123456789012345678901234567890',
82-
]);
83-
});
84-
});
85-
8667
describe('constructState', () => {
8768
const getSelectedAccount = jest.fn();
8869

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

+2-24
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,13 @@ import type {
2222
} from '@metamask/snaps-sdk/jsx';
2323
import { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';
2424
import {
25+
createAddressList,
2526
getJsonSizeUnsafe,
2627
getJsxChildren,
2728
getJsxElementFromComponent,
2829
walkJsx,
2930
} from '@metamask/snaps-utils';
30-
import type { CaipAccountId, CaipChainId } from '@metamask/utils';
31-
import {
32-
parseCaipAccountId,
33-
parseCaipChainId,
34-
toCaipAccountId,
35-
type CaipAccountAddress,
36-
} from '@metamask/utils';
31+
import { parseCaipAccountId, type CaipAccountAddress } from '@metamask/utils';
3732

3833
type GetSelectedAccount = () => InternalAccount | undefined;
3934
type GetAccountByAddress = (
@@ -70,23 +65,6 @@ export function assertNameIsUnique(state: InterfaceState, name: string) {
7065
);
7166
}
7267

73-
/**
74-
* Create a list of CAIP account IDs from an address and a list of scopes.
75-
*
76-
* @param address - The address to create the account IDs from.
77-
* @param scopes - The scopes to create the account IDs from.
78-
* @returns The list of CAIP account IDs.
79-
*/
80-
export function createAddressList(
81-
address: string,
82-
scopes: CaipChainId[],
83-
): CaipAccountId[] {
84-
return scopes.map((scope) => {
85-
const { namespace, reference } = parseCaipChainId(scope);
86-
return toCaipAccountId(namespace, reference, address);
87-
});
88-
}
89-
9068
/**
9169
* Construct default state for a component.
9270
*

packages/snaps-utils/coverage.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"branches": 99.74,
33
"functions": 98.92,
44
"lines": 99.61,
5-
"statements": 96.91
5+
"statements": 96.92
66
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createAddressList } from './address';
2+
3+
describe('createAddressList', () => {
4+
it('creates an address list from an account', () => {
5+
const result = createAddressList(
6+
'0x1234567890123456789012345678901234567890',
7+
['eip155:1', 'eip155:2'],
8+
);
9+
10+
expect(result).toStrictEqual([
11+
'eip155:1:0x1234567890123456789012345678901234567890',
12+
'eip155:2:0x1234567890123456789012345678901234567890',
13+
]);
14+
});
15+
});

packages/snaps-utils/src/address.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { CaipAccountId, CaipChainId } from '@metamask/utils';
2+
import { parseCaipChainId, toCaipAccountId } from '@metamask/utils';
3+
4+
/**
5+
* Create a list of CAIP account IDs from an address and a list of scopes.
6+
*
7+
* @param address - The address to create the account IDs from.
8+
* @param scopes - The scopes to create the account IDs from.
9+
* @returns The list of CAIP account IDs.
10+
*/
11+
export function createAddressList(
12+
address: string,
13+
scopes: CaipChainId[],
14+
): CaipAccountId[] {
15+
return scopes.map((scope) => {
16+
const { namespace, reference } = parseCaipChainId(scope);
17+
return toCaipAccountId(namespace, reference, address);
18+
});
19+
}

packages/snaps-utils/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './address';
12
export * from './array';
23
export * from './auxiliary-files';
34
export * from './base64';

0 commit comments

Comments
 (0)