Skip to content

Commit d327019

Browse files
committed
fix tests
1 parent c74e27e commit d327019

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

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

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import {
2626
stringToBytes,
2727
bigIntToHex,
2828
} from '@metamask/utils';
29+
import { gcm } from '@noble/ciphers/aes';
30+
import { utf8ToBytes } from '@noble/ciphers/utils';
31+
import { managedNonce } from '@noble/ciphers/webcrypto';
2932
import type { webcrypto } from 'node:crypto';
3033

3134
import {
@@ -396,10 +399,14 @@ async function createMockVault(
396399
const { vault: encryptedMockVault, exportedKeyString } =
397400
await encryptor.encryptWithDetail(MOCK_PASSWORD, serializedKeyData);
398401

402+
const aes = managedNonce(gcm)(encKey);
403+
const encryptedPassword = aes.encrypt(utf8ToBytes(MOCK_PASSWORD));
404+
399405
return {
400406
encryptedMockVault,
401407
vaultEncryptionKey: exportedKeyString,
402408
vaultEncryptionSalt: JSON.parse(encryptedMockVault).salt,
409+
encryptedPassword,
403410
revokeToken: mockRevokeToken,
404411
};
405412
}
@@ -445,6 +452,7 @@ async function decryptVault(vault: string, password: string) {
445452
* @param options.vault - The mock vault data.
446453
* @param options.vaultEncryptionKey - The mock vault encryption key.
447454
* @param options.vaultEncryptionSalt - The mock vault encryption salt.
455+
* @param options.encryptedPassword - The mock encrypted password.
448456
* @returns The initial controller state with the mock authenticated user.
449457
*/
450458
function getMockInitialControllerState(options?: {
@@ -455,6 +463,7 @@ function getMockInitialControllerState(options?: {
455463
vault?: string;
456464
vaultEncryptionKey?: string;
457465
vaultEncryptionSalt?: string;
466+
encryptedPassword?: string;
458467
}): Partial<SeedlessOnboardingControllerState> {
459468
const state = getDefaultSeedlessOnboardingControllerState();
460469

@@ -486,6 +495,10 @@ function getMockInitialControllerState(options?: {
486495
state.authPubKey = options.authPubKey ?? MOCK_AUTH_PUB_KEY;
487496
}
488497

498+
if (options?.encryptedPassword) {
499+
state.encryptedPassword = options.encryptedPassword;
500+
}
501+
489502
return state;
490503
}
491504

@@ -2955,6 +2968,14 @@ describe('SeedlessOnboardingController', () => {
29552968
}),
29562969
},
29572970
async ({ controller, toprfClient }) => {
2971+
await mockCreateToprfKeyAndBackupSeedPhrase(
2972+
toprfClient,
2973+
controller,
2974+
RECOVERED_PASSWORD,
2975+
MOCK_SEED_PHRASE,
2976+
MOCK_KEYRING_ID,
2977+
);
2978+
29582979
// Mock recoverEncKey for the global password
29592980
const mockToprfEncryptor = createMockToprfEncryptor();
29602981
const encKey = mockToprfEncryptor.deriveEncKey(GLOBAL_PASSWORD);
@@ -2968,8 +2989,10 @@ describe('SeedlessOnboardingController', () => {
29682989
});
29692990

29702991
// Mock toprfClient.recoverPassword
2992+
const recoveredEncKey =
2993+
mockToprfEncryptor.deriveEncKey(RECOVERED_PASSWORD);
29712994
jest.spyOn(toprfClient, 'recoverPassword').mockResolvedValueOnce({
2972-
password: RECOVERED_PASSWORD,
2995+
password: bytesToBase64(recoveredEncKey),
29732996
});
29742997

29752998
const result = await controller.recoverCurrentDevicePassword({
@@ -2983,6 +3006,45 @@ describe('SeedlessOnboardingController', () => {
29833006
);
29843007
});
29853008

3009+
it('should throw if encryptedPassword not set', async () => {
3010+
await withController(
3011+
{
3012+
state: getMockInitialControllerState({
3013+
withMockAuthenticatedUser: true,
3014+
withMockAuthPubKey: true,
3015+
}),
3016+
},
3017+
async ({ controller, toprfClient }) => {
3018+
// Mock recoverEncKey for the global password
3019+
const mockToprfEncryptor = createMockToprfEncryptor();
3020+
const encKey = mockToprfEncryptor.deriveEncKey(GLOBAL_PASSWORD);
3021+
const authKeyPair =
3022+
mockToprfEncryptor.deriveAuthKeyPair(GLOBAL_PASSWORD);
3023+
jest.spyOn(toprfClient, 'recoverEncKey').mockResolvedValueOnce({
3024+
encKey,
3025+
authKeyPair,
3026+
rateLimitResetResult: Promise.resolve(),
3027+
keyShareIndex: 1,
3028+
});
3029+
3030+
// Mock toprfClient.recoverPassword
3031+
const recoveredEncKey =
3032+
mockToprfEncryptor.deriveEncKey(RECOVERED_PASSWORD);
3033+
jest.spyOn(toprfClient, 'recoverPassword').mockResolvedValueOnce({
3034+
password: bytesToBase64(recoveredEncKey),
3035+
});
3036+
3037+
await expect(
3038+
controller.recoverCurrentDevicePassword({
3039+
globalPassword: GLOBAL_PASSWORD,
3040+
}),
3041+
).rejects.toThrow(
3042+
SeedlessOnboardingControllerErrorMessage.CouldNotRecoverPassword,
3043+
);
3044+
},
3045+
);
3046+
});
3047+
29863048
it('should throw SRPNotBackedUpError if no authPubKey in state', async () => {
29873049
await withController(
29883050
{
@@ -4071,6 +4133,7 @@ describe('SeedlessOnboardingController', () => {
40714133
let INITIAL_AUTH_PUB_KEY: string;
40724134
let initialAuthKeyPair: KeyPair; // Store initial keypair for vault creation
40734135
let initialEncKey: Uint8Array; // Store initial encKey for vault creation
4136+
let initialEncryptedPassword: Uint8Array;
40744137

40754138
// Generate initial keys and vault state before tests run
40764139
beforeAll(async () => {
@@ -4090,6 +4153,7 @@ describe('SeedlessOnboardingController', () => {
40904153
MOCK_VAULT = mockResult.encryptedMockVault;
40914154
MOCK_VAULT_ENCRYPTION_KEY = mockResult.vaultEncryptionKey;
40924155
MOCK_VAULT_ENCRYPTION_SALT = mockResult.vaultEncryptionSalt;
4156+
initialEncryptedPassword = mockResult.encryptedPassword;
40934157
});
40944158

40954159
it('should retry recoverCurrentDevicePassword after refreshing expired tokens', async () => {
@@ -4101,6 +4165,7 @@ describe('SeedlessOnboardingController', () => {
41014165
vault: MOCK_VAULT,
41024166
vaultEncryptionKey: MOCK_VAULT_ENCRYPTION_KEY,
41034167
vaultEncryptionSalt: MOCK_VAULT_ENCRYPTION_SALT,
4168+
encryptedPassword: bytesToBase64(initialEncryptedPassword),
41044169
}),
41054170
},
41064171
async ({ controller, toprfClient, mockRefreshJWTToken }) => {
@@ -4122,7 +4187,7 @@ describe('SeedlessOnboardingController', () => {
41224187
);
41234188
})
41244189
.mockResolvedValueOnce({
4125-
password: MOCK_PASSWORD,
4190+
password: bytesToBase64(initialEncKey),
41264191
});
41274192

41284193
// Mock authenticate for token refresh

0 commit comments

Comments
 (0)