From 3c789459ec7ab116167bb48f4744c6b18d0cc20f Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 7 Sep 2023 12:34:05 -0400 Subject: [PATCH] refactor: extract signRegistryEntry as a utility function and make the instance method a wrapper --- src/service/registry.ts | 42 ++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/service/registry.ts b/src/service/registry.ts index 562e6f4..bc4bc18 100644 --- a/src/service/registry.ts +++ b/src/service/registry.ts @@ -183,21 +183,7 @@ export class RegistryService { data: Uint8Array; revision: number; }): SignedRegistryEntry { - const list = new Uint8Array([ - recordTypeRegistryEntry, - ...encodeEndian(revision, 8), - data.length, - ...data, - ]); - - const signature = ed25519.sign(list, kp.extractBytes()); - - return { - pk: kp.publicKey, - revision, - data, - signature: new Uint8Array(signature), - }; + return this.signRegistryEntry({ kp, data, revision }); } async getFromDB(pk: Uint8Array): Promise { @@ -281,3 +267,29 @@ export function serializeRegistryEntry(sre: SignedRegistryEntry): Uint8Array { ...sre.signature, ]); } + +export function signRegistryEntry({ + kp, + data, + revision, +}: { + kp: KeyPairEd25519; + data: Uint8Array; + revision: number; +}): SignedRegistryEntry { + const list = new Uint8Array([ + recordTypeRegistryEntry, + ...encodeEndian(revision, 8), + data.length, + ...data, + ]); + + const signature = ed25519.sign(list, kp.extractBytes()); + + return { + pk: kp.publicKey, + revision, + data, + signature: new Uint8Array(signature), + }; +}