From 693081c7408b9894617ad62dfeccee54eeda4f36 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 22 Jul 2022 19:49:39 -0400 Subject: [PATCH] *Move hashDataKey to a dedicated utility package --- package.json | 1 + src/dns.ts | 3 ++- src/util.ts | 27 --------------------------- 3 files changed, 3 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 6ff15e5..66ce3ae 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "@hyperswarm/dht": "^6.0.1", "@hyperswarm/dht-relay": "^0.3.0", + "@lumeweb/kernel-utils": "https://github.com/LumeWeb/kernel-utils.git", "@pokt-network/pocket-js": "^0.8.0-rc", "@solana/web3.js": "^1.47.3", "@types/acme-client": "^3.3.0", diff --git a/src/dns.ts b/src/dns.ts index 90ae3d4..d64afad 100644 --- a/src/dns.ts +++ b/src/dns.ts @@ -5,9 +5,10 @@ import { overwriteRegistryEntry } from "libskynetnode"; import { Buffer } from "buffer"; import { Parser } from "xml2js"; import { URL } from "url"; -import { errorExit, hashDataKey } from "./util.js"; +import { errorExit } from "./util.js"; import { pack } from "msgpackr"; import config from "./config.js"; +import { hashDataKey } from "@lumeweb/kernel-utils"; const { createHash } = await import("crypto"); diff --git a/src/util.ts b/src/util.ts index 90f4a96..a8b832f 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,6 +1,4 @@ import * as chainNetworks from "./networks.json" assert { type: "json" }; -import { Buffer } from "buffer"; -import { blake2b } from "libskynet"; type networks = { [net: string]: string }; @@ -39,28 +37,3 @@ export function isIp(ip: string) { 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; -}