Skip to content

Commit 62318cc

Browse files
committed
fix seedless-onboarding-controller tests
1 parent 307119c commit 62318cc

File tree

5 files changed

+170
-122
lines changed

5 files changed

+170
-122
lines changed

packages/keyring-controller/src/KeyringController.ts

Lines changed: 90 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -374,94 +374,96 @@ export type GenericEncryptor = {
374374
* An encryptor interface that supports encrypting and decrypting
375375
* serializable data with a password, and exporting and importing keys.
376376
*/
377-
export type ExportableKeyEncryptor<EncryptionKey = unknown> =
378-
GenericEncryptor & {
379-
/**
380-
* Encrypts the given object with the given encryption key.
381-
*
382-
* @param key - The encryption key to encrypt with.
383-
* @param object - The object to encrypt.
384-
* @returns The encryption result.
385-
*/
386-
encryptWithKey: (
387-
key: EncryptionKey,
388-
object: Json,
389-
) => Promise<encryptorUtils.EncryptionResult>;
390-
/**
391-
* Encrypts the given object with the given password, and returns the
392-
* encryption result and the exported key string.
393-
*
394-
* @param password - The password to encrypt with.
395-
* @param object - The object to encrypt.
396-
* @param salt - The optional salt to use for encryption.
397-
* @returns The encrypted string and the exported key string.
398-
*/
399-
encryptWithDetail: (
400-
password: string,
401-
object: Json,
402-
salt?: string,
403-
) => Promise<encryptorUtils.DetailedEncryptionResult>;
404-
/**
405-
* Decrypts the given encrypted string with the given encryption key.
406-
*
407-
* @param key - The encryption key to decrypt with.
408-
* @param encryptedString - The encrypted string to decrypt.
409-
* @returns The decrypted object.
410-
*/
411-
decryptWithKey: (
412-
key: EncryptionKey,
413-
encryptedString: string,
414-
) => Promise<unknown>;
415-
/**
416-
* Decrypts the given encrypted string with the given password, and returns
417-
* the decrypted object and the salt and exported key string used for
418-
* encryption.
419-
*
420-
* @param password - The password to decrypt with.
421-
* @param encryptedString - The encrypted string to decrypt.
422-
* @returns The decrypted object and the salt and exported key string used for
423-
* encryption.
424-
*/
425-
decryptWithDetail: (
426-
password: string,
427-
encryptedString: string,
428-
) => Promise<encryptorUtils.DetailedDecryptResult>;
429-
/**
430-
* Generates an encryption key from exported key string.
431-
*
432-
* @param key - The exported key string.
433-
* @returns The encryption key.
434-
*/
435-
importKey: (key: string) => Promise<EncryptionKey>;
436-
/**
437-
* Exports the encryption key as a string.
438-
*
439-
* @param key - The encryption key to export.
440-
* @returns The exported key string.
441-
*/
442-
exportKey: (key: EncryptionKey) => Promise<string>;
443-
/**
444-
* Derives an encryption key from a password.
445-
*
446-
* @param password - The password to derive the key from.
447-
* @param salt - The salt to use for key derivation.
448-
* @param exportable - Whether the key should be exportable or not.
449-
* @param options - Optional key derivation options.
450-
* @returns The derived encryption key.
451-
*/
452-
keyFromPassword: (
453-
password: string,
454-
salt: string,
455-
exportable?: boolean,
456-
// setting this to unknown as currently each client has different
457-
// key derivation options
458-
keyDerivationOptions?: unknown,
459-
) => Promise<EncryptionKey>;
460-
/**
461-
* Generates a random salt for key derivation.
462-
*/
463-
generateSalt: typeof encryptorUtils.generateSalt;
464-
};
377+
export type ExportableKeyEncryptor<
378+
EncryptionKey = unknown,
379+
SupportedKeyDerivationParams = unknown,
380+
> = GenericEncryptor & {
381+
/**
382+
* Encrypts the given object with the given encryption key.
383+
*
384+
* @param key - The encryption key to encrypt with.
385+
* @param object - The object to encrypt.
386+
* @returns The encryption result.
387+
*/
388+
encryptWithKey: (
389+
key: EncryptionKey,
390+
object: Json,
391+
) => Promise<encryptorUtils.EncryptionResult>;
392+
/**
393+
* Encrypts the given object with the given password, and returns the
394+
* encryption result and the exported key string.
395+
*
396+
* @param password - The password to encrypt with.
397+
* @param object - The object to encrypt.
398+
* @param salt - The optional salt to use for encryption.
399+
* @returns The encrypted string and the exported key string.
400+
*/
401+
encryptWithDetail: (
402+
password: string,
403+
object: Json,
404+
salt?: string,
405+
) => Promise<encryptorUtils.DetailedEncryptionResult>;
406+
/**
407+
* Decrypts the given encrypted string with the given encryption key.
408+
*
409+
* @param key - The encryption key to decrypt with.
410+
* @param encryptedString - The encrypted string to decrypt.
411+
* @returns The decrypted object.
412+
*/
413+
decryptWithKey: (
414+
key: EncryptionKey,
415+
encryptedString: string,
416+
) => Promise<unknown>;
417+
/**
418+
* Decrypts the given encrypted string with the given password, and returns
419+
* the decrypted object and the salt and exported key string used for
420+
* encryption.
421+
*
422+
* @param password - The password to decrypt with.
423+
* @param encryptedString - The encrypted string to decrypt.
424+
* @returns The decrypted object and the salt and exported key string used for
425+
* encryption.
426+
*/
427+
decryptWithDetail: (
428+
password: string,
429+
encryptedString: string,
430+
) => Promise<encryptorUtils.DetailedDecryptResult>;
431+
/**
432+
* Generates an encryption key from exported key string.
433+
*
434+
* @param key - The exported key string.
435+
* @returns The encryption key.
436+
*/
437+
importKey: (key: string) => Promise<EncryptionKey>;
438+
/**
439+
* Exports the encryption key as a string.
440+
*
441+
* @param key - The encryption key to export.
442+
* @returns The exported key string.
443+
*/
444+
exportKey: (key: EncryptionKey) => Promise<string>;
445+
/**
446+
* Derives an encryption key from a password.
447+
*
448+
* @param password - The password to derive the key from.
449+
* @param salt - The salt to use for key derivation.
450+
* @param exportable - Whether the key should be exportable or not.
451+
* @param options - Optional key derivation options.
452+
* @returns The derived encryption key.
453+
*/
454+
keyFromPassword: (
455+
password: string,
456+
salt: string,
457+
exportable?: boolean,
458+
// setting this to unknown as currently each client has different
459+
// key derivation options
460+
keyDerivationOptions?: SupportedKeyDerivationParams,
461+
) => Promise<EncryptionKey>;
462+
/**
463+
* Generates a random salt for key derivation.
464+
*/
465+
generateSalt: typeof encryptorUtils.generateSalt;
466+
};
465467

466468
export type KeyringSelector =
467469
| {

packages/seedless-onboarding-controller/src/SeedlessOnboardingController.test.ts

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { keccak256AndHexify } from '@metamask/auth-network-utils';
22
import type { Messenger } from '@metamask/base-controller';
3-
import type { EncryptionKey } from '@metamask/browser-passworder';
3+
import type {
4+
EncryptionKey,
5+
KeyDerivationOptions,
6+
} from '@metamask/browser-passworder';
47
import {
58
encrypt,
69
decrypt,
710
decryptWithDetail,
811
encryptWithDetail,
912
decryptWithKey as decryptWithKeyBrowserPassworder,
1013
importKey as importKeyBrowserPassworder,
14+
exportKey as exportKeyBrowserPassworder,
15+
generateSalt as generateSaltBrowserPassworder,
16+
keyFromPassword as keyFromPasswordBrowserPassworder,
1117
} from '@metamask/browser-passworder';
1218
import {
1319
TOPRFError,
@@ -99,27 +105,34 @@ const MOCK_AUTH_PUB_KEY = 'A09CwPHdl/qo2AjBOHen5d4QORaLedxOrSdgReq8IhzQ';
99105
const MOCK_AUTH_PUB_KEY_OUTDATED =
100106
'Ao2sa8imX7SD4KE4fJLoJ/iBufmaBxSFygG1qUhW2qAb';
101107

102-
type WithControllerCallback<ReturnValue, EKey> = ({
103-
controller,
104-
initialState,
105-
encryptor,
106-
messenger,
107-
}: {
108-
controller: SeedlessOnboardingController<EKey>;
109-
encryptor: VaultEncryptor<EKey>;
110-
initialState: SeedlessOnboardingControllerState;
111-
messenger: SeedlessOnboardingControllerMessenger;
112-
baseMessenger: Messenger<AllowedActions, AllowedEvents>;
113-
toprfClient: ToprfSecureBackup;
114-
}) => Promise<ReturnValue> | ReturnValue;
115-
116-
type WithControllerOptions<EKey> = Partial<
117-
SeedlessOnboardingControllerOptions<EKey>
108+
type WithControllerCallback<ReturnValue, EKey, SupportedKeyDerivationOptions> =
109+
({
110+
controller,
111+
initialState,
112+
encryptor,
113+
messenger,
114+
}: {
115+
controller: SeedlessOnboardingController<
116+
EKey,
117+
SupportedKeyDerivationOptions
118+
>;
119+
encryptor: VaultEncryptor<EKey, KeyDerivationOptions>;
120+
initialState: SeedlessOnboardingControllerState;
121+
messenger: SeedlessOnboardingControllerMessenger;
122+
baseMessenger: Messenger<AllowedActions, AllowedEvents>;
123+
toprfClient: ToprfSecureBackup;
124+
}) => Promise<ReturnValue> | ReturnValue;
125+
126+
type WithControllerOptions<EKey, SupportedKeyDerivationOptions> = Partial<
127+
SeedlessOnboardingControllerOptions<EKey, SupportedKeyDerivationOptions>
118128
>;
119129

120-
type WithControllerArgs<ReturnValue, EKey> =
121-
| [WithControllerCallback<ReturnValue, EKey>]
122-
| [WithControllerOptions<EKey>, WithControllerCallback<ReturnValue, EKey>];
130+
type WithControllerArgs<ReturnValue, EKey, SupportedKeyDerivationOptions> =
131+
| [WithControllerCallback<ReturnValue, EKey, SupportedKeyDerivationOptions>]
132+
| [
133+
WithControllerOptions<EKey, SupportedKeyDerivationOptions>,
134+
WithControllerCallback<ReturnValue, EKey, SupportedKeyDerivationOptions>,
135+
];
123136

124137
/**
125138
* Get the default vault encryptor for the Seedless Onboarding Controller.
@@ -139,6 +152,9 @@ function getDefaultSeedlessOnboardingVaultEncryptor() {
139152
payload: unknown,
140153
) => Promise<unknown>,
141154
importKey: importKeyBrowserPassworder,
155+
exportKey: exportKeyBrowserPassworder,
156+
generateSalt: generateSaltBrowserPassworder,
157+
keyFromPassword: keyFromPasswordBrowserPassworder,
142158
};
143159
}
144160

@@ -162,13 +178,20 @@ function createMockVaultEncryptor() {
162178
* @returns Whatever the callback returns.
163179
*/
164180
async function withController<ReturnValue>(
165-
...args: WithControllerArgs<ReturnValue, EncryptionKey | webcrypto.CryptoKey>
181+
...args: WithControllerArgs<
182+
ReturnValue,
183+
EncryptionKey | webcrypto.CryptoKey,
184+
KeyDerivationOptions
185+
>
166186
) {
167187
const [{ ...rest }, fn] = args.length === 2 ? args : [{}, args[0]];
168188
const encryptor = new MockVaultEncryptor();
169189
const { messenger, baseMessenger } = mockSeedlessOnboardingMessenger();
170190

171-
const controller = new SeedlessOnboardingController({
191+
const controller = new SeedlessOnboardingController<
192+
EncryptionKey | webcrypto.CryptoKey,
193+
KeyDerivationOptions
194+
>({
172195
encryptor,
173196
messenger,
174197
network: Web3AuthNetwork.Devnet,
@@ -313,9 +336,12 @@ function mockChangeEncKey(
313336
* @param seedPhrase - The mock seed phrase.
314337
* @param keyringId - The mock keyring id.
315338
*/
316-
async function mockCreateToprfKeyAndBackupSeedPhrase<EKey>(
339+
async function mockCreateToprfKeyAndBackupSeedPhrase<
340+
EKey,
341+
SupportedKeyDerivationOptions,
342+
>(
317343
toprfClient: ToprfSecureBackup,
318-
controller: SeedlessOnboardingController<EKey>,
344+
controller: SeedlessOnboardingController<EKey, SupportedKeyDerivationOptions>,
319345
password: string,
320346
seedPhrase: Uint8Array,
321347
keyringId: string,
@@ -462,7 +488,10 @@ describe('SeedlessOnboardingController', () => {
462488
describe('constructor', () => {
463489
it('should be able to instantiate', () => {
464490
const { messenger } = mockSeedlessOnboardingMessenger();
465-
const controller = new SeedlessOnboardingController({
491+
const controller = new SeedlessOnboardingController<
492+
EncryptionKey | webcrypto.CryptoKey,
493+
KeyDerivationOptions
494+
>({
466495
messenger,
467496
encryptor: getDefaultSeedlessOnboardingVaultEncryptor(),
468497
});

packages/seedless-onboarding-controller/src/SeedlessOnboardingController.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,18 @@ const seedlessOnboardingMetadata: StateMetadata<SeedlessOnboardingControllerStat
111111
},
112112
};
113113

114-
export class SeedlessOnboardingController<EncryptionKey> extends BaseController<
114+
export class SeedlessOnboardingController<
115+
EncryptionKey,
116+
SupportedKeyDerivationOptions,
117+
> extends BaseController<
115118
typeof controllerName,
116119
SeedlessOnboardingControllerState,
117120
SeedlessOnboardingControllerMessenger
118121
> {
119-
readonly #vaultEncryptor: VaultEncryptor<EncryptionKey>;
122+
readonly #vaultEncryptor: VaultEncryptor<
123+
EncryptionKey,
124+
SupportedKeyDerivationOptions
125+
>;
120126

121127
readonly #controllerOperationMutex = new Mutex();
122128

@@ -147,7 +153,10 @@ export class SeedlessOnboardingController<EncryptionKey> extends BaseController<
147153
encryptor,
148154
toprfKeyDeriver,
149155
network = Web3AuthNetwork.Mainnet,
150-
}: SeedlessOnboardingControllerOptions<EncryptionKey>) {
156+
}: SeedlessOnboardingControllerOptions<
157+
EncryptionKey,
158+
SupportedKeyDerivationOptions
159+
>) {
151160
super({
152161
name: controllerName,
153162
metadata: seedlessOnboardingMetadata,

packages/seedless-onboarding-controller/src/types.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ export type SeedlessOnboardingControllerMessenger = RestrictedMessenger<
157157
/**
158158
* Encryptor interface for encrypting and decrypting seedless onboarding vault.
159159
*/
160-
export type VaultEncryptor<EncryptionKey> = Omit<
161-
ExportableKeyEncryptor<EncryptionKey>,
160+
export type VaultEncryptor<EncryptionKey, KeyDerivationParams> = Omit<
161+
ExportableKeyEncryptor<EncryptionKey, KeyDerivationParams>,
162162
'encryptWithKey'
163163
>;
164164

@@ -189,7 +189,10 @@ export type ToprfKeyDeriver = {
189189
* @param state - The initial state to set on this controller.
190190
* @param encryptor - The encryptor to use for encrypting and decrypting seedless onboarding vault.
191191
*/
192-
export type SeedlessOnboardingControllerOptions<EncryptionKey> = {
192+
export type SeedlessOnboardingControllerOptions<
193+
EncryptionKey,
194+
SupportedKeyDerivationOptions,
195+
> = {
193196
messenger: SeedlessOnboardingControllerMessenger;
194197

195198
/**
@@ -202,7 +205,7 @@ export type SeedlessOnboardingControllerOptions<EncryptionKey> = {
202205
*
203206
* @default browser-passworder @link https://github.com/MetaMask/browser-passworder
204207
*/
205-
encryptor: VaultEncryptor<EncryptionKey>;
208+
encryptor: VaultEncryptor<EncryptionKey, SupportedKeyDerivationOptions>;
206209

207210
/**
208211
* Optional key derivation interface for the TOPRF client.

0 commit comments

Comments
 (0)