generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathaccount-options.ts
More file actions
217 lines (197 loc) · 5.46 KB
/
account-options.ts
File metadata and controls
217 lines (197 loc) · 5.46 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { exactOptional, selectiveUnion, type } from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import {
boolean,
intersection,
literal,
number,
object,
record,
string,
} from '@metamask/superstruct';
import { isPlainObject, JsonStruct } from '@metamask/utils';
/**
* Keyring account entropy valid types.
*/
export enum KeyringAccountEntropyTypeOption {
/**
* Indicates that the account was created from a mnemonic phrase.
*/
Mnemonic = 'mnemonic',
/**
* Indicates that the account was imported from a private key.
*/
PrivateKey = 'private-key',
/**
* Indicates that the account was created from a hardware wallet.
*/
Hardware = 'hardware',
/**
* Indicates that the account was created with custom, keyring-specific entropy.
* This is an opaque type where the entropy source is managed internally by the keyring.
*/
Custom = 'custom',
}
/**
* Keyring account options struct for mnemonics (BIP-44).
*/
export const KeyringAccountEntropyMnemonicOptionsStruct = object({
/**
* Indicates that the account was created from a mnemonic phrase.
*/
type: literal(`${KeyringAccountEntropyTypeOption.Mnemonic}`),
/**
* The ID of the entropy source.
*/
id: string(), // TODO: Define a struct for entropy source.
/**
* The BIP-44 derivation path used to derive the account.
*/
derivationPath: string(),
/**
* Index used to group accounts in the UI.
*
* Accounts sharing the same `groupIndex` are displayed together as a
* multichain account group.
*/
groupIndex: number(),
});
/**
* Keyring account options for mnemonics (BIP-44) {@link KeyringAccountEntropyMnemonicOptionsStruct}.
*/
export type KeyringAccountEntropyMnemonicOptions = Infer<
typeof KeyringAccountEntropyMnemonicOptionsStruct
>;
/**
* Keyring account options struct for private keys.
*/
export const KeyringAccountEntropyPrivateKeyOptionsStruct = object({
/**
* Indicates that the account was imported from a private key.
*/
type: literal(`${KeyringAccountEntropyTypeOption.PrivateKey}`),
});
/**
* Keyring account options for private keys {@link KeyringAccountEntropyPrivateKeyOptionsStruct}.
*/
export type KeyringAccountEntropyPrivateKeyOptions = Infer<
typeof KeyringAccountEntropyPrivateKeyOptionsStruct
>;
/**
* Keyring account options struct for custom entropy.
*
* This is an opaque type where the entropy source is managed internally by the keyring.
* It behaves similarly to a private key import but allows keyrings to define their own
* entropy management strategy.
*/
export const KeyringAccountEntropyCustomOptionsStruct = object({
/**
* Indicates that the account was created with custom, keyring-specific entropy.
*/
type: literal(`${KeyringAccountEntropyTypeOption.Custom}`),
});
/**
* Keyring account options for custom entropy {@link KeyringAccountEntropyCustomOptionsStruct}.
*/
export type KeyringAccountEntropyCustomOptions = Infer<
typeof KeyringAccountEntropyCustomOptionsStruct
>;
/**
* Keyring account entropy options struct.
*/
export const KeyringAccountEntropyOptionsStruct = selectiveUnion(
(value: any) => {
if (!isPlainObject(value)) {
return KeyringAccountEntropyMnemonicOptionsStruct;
}
switch (value.type) {
case KeyringAccountEntropyTypeOption.PrivateKey:
return KeyringAccountEntropyPrivateKeyOptionsStruct;
case KeyringAccountEntropyTypeOption.Custom:
return KeyringAccountEntropyCustomOptionsStruct;
case KeyringAccountEntropyTypeOption.Mnemonic:
return KeyringAccountEntropyMnemonicOptionsStruct;
default:
return KeyringAccountEntropyMnemonicOptionsStruct;
}
},
);
/**
* Keyring account entropy options {@link KeyringAccountEntropyOptionsStruct}.
*/
export type KeyringAccountEntropyOptions = Infer<
typeof KeyringAccountEntropyOptionsStruct
>;
/**
* Keyring options struct. This represents various options for a Keyring account object.
*
* See {@link KeyringAccountEntropyMnemonicOptionsStruct},
* {@link KeyringAccountEntropyPrivateKeyOptionsStruct}, and
* {@link KeyringAccountEntropyCustomOptionsStruct}.
*
* @example
* ```ts
* {
* entropy: {
* type: 'mnemonic',
* id: '01K0BX6VDR5DPDPGGNA8PZVBVB',
* derivationPath: "m/44'/60'/0'/0/0",
* groupIndex: 0,
* },
* }
* ```
*
* @example
* ```ts
* {
* entropy: {
* type: 'private-key',
* },
* exportable: true,
* }
* ```
*
* @example
* ```ts
* {
* entropy: {
* type: 'custom',
* },
* }
* ```
*
* @example
* ```ts
* {
* some: {
* untyped: 'options',
* something: true,
* },
* }
* ```
*/
export const KeyringAccountOptionsStruct = intersection([
// Non-Typed options (retro-compatibility):
record(string(), JsonStruct),
// Typed options. We use `type` instead of `object` here, to allow
// extra properties. Also, since we use `record` + `intersection` we
// are guaranteed that all field values will match the `JsonStruct`.
//
// READ THIS CAREFULLY:
// Previous options that can be matched by this struct will be breaking
// existing keyring account options.
type({
/**
* Entropy options.
*/
entropy: exactOptional(KeyringAccountEntropyOptionsStruct),
/**
* Indicates whether the account can be exported.
*/
exportable: exactOptional(boolean()),
}),
]);
/**
* Keyring account options {@link KeyringAccountOptionsStruct}.
*/
export type KeyringAccountOptions = Infer<typeof KeyringAccountOptionsStruct>;