-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathqr-keyring-v2.ts
More file actions
439 lines (387 loc) · 12.7 KB
/
Copy pathqr-keyring-v2.ts
File metadata and controls
439 lines (387 loc) · 12.7 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import type { Bip44Account } from '@metamask/account-api';
import {
type CreateAccountOptions,
EthAccountType,
EthKeyringWrapper,
EthMethod,
EthScope,
type KeyringAccount,
KeyringAccountEntropyTypeOption,
type KeyringCapabilities,
type KeyringV2,
KeyringType,
type EntropySourceId,
type CreateAccountBip44DeriveIndexOptions,
} from '@metamask/keyring-api';
import type { AccountId } from '@metamask/keyring-utils';
import type { Hex } from '@metamask/utils';
import { DeviceMode } from './device';
import type { QrKeyring } from './qr-keyring';
/**
* Methods supported by QR keyring EOA accounts.
* QR keyrings support a subset of signing methods (no encryption, app keys, or EIP-7702).
*/
const QR_KEYRING_METHODS = [
EthMethod.SignTransaction,
EthMethod.PersonalSign,
EthMethod.SignTypedDataV4,
];
/**
* Capabilities for HD mode devices (index-based derivation supported).
*/
const HD_MODE_CAPABILITIES: KeyringCapabilities = {
scopes: [EthScope.Eoa],
bip44: {
deriveIndex: true,
derivePath: false,
discover: false,
},
};
/**
* Capabilities for Account mode devices (custom account selection).
*/
const ACCOUNT_MODE_CAPABILITIES: KeyringCapabilities = {
scopes: [EthScope.Eoa],
custom: {
createAccounts: true,
},
};
/**
* Custom options for creating accounts on Account mode QR devices.
* Account mode devices provide pre-defined addresses that must be selected by index.
*/
export type QrAccountModeCreateOptions = {
type: 'custom';
/**
* The entropy source ID (device fingerprint) to verify we're targeting the correct device.
*/
entropySource: EntropySourceId;
/**
* The index of the pre-defined address to add from the device.
* This refers to the position in the device's address list, not a BIP-44 derivation index.
*/
addressIndex: number;
};
/**
* Account creation options for QR keyring.
* Excludes unsupported types (custom, private-key:import) and adds our specific QrAccountModeCreateOptions.
*/
export type QrKeyringCreateAccountOptions =
| CreateAccountBip44DeriveIndexOptions
| QrAccountModeCreateOptions;
/**
* Concrete {@link KeyringV2} adapter for {@link QrKeyring}.
*
* This wrapper exposes the accounts and signing capabilities of the legacy
* QR keyring via the unified V2 interface.
*
* Account handling differs by device mode:
* - **HD mode**: Accounts are derived by index with `groupIndex` values.
* Supports `bip44:derive-index` for index-based derivation.
* - **Account mode**: Accounts are treated as private key imports since the
* device provides pre-defined addresses with arbitrary paths. Uses `custom`
* account creation type to select addresses by their position in the device.
*/
export type QrKeyringV2Options = {
legacyKeyring: QrKeyring;
entropySource: EntropySourceId;
};
export class QrKeyringV2
extends EthKeyringWrapper<QrKeyring>
implements KeyringV2
{
readonly entropySource: EntropySourceId;
constructor(options: QrKeyringV2Options) {
super({
type: KeyringType.Qr,
inner: options.legacyKeyring,
// Default capabilities - overridden by getter below
capabilities: HD_MODE_CAPABILITIES,
});
this.entropySource = options.entropySource;
}
/**
* Get the capabilities of this keyring.
*
* Overrides the base class getter to return capabilities dynamically
* based on the current device mode.
*
* @returns The keyring's capabilities.
*/
override get capabilities(): KeyringCapabilities {
return this.inner.getMode() === DeviceMode.ACCOUNT
? ACCOUNT_MODE_CAPABILITIES
: HD_MODE_CAPABILITIES;
}
/**
* Get the device state from the inner keyring.
*
* @returns The device state, or undefined if no device is paired.
*/
async #getDeviceState(): Promise<
| {
mode: DeviceMode.HD;
indexes: Record<Hex, number>;
}
| {
mode: DeviceMode.ACCOUNT;
indexes: Record<Hex, number>;
paths: Record<Hex, string>;
}
| undefined
> {
const state = await this.inner.serialize();
if (!state?.initialized) {
return undefined;
}
if (state.keyringMode === DeviceMode.ACCOUNT) {
return {
mode: DeviceMode.ACCOUNT,
indexes: state.indexes,
paths: state.paths,
};
}
return {
mode: DeviceMode.HD,
indexes: state.indexes,
};
}
/**
* Creates a Bip44Account for HD mode devices.
*
* @param address - The account address.
* @param addressIndex - The account index in the derivation path.
* @returns The created Bip44Account.
*/
#createHdModeAccount(
address: Hex,
addressIndex: number,
): Bip44Account<KeyringAccount> {
const id = this.registry.register(address);
const derivationPath = this.inner.getPathFromAddress(address);
if (!derivationPath) {
throw new Error(
`Cannot create account for address ${address}: derivation path not found in keyring.`,
);
}
const account: Bip44Account<KeyringAccount> = {
id,
type: EthAccountType.Eoa,
address,
scopes: [...this.capabilities.scopes],
methods: [...QR_KEYRING_METHODS],
options: {
entropy: {
type: KeyringAccountEntropyTypeOption.Mnemonic,
id: this.entropySource,
groupIndex: addressIndex,
derivationPath,
},
},
};
this.registry.set(account);
return account;
}
/**
* Creates a KeyringAccount for Account mode devices.
*
* Account mode devices provide pre-defined addresses with arbitrary derivation
* paths, so we treat them as private key imports rather than BIP-44 accounts.
*
* @param address - The account address.
* @returns The created KeyringAccount.
*/
#createAccountModeAccount(address: Hex): KeyringAccount {
const id = this.registry.register(address);
const account: KeyringAccount = {
id,
type: EthAccountType.Eoa,
address,
scopes: [...this.capabilities.scopes],
methods: [...QR_KEYRING_METHODS],
options: {
entropy: {
type: KeyringAccountEntropyTypeOption.Custom,
},
},
};
this.registry.set(account);
return account;
}
async getAccounts(): Promise<KeyringAccount[]> {
const addresses = await this.inner.getAccounts();
const deviceState = await this.#getDeviceState();
if (!deviceState) {
// No device paired yet, return empty
return [];
}
const { mode, indexes } = deviceState;
return addresses.map((address) => {
// Check if we already have this account in the registry
const existingId = this.registry.getAccountId(address);
if (existingId) {
const cached = this.registry.get(existingId);
if (cached) {
return cached;
}
}
if (mode === DeviceMode.HD) {
// HD mode: index must be in the map
const addressIndex = indexes[address];
if (addressIndex === undefined) {
throw new Error(
`Address ${address} not found in device indexes. This indicates an inconsistent keyring state.`,
);
}
return this.#createHdModeAccount(address, addressIndex);
}
// Account mode: validate address exists in paths map
const { paths } = deviceState;
if (paths[address] === undefined) {
throw new Error(
`Address ${address} not found in device paths. This indicates an inconsistent keyring state.`,
);
}
return this.#createAccountModeAccount(address);
});
}
async createAccounts(
options: QrKeyringCreateAccountOptions,
): Promise<KeyringAccount[]> {
return this.withLock(async () => {
const deviceState = await this.#getDeviceState();
if (!deviceState) {
throw new Error('No device paired. Cannot create accounts.');
}
// Validate entropy source for all account creation types
if (options.entropySource !== this.entropySource) {
throw new Error(
`Entropy source mismatch: expected '${this.entropySource}', got '${options.entropySource}'`,
);
}
// Handle Account mode with custom account creation
if (deviceState.mode === DeviceMode.ACCOUNT) {
if (options.type !== 'custom') {
throw new Error(
`Account mode devices only support 'custom' account creation type, got '${options.type}'. ` +
`Use { type: 'custom', entropySource, addressIndex } to select a pre-defined address.`,
);
}
return this.#createAccountModeAccounts(options, deviceState);
}
// HD mode: support derive-index
return this.#createHdModeAccounts(options, deviceState);
});
}
/**
* Creates accounts for Account mode devices using custom options.
*
* @param options - The account creation options.
* @param deviceState - The current device state.
* @param deviceState.mode - The device mode (ACCOUNT).
* @param deviceState.paths - Map of addresses to derivation paths.
* @param deviceState.indexes - Map of addresses to their indexes.
* @returns The created accounts.
*/
async #createAccountModeAccounts(
options: QrAccountModeCreateOptions,
deviceState: {
mode: DeviceMode.ACCOUNT;
paths: Record<Hex, string>;
indexes: Record<Hex, number>;
},
): Promise<KeyringAccount[]> {
const { addressIndex } = options;
if (!Number.isInteger(addressIndex) || addressIndex < 0) {
throw new Error(
`Invalid addressIndex: ${String(
addressIndex,
)}. Must be a non-negative integer.`,
);
}
// Get available addresses from paths
const availableAddresses = Object.keys(deviceState.paths) as Hex[];
if (addressIndex >= availableAddresses.length) {
throw new Error(
`Address index ${addressIndex} is out of bounds. Device has ${availableAddresses.length} pre-defined address(es).`,
);
}
const address = availableAddresses[addressIndex];
// Check if already exists
const currentAccounts = await this.getAccounts();
const existingAccount = currentAccounts.find(
(account) => account.address.toLowerCase() === address?.toLowerCase(),
);
if (existingAccount) {
return [existingAccount];
}
// Add the account via the inner keyring
this.inner.setAccountToUnlock(addressIndex);
const [newAddress] = await this.inner.addAccounts(1);
if (!newAddress) {
throw new Error('Failed to create new account');
}
const newAccount = this.#createAccountModeAccount(newAddress);
return [newAccount];
}
/**
* Creates accounts for HD mode devices using index-based derivation.
*
* @param options - The account creation options.
* @param _deviceState - The current device state (reserved for future use).
* @param _deviceState.indexes - Map of addresses to their indexes.
* @returns The created accounts.
*/
async #createHdModeAccounts(
options: CreateAccountOptions,
_deviceState: { indexes: Record<Hex, number> },
): Promise<Bip44Account<KeyringAccount>[]> {
if (options.type !== 'bip44:derive-index') {
throw new Error(
`Unsupported account creation type for HD mode QrKeyring: ${String(
options.type,
)}. Supported type: 'bip44:derive-index'.`,
);
}
if (!Number.isInteger(options.groupIndex) || options.groupIndex < 0) {
throw new Error(
`Invalid groupIndex: ${options.groupIndex}. Must be a non-negative integer.`,
);
}
const targetIndex = options.groupIndex;
// Check if an account at this index already exists
const currentAccounts = await this.getAccounts();
const existingAccount = currentAccounts.find(
(account) =>
account.options.entropy?.type ===
KeyringAccountEntropyTypeOption.Mnemonic &&
account.options.entropy.groupIndex === targetIndex,
);
if (existingAccount) {
return [existingAccount as Bip44Account<KeyringAccount>];
}
// Derive the account at the specified index
this.inner.setAccountToUnlock(targetIndex);
const [newAddress] = await this.inner.addAccounts(1);
if (!newAddress) {
throw new Error('Failed to create new account');
}
const newAccount = this.#createHdModeAccount(newAddress, targetIndex);
return [newAccount];
}
/**
* Delete an account from the keyring.
*
* @param accountId - The account ID to delete.
*/
async deleteAccount(accountId: AccountId): Promise<void> {
await this.withLock(async () => {
const { address } = await this.getAccount(accountId);
const hexAddress = this.toHexAddress(address);
// Remove from the legacy keyring
this.inner.removeAccount(hexAddress);
// Remove from the registry
this.registry.delete(accountId);
});
}
}