Skip to content

Commit ec7bdda

Browse files
perf: Skip updating registry if signature has not changed
1 parent 24b87d3 commit ec7bdda

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

packages/snaps-controllers/src/snaps/registry/json.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ describe('JsonSnapsRegistry', () => {
667667
"database": null,
668668
"databaseUnavailable": false,
669669
"lastUpdated": null,
670+
"signature": null,
670671
}
671672
`);
672673
});
@@ -681,6 +682,7 @@ describe('JsonSnapsRegistry', () => {
681682
"database": null,
682683
"databaseUnavailable": false,
683684
"lastUpdated": null,
685+
"signature": null,
684686
}
685687
`);
686688
});

packages/snaps-controllers/src/snaps/registry/json.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import type {
44
} from '@metamask/base-controller';
55
import { BaseController } from '@metamask/base-controller';
66
import type { Messenger } from '@metamask/messenger';
7-
import type { SnapsRegistryDatabase } from '@metamask/snaps-registry';
7+
import type {
8+
SnapsRegistryDatabase,
9+
SignatureStruct,
10+
} from '@metamask/snaps-registry';
811
import { verify } from '@metamask/snaps-registry';
912
import { getTargetVersion } from '@metamask/snaps-utils';
13+
import type { Infer } from '@metamask/superstruct';
1014
import type { Hex, SemVerRange, SemVerVersion } from '@metamask/utils';
1115
import {
1216
assert,
@@ -102,6 +106,7 @@ export type SnapsRegistryMessenger = Messenger<
102106

103107
export type SnapsRegistryState = {
104108
database: SnapsRegistryDatabase | null;
109+
signature: string | null;
105110
lastUpdated: number | null;
106111
databaseUnavailable: boolean;
107112
};
@@ -110,6 +115,7 @@ const controllerName = 'SnapsRegistry';
110115

111116
const defaultState = {
112117
database: null,
118+
signature: null,
113119
lastUpdated: null,
114120
databaseUnavailable: false,
115121
};
@@ -155,6 +161,12 @@ export class JsonSnapsRegistry extends BaseController<
155161
includeInDebugSnapshot: false,
156162
usedInUi: true,
157163
},
164+
signature: {
165+
includeInStateLogs: true,
166+
persist: true,
167+
includeInDebugSnapshot: true,
168+
usedInUi: false,
169+
},
158170
lastUpdated: {
159171
includeInStateLogs: true,
160172
persist: true,
@@ -244,7 +256,18 @@ export class JsonSnapsRegistry extends BaseController<
244256
this.#safeFetch(this.#url.signature),
245257
]);
246258

247-
await this.#verifySignature(database, signature);
259+
const signatureJson = JSON.parse(signature);
260+
261+
// If the signature matches the existing state, we can skip verification and don't need to update the database.
262+
if (signatureJson.signature === this.state.signature) {
263+
this.update((state) => {
264+
state.lastUpdated = Date.now();
265+
state.databaseUnavailable = false;
266+
});
267+
return;
268+
}
269+
270+
await this.#verifySignature(database, signatureJson);
248271

249272
this.update((state) => {
250273
state.database = JSON.parse(database);
@@ -402,12 +425,15 @@ export class JsonSnapsRegistry extends BaseController<
402425
* @param signature - The signature of the registry.
403426
* @throws If the signature is invalid.
404427
*/
405-
async #verifySignature(database: string, signature: string) {
428+
async #verifySignature(
429+
database: string,
430+
signature: Infer<typeof SignatureStruct>,
431+
) {
406432
assert(this.#publicKey, 'No public key provided.');
407433

408434
const valid = await verify({
409435
registry: database,
410-
signature: JSON.parse(signature),
436+
signature,
411437
publicKey: this.#publicKey,
412438
});
413439

0 commit comments

Comments
 (0)