*Move hashDataKey to a dedicated utility package

This commit is contained in:
Derrick Hammer 2022-07-22 19:49:39 -04:00
parent 20e9511eae
commit 693081c740
3 changed files with 3 additions and 28 deletions

View File

@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"@hyperswarm/dht": "^6.0.1", "@hyperswarm/dht": "^6.0.1",
"@hyperswarm/dht-relay": "^0.3.0", "@hyperswarm/dht-relay": "^0.3.0",
"@lumeweb/kernel-utils": "https://github.com/LumeWeb/kernel-utils.git",
"@pokt-network/pocket-js": "^0.8.0-rc", "@pokt-network/pocket-js": "^0.8.0-rc",
"@solana/web3.js": "^1.47.3", "@solana/web3.js": "^1.47.3",
"@types/acme-client": "^3.3.0", "@types/acme-client": "^3.3.0",

View File

@ -5,9 +5,10 @@ import { overwriteRegistryEntry } from "libskynetnode";
import { Buffer } from "buffer"; import { Buffer } from "buffer";
import { Parser } from "xml2js"; import { Parser } from "xml2js";
import { URL } from "url"; import { URL } from "url";
import { errorExit, hashDataKey } from "./util.js"; import { errorExit } from "./util.js";
import { pack } from "msgpackr"; import { pack } from "msgpackr";
import config from "./config.js"; import config from "./config.js";
import { hashDataKey } from "@lumeweb/kernel-utils";
const { createHash } = await import("crypto"); const { createHash } = await import("crypto");

View File

@ -1,6 +1,4 @@
import * as chainNetworks from "./networks.json" assert { type: "json" }; import * as chainNetworks from "./networks.json" assert { type: "json" };
import { Buffer } from "buffer";
import { blake2b } from "libskynet";
type networks = { [net: string]: string }; type networks = { [net: string]: string };
@ -39,28 +37,3 @@ export function isIp(ip: string) {
ip ip
); );
} }
export function hashDataKey(dataKey: string): Uint8Array {
return blake2b(encodeUtf8String(dataKey));
}
function encodeUtf8String(str: string): Uint8Array {
const byteArray = stringToUint8ArrayUtf8(str);
const encoded = new Uint8Array(8 + byteArray.length);
encoded.set(encodeNumber(byteArray.length));
encoded.set(byteArray, 8);
return encoded;
}
function stringToUint8ArrayUtf8(str: string): Uint8Array {
return Uint8Array.from(Buffer.from(str, "utf-8"));
}
function encodeNumber(num: number): Uint8Array {
const encoded = new Uint8Array(8);
for (let index = 0; index < encoded.length; index++) {
encoded[index] = num & 0xff;
num = num >> 8;
}
return encoded;
}