Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle primary SRP switch cases #5478

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,14 @@ describe('authentication/authentication-controller - performSignIn() tests', ()
it('leverages the _snapSignMessageCache', async () => {
const metametrics = createMockAuthMetaMetrics();
const mockEndpoints = arrangeAuthAPIs();
const { messenger, mockSnapGetPublicKey, mockSnapSignMessage } =
const { messenger, mockSnapSignMessage } =
createMockAuthenticationMessenger();

const controller = new AuthenticationController({ messenger, metametrics });

await controller.performSignIn();
controller.performSignOut();
await controller.performSignIn();
expect(mockSnapGetPublicKey).toHaveBeenCalledTimes(1);
expect(mockSnapSignMessage).toHaveBeenCalledTimes(1);
mockEndpoints.mockNonceUrl.done();
mockEndpoints.mockSrpLoginUrl.done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,20 @@ export default class AuthenticationController extends BaseController<
return null;
}

return {
...this.state.sessionData,
profile: {
...this.state.sessionData.profile,
metaMetricsId: await this.#metametrics.getMetaMetricsId(),
},
};
return this.state.sessionData;
}

async #setLoginResponseToState(loginResponse: LoginResponse) {
const metaMetricsId = await this.#metametrics.getMetaMetricsId();
this.update((state) => {
state.isSignedIn = true;
state.sessionData = loginResponse;
state.sessionData = {
...loginResponse,
profile: {
...loginResponse.profile,
metaMetricsId,
},
};
});
}

Expand Down Expand Up @@ -280,27 +281,19 @@ export default class AuthenticationController extends BaseController<
return this.state.isSignedIn;
}

#_snapPublicKeyCache: string | undefined;

/**
* Returns the auth snap public key.
*
* @returns The snap public key.
*/
async #snapGetPublicKey(): Promise<string> {
if (this.#_snapPublicKeyCache) {
return this.#_snapPublicKeyCache;
}

this.#assertIsUnlocked('#snapGetPublicKey');

const result = (await this.messagingSystem.call(
'SnapController:handleRequest',
createSnapPublicKeyRequest(),
)) as string;

this.#_snapPublicKeyCache = result;

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export default class UserStorageController extends BaseController<

#isUnlocked = false;

#storageKeyCache: string | null = null;
#storageKeyCache: Record<`metamask:${string}`, string> = {};

readonly #keyringController = {
setupLockedStateSubscriptions: () => {
Expand Down Expand Up @@ -377,9 +377,10 @@ export default class UserStorageController extends BaseController<
},
{
storage: {
getStorageKey: async () => this.#storageKeyCache,
setStorageKey: async (key) => {
this.#storageKeyCache = key;
getStorageKey: async (message) =>
this.#storageKeyCache[message] ?? null,
setStorageKey: async (message, key) => {
this.#storageKeyCache[message] = key;
},
},
},
Expand Down Expand Up @@ -596,8 +597,13 @@ export default class UserStorageController extends BaseController<
return await this.#userStorage.getStorageKey();
}

/**
* Flushes the storage key cache.
* CAUTION: This is only public for testing purposes.
* It should not be used in production code.
*/
public flushStorageKeyCache(): void {
this.#storageKeyCache = null;
this.#storageKeyCache = {};
}

#_snapSignMessageCache: Record<`metamask:${string}`, string> = {};
Expand Down
18 changes: 18 additions & 0 deletions packages/profile-sync-controller/src/sdk/user-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,24 @@ describe('User Storage', () => {
);
expect(mockAuthSignMessage).toHaveBeenCalled(); // SignMessage called since generating new key
});

it('uses existing storage key (in storage)', async () => {
const { auth } = arrangeAuth('SRP', MOCK_SRP);
const { userStorage, mockGetStorageKey } = arrangeUserStorage(auth);
mockGetStorageKey.mockResolvedValue(MOCK_STORAGE_KEY);

const mockAuthSignMessage = jest
.spyOn(auth, 'signMessage')
.mockResolvedValue(MOCK_STORAGE_KEY);

handleMockUserStoragePut();

await userStorage.setItem(
`${USER_STORAGE_FEATURE_NAMES.notifications}.notification_settings`,
'some fake data',
);
expect(mockAuthSignMessage).not.toHaveBeenCalled(); // SignMessage not called since key already exists
});
});

/**
Expand Down
19 changes: 11 additions & 8 deletions packages/profile-sync-controller/src/sdk/user-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export type UserStorageConfig = {
};

export type StorageOptions = {
getStorageKey: () => Promise<string | null>;
setStorageKey: (val: string) => Promise<void>;
getStorageKey: (message: `metamask:${string}`) => Promise<string | null>;
setStorageKey: (message: `metamask:${string}`, val: string) => Promise<void>;
};

export type UserStorageOptions = {
Expand Down Expand Up @@ -110,17 +110,20 @@ export class UserStorage {
}

async getStorageKey(): Promise<string> {
const storageKey = await this.options.storage?.getStorageKey();
const userProfile = await this.config.auth.getUserProfile();
const message = `metamask:${userProfile.profileId}` as const;

const storageKey = await this.options.storage?.getStorageKey(message);
if (storageKey) {
return storageKey;
}

const userProfile = await this.config.auth.getUserProfile();
const storageKeySignature = await this.config.auth.signMessage(
`metamask:${userProfile.profileId}`,
);
const storageKeySignature = await this.config.auth.signMessage(message);
const hashedStorageKeySignature = createSHA256Hash(storageKeySignature);
await this.options.storage?.setStorageKey(hashedStorageKeySignature);
await this.options.storage?.setStorageKey(
message,
hashedStorageKeySignature,
);
return hashedStorageKeySignature;
}

Expand Down
Loading