From 66612e9afc078eb58d046b32b9c4499c187c9304 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 4 Sep 2023 08:48:01 -0400 Subject: [PATCH] refactor: make instance methods deserializeRegistryEntry, verifyRegistryEntry, serializeRegistryEntry wrappers of the functional ones --- src/service/registry.ts | 59 +++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/src/service/registry.ts b/src/service/registry.ts index 832a1f9..562e6f4 100644 --- a/src/service/registry.ts +++ b/src/service/registry.ts @@ -234,37 +234,50 @@ export class RegistryService { } public deserializeRegistryEntry(event: Uint8Array): SignedRegistryEntry { - const dataLength = event[42]; - return { - pk: event.slice(1, 34), - revision: decodeEndian(event.slice(34, 42)), - data: event.slice(43, 43 + dataLength), - signature: event.slice(43 + dataLength), - }; + return deserializeRegistryEntry(event); } public verifyRegistryEntry(sre: SignedRegistryEntry): boolean { - const list: Uint8Array = Uint8Array.from([ - recordTypeRegistryEntry, - ...encodeEndian(sre.revision, 8), - sre.data.length, // 1 byte - ...sre.data, - ]); - - return ed25519.verify(sre.signature, list, sre.pk.slice(1)); + return verifyRegistryEntry(sre); } public serializeRegistryEntry(sre: SignedRegistryEntry): Uint8Array { - return Uint8Array.from([ - recordTypeRegistryEntry, - ...sre.pk, - ...encodeEndian(sre.revision, 8), - sre.data.length, - ...sre.data, - ...sre.signature, - ]); + return serializeRegistryEntry(sre); } } async function pTimeout(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); } + +export function deserializeRegistryEntry( + event: Uint8Array, +): SignedRegistryEntry { + const dataLength = event[42]; + return { + pk: event.slice(1, 34), + revision: decodeEndian(event.slice(34, 42)), + data: event.slice(43, 43 + dataLength), + signature: event.slice(43 + dataLength), + }; +} + +export function verifyRegistryEntry(sre: SignedRegistryEntry): boolean { + const list: Uint8Array = Uint8Array.from([ + recordTypeRegistryEntry, + ...encodeEndian(sre.revision, 8), + sre.data.length, // 1 byte + ...sre.data, + ]); + + return ed25519.verify(sre.signature, list, sre.pk.slice(1)); +} +export function serializeRegistryEntry(sre: SignedRegistryEntry): Uint8Array { + return Uint8Array.from([ + recordTypeRegistryEntry, + ...sre.pk, + ...encodeEndian(sre.revision, 8), + sre.data.length, + ...sre.data, + ...sre.signature, + ]); +}