Compare commits

..

6 Commits

5 changed files with 34 additions and 21 deletions

View File

@ -41,6 +41,7 @@
"fetch-blob": "https://github.com/LumeWeb/fetch-blob.git", "fetch-blob": "https://github.com/LumeWeb/fetch-blob.git",
"hyperswarm": "^3.0.4", "hyperswarm": "^3.0.4",
"json-stable-stringify": "^1.0.1", "json-stable-stringify": "^1.0.1",
"json-stringify-deterministic": "^1.0.7",
"libskynet": "https://github.com/LumeWeb/libskynet.git", "libskynet": "https://github.com/LumeWeb/libskynet.git",
"libskynetnode": "https://github.com/LumeWeb/libskynetnode.git", "libskynetnode": "https://github.com/LumeWeb/libskynetnode.git",
"loady": "https://github.com/LumeWeb/loady.git", "loady": "https://github.com/LumeWeb/loady.git",
@ -49,7 +50,6 @@
"node-cache": "^5.1.2", "node-cache": "^5.1.2",
"node-cron": "^3.0.1", "node-cron": "^3.0.1",
"node-fetch": "2", "node-fetch": "2",
"ordered-json": "^0.1.1",
"promise-retry": "^2.0.1", "promise-retry": "^2.0.1",
"protomux": "^3.4.0", "protomux": "^3.4.0",
"protomux-rpc": "^1.3.0", "protomux-rpc": "^1.3.0",

View File

@ -4,6 +4,7 @@ import {
PluginAPI, PluginAPI,
RPCBroadcastRequest, RPCBroadcastRequest,
RPCBroadcastResponse, RPCBroadcastResponse,
RPCCacheItem,
RPCRequest, RPCRequest,
RPCResponse, RPCResponse,
} from "@lumeweb/relay-types"; } from "@lumeweb/relay-types";
@ -58,8 +59,8 @@ const plugin: Plugin = {
return { return {
data: true, data: true,
...cache[req]?.value, ...cache.get<RPCCacheItem>(req)?.value,
signature: cache[req]?.signature, signature: cache.get<RPCCacheItem>(req)?.signature,
}; };
}, },
}); });
@ -92,6 +93,13 @@ const plugin: Plugin = {
throw new Error("relays required"); throw new Error("relays required");
} }
if (
req?.request?.module === "rpc" &&
req?.request?.method === "broadcast_request"
) {
throw new Error("recursive broadcast_request calls are not allowed");
}
let resp = await broadcastRequest(req.request, req.relays); let resp = await broadcastRequest(req.request, req.relays);
const result: RPCBroadcastResponse = { const result: RPCBroadcastResponse = {

View File

@ -1,17 +1,10 @@
import EventEmitter from "events"; import EventEmitter from "events";
import DHTCache from "@lumeweb/dht-cache"; import DHTCache from "@lumeweb/dht-cache";
import { import { RPCCacheItem, RPCRequest, RPCResponse } from "@lumeweb/relay-types";
RPCCacheData,
RPCCacheItem,
RPCRequest,
RPCResponse,
} from "@lumeweb/relay-types";
import { getRpcByPeer } from "../rpc";
import b4a from "b4a";
import { get as getSwarm } from "../swarm"; import { get as getSwarm } from "../swarm";
import { RPCServer } from "./server"; import { RPCServer } from "./server";
// @ts-ignore // @ts-ignore
import orderedJSON from "ordered-json"; import jsonStringify from "json-stringify-deterministic";
// @ts-ignore // @ts-ignore
import crypto from "hypercore-crypto"; import crypto from "hypercore-crypto";
import NodeCache from "node-cache"; import NodeCache from "node-cache";
@ -50,8 +43,11 @@ export class RPCCache extends EventEmitter {
const field = item.value.signedField || "data"; const field = item.value.signedField || "data";
const updated = item.value.updated; const updated = item.value.updated;
// @ts-ignore // @ts-ignore
const data = item.value[field]; let json = item.value[field];
const json = orderedJSON.stringify(data);
if (typeof json !== "string") {
json = jsonStringify(json);
}
return this.server.signData(`${updated}${json}`); return this.server.signData(`${updated}${json}`);
} }
@ -60,8 +56,11 @@ export class RPCCache extends EventEmitter {
const field = item.value.signedField || "data"; const field = item.value.signedField || "data";
const updated = item.value.updated; const updated = item.value.updated;
// @ts-ignore // @ts-ignore
const data = item.value[field]; let json = item.value[field];
const json = orderedJSON.stringify(data);
if (typeof json !== "string") {
json = jsonStringify(json);
}
try { try {
if ( if (

View File

@ -15,9 +15,10 @@ import c from "compact-encoding";
// @ts-ignore // @ts-ignore
import crypto from "hypercore-crypto"; import crypto from "hypercore-crypto";
// @ts-ignore // @ts-ignore
import orderedJSON from "ordered-json";
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
import { RPCCache } from "./cache"; import { RPCCache } from "./cache";
// @ts-ignore
import jsonStringify from "json-stringify-deterministic";
const sodium = require("sodium-universal"); const sodium = require("sodium-universal");
let server: RPCServer; let server: RPCServer;
@ -55,7 +56,7 @@ export class RPCServer extends EventEmitter {
const queryHash = Buffer.allocUnsafe(32); const queryHash = Buffer.allocUnsafe(32);
sodium.crypto_generichash( sodium.crypto_generichash(
queryHash, queryHash,
Buffer.from(orderedJSON.stringify(clonedQuery)) Buffer.from(jsonStringify(clonedQuery))
); );
return queryHash.toString("hex"); return queryHash.toString("hex");
} }
@ -128,7 +129,7 @@ export class RPCServer extends EventEmitter {
public signData(data: any): string { public signData(data: any): string {
let raw = data; let raw = data;
if (typeof data !== "string") { if (typeof data !== "string") {
raw = orderedJSON.stringify(data); raw = jsonStringify(data);
} }
return crypto return crypto
@ -147,7 +148,7 @@ export class RPCServer extends EventEmitter {
if (cachedRequest) { if (cachedRequest) {
this.getRequestLock(request)?.release(); this.getRequestLock(request)?.release();
return cachedRequest.value; return { ...cachedRequest.value, signature: cachedRequest.signature };
} }
let method = this.getMethodByRequest(request); let method = this.getMethodByRequest(request);

View File

@ -558,7 +558,7 @@
"@lumeweb/relay-types@https://git.lumeweb.com/LumeWeb/relay-types.git": "@lumeweb/relay-types@https://git.lumeweb.com/LumeWeb/relay-types.git":
version "0.1.0" version "0.1.0"
resolved "https://git.lumeweb.com/LumeWeb/relay-types.git#33b87a1075441043e7e0219264d66f6610864685" resolved "https://git.lumeweb.com/LumeWeb/relay-types.git#298d043b3924bbe427e047d1c02db7c239fdadac"
"@msgpackr-extract/msgpackr-extract-darwin-arm64@2.2.0": "@msgpackr-extract/msgpackr-extract-darwin-arm64@2.2.0":
version "2.2.0" version "2.2.0"
@ -2300,6 +2300,11 @@ json-stable-stringify@^1.0.1:
dependencies: dependencies:
jsonify "^0.0.1" jsonify "^0.0.1"
json-stringify-deterministic@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/json-stringify-deterministic/-/json-stringify-deterministic-1.0.7.tgz#b5e37549581ac8ec8439ca7e9b746d23da56ac20"
integrity sha512-VGSL+V2s/AqL25ixC4459kAlyIYsS+VUJ3owa/FKr4ZeMJeTZERlzGXJ2xWIHcTfd/fwgTvNyh7/RWMDvkFciw==
json-stringify-safe@^5.0.1: json-stringify-safe@^5.0.1:
version "5.0.1" version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"