refactor: extract signRegistryEntry as a utility function and make the instance method a wrapper

This commit is contained in:
Derrick Hammer 2023-09-07 12:34:05 -04:00
parent 085c97354e
commit 3c789459ec
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 27 additions and 15 deletions

View File

@ -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<SignedRegistryEntry | null> {
@ -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),
};
}