Skip to content

Commit 9fbf2ff

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

2 files changed

Lines changed: 53 additions & 21 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: 50 additions & 18 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,16 +58,16 @@ 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

8169
/**
82-
* Account wallet type.
70+
* Keyring account wallet type.
8371
*/
8472
get type(): AccountWalletType;
8573

@@ -104,6 +92,50 @@ export type AccountWallet<Account extends KeyringAccount> = {
10492
getAccountGroups(): AccountGroup<Account>[];
10593
};
10694

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

0 commit comments

Comments
 (0)