Skip to content

Commit f9180a1

Browse files
committed
refactor: add more wallet type classes
1 parent 729ccdb commit f9180a1

2 files changed

Lines changed: 52 additions & 25 deletions

File tree

packages/account-api/src/api/multichain/wallet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55

66
import type { MultichainAccountGroup } from './group';
77
import type { Bip44Account } from '../bip44';
8-
import type { AccountWallet, BaseAccountWalletStatus } from '../wallet';
8+
import type { AccountWalletStatus, BaseAccountWallet } from '../wallet';
99
import { AccountWalletType } from '../wallet';
1010

1111
/**
@@ -36,7 +36,7 @@ export type ParsedMultichainAccountWalletId = {
3636
* cannot have multiple status at once.
3737
*/
3838
export type MultichainAccountWalletStatus =
39-
| BaseAccountWalletStatus
39+
| AccountWalletStatus
4040
/**
4141
* Discovery is in progress for this wallet. New account groups will be
4242
* automatically added based on the account provider discovery result.
@@ -60,7 +60,7 @@ export type MultichainAccountWalletStatus =
6060
*/
6161
export type MultichainAccountWallet<
6262
Account extends Bip44Account<KeyringAccount>,
63-
> = AccountWallet<Account> & {
63+
> = BaseAccountWallet<Account> & {
6464
/**
6565
* Multichain account wallet ID.
6666
*/

packages/account-api/src/api/wallet.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { KeyringAccount } from '@metamask/keyring-api';
22

33
// Circular import are allowed when using `import type`.
4+
import type { Bip44Account } from './bip44';
45
import type { AccountGroup, AccountGroupId } from './group';
6+
import type { MultichainAccountWallet } from './multichain';
57

68
/**
79
* Wallet type.
@@ -37,7 +39,7 @@ export const ACCOUNT_WALLET_ID_REGEX =
3739
* in. All of those operations cannot run concurrently, thus, the wallet
3840
* cannot have multiple status at once.
3941
*/
40-
export type BaseAccountWalletStatus =
42+
export type AccountWalletStatus =
4143
/**
4244
* The wallet is not initialized yet.
4345
*/
@@ -47,20 +49,6 @@ export type BaseAccountWalletStatus =
4749
*/
4850
| 'ready';
4951

50-
/**
51-
* Wallet status.
52-
*
53-
* Those status are used to report in which "state" the wallet is currently
54-
* in. All of those operations cannot run concurrently, thus, the wallet
55-
* cannot have multiple status at once.
56-
*/
57-
export type AccountWalletStatus =
58-
| BaseAccountWalletStatus
59-
/**
60-
* The wallet is running an operation.
61-
*/
62-
| `in-progress:${string}`;
63-
6452
/**
6553
* Parsed account wallet ID with its wallet type and sub-ID.
6654
*/
@@ -70,19 +58,14 @@ export type ParsedAccountWalletId = {
7058
};
7159

7260
/**
73-
* Account wallet that can hold multiple account groups.
61+
* Keyring account wallet that can hold multiple account groups.
7462
*/
75-
export type AccountWallet<Account extends KeyringAccount> = {
63+
export type BaseAccountWallet<Account extends KeyringAccount> = {
7664
/**
7765
* Account wallet ID.
7866
*/
7967
get id(): AccountWalletId;
8068

81-
/**
82-
* Account wallet type.
83-
*/
84-
get type(): AccountWalletType;
85-
8669
/**
8770
* Account wallet status.
8871
*/
@@ -104,6 +87,50 @@ export type AccountWallet<Account extends KeyringAccount> = {
10487
getAccountGroups(): AccountGroup<Account>[];
10588
};
10689

90+
/**
91+
* Keyring account wallet that can hold multiple account groups.
92+
*/
93+
export type KeyringAccountWallet<Account extends KeyringAccount> =
94+
BaseAccountWallet<Account> & {
95+
/**
96+
* Keyring account wallet type.
97+
*/
98+
get type(): AccountWalletType.Keyring;
99+
};
100+
101+
/**
102+
* Snap keyring account wallet that can hold multiple account groups.
103+
*/
104+
export type SnapAccountWallet<Account extends KeyringAccount> =
105+
BaseAccountWallet<Account> & {
106+
/**
107+
* Snap account wallet type.
108+
*/
109+
get type(): AccountWalletType.Snap;
110+
};
111+
112+
/**
113+
* Type constraint for a {@link AccountGroupObject}. If one of its union-members
114+
* does not match this contraint, {@link AccountGroupObject} will resolve
115+
* to `never`.
116+
*/
117+
type IsAccountWallet<
118+
Wallet extends BaseAccountWallet<Account> & {
119+
get type(): AccountWalletType;
120+
},
121+
Account extends KeyringAccount,
122+
> = Wallet;
123+
124+
/**
125+
* Account wallet that can hold multiple account groups.
126+
*/
127+
export type AccountWallet<Account extends KeyringAccount> = IsAccountWallet<
128+
| KeyringAccountWallet<Account>
129+
| SnapAccountWallet<Account>
130+
| MultichainAccountWallet<Bip44Account<Account>>,
131+
Account
132+
>;
133+
107134
/**
108135
* Type utility to compute a constrained {@link AccountWalletId} type given a
109136
* specifc {@link AccountWalletType}.

0 commit comments

Comments
 (0)