Skip to content

Commit 1adf53c

Browse files
Short-circuit when cached encryption key already exists
1 parent 54df384 commit 1adf53c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

packages/snaps-controllers/coverage.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"branches": 92.63,
2+
"branches": 92.69,
33
"functions": 96.65,
44
"lines": 97.97,
55
"statements": 97.67

packages/snaps-controllers/src/snaps/SnapController.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,17 @@ export class SnapController extends BaseController<
17341734
return { key: encryptionKey, salt };
17351735
}
17361736

1737+
/**
1738+
* Check if a given Snap has a cached encryption key stored in the runtime.
1739+
*
1740+
* @param snapId - The Snap ID.
1741+
* @returns True if the Snap has a cached encryption key, otherwise false.
1742+
*/
1743+
#hasCachedEncryptionKey(snapId: SnapId) {
1744+
const runtime = this.#getRuntimeExpect(snapId);
1745+
return runtime.encryptionKey !== null && runtime.encryptionSalt !== null;
1746+
}
1747+
17371748
/**
17381749
* Decrypt the encrypted state for a given Snap.
17391750
*
@@ -1748,7 +1759,11 @@ export class SnapController extends BaseController<
17481759
// This lets us skip JSON validation.
17491760
const parsed = JSON.parse(state) as EncryptionResult;
17501761
const { salt, keyMetadata } = parsed;
1751-
const useCache = this.#encryptor.isVaultUpdated(state);
1762+
1763+
// We only cache encryption keys if they are already cached or if the encryption key is using the latest key derivation params.
1764+
const useCache =
1765+
this.#hasCachedEncryptionKey(snapId) ||
1766+
this.#encryptor.isVaultUpdated(state);
17521767
const { key } = await this.#getSnapEncryptionKey({
17531768
snapId,
17541769
salt,

0 commit comments

Comments
 (0)