*Add hashQuery utility function

This commit is contained in:
Derrick Hammer 2022-12-04 05:34:21 -05:00
parent 271a0c2911
commit 144e19e635
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 14 additions and 0 deletions

View File

@ -3,6 +3,8 @@ import stringify from "json-stringify-deterministic";
import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types"; import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types";
// @ts-ignore // @ts-ignore
import crypto from "hypercore-crypto"; import crypto from "hypercore-crypto";
// @ts-ignore
import sodium from "sodium-universal";
import b4a from "b4a"; import b4a from "b4a";
export function isPromise(obj: Promise<any>) { export function isPromise(obj: Promise<any>) {
@ -91,3 +93,15 @@ export function validateTimestampedResponse(
): boolean { ): boolean {
return validateResponse(relay, response, true); return validateResponse(relay, response, true);
} }
export function hashQuery(query: RPCRequest): string {
const clonedQuery: RPCRequest = {
module: query.module,
method: query.method,
data: query.data,
};
const queryHash = Buffer.allocUnsafe(32);
sodium.crypto_generichash(queryHash, Buffer.from(stringify(clonedQuery)));
return queryHash.toString("hex");
}