*Update dist
This commit is contained in:
parent
fc63e98557
commit
27d396d969
|
@ -1,10 +1,8 @@
|
||||||
import RPC from "@lumeweb/rpc";
|
|
||||||
import SimpleRpcQuery from "./query/simple.js";
|
import SimpleRpcQuery from "./query/simple.js";
|
||||||
import WisdomRpcQuery from "./query/wisdom.js";
|
import WisdomRpcQuery from "./query/wisdom.js";
|
||||||
|
import { RpcQueryOptions } from "./types.js";
|
||||||
export default class RpcNetwork {
|
export default class RpcNetwork {
|
||||||
constructor(dht?: any);
|
constructor(dht?: any);
|
||||||
private _activeRelay?;
|
|
||||||
get activeRelay(): RPC;
|
|
||||||
private _dht;
|
private _dht;
|
||||||
get dht(): any;
|
get dht(): any;
|
||||||
private _majorityThreshold;
|
private _majorityThreshold;
|
||||||
|
@ -40,9 +38,16 @@ export default class RpcNetwork {
|
||||||
relay: string,
|
relay: string,
|
||||||
method: string,
|
method: string,
|
||||||
module: string,
|
module: string,
|
||||||
data: object | any[] | undefined,
|
data?: object | any[],
|
||||||
bypassCache: boolean | undefined,
|
bypassCache?: boolean,
|
||||||
options: {}
|
options?: RpcQueryOptions
|
||||||
|
): SimpleRpcQuery;
|
||||||
|
clearCacheQuery(
|
||||||
|
relays: string[],
|
||||||
|
method: string,
|
||||||
|
module: string,
|
||||||
|
data?: object | any[],
|
||||||
|
options?: RpcQueryOptions
|
||||||
): SimpleRpcQuery;
|
): SimpleRpcQuery;
|
||||||
}
|
}
|
||||||
//# sourceMappingURL=network.d.ts.map
|
//# sourceMappingURL=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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,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"}
|
|
@ -2,14 +2,11 @@
|
||||||
import DHT from "@hyperswarm/dht";
|
import DHT from "@hyperswarm/dht";
|
||||||
import SimpleRpcQuery from "./query/simple.js";
|
import SimpleRpcQuery from "./query/simple.js";
|
||||||
import WisdomRpcQuery from "./query/wisdom.js";
|
import WisdomRpcQuery from "./query/wisdom.js";
|
||||||
|
import ClearCacheRpcQuery from "./query/clearCache.js";
|
||||||
export default class RpcNetwork {
|
export default class RpcNetwork {
|
||||||
constructor(dht = new DHT()) {
|
constructor(dht = new DHT()) {
|
||||||
this._dht = dht;
|
this._dht = dht;
|
||||||
}
|
}
|
||||||
_activeRelay;
|
|
||||||
get activeRelay() {
|
|
||||||
return this._activeRelay;
|
|
||||||
}
|
|
||||||
_dht;
|
_dht;
|
||||||
get dht() {
|
get dht() {
|
||||||
return this._dht;
|
return this._dht;
|
||||||
|
@ -83,7 +80,7 @@ export default class RpcNetwork {
|
||||||
bypassCache: bypassCache || this._bypassCache,
|
bypassCache: bypassCache || this._bypassCache,
|
||||||
}, options).run();
|
}, options).run();
|
||||||
}
|
}
|
||||||
simpleQuery(relay, method, module, data = {}, bypassCache = false, options) {
|
simpleQuery(relay, method, module, data = {}, bypassCache = false, options = {}) {
|
||||||
return new SimpleRpcQuery(this, relay, {
|
return new SimpleRpcQuery(this, relay, {
|
||||||
method,
|
method,
|
||||||
module,
|
module,
|
||||||
|
@ -91,4 +88,11 @@ export default class RpcNetwork {
|
||||||
bypassCache: bypassCache || this._bypassCache,
|
bypassCache: bypassCache || this._bypassCache,
|
||||||
}, options).run();
|
}, options).run();
|
||||||
}
|
}
|
||||||
|
clearCacheQuery(relays, method, module, data = {}, options = {}) {
|
||||||
|
return new ClearCacheRpcQuery(this, relays, {
|
||||||
|
method,
|
||||||
|
module,
|
||||||
|
data,
|
||||||
|
}, options).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<void>;
|
||||||
|
protected queryRelay(): Promise<any>;
|
||||||
|
}
|
||||||
|
//# sourceMappingURL=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"}
|
|
@ -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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,15 +1,17 @@
|
||||||
import { RPCBroadcastResponse, RPCResponse } from "@lumeweb/relay-types";
|
import {
|
||||||
|
ClientRPCRequest,
|
||||||
|
RPCBroadcastResponse,
|
||||||
|
RPCResponse,
|
||||||
|
} from "@lumeweb/relay-types";
|
||||||
import RpcQueryBase from "./base.js";
|
import RpcQueryBase from "./base.js";
|
||||||
export default class WisdomRpcQuery extends RpcQueryBase {
|
export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
protected _response?: RPCBroadcastResponse;
|
protected _response?: RPCBroadcastResponse;
|
||||||
private static _activeRelay;
|
protected _query: ClientRPCRequest;
|
||||||
static get activeRelay(): any;
|
|
||||||
get result(): Promise<RPCResponse>;
|
get result(): Promise<RPCResponse>;
|
||||||
protected _run(): Promise<void>;
|
protected _run(): Promise<void>;
|
||||||
protected resolve(data?: RPCResponse, timeout?: boolean): void;
|
protected resolve(data?: RPCResponse, timeout?: boolean): void;
|
||||||
protected queryRelay(): Promise<any>;
|
protected queryRelay(): Promise<any>;
|
||||||
protected checkResponse(): Promise<void>;
|
protected checkResponse(): Promise<void>;
|
||||||
protected getRelays(): string[] | [];
|
protected getRelays(): string[];
|
||||||
private setupRelay;
|
|
||||||
}
|
}
|
||||||
//# sourceMappingURL=wisdom.d.ts.map
|
//# sourceMappingURL=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"}
|
{"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"}
|
|
@ -1,24 +1,20 @@
|
||||||
import { clearTimeout } from "timers";
|
import { clearTimeout } from "timers";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
import { flatten, isPromise, validateResponse, validateTimestampedResponse, } from "../util.js";
|
import { flatten, validateResponse, validateTimestampedResponse, } from "../util.js";
|
||||||
import RPC from "@lumeweb/rpc";
|
|
||||||
import { blake2b } from "libskynet";
|
import { blake2b } from "libskynet";
|
||||||
import { ERR_INVALID_SIGNATURE, ERR_NO_RELAYS } from "../error.js";
|
import { ERR_INVALID_SIGNATURE, ERR_NO_RELAYS } from "../error.js";
|
||||||
import RpcQueryBase from "./base.js";
|
import RpcQueryBase from "./base.js";
|
||||||
|
import { getActiveRelay, setupRelay } from "../sharedRelay.js";
|
||||||
function flatHash(data) {
|
function flatHash(data) {
|
||||||
const flattenedData = flatten(data).sort();
|
const flattenedData = flatten(data).sort();
|
||||||
return Buffer.from(blake2b(Buffer.from(JSON.stringify(flattenedData)))).toString("hex");
|
return Buffer.from(blake2b(Buffer.from(JSON.stringify(flattenedData)))).toString("hex");
|
||||||
}
|
}
|
||||||
export default class WisdomRpcQuery extends RpcQueryBase {
|
export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
static _activeRelay;
|
|
||||||
static get activeRelay() {
|
|
||||||
return this._activeRelay;
|
|
||||||
}
|
|
||||||
get result() {
|
get result() {
|
||||||
return this._promise;
|
return this._promise;
|
||||||
}
|
}
|
||||||
async _run() {
|
async _run() {
|
||||||
await this.setupRelay();
|
await setupRelay(this._network);
|
||||||
await this.queryRelay();
|
await this.queryRelay();
|
||||||
await this.checkResponse();
|
await this.checkResponse();
|
||||||
}
|
}
|
||||||
|
@ -34,11 +30,19 @@ export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
this._promiseResolve?.(data);
|
this._promiseResolve?.(data);
|
||||||
}
|
}
|
||||||
async queryRelay() {
|
async queryRelay() {
|
||||||
let activeRelay = WisdomRpcQuery.activeRelay;
|
let activeRelay = getActiveRelay();
|
||||||
let relays = this.getRelays();
|
let relays = this.getRelays();
|
||||||
if (!relays.length) {
|
if (!relays.length) {
|
||||||
throw new Error(ERR_NO_RELAYS);
|
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, {
|
return this.queryRpc(activeRelay, {
|
||||||
module: "rpc",
|
module: "rpc",
|
||||||
method: "broadcast_request",
|
method: "broadcast_request",
|
||||||
|
@ -53,7 +57,9 @@ export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
this.resolve({ error: this._error });
|
this.resolve({ error: this._error });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!validateResponse(WisdomRpcQuery.activeRelay.stream.remotePublicKey, this._response)) {
|
if (!validateResponse(
|
||||||
|
// @ts-ignore
|
||||||
|
getActiveRelay().stream.remotePublicKey, this._response)) {
|
||||||
this.resolve({ error: ERR_INVALID_SIGNATURE });
|
this.resolve({ error: ERR_INVALID_SIGNATURE });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -107,23 +113,4 @@ export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
}
|
}
|
||||||
return list;
|
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;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
import RPC from "@lumeweb/rpc";
|
||||||
|
import RpcNetwork from "./network.js";
|
||||||
|
export declare function setupRelay(network: RpcNetwork): Promise<void>;
|
||||||
|
export declare function getActiveRelay(): RPC;
|
||||||
|
//# sourceMappingURL=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"}
|
|
@ -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;
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
/// <reference types="node" />
|
/// <reference types="node" />
|
||||||
import type { RPCResponse } from "@lumeweb/relay-types";
|
import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types";
|
||||||
export declare function isPromise(obj: Promise<any>): boolean;
|
export declare function isPromise(obj: Promise<any>): boolean;
|
||||||
export declare function flatten(target: any, opts?: any): any[];
|
export declare function flatten(target: any, opts?: any): any[];
|
||||||
export declare function validateResponse(
|
export declare function validateResponse(
|
||||||
|
@ -11,4 +11,5 @@ export declare function validateTimestampedResponse(
|
||||||
relay: Buffer,
|
relay: Buffer,
|
||||||
response: RPCResponse
|
response: RPCResponse
|
||||||
): boolean;
|
): boolean;
|
||||||
|
export declare function hashQuery(query: RPCRequest): string;
|
||||||
//# sourceMappingURL=util.d.ts.map
|
//# sourceMappingURL=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"}
|
{"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"}
|
|
@ -2,6 +2,8 @@
|
||||||
import stringify from "json-stringify-deterministic";
|
import stringify from "json-stringify-deterministic";
|
||||||
// @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) {
|
export function isPromise(obj) {
|
||||||
return (!!obj &&
|
return (!!obj &&
|
||||||
|
@ -61,3 +63,13 @@ export function validateResponse(relay, response, timestamped = false) {
|
||||||
export function validateTimestampedResponse(relay, response) {
|
export function validateTimestampedResponse(relay, response) {
|
||||||
return validateResponse(relay, response, true);
|
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");
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue