Skip to content

Commit d2326b8

Browse files
committed
Fix coverage
1 parent c5e08f9 commit d2326b8

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 93.34,
3-
"functions": 97.37,
4-
"lines": 98.33,
2+
"branches": 93.36,
3+
"functions": 97.38,
4+
"lines": 98.34,
55
"statements": 98.07
66
}

packages/snaps-controllers/src/snaps/SnapController.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9736,10 +9736,27 @@ describe('SnapController', () => {
97369736
expect(encryptWithKey).not.toHaveBeenCalled();
97379737

97389738
jest.advanceTimersByTime(5_000);
9739-
97409739
await promise;
9740+
97419741
expect(encryptWithKey).toHaveBeenCalledTimes(1);
97429742

9743+
const nextStateChange = waitForStateChange(messenger);
9744+
await messenger.call(
9745+
'SnapController:updateSnapState',
9746+
MOCK_SNAP_ID,
9747+
{ qux: 'quux' },
9748+
true,
9749+
);
9750+
9751+
expect(
9752+
await messenger.call('SnapController:getSnapState', MOCK_SNAP_ID, true),
9753+
).toStrictEqual({ qux: 'quux' });
9754+
9755+
jest.advanceTimersByTime(5_000);
9756+
await nextStateChange;
9757+
9758+
expect(encryptWithKey).toHaveBeenCalledTimes(2);
9759+
97439760
snapController.destroy();
97449761
});
97459762

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,23 @@ export class SnapController extends BaseController<
17991799
return truncateSnap(this.getExpect(snapId));
18001800
}
18011801

1802+
/**
1803+
* Check if a given Snap has a cached encryption key stored in the runtime.
1804+
*
1805+
* @param snapId - The Snap ID.
1806+
* @param runtime - The Snap runtime data.
1807+
* @returns True if the Snap has a cached encryption key, otherwise false.
1808+
*/
1809+
#hasCachedEncryptionKey(
1810+
snapId: SnapId,
1811+
runtime = this.#getRuntimeExpect(snapId),
1812+
): runtime is SnapRuntimeData & {
1813+
encryptionKey: string;
1814+
encryptionSalt: string;
1815+
} {
1816+
return runtime.encryptionKey !== null && runtime.encryptionSalt !== null;
1817+
}
1818+
18021819
/**
18031820
* Generate an encryption key to be used for state encryption for a given Snap.
18041821
*
@@ -1822,7 +1839,7 @@ export class SnapController extends BaseController<
18221839
}): Promise<{ key: unknown; salt: string }> {
18231840
const runtime = this.#getRuntimeExpect(snapId);
18241841

1825-
if (runtime.encryptionKey && runtime.encryptionSalt && useCache) {
1842+
if (this.#hasCachedEncryptionKey(snapId, runtime) && useCache) {
18261843
return {
18271844
key: await this.#encryptor.importKey(runtime.encryptionKey),
18281845
salt: runtime.encryptionSalt,
@@ -1854,17 +1871,6 @@ export class SnapController extends BaseController<
18541871
return { key: encryptionKey, salt };
18551872
}
18561873

1857-
/**
1858-
* Check if a given Snap has a cached encryption key stored in the runtime.
1859-
*
1860-
* @param snapId - The Snap ID.
1861-
* @returns True if the Snap has a cached encryption key, otherwise false.
1862-
*/
1863-
#hasCachedEncryptionKey(snapId: SnapId) {
1864-
const runtime = this.#getRuntimeExpect(snapId);
1865-
return runtime.encryptionKey !== null && runtime.encryptionSalt !== null;
1866-
}
1867-
18681874
/**
18691875
* Decrypt the encrypted state for a given Snap.
18701876
*

0 commit comments

Comments
 (0)