refactor: make instance methods deserializeRegistryEntry, verifyRegistryEntry, serializeRegistryEntry wrappers of the functional ones

This commit is contained in:
Derrick Hammer 2023-09-04 08:48:01 -04:00
parent cb4f23ed7e
commit 66612e9afc
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 36 additions and 23 deletions

View File

@ -234,37 +234,50 @@ export class RegistryService {
} }
public deserializeRegistryEntry(event: Uint8Array): SignedRegistryEntry { public deserializeRegistryEntry(event: Uint8Array): SignedRegistryEntry {
const dataLength = event[42]; return deserializeRegistryEntry(event);
return {
pk: event.slice(1, 34),
revision: decodeEndian(event.slice(34, 42)),
data: event.slice(43, 43 + dataLength),
signature: event.slice(43 + dataLength),
};
} }
public verifyRegistryEntry(sre: SignedRegistryEntry): boolean { public verifyRegistryEntry(sre: SignedRegistryEntry): boolean {
const list: Uint8Array = Uint8Array.from([ return verifyRegistryEntry(sre);
recordTypeRegistryEntry,
...encodeEndian(sre.revision, 8),
sre.data.length, // 1 byte
...sre.data,
]);
return ed25519.verify(sre.signature, list, sre.pk.slice(1));
} }
public serializeRegistryEntry(sre: SignedRegistryEntry): Uint8Array { public serializeRegistryEntry(sre: SignedRegistryEntry): Uint8Array {
return Uint8Array.from([ return serializeRegistryEntry(sre);
recordTypeRegistryEntry,
...sre.pk,
...encodeEndian(sre.revision, 8),
sre.data.length,
...sre.data,
...sre.signature,
]);
} }
} }
async function pTimeout(ms: number) { async function pTimeout(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms)); 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,
]);
}