From 27d396d969f22abad43ebfe20dc7cf7da5d97276 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 4 Dec 2022 05:39:59 -0500 Subject: [PATCH] *Update dist --- dist/network.d.ts | 17 +++++++++----- dist/network.d.ts.map | 2 +- dist/network.js | 14 +++++++---- dist/query/clearCache.d.ts | 16 +++++++++++++ dist/query/clearCache.d.ts.map | 1 + dist/query/clearCache.js | 31 ++++++++++++++++++++++++ dist/query/wisdom.d.ts | 12 ++++++---- dist/query/wisdom.d.ts.map | 2 +- dist/query/wisdom.js | 43 ++++++++++++---------------------- dist/sharedRelay.d.ts | 5 ++++ dist/sharedRelay.d.ts.map | 1 + dist/sharedRelay.js | 26 ++++++++++++++++++++ dist/util.d.ts | 3 ++- dist/util.d.ts.map | 2 +- dist/util.js | 12 ++++++++++ 15 files changed, 139 insertions(+), 48 deletions(-) create mode 100644 dist/query/clearCache.d.ts create mode 100644 dist/query/clearCache.d.ts.map create mode 100644 dist/query/clearCache.js create mode 100644 dist/sharedRelay.d.ts create mode 100644 dist/sharedRelay.d.ts.map create mode 100644 dist/sharedRelay.js diff --git a/dist/network.d.ts b/dist/network.d.ts index dc93fc8..af7d2b9 100644 --- a/dist/network.d.ts +++ b/dist/network.d.ts @@ -1,10 +1,8 @@ -import RPC from "@lumeweb/rpc"; import SimpleRpcQuery from "./query/simple.js"; import WisdomRpcQuery from "./query/wisdom.js"; +import { RpcQueryOptions } from "./types.js"; export default class RpcNetwork { constructor(dht?: any); - private _activeRelay?; - get activeRelay(): RPC; private _dht; get dht(): any; private _majorityThreshold; @@ -40,9 +38,16 @@ export default class RpcNetwork { relay: string, method: string, module: string, - data: object | any[] | undefined, - bypassCache: boolean | undefined, - options: {} + data?: object | any[], + bypassCache?: boolean, + options?: RpcQueryOptions + ): SimpleRpcQuery; + clearCacheQuery( + relays: string[], + method: string, + module: string, + data?: object | any[], + options?: RpcQueryOptions ): SimpleRpcQuery; } //# sourceMappingURL=network.d.ts.map diff --git a/dist/network.d.ts.map b/dist/network.d.ts.map index e31f808..07f6762 100644 --- a/dist/network.d.ts.map +++ b/dist/network.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../src/network.ts"],"names":[],"mappings":"AAGA,OAAO,GAAG,MAAM,cAAc,CAAC;AAE/B,OAAO,cAAc,MAAM,mBAAmB,CAAC;AAC/C,OAAO,cAAc,MAAM,mBAAmB,CAAC;AAE/C,MAAM,CAAC,OAAO,OAAO,UAAU;gBACjB,GAAG,MAAY;IAI3B,OAAO,CAAC,YAAY,CAAC,CAAM;IAE3B,IAAI,WAAW,IAAI,GAAG,CAErB;IAED,OAAO,CAAC,IAAI,CAAa;IAEzB,IAAI,GAAG,QAEN;IAED,OAAO,CAAC,kBAAkB,CAAQ;IAElC,IAAI,iBAAiB,IAAI,MAAM,CAE9B;IAED,IAAI,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAElC;IAED,OAAO,CAAC,aAAa,CAAM;IAE3B,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,OAAO,CAAC,aAAa,CAAK;IAE1B,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,OAAO,CAAC,OAAO,CAAgB;IAE/B,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAED,OAAO,CAAC,MAAM,CAAC,CAAgB;IAE/B,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAMzB;IAED,OAAO,CAAC,YAAY,CAAkB;IAEtC,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,IAAI,WAAW,CAAC,KAAK,EAAE,OAAO,EAE7B;IAED,OAAO,CAAC,UAAU,CAAa;IAE/B,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED,IAAI,SAAS,CAAC,KAAK,EAAE,MAAM,EAE1B;IAEM,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAK9B,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAWpC,WAAW,IAAI,IAAI;IAInB,WAAW,CAChB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,IAAI,GAAE,MAAM,GAAG,GAAG,EAAO,EACzB,WAAW,GAAE,OAAe,EAC5B,OAAO,KAAK,GACX,cAAc;IAYV,WAAW,CAChB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,IAAI,4BAAqB,EACzB,WAAW,qBAAiB,EAC5B,OAAO,EAAE,EAAE,GACV,cAAc;CAalB"} \ No newline at end of file +{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../src/network.ts"],"names":[],"mappings":"AAKA,OAAO,cAAc,MAAM,mBAAmB,CAAC;AAC/C,OAAO,cAAc,MAAM,mBAAmB,CAAC;AAE/C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,CAAC,OAAO,OAAO,UAAU;gBACjB,GAAG,MAAY;IAI3B,OAAO,CAAC,IAAI,CAAa;IAEzB,IAAI,GAAG,QAEN;IAED,OAAO,CAAC,kBAAkB,CAAQ;IAElC,IAAI,iBAAiB,IAAI,MAAM,CAE9B;IAED,IAAI,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAElC;IAED,OAAO,CAAC,aAAa,CAAM;IAE3B,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,OAAO,CAAC,aAAa,CAAK;IAE1B,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,OAAO,CAAC,OAAO,CAAgB;IAE/B,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAED,OAAO,CAAC,MAAM,CAAC,CAAgB;IAE/B,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAMzB;IAED,OAAO,CAAC,YAAY,CAAkB;IAEtC,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,IAAI,WAAW,CAAC,KAAK,EAAE,OAAO,EAE7B;IAED,OAAO,CAAC,UAAU,CAAa;IAE/B,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED,IAAI,SAAS,CAAC,KAAK,EAAE,MAAM,EAE1B;IAEM,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAK9B,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAWpC,WAAW,IAAI,IAAI;IAInB,WAAW,CAChB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,IAAI,GAAE,MAAM,GAAG,GAAG,EAAO,EACzB,WAAW,GAAE,OAAe,EAC5B,OAAO,KAAK,GACX,cAAc;IAYV,WAAW,CAChB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,IAAI,GAAE,MAAM,GAAG,GAAG,EAAO,EACzB,WAAW,GAAE,OAAe,EAC5B,OAAO,GAAE,eAAoB,GAC5B,cAAc;IAcV,eAAe,CACpB,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,IAAI,GAAE,MAAM,GAAG,GAAG,EAAO,EACzB,OAAO,GAAE,eAAoB,GAC5B,cAAc;CAYlB"} \ No newline at end of file diff --git a/dist/network.js b/dist/network.js index 57c226c..1db8f94 100644 --- a/dist/network.js +++ b/dist/network.js @@ -2,14 +2,11 @@ import DHT from "@hyperswarm/dht"; import SimpleRpcQuery from "./query/simple.js"; import WisdomRpcQuery from "./query/wisdom.js"; +import ClearCacheRpcQuery from "./query/clearCache.js"; export default class RpcNetwork { constructor(dht = new DHT()) { this._dht = dht; } - _activeRelay; - get activeRelay() { - return this._activeRelay; - } _dht; get dht() { return this._dht; @@ -83,7 +80,7 @@ export default class RpcNetwork { bypassCache: bypassCache || this._bypassCache, }, options).run(); } - simpleQuery(relay, method, module, data = {}, bypassCache = false, options) { + simpleQuery(relay, method, module, data = {}, bypassCache = false, options = {}) { return new SimpleRpcQuery(this, relay, { method, module, @@ -91,4 +88,11 @@ export default class RpcNetwork { bypassCache: bypassCache || this._bypassCache, }, options).run(); } + clearCacheQuery(relays, method, module, data = {}, options = {}) { + return new ClearCacheRpcQuery(this, relays, { + method, + module, + data, + }, options).run(); + } } diff --git a/dist/query/clearCache.d.ts b/dist/query/clearCache.d.ts new file mode 100644 index 0000000..a9577c4 --- /dev/null +++ b/dist/query/clearCache.d.ts @@ -0,0 +1,16 @@ +import RpcNetwork from "../network.js"; +import { RPCRequest } from "@lumeweb/relay-types"; +import { RpcQueryOptions } from "../types.js"; +import SimpleRpcQuery from "./simple.js"; +export default class ClearCacheRpcQuery extends SimpleRpcQuery { + protected _relays: string[]; + constructor( + network: RpcNetwork, + relays: string[], + query: RPCRequest, + options: RpcQueryOptions + ); + protected _run(): Promise; + protected queryRelay(): Promise; +} +//# sourceMappingURL=clearCache.d.ts.map diff --git a/dist/query/clearCache.d.ts.map b/dist/query/clearCache.d.ts.map new file mode 100644 index 0000000..5ff71b2 --- /dev/null +++ b/dist/query/clearCache.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"clearCache.d.ts","sourceRoot":"","sources":["../../src/query/clearCache.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EAAuB,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAG9C,OAAO,cAAc,MAAM,aAAa,CAAC;AAEzC,MAAM,CAAC,OAAO,OAAO,kBAAmB,SAAQ,cAAc;IAC5D,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;gBAG1B,OAAO,EAAE,UAAU,EACnB,MAAM,EAAE,MAAM,EAAE,EAChB,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,eAAe;cAMV,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;cAQrB,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;CAc3C"} \ No newline at end of file diff --git a/dist/query/clearCache.js b/dist/query/clearCache.js new file mode 100644 index 0000000..84852a7 --- /dev/null +++ b/dist/query/clearCache.js @@ -0,0 +1,31 @@ +import { hashQuery } from "../util.js"; +import { getActiveRelay, setupRelay } from "../sharedRelay.js"; +import SimpleRpcQuery from "./simple.js"; +export default class ClearCacheRpcQuery extends SimpleRpcQuery { + _relays; + constructor(network, relays, query, options) { + super(network, "", query, options); + this._relays = relays; + } + async _run() { + await setupRelay(this._network); + // @ts-ignore + this._relay = getActiveRelay().stream.remotePublicKey; + await this.queryRelay(); + await this.checkResponses(); + } + async queryRelay() { + return this.queryRpc(getActiveRelay(), { + module: "rpc", + method: "broadcast_request", + data: { + request: { + module: "rpc", + method: "clear_cached_item", + data: hashQuery(this._query), + }, + relays: this._relays, + }, + }); + } +} diff --git a/dist/query/wisdom.d.ts b/dist/query/wisdom.d.ts index 11d43d2..c65d77d 100644 --- a/dist/query/wisdom.d.ts +++ b/dist/query/wisdom.d.ts @@ -1,15 +1,17 @@ -import { RPCBroadcastResponse, RPCResponse } from "@lumeweb/relay-types"; +import { + ClientRPCRequest, + RPCBroadcastResponse, + RPCResponse, +} from "@lumeweb/relay-types"; import RpcQueryBase from "./base.js"; export default class WisdomRpcQuery extends RpcQueryBase { protected _response?: RPCBroadcastResponse; - private static _activeRelay; - static get activeRelay(): any; + protected _query: ClientRPCRequest; get result(): Promise; protected _run(): Promise; protected resolve(data?: RPCResponse, timeout?: boolean): void; protected queryRelay(): Promise; protected checkResponse(): Promise; - protected getRelays(): string[] | []; - private setupRelay; + protected getRelays(): string[]; } //# sourceMappingURL=wisdom.d.ts.map diff --git a/dist/query/wisdom.d.ts.map b/dist/query/wisdom.d.ts.map index f41cff3..6d2adc3 100644 --- a/dist/query/wisdom.d.ts.map +++ b/dist/query/wisdom.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"wisdom.d.ts","sourceRoot":"","sources":["../../src/query/wisdom.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,oBAAoB,EAEpB,WAAW,EACZ,MAAM,sBAAsB,CAAC;AAY9B,OAAO,YAAY,MAAM,WAAW,CAAC;AASrC,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,YAAY;IACtD,UAAkB,SAAS,CAAC,EAAE,oBAAoB,CAAC;IACnD,OAAO,CAAC,MAAM,CAAC,YAAY,CAAM;IAEjC,MAAM,KAAK,WAAW,IAAI,GAAG,CAE5B;IAED,IAAI,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,CAEjC;cAEe,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAMrC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,WAAW,EAAE,OAAO,GAAE,OAAe,GAAG,IAAI;cAcrD,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;cAkB1B,aAAa;IAwE7B,SAAS,CAAC,SAAS,IAAI,MAAM,EAAE,GAAG,EAAE;YAoBtB,UAAU;CAsBzB"} \ No newline at end of file +{"version":3,"file":"wisdom.d.ts","sourceRoot":"","sources":["../../src/query/wisdom.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAEhB,oBAAoB,EAEpB,WAAW,EACZ,MAAM,sBAAsB,CAAC;AAW9B,OAAO,YAAY,MAAM,WAAW,CAAC;AAUrC,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,YAAY;IACtD,UAAkB,SAAS,CAAC,EAAE,oBAAoB,CAAC;IACnD,UAAkB,MAAM,EAAE,gBAAgB,CAAC;IAC3C,IAAI,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,CAEjC;cAEe,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAMrC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,WAAW,EAAE,OAAO,GAAE,OAAe,GAAG,IAAI;cAcrD,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;cAgC1B,aAAa;IAyE7B,SAAS,CAAC,SAAS,IAAI,MAAM,EAAE;CAmBhC"} \ No newline at end of file diff --git a/dist/query/wisdom.js b/dist/query/wisdom.js index a2e824a..409851a 100644 --- a/dist/query/wisdom.js +++ b/dist/query/wisdom.js @@ -1,24 +1,20 @@ import { clearTimeout } from "timers"; import b4a from "b4a"; -import { flatten, isPromise, validateResponse, validateTimestampedResponse, } from "../util.js"; -import RPC from "@lumeweb/rpc"; +import { flatten, validateResponse, validateTimestampedResponse, } from "../util.js"; import { blake2b } from "libskynet"; import { ERR_INVALID_SIGNATURE, ERR_NO_RELAYS } from "../error.js"; import RpcQueryBase from "./base.js"; +import { getActiveRelay, setupRelay } from "../sharedRelay.js"; function flatHash(data) { const flattenedData = flatten(data).sort(); return Buffer.from(blake2b(Buffer.from(JSON.stringify(flattenedData)))).toString("hex"); } export default class WisdomRpcQuery extends RpcQueryBase { - static _activeRelay; - static get activeRelay() { - return this._activeRelay; - } get result() { return this._promise; } async _run() { - await this.setupRelay(); + await setupRelay(this._network); await this.queryRelay(); await this.checkResponse(); } @@ -34,11 +30,19 @@ export default class WisdomRpcQuery extends RpcQueryBase { this._promiseResolve?.(data); } async queryRelay() { - let activeRelay = WisdomRpcQuery.activeRelay; + let activeRelay = getActiveRelay(); let relays = this.getRelays(); if (!relays.length) { throw new Error(ERR_NO_RELAYS); } + if (this._query.bypassCache) { + delete this._query.bypassCache; + const clearCacheQuery = this._network.clearCacheQuery(relays, this._query.method, this._query.module, this._query.data); + await clearCacheQuery.result; + } + if ("bypassCache" in this._query) { + delete this._query.bypassCache; + } return this.queryRpc(activeRelay, { module: "rpc", method: "broadcast_request", @@ -53,7 +57,9 @@ export default class WisdomRpcQuery extends RpcQueryBase { this.resolve({ error: this._error }); return; } - if (!validateResponse(WisdomRpcQuery.activeRelay.stream.remotePublicKey, this._response)) { + if (!validateResponse( + // @ts-ignore + getActiveRelay().stream.remotePublicKey, this._response)) { this.resolve({ error: ERR_INVALID_SIGNATURE }); return; } @@ -107,23 +113,4 @@ export default class WisdomRpcQuery extends RpcQueryBase { } return list; } - async setupRelay() { - let active = WisdomRpcQuery.activeRelay; - let relays = this._network.relays; - if (!active) { - if (!relays.length) { - throw new Error(ERR_NO_RELAYS); - } - let relay = relays[Math.floor(Math.random() * relays.length)]; - let socket = this._network.dht.connect(b4a.from(relay, "hex")); - if (isPromise(socket)) { - socket = await socket; - } - await socket.opened; - WisdomRpcQuery._activeRelay = new RPC(socket); - socket.once("close", () => { - WisdomRpcQuery._activeRelay = undefined; - }); - } - } } diff --git a/dist/sharedRelay.d.ts b/dist/sharedRelay.d.ts new file mode 100644 index 0000000..adf5c6e --- /dev/null +++ b/dist/sharedRelay.d.ts @@ -0,0 +1,5 @@ +import RPC from "@lumeweb/rpc"; +import RpcNetwork from "./network.js"; +export declare function setupRelay(network: RpcNetwork): Promise; +export declare function getActiveRelay(): RPC; +//# sourceMappingURL=sharedRelay.d.ts.map diff --git a/dist/sharedRelay.d.ts.map b/dist/sharedRelay.d.ts.map new file mode 100644 index 0000000..e77c41e --- /dev/null +++ b/dist/sharedRelay.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"sharedRelay.d.ts","sourceRoot":"","sources":["../src/sharedRelay.ts"],"names":[],"mappings":"AAGA,OAAO,GAAG,MAAM,cAAc,CAAC;AAC/B,OAAO,UAAU,MAAM,cAAc,CAAC;AAItC,wBAAsB,UAAU,CAAC,OAAO,EAAE,UAAU,iBAqBnD;AAED,wBAAgB,cAAc,IAAI,GAAG,CAEpC"} \ No newline at end of file diff --git a/dist/sharedRelay.js b/dist/sharedRelay.js new file mode 100644 index 0000000..6253d63 --- /dev/null +++ b/dist/sharedRelay.js @@ -0,0 +1,26 @@ +import { ERR_NO_RELAYS } from "./error.js"; +import b4a from "b4a"; +import { isPromise } from "./util.js"; +import RPC from "@lumeweb/rpc"; +let activeRelay; +export async function setupRelay(network) { + const relays = network.relays; + if (!activeRelay) { + if (!relays.length) { + throw new Error(ERR_NO_RELAYS); + } + let relay = relays[Math.floor(Math.random() * relays.length)]; + let socket = network.dht.connect(b4a.from(relay, "hex")); + if (isPromise(socket)) { + socket = await socket; + } + await socket.opened; + activeRelay = new RPC(socket); + socket.once("close", () => { + activeRelay = undefined; + }); + } +} +export function getActiveRelay() { + return activeRelay; +} diff --git a/dist/util.d.ts b/dist/util.d.ts index a023c45..64398e3 100644 --- a/dist/util.d.ts +++ b/dist/util.d.ts @@ -1,5 +1,5 @@ /// -import type { RPCResponse } from "@lumeweb/relay-types"; +import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types"; export declare function isPromise(obj: Promise): boolean; export declare function flatten(target: any, opts?: any): any[]; export declare function validateResponse( @@ -11,4 +11,5 @@ export declare function validateTimestampedResponse( relay: Buffer, response: RPCResponse ): boolean; +export declare function hashQuery(query: RPCRequest): string; //# sourceMappingURL=util.d.ts.map diff --git a/dist/util.d.ts.map b/dist/util.d.ts.map index 5a76bf4..71f0d25 100644 --- a/dist/util.d.ts.map +++ b/dist/util.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAc,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAKpE,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,WAM1C;AAKD,wBAAgB,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,GAAE,GAAQ,GAAG,GAAG,EAAE,CA0C1D;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,EACrB,WAAW,UAAQ,GAClB,OAAO,CAmBT;AAED,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,GACpB,OAAO,CAET"} \ No newline at end of file +{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAOpE,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,WAM1C;AAKD,wBAAgB,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,GAAE,GAAQ,GAAG,GAAG,EAAE,CA0C1D;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,EACrB,WAAW,UAAQ,GAClB,OAAO,CAmBT;AAED,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,GACpB,OAAO,CAET;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAUnD"} \ No newline at end of file diff --git a/dist/util.js b/dist/util.js index f5697a1..fab8a9c 100644 --- a/dist/util.js +++ b/dist/util.js @@ -2,6 +2,8 @@ import stringify from "json-stringify-deterministic"; // @ts-ignore import crypto from "hypercore-crypto"; +// @ts-ignore +import sodium from "sodium-universal"; import b4a from "b4a"; export function isPromise(obj) { return (!!obj && @@ -61,3 +63,13 @@ export function validateResponse(relay, response, timestamped = false) { export function validateTimestampedResponse(relay, response) { return validateResponse(relay, response, true); } +export function hashQuery(query) { + const clonedQuery = { + 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"); +}