generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbip44.ts
More file actions
67 lines (62 loc) · 1.8 KB
/
bip44.ts
File metadata and controls
67 lines (62 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type {
CreateAccountOptions,
KeyringAccount,
KeyringAccountEntropyMnemonicOptions,
KeyringCapabilities,
AccountCreationType,
} from '@metamask/keyring-api';
/**
* BIP-44 compatible account type.
*/
export type Bip44Account<Account extends KeyringAccount> = Account & {
// We force the option type for those accounts. (That's how we identify
// if an account is BIP-44 compatible).
options: {
entropy: KeyringAccountEntropyMnemonicOptions;
};
};
/**
* BIP-44 compatible account creation options.
*/
export type Bip44CreateAccountOptions = Extract<
CreateAccountOptions,
{
type:
| `${AccountCreationType.Bip44DerivePath}`
| `${AccountCreationType.Bip44DeriveIndex}`
| `${AccountCreationType.Bip44DeriveIndexRange}`
| `${AccountCreationType.Bip44Discover}`;
}
>;
/**
* BIP-44 compatible keyring capabilities.
*/
export type Bip44KeyringCapabilities = Required<
Pick<KeyringCapabilities, 'scopes' | 'bip44'>
>;
/**
* Checks if an account is BIP-44 compatible.
*
* @param account - The account to be tested.
* @returns True if the account is BIP-44 compatible.
*/
export function isBip44Account<Account extends KeyringAccount>(
account: Account,
): account is Bip44Account<Account> {
// To be BIP-44 compatible, we just check for the entropy type (the
// the `entropy` shape will be inferred automatically).
return account.options.entropy?.type === 'mnemonic';
}
/**
* Asserts a keyring account is BIP-44 compatible.
*
* @param account - Keyring account to check.
* @throws If the keyring account is not compatible.
*/
export function assertIsBip44Account<Account extends KeyringAccount>(
account: Account,
): asserts account is Bip44Account<Account> {
if (!isBip44Account(account)) {
throw new Error('Account is not BIP-44 compatible');
}
}