fix: only submit a new registry entry if the data has changed

This commit is contained in:
Derrick Hammer 2023-09-02 08:58:18 -04:00
parent 0d64ec45f2
commit 421cf81f36
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 23 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import {
createNode, createNode,
REGISTRY_TYPES, REGISTRY_TYPES,
S5NodeConfig, S5NodeConfig,
SignedRegistryEntry,
} from "@lumeweb/libs5"; } from "@lumeweb/libs5";
import KeyPairEd25519 from "@lumeweb/libs5/lib/ed25519.js"; import KeyPairEd25519 from "@lumeweb/libs5/lib/ed25519.js";
import fs from "fs/promises"; import fs from "fs/promises";
@ -20,6 +21,7 @@ import * as util from "util";
import { import {
CID, CID,
concatBytes, concatBytes,
equalBytes,
hexToBytes, hexToBytes,
loginActivePortals, loginActivePortals,
maybeInitDefaultPortals, maybeInitDefaultPortals,
@ -176,6 +178,7 @@ await peerDefer.promise;
{ {
const cidBytes = base58btc.decode(cid); const cidBytes = base58btc.decode(cid);
const key = hdKey as HDKey; const key = hdKey as HDKey;
let sre: SignedRegistryEntry;
let revision = 0; let revision = 0;
@ -186,20 +189,27 @@ await peerDefer.promise;
if (ret) { if (ret) {
revision = ret.revision + 1; revision = ret.revision + 1;
} }
const sre = node.services.registry.signRegistryEntry({
kp: new KeyPairEd25519((hdKey as HDKey).privateKey),
data: concatBytes(
Uint8Array.from([
REGISTRY_TYPES.CID,
CID_TYPES.RESOLVER,
CID_HASH_TYPES.BLAKE3,
]),
cidBytes,
),
revision,
});
await node.services.registry.set(sre); const newEntry = concatBytes(
Uint8Array.from([
REGISTRY_TYPES.CID,
CID_TYPES.RESOLVER,
CID_HASH_TYPES.BLAKE3,
]),
cidBytes,
);
if (!equalBytes(ret?.data ?? new Uint8Array(), newEntry)) {
sre = node.services.registry.signRegistryEntry({
kp: new KeyPairEd25519((hdKey as HDKey).privateKey),
data: newEntry,
revision,
});
await node.services.registry.set(sre);
} else {
sre = ret as SignedRegistryEntry;
}
console.log( console.log(
util.format( util.format(