From 6388669ca8be6d8fb52252dc68f739d140f72e07 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 12 Dec 2023 16:12:34 -0500 Subject: [PATCH] refactor: bug fixes and return early if the entry is the same --- src/methods/registry.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/methods/registry.ts b/src/methods/registry.ts index 6b40f2a..554daf4 100644 --- a/src/methods/registry.ts +++ b/src/methods/registry.ts @@ -137,27 +137,34 @@ export async function createEntry( sk = createKeyPair(sk); } + let existing = true; let entry = await this.getEntry(sk.publicKey); if (!entry) { + existing = false; entry = { - pk: sk, + pk: sk.publicKey, data: cid.toRegistryEntry(), revision, } as unknown as SignedRegistryEntry; - } else { - if (!equalBytes(sk.publicKey, entry.pk)) { - throwValidationError( - "entry.pk", // name of the variable - Buffer.from(entry.pk).toString("hex"), // actual value - "result", // valueKind (assuming it's a function parameter) - Buffer.from(sk.publicKey).toString("hex"), // expected description - ); + } + + if (!equalBytes(sk.publicKey, entry.pk)) { + throwValidationError( + "entry.pk", // name of the variable + Buffer.from(entry.pk).toString("hex"), // actual value + "result", // valueKind (assuming it's a function parameter) + Buffer.from(sk.publicKey).toString("hex"), // expected description + ); + } + + if (existing) { + if (equalBytes(entry.data, cid.toRegistryEntry())) { + return entry; } entry.revision++; } - const signedEntry = signRegistryEntry({ kp: sk, data: entry.data,