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

BREAKING: Refactor encryption to enable caching #2316

Merged
merged 10 commits into from
Apr 10, 2024

Conversation

FrederikBolding
Copy link
Member

@FrederikBolding FrederikBolding commented Apr 8, 2024

This PR is a refactor of the Snap state encryption logic, it moves the logic from the manageState RPC method implementation to the SnapController. It re-implements the encryption/decryption logic using a different set of methods, as the previously used implementation had encapsulated the encryption key. With this refactor it is now possible to cache the encryption key used by the Snap in the runtime, making continuous requests to update the state much faster (numbers TBD).

With this refactor it is now the responsibility of the SnapController to manage encrypting/decrypting the Snap state and thus the method hooks getSnapState and updateSnapState have had breaking changes, they are now asynchronous and expected to fully handle encryption/decryption. Because of that, this change required changes to both snaps-jest and snaps-simulator for compatibility.

This will not work as-is on mobile, since the encryptor on mobile does not have feature parity with browser-passworder. This will need to be addressed before we can use this change on mobile.

Closes #2315

Copy link

socket-security bot commented Apr 9, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/locate-app@2.2.29 environment, filesystem, shell +1 28.7 MB hejny
npm/n12@1.8.32 None 0 68.7 kB hejny

🚮 Removed packages: npm/locate-app@2.1.0, npm/n12@0.4.0

View full report↗︎

packages/snaps-controllers/src/snaps/SnapController.ts Outdated Show resolved Hide resolved
packages/snaps-controllers/src/types/encryptor.ts Outdated Show resolved Hide resolved
packages/snaps-controllers/src/types/encryptor.ts Outdated Show resolved Hide resolved
packages/snaps-controllers/src/types/encryptor.ts Outdated Show resolved Hide resolved
packages/snaps-controllers/src/types/encryptor.ts Outdated Show resolved Hide resolved
@FrederikBolding FrederikBolding marked this pull request as ready for review April 9, 2024 12:33
@FrederikBolding FrederikBolding requested a review from a team as a code owner April 9, 2024 12:33
* @param opts - The options to use for key derivation.
* @returns The encryption key.
*/
keyFromPassword: <Type>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the key type is used in more functions, I would add it as generic for the type itself.

type ExportableKeyEncryptor<Key> = ... & {
  decryptWithKey: (key: Key, ...) => Promise<...>;
  importKey: <Key>(key: string) => Promise<Key>;
  keyFromPassword: (...) => Promise<Key>;
  // etc.
};

That guarantees we use the same key for an encryptor.

@FrederikBolding FrederikBolding merged commit f6ac2b0 into main Apr 10, 2024
148 checks passed
@FrederikBolding FrederikBolding deleted the fb/encryption-refactor branch April 10, 2024 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cache encryption keys for improved manageState performance
3 participants