Skip to content

Commit 8df9085

Browse files
committed
refactor: split all files
1 parent daddb4b commit 8df9085

7 files changed

Lines changed: 157 additions & 138 deletions

File tree

packages/multichain-account-api/src/api.ts renamed to packages/multichain-account-api/src/adapters.ts

Lines changed: 9 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -7,100 +7,16 @@ import type { EntropySourceId } from '@metamask/keyring-api';
77
import type { InternalAccount } from '@metamask/keyring-internal-api';
88
import type { AccountId } from '@metamask/keyring-utils';
99
import { isScopeEqualToAny } from '@metamask/keyring-utils';
10-
import type { CaipChainId } from '@metamask/utils';
1110

12-
export type AccountType = string;
13-
14-
export type AccountMethod = string;
15-
16-
export type AccountProvider = {
17-
getEntropySources: () => EntropySourceId[];
18-
19-
getAccounts: (opts: {
20-
entropySource: EntropySourceId;
21-
groupIndex: number;
22-
}) => InternalAccount[];
23-
24-
createAccounts: (opts: {
25-
entropySource: EntropySourceId;
26-
groupIndex: number;
27-
}) => Promise<InternalAccount[]>;
28-
29-
discoverAndCreateAccounts: (opts: {
30-
entropySource: EntropySourceId;
31-
groupIndex: number;
32-
}) => Promise<InternalAccount[]>;
33-
};
34-
35-
export type MultichainAccountWalletId = `multichain-account-wallet:${string}`;
36-
37-
export type MultichainAccountId = `${MultichainAccountWalletId}:${number}`; // Use number for the account group index.
38-
39-
export type MultichainAccountSelector = {
40-
id?: AccountId;
41-
address?: string;
42-
type?: AccountType;
43-
methods?: AccountMethod[];
44-
scopes?: CaipChainId[];
45-
};
46-
47-
export type MultichainAccount = {
48-
get id(): MultichainAccountId;
49-
get wallet(): MultichainAccountWallet;
50-
get index(): number;
51-
get accounts(): InternalAccount[];
52-
53-
/**
54-
* Gets the "blockchain" account for a given account ID.
55-
*
56-
* @param id - Account ID.
57-
* @returns The "blockchain" account or undefined if not found.
58-
*/
59-
getAccount(id: AccountId): InternalAccount | undefined;
60-
61-
/**
62-
* Query a "blockchain" account matching the selector.
63-
*
64-
* @param selector - Query selector.
65-
* @returns The "blockchain" account matching the selector or undefined if not matching.
66-
* @throws If multiple accounts match the selector.
67-
*/
68-
get(selector: MultichainAccountSelector): InternalAccount | undefined;
69-
70-
/**
71-
* Query "blockchain" accounts matching the selector.
72-
*
73-
* @param selector - Query selector.
74-
* @returns The "blockchain" accounts matching the selector.
75-
*/
76-
select(selector: MultichainAccountSelector): InternalAccount[];
77-
};
78-
79-
/**
80-
* Gets the multichain account wallet ID from its entropy source.
81-
*
82-
* @param entropySource - Entropy source ID of that wallet.
83-
* @returns The multichain account wallet ID.
84-
*/
85-
export function toMultichainAccountWalletId(
86-
entropySource: EntropySourceId,
87-
): MultichainAccountWalletId {
88-
return `multichain-account-wallet:${entropySource}`;
89-
}
90-
91-
/**
92-
* Gets the multichain account ID from its multichain account wallet ID and its index.
93-
*
94-
* @param walletId - Multichain account wallet ID.
95-
* @param groupIndex - Index of that multichain account.
96-
* @returns The multichain account ID.
97-
*/
98-
export function toMultichainAccountId(
99-
walletId: MultichainAccountWalletId,
100-
groupIndex: number,
101-
): MultichainAccountId {
102-
return `${walletId}:${groupIndex}`;
103-
}
11+
import type {
12+
AccountProvider,
13+
MultichainAccount,
14+
MultichainAccountId,
15+
MultichainAccountSelector,
16+
MultichainAccountWallet,
17+
MultichainAccountWalletId,
18+
} from './api';
19+
import { toMultichainAccountId, toMultichainAccountWalletId } from './api';
10420

10521
export class MultichainAccountAdapter implements MultichainAccount {
10622
readonly #id: MultichainAccountId;
@@ -197,45 +113,6 @@ export class MultichainAccountAdapter implements MultichainAccount {
197113
}
198114
}
199115

200-
export type MultichainAccountWallet = {
201-
get id(): MultichainAccountWalletId;
202-
203-
get entropySource(): EntropySourceId;
204-
205-
get accounts(): MultichainAccount[];
206-
207-
/**
208-
* Gets the next available account index (named group index internally).
209-
*
210-
* @returns Next available group index.
211-
*/
212-
getNextGroupIndex(): number;
213-
214-
/**
215-
* Creates a new multichain account on a given group index.
216-
*
217-
* NOTE: This method is idempotent.
218-
*
219-
* @param groupIndex - Next available group index.
220-
* @returns New (or existing) multichain account for the given group index.
221-
*/
222-
createMultichainAccount(groupIndex: number): Promise<MultichainAccount>;
223-
224-
/**
225-
* Creates a new multichain account for the next available group index.
226-
*
227-
* @returns Next multichain account.
228-
*/
229-
createNextMultichainAccount(): Promise<MultichainAccount>;
230-
231-
/**
232-
* Discovers and automatically create multichain accounts for that wallet.
233-
*
234-
* @returns List of all multichain accounts that got discovered or automatically created.
235-
*/
236-
discoverAndCreateMultichainAccounts(): Promise<MultichainAccount[]>;
237-
};
238-
239116
export class MultichainAccountWalletAdapter implements MultichainAccountWallet {
240117
readonly #id: MultichainAccountWalletId;
241118

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { EntropySourceId } from '@metamask/keyring-api';
2+
3+
export type MultichainAccountWalletId = `multichain-account-wallet:${string}`;
4+
5+
export type MultichainAccountId = `${MultichainAccountWalletId}:${number}`; // Use number for the account group index.
6+
7+
/**
8+
* Gets the multichain account wallet ID from its entropy source.
9+
*
10+
* @param entropySource - Entropy source ID of that wallet.
11+
* @returns The multichain account wallet ID.
12+
*/
13+
export function toMultichainAccountWalletId(
14+
entropySource: EntropySourceId,
15+
): MultichainAccountWalletId {
16+
return `multichain-account-wallet:${entropySource}`;
17+
}
18+
19+
/**
20+
* Gets the multichain account ID from its multichain account wallet ID and its index.
21+
*
22+
* @param walletId - Multichain account wallet ID.
23+
* @param groupIndex - Index of that multichain account.
24+
* @returns The multichain account ID.
25+
*/
26+
export function toMultichainAccountId(
27+
walletId: MultichainAccountWalletId,
28+
groupIndex: number,
29+
): MultichainAccountId {
30+
return `${walletId}:${groupIndex}`;
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './id';
2+
export type * from './providers';
3+
export type * from './types';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { EntropySourceId } from '@metamask/keyring-api';
2+
import type { InternalAccount } from '@metamask/keyring-internal-api';
3+
4+
export type AccountProvider = {
5+
getEntropySources: () => EntropySourceId[];
6+
7+
getAccounts: (opts: {
8+
entropySource: EntropySourceId;
9+
groupIndex: number;
10+
}) => InternalAccount[];
11+
12+
createAccounts: (opts: {
13+
entropySource: EntropySourceId;
14+
groupIndex: number;
15+
}) => Promise<InternalAccount[]>;
16+
17+
discoverAndCreateAccounts: (opts: {
18+
entropySource: EntropySourceId;
19+
groupIndex: number;
20+
}) => Promise<InternalAccount[]>;
21+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import type { CaipChainId, EntropySourceId } from '@metamask/keyring-api';
2+
import type { InternalAccount } from '@metamask/keyring-internal-api';
3+
import type { AccountId } from '@metamask/keyring-utils';
4+
5+
import type { MultichainAccountId, MultichainAccountWalletId } from './id';
6+
7+
export type AccountType = string;
8+
9+
export type AccountMethod = string;
10+
11+
export type MultichainAccountSelector = {
12+
id?: AccountId;
13+
address?: string;
14+
type?: AccountType;
15+
methods?: AccountMethod[];
16+
scopes?: CaipChainId[];
17+
};
18+
export type MultichainAccount = {
19+
get id(): MultichainAccountId;
20+
get wallet(): MultichainAccountWallet;
21+
get index(): number;
22+
get accounts(): InternalAccount[];
23+
24+
/**
25+
* Gets the "blockchain" account for a given account ID.
26+
*
27+
* @param id - Account ID.
28+
* @returns The "blockchain" account or undefined if not found.
29+
*/
30+
getAccount(id: AccountId): InternalAccount | undefined;
31+
32+
/**
33+
* Query a "blockchain" account matching the selector.
34+
*
35+
* @param selector - Query selector.
36+
* @returns The "blockchain" account matching the selector or undefined if not matching.
37+
* @throws If multiple accounts match the selector.
38+
*/
39+
get(selector: MultichainAccountSelector): InternalAccount | undefined;
40+
41+
/**
42+
* Query "blockchain" accounts matching the selector.
43+
*
44+
* @param selector - Query selector.
45+
* @returns The "blockchain" accounts matching the selector.
46+
*/
47+
select(selector: MultichainAccountSelector): InternalAccount[];
48+
};
49+
50+
export type MultichainAccountWallet = {
51+
get id(): MultichainAccountWalletId;
52+
53+
get entropySource(): EntropySourceId;
54+
55+
get accounts(): MultichainAccount[];
56+
57+
/**
58+
* Gets the next available account index (named group index internally).
59+
*
60+
* @returns Next available group index.
61+
*/
62+
getNextGroupIndex(): number;
63+
64+
/**
65+
* Creates a new multichain account on a given group index.
66+
*
67+
* NOTE: This method is idempotent.
68+
*
69+
* @param groupIndex - Next available group index.
70+
* @returns New (or existing) multichain account for the given group index.
71+
*/
72+
createMultichainAccount(groupIndex: number): Promise<MultichainAccount>;
73+
74+
/**
75+
* Creates a new multichain account for the next available group index.
76+
*
77+
* @returns Next multichain account.
78+
*/
79+
createNextMultichainAccount(): Promise<MultichainAccount>;
80+
81+
/**
82+
* Discovers and automatically create multichain accounts for that wallet.
83+
*
84+
* @returns List of all multichain accounts that got discovered or automatically created.
85+
*/
86+
discoverAndCreateMultichainAccounts(): Promise<MultichainAccount[]>;
87+
};

packages/multichain-account-api/src/index.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@ import {
1919
import type { InternalAccount } from '@metamask/keyring-internal-api';
2020
import { v4 as uuid } from 'uuid';
2121

22+
import {
23+
MultichainAccountAdapter,
24+
MultichainAccountWalletAdapter,
25+
} from './adapters';
2226
import type {
2327
AccountProvider,
2428
MultichainAccount,
2529
MultichainAccountSelector,
2630
MultichainAccountWallet,
2731
} from './api';
28-
import {
29-
MultichainAccountAdapter,
30-
MultichainAccountWalletAdapter,
31-
toMultichainAccountId,
32-
toMultichainAccountWalletId,
33-
} from './api';
32+
import { toMultichainAccountId, toMultichainAccountWalletId } from './api';
3433

3534
const mockEntropySource = 'mock-entropy-source';
3635

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './api';
2+
export * from './adapters';

0 commit comments

Comments
 (0)