Skip to content

Commit

Permalink
chore: profile sync controller - add snap caching (#4532)
Browse files Browse the repository at this point in the history
## Explanation

making multiple calls to snaps can cause an internal 429 too many
requests as snap requests are queued. This adds simple in-mem caching to
prevent calling the snap with the same values.

## References

[NOTIFY-847](https://consensyssoftware.atlassian.net/browse/NOTIFY-847)

## Changelog

### `@metamask/profile-sync-controller`

- **ADDED**: Internal in-memory caching to pre-installed snap calls
- **<CATEGORY>**: Your change here

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
Prithpal-Sooriya authored Jul 18, 2024
1 parent 4bc736a commit aa1912c
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -311,28 +311,48 @@ export default class AuthenticationController extends BaseController<
return THIRTY_MIN_MS > diffMs;
}

#_snapPublicKeyCache: string | undefined;

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

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

this.#_snapPublicKeyCache = result;

return result;
}

#_snapSignMessageCache: Record<`metamask:${string}`, string> = {};

/**
* Signs a specific message using an underlying auth snap.
*
* @param message - A specific tagged message to sign.
* @returns A Signature created by the snap.
*/
#snapSignMessage(message: `metamask:${string}`): Promise<string> {
return this.messagingSystem.call(
async #snapSignMessage(message: `metamask:${string}`): Promise<string> {
if (this.#_snapSignMessageCache[message]) {
return this.#_snapSignMessageCache[message];
}

const result = (await this.messagingSystem.call(
'SnapController:handleRequest',
createSnapSignMessageRequest(message),
) as Promise<string>;
)) as string;

this.#_snapSignMessageCache[message] = result;

return result;
}
}
Original file line number Diff line number Diff line change
@@ -373,17 +373,27 @@ export default class UserStorageController extends BaseController<
return storageKey;
}

#_snapSignMessageCache: Record<`metamask:${string}`, string> = {};

/**
* Signs a specific message using an underlying auth snap.
*
* @param message - A specific tagged message to sign.
* @returns A Signature created by the snap.
*/
#snapSignMessage(message: `metamask:${string}`): Promise<string> {
return this.messagingSystem.call(
async #snapSignMessage(message: `metamask:${string}`): Promise<string> {
if (this.#_snapSignMessageCache[message]) {
return this.#_snapSignMessageCache[message];
}

const result = (await this.messagingSystem.call(
'SnapController:handleRequest',
createSnapSignMessageRequest(message),
) as Promise<string>;
)) as string;

this.#_snapSignMessageCache[message] = result;

return result;
}

#setIsProfileSyncingUpdateLoading(

0 comments on commit aa1912c

Please sign in to comment.