Skip to content

Commit

Permalink
move function to snap-utils and add JSDocs
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Feb 12, 2025
1 parent ee67e9e commit 311858f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 48 deletions.
4 changes: 2 additions & 2 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 93.4,
"functions": 96.81,
"lines": 98.18,
"functions": 96.8,
"lines": 98.17,
"statements": 97.91
}
Original file line number Diff line number Diff line change
Expand Up @@ -457,21 +457,37 @@ export class SnapInterfaceController extends BaseController<
);
}

/**
* Get an account by address.
*
* @param address - The address of the account.
* @returns The account.
*/
#getAccountByAddress(address: string) {
return this.messagingSystem.call(
'AccountsController:getAccountByAddress',
address,
);
}

/**
* Get the selected account in the client.
*
* @returns The selected account.
*/
#getSelectedAccount() {
return this.messagingSystem.call(
'AccountsController:getSelectedMultichainAccount',
);
}

/**
* Set the selected account in the client.
*
* @param accountId - The account id.
*/
#setSelectedAccount(accountId: string) {
return this.messagingSystem.call(
this.messagingSystem.call(
'AccountsController:setSelectedAccount',
accountId,
);
Expand Down
21 changes: 1 addition & 20 deletions packages/snaps-controllers/src/interface/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ import {
AccountSelector,
} from '@metamask/snaps-sdk/jsx';

import {
assertNameIsUnique,
constructState,
createAddressList,
getJsxInterface,
} from './utils';
import { assertNameIsUnique, constructState, getJsxInterface } from './utils';

describe('getJsxInterface', () => {
it('returns the JSX interface for a JSX element', () => {
Expand Down Expand Up @@ -69,20 +64,6 @@ describe('assertNameIsUnique', () => {
});
});

describe('createAddressList', () => {
it('creates an address list from an account', () => {
const result = createAddressList(
'0x1234567890123456789012345678901234567890',
['eip155:1', 'eip155:2'],
);

expect(result).toStrictEqual([
'eip155:1:0x1234567890123456789012345678901234567890',
'eip155:2:0x1234567890123456789012345678901234567890',
]);
});
});

describe('constructState', () => {
const getSelectedAccount = jest.fn();

Expand Down
26 changes: 2 additions & 24 deletions packages/snaps-controllers/src/interface/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ import type {
} from '@metamask/snaps-sdk/jsx';
import { isJSXElementUnsafe } from '@metamask/snaps-sdk/jsx';
import {
createAddressList,
getJsonSizeUnsafe,
getJsxChildren,
getJsxElementFromComponent,
walkJsx,
} from '@metamask/snaps-utils';
import type { CaipAccountId, CaipChainId } from '@metamask/utils';
import {
parseCaipAccountId,
parseCaipChainId,
toCaipAccountId,
type CaipAccountAddress,
} from '@metamask/utils';
import { parseCaipAccountId, type CaipAccountAddress } from '@metamask/utils';

type GetSelectedAccount = () => InternalAccount | undefined;
type GetAccountByAddress = (
Expand Down Expand Up @@ -70,23 +65,6 @@ export function assertNameIsUnique(state: InterfaceState, name: string) {
);
}

/**
* Create a list of CAIP account IDs from an address and a list of scopes.
*
* @param address - The address to create the account IDs from.
* @param scopes - The scopes to create the account IDs from.
* @returns The list of CAIP account IDs.
*/
export function createAddressList(
address: string,
scopes: CaipChainId[],
): CaipAccountId[] {
return scopes.map((scope) => {
const { namespace, reference } = parseCaipChainId(scope);
return toCaipAccountId(namespace, reference, address);
});
}

/**
* Construct default state for a component.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"branches": 99.74,
"functions": 98.92,
"lines": 99.61,
"statements": 96.91
"statements": 96.92
}
15 changes: 15 additions & 0 deletions packages/snaps-utils/src/address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createAddressList } from './address';

describe('createAddressList', () => {
it('creates an address list from an account', () => {
const result = createAddressList(
'0x1234567890123456789012345678901234567890',
['eip155:1', 'eip155:2'],
);

expect(result).toStrictEqual([
'eip155:1:0x1234567890123456789012345678901234567890',
'eip155:2:0x1234567890123456789012345678901234567890',
]);
});
});
19 changes: 19 additions & 0 deletions packages/snaps-utils/src/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { CaipAccountId, CaipChainId } from '@metamask/utils';
import { parseCaipChainId, toCaipAccountId } from '@metamask/utils';

/**
* Create a list of CAIP account IDs from an address and a list of scopes.
*
* @param address - The address to create the account IDs from.
* @param scopes - The scopes to create the account IDs from.
* @returns The list of CAIP account IDs.
*/
export function createAddressList(
address: string,
scopes: CaipChainId[],
): CaipAccountId[] {
return scopes.map((scope) => {
const { namespace, reference } = parseCaipChainId(scope);
return toCaipAccountId(namespace, reference, address);
});
}
1 change: 1 addition & 0 deletions packages/snaps-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './address';
export * from './array';
export * from './auxiliary-files';
export * from './base64';
Expand Down

0 comments on commit 311858f

Please sign in to comment.