refactor: switch to using encodeRegistryValue, and encodeRegistryCid

This commit is contained in:
Derrick Hammer 2023-09-03 20:43:29 -04:00
parent db24524f87
commit e9e05b4caf
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 15 additions and 20 deletions

View File

@ -5,7 +5,8 @@ import * as process from "process";
import fs from "fs/promises"; import fs from "fs/promises";
import path from "path"; import path from "path";
import { import {
concatBytes, encodeRegistryCid,
encodeRegistryValue,
equalBytes, equalBytes,
hexToBytes, hexToBytes,
maybeInitDefaultPortals, maybeInitDefaultPortals,
@ -21,18 +22,13 @@ import { HDKey } from "ed25519-keygen/hdkey";
import { import {
BOOTSTRAP_NODES, BOOTSTRAP_NODES,
CID_HASH_TYPES,
CID_TYPES,
createKeyPair, createKeyPair,
createNode, createNode,
Logger,
REGISTRY_TYPES,
S5NodeConfig, S5NodeConfig,
SignedRegistryEntry, SignedRegistryEntry,
} from "@lumeweb/libs5"; } from "@lumeweb/libs5";
import { MemoryLevel } from "memory-level"; import { MemoryLevel } from "memory-level";
import { base58btc } from "multiformats/bases/base58";
import KeyPairEd25519 from "@lumeweb/libs5/lib/ed25519.js"; import KeyPairEd25519 from "@lumeweb/libs5/lib/ed25519.js";
import defer from "p-defer"; import defer from "p-defer";
import { decodeCid, encodeCid } from "@lumeweb/libportal"; import { decodeCid, encodeCid } from "@lumeweb/libportal";
@ -163,7 +159,6 @@ node.services.p2p.once("peerConnected", peerDefer.resolve);
await peerDefer.promise; await peerDefer.promise;
{ {
const cidBytes = decodeCid(cid);
const key = hdKey as HDKey; const key = hdKey as HDKey;
let revision = 0; let revision = 0;
@ -177,14 +172,11 @@ await peerDefer.promise;
revision = ret.revision + 1; revision = ret.revision + 1;
} }
const newEntry = concatBytes( let [newEntry, err] = encodeRegistryValue(cid);
Uint8Array.from([
REGISTRY_TYPES.CID, if (err) {
CID_TYPES.RESOLVER, throw new Error(err);
CID_HASH_TYPES.BLAKE3, }
]),
cidBytes.hash,
);
if (!equalBytes(ret?.data ?? new Uint8Array(), newEntry)) { if (!equalBytes(ret?.data ?? new Uint8Array(), newEntry)) {
sre = node.services.registry.signRegistryEntry({ sre = node.services.registry.signRegistryEntry({
@ -198,12 +190,15 @@ await peerDefer.promise;
sre = ret as SignedRegistryEntry; sre = ret as SignedRegistryEntry;
} }
let resolverCid;
[resolverCid, err] = encodeRegistryCid(sre.pk);
if (err) {
throw new Error(err);
}
console.log( console.log(
util.format( util.format("%s: %s", chalk.green("Resolver entry"), resolverCid),
"%s: %s",
chalk.green("Resolver entry"),
encodeCid(sre.pk.slice(1), 0, CID_TYPES.RESOLVER, CID_HASH_TYPES.ED25519),
),
); );
await node.stop(); await node.stop();
} }