refactor: bug fixes and return early if the entry is the same

This commit is contained in:
Derrick Hammer 2023-12-12 16:12:34 -05:00
parent 9ecea29244
commit 6388669ca8
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 17 additions and 10 deletions

View File

@ -137,15 +137,18 @@ export async function createEntry(
sk = createKeyPair(sk); sk = createKeyPair(sk);
} }
let existing = true;
let entry = await this.getEntry(sk.publicKey); let entry = await this.getEntry(sk.publicKey);
if (!entry) { if (!entry) {
existing = false;
entry = { entry = {
pk: sk, pk: sk.publicKey,
data: cid.toRegistryEntry(), data: cid.toRegistryEntry(),
revision, revision,
} as unknown as SignedRegistryEntry; } as unknown as SignedRegistryEntry;
} else { }
if (!equalBytes(sk.publicKey, entry.pk)) { if (!equalBytes(sk.publicKey, entry.pk)) {
throwValidationError( throwValidationError(
"entry.pk", // name of the variable "entry.pk", // name of the variable
@ -155,9 +158,13 @@ export async function createEntry(
); );
} }
entry.revision++; if (existing) {
if (equalBytes(entry.data, cid.toRegistryEntry())) {
return entry;
} }
entry.revision++;
}
const signedEntry = signRegistryEntry({ const signedEntry = signRegistryEntry({
kp: sk, kp: sk,
data: entry.data, data: entry.data,