2021-05-10 15:53:05 +10:00
|
|
|
/**
|
|
|
|
|
* @hidden
|
|
|
|
|
* @module
|
|
|
|
|
*/
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
import { ChildProcess, spawn } from "child_process";
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
import appRoot from "app-root-path";
|
|
|
|
|
import debug from "debug";
|
|
|
|
|
import { Multiaddr, multiaddr } from "multiaddr";
|
|
|
|
|
import PeerId from "peer-id";
|
|
|
|
|
import portfinder from "portfinder";
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
import { hexToBuf } from "../lib/utils";
|
|
|
|
|
import { DefaultPubSubTopic } from "../lib/waku";
|
|
|
|
|
import { WakuMessage } from "../lib/waku_message";
|
|
|
|
|
import * as proto from "../proto/waku/v2/message";
|
2021-03-12 10:35:50 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
import { existsAsync, mkdirAsync, openAsync } from "./async_fs";
|
|
|
|
|
import waitForLine from "./log_file";
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const dbg = debug("waku:nim-waku");
|
2021-04-06 11:00:40 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const NIM_WAKU_DIR = appRoot + "/nim-waku";
|
|
|
|
|
const NIM_WAKU_BIN = NIM_WAKU_DIR + "/build/wakunode2";
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const LOG_DIR = "./log";
|
2021-03-15 13:38:36 +11:00
|
|
|
|
2021-03-11 10:54:35 +11:00
|
|
|
export interface Args {
|
|
|
|
|
staticnode?: string;
|
2022-02-04 14:12:00 +11:00
|
|
|
nat?: "none";
|
2021-03-11 10:54:35 +11:00
|
|
|
listenAddress?: string;
|
|
|
|
|
relay?: boolean;
|
|
|
|
|
rpc?: boolean;
|
|
|
|
|
rpcAdmin?: boolean;
|
|
|
|
|
nodekey?: string;
|
2021-03-12 17:08:42 +11:00
|
|
|
portsShift?: number;
|
2021-04-09 11:23:00 +10:00
|
|
|
logLevel?: LogLevel;
|
2021-05-06 10:37:12 +10:00
|
|
|
persistMessages?: boolean;
|
2021-05-19 11:00:43 +10:00
|
|
|
lightpush?: boolean;
|
2021-06-09 12:25:56 +10:00
|
|
|
topics?: string;
|
2021-07-09 10:07:39 +10:00
|
|
|
rpcPrivate?: boolean;
|
2022-01-20 13:00:58 +11:00
|
|
|
websocketSupport?: boolean;
|
2022-02-01 12:54:54 +11:00
|
|
|
tcpPort?: number;
|
|
|
|
|
rpcPort?: number;
|
|
|
|
|
websocketPort?: number;
|
2021-04-09 11:23:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export enum LogLevel {
|
2022-02-04 14:12:00 +11:00
|
|
|
Error = "error",
|
|
|
|
|
Info = "info",
|
|
|
|
|
Warn = "warn",
|
|
|
|
|
Debug = "debug",
|
|
|
|
|
Trace = "trace",
|
|
|
|
|
Notice = "notice",
|
|
|
|
|
Fatal = "fatal",
|
2021-03-11 10:54:35 +11:00
|
|
|
}
|
|
|
|
|
|
2021-07-09 10:07:39 +10:00
|
|
|
export interface KeyPair {
|
|
|
|
|
privateKey: string;
|
|
|
|
|
publicKey: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface WakuRelayMessage {
|
|
|
|
|
payload: string;
|
|
|
|
|
contentTopic?: string;
|
2021-07-20 13:55:01 +10:00
|
|
|
timestamp?: number; // Float in seconds
|
2021-07-09 10:07:39 +10:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 17:39:53 +11:00
|
|
|
export class NimWaku {
|
|
|
|
|
private process?: ChildProcess;
|
2021-03-26 13:07:47 +11:00
|
|
|
private pid?: number;
|
2021-03-12 17:08:42 +11:00
|
|
|
private peerId?: PeerId;
|
2021-03-23 11:14:51 +11:00
|
|
|
private multiaddrWithId?: Multiaddr;
|
2022-02-01 12:54:54 +11:00
|
|
|
private readonly logPath: string;
|
|
|
|
|
private rpcPort?: number;
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-25 15:49:07 +11:00
|
|
|
constructor(logName: string) {
|
|
|
|
|
this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`;
|
2021-03-15 13:25:14 +11:00
|
|
|
}
|
2021-03-11 11:11:37 +11:00
|
|
|
|
2021-05-03 15:52:38 +10:00
|
|
|
async start(args?: Args): Promise<void> {
|
2021-03-15 13:38:36 +11:00
|
|
|
try {
|
|
|
|
|
await existsAsync(LOG_DIR);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
try {
|
|
|
|
|
await mkdirAsync(LOG_DIR);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Looks like 2 tests tried to create the director at the same time,
|
|
|
|
|
// it can be ignored
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const logFile = await openAsync(this.logPath, "w");
|
2021-03-11 10:54:35 +11:00
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
const mergedArgs = defaultArgs();
|
|
|
|
|
|
2022-02-01 12:54:54 +11:00
|
|
|
const ports: number[] = await new Promise((resolve, reject) => {
|
|
|
|
|
portfinder.getPorts(3, {}, (err, ports) => {
|
|
|
|
|
if (err) reject(err);
|
|
|
|
|
resolve(ports);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.rpcPort = ports[0];
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
// Object.assign overrides the properties with the source (if there are conflicts)
|
2021-04-09 11:23:00 +10:00
|
|
|
Object.assign(
|
|
|
|
|
mergedArgs,
|
2022-02-01 12:54:54 +11:00
|
|
|
{
|
|
|
|
|
tcpPort: ports[1],
|
|
|
|
|
rpcPort: this.rpcPort,
|
|
|
|
|
websocketPort: ports[2],
|
|
|
|
|
logLevel: LogLevel.Trace,
|
|
|
|
|
},
|
2021-04-09 11:23:00 +10:00
|
|
|
args
|
|
|
|
|
);
|
2021-03-12 17:08:42 +11:00
|
|
|
|
|
|
|
|
const argsArray = argsToArray(mergedArgs);
|
2022-02-13 19:04:50 +11:00
|
|
|
dbg(`nim-waku args: ${argsArray.join(" ")}`);
|
2021-03-12 17:08:42 +11:00
|
|
|
this.process = spawn(NIM_WAKU_BIN, argsArray, {
|
2021-03-15 16:22:26 +11:00
|
|
|
cwd: NIM_WAKU_DIR,
|
2021-03-11 10:54:35 +11:00
|
|
|
stdio: [
|
2022-02-04 14:12:00 +11:00
|
|
|
"ignore", // stdin
|
2021-03-11 10:54:35 +11:00
|
|
|
logFile, // stdout
|
|
|
|
|
logFile, // stderr
|
2021-03-10 17:39:53 +11:00
|
|
|
],
|
2021-03-11 10:54:35 +11:00
|
|
|
});
|
2021-03-26 13:07:47 +11:00
|
|
|
this.pid = this.process.pid;
|
2021-04-06 11:00:40 +10:00
|
|
|
dbg(
|
2021-03-26 13:07:47 +11:00
|
|
|
`nim-waku ${
|
|
|
|
|
this.process.pid
|
|
|
|
|
} started at ${new Date().toLocaleTimeString()}`
|
|
|
|
|
);
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
this.process.on("exit", (signal) => {
|
2021-04-06 11:00:40 +10:00
|
|
|
dbg(
|
2021-03-26 13:07:47 +11:00
|
|
|
`nim-waku ${
|
|
|
|
|
this.process ? this.process.pid : this.pid
|
|
|
|
|
} process exited with ${signal} at ${new Date().toLocaleTimeString()}`
|
|
|
|
|
);
|
2021-03-25 16:29:35 +11:00
|
|
|
});
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
this.process.on("error", (err) => {
|
2021-03-26 13:07:47 +11:00
|
|
|
console.log(
|
|
|
|
|
`nim-waku ${
|
|
|
|
|
this.process ? this.process.pid : this.pid
|
|
|
|
|
} process encountered an error: ${err} at ${new Date().toLocaleTimeString()}`
|
|
|
|
|
);
|
2021-03-25 16:29:35 +11:00
|
|
|
});
|
|
|
|
|
|
2021-08-09 12:27:22 +10:00
|
|
|
dbg("Waiting to see 'Node setup complete' in nim-waku logs");
|
2022-02-04 14:12:00 +11:00
|
|
|
await this.waitForLog("Node setup complete", 9000);
|
|
|
|
|
dbg("nim-waku node has been started");
|
2021-03-15 13:25:14 +11:00
|
|
|
}
|
|
|
|
|
|
2021-05-03 15:52:38 +10:00
|
|
|
public stop(): void {
|
2022-01-31 15:30:49 +11:00
|
|
|
const pid = this.process ? this.process.pid : this.pid;
|
|
|
|
|
dbg(`nim-waku ${pid} getting SIGINT at ${new Date().toLocaleTimeString()}`);
|
2022-02-04 14:12:00 +11:00
|
|
|
if (!this.process) throw "nim-waku process not set";
|
|
|
|
|
const res = this.process.kill("SIGINT");
|
2022-01-31 15:30:49 +11:00
|
|
|
dbg(`nim-waku ${pid} interrupted:`, res);
|
|
|
|
|
this.process = undefined;
|
2021-03-19 16:07:56 +11:00
|
|
|
}
|
|
|
|
|
|
2021-08-09 11:58:58 +10:00
|
|
|
async waitForLog(msg: string, timeout: number): Promise<void> {
|
|
|
|
|
return waitForLine(this.logPath, msg, timeout);
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Calls nim-waku2 JSON-RPC API `get_waku_v2_admin_v1_peers` to check
|
|
|
|
|
* for known peers
|
|
|
|
|
* @throws if nim-waku2 isn't started.
|
|
|
|
|
*/
|
2021-05-03 15:52:38 +10:00
|
|
|
async peers(): Promise<string[]> {
|
2021-03-10 17:39:53 +11:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
return this.rpcCall<string[]>("get_waku_v2_admin_v1_peers", []);
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
async info(): Promise<RpcInfoResponse> {
|
2021-03-10 17:39:53 +11:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
return this.rpcCall<RpcInfoResponse>("get_waku_v2_debug_v1_info", []);
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 12:25:56 +10:00
|
|
|
async sendMessage(
|
|
|
|
|
message: WakuMessage,
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic?: string
|
2021-06-09 12:25:56 +10:00
|
|
|
): Promise<boolean> {
|
2021-03-12 10:35:50 +11:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
2021-03-22 15:34:13 +11:00
|
|
|
if (!message.payload) {
|
2022-02-04 14:12:00 +11:00
|
|
|
throw "Attempting to send empty message";
|
2021-03-22 15:34:13 +11:00
|
|
|
}
|
2021-07-20 13:55:01 +10:00
|
|
|
let timestamp;
|
|
|
|
|
if (message.timestamp) {
|
|
|
|
|
timestamp = message.timestamp.valueOf() / 1000;
|
2021-08-05 13:32:37 +10:00
|
|
|
if (Number.isInteger(timestamp)) {
|
|
|
|
|
// Add a millisecond to ensure it's not an integer
|
|
|
|
|
// Until https://github.com/status-im/nim-waku/issues/691 is done
|
|
|
|
|
timestamp += 0.001;
|
|
|
|
|
}
|
2021-07-20 13:55:01 +10:00
|
|
|
}
|
2021-03-22 15:34:13 +11:00
|
|
|
|
2021-03-12 10:35:50 +11:00
|
|
|
const rpcMessage = {
|
2021-03-15 16:26:07 +11:00
|
|
|
payload: bufToHex(message.payload),
|
2021-03-12 10:35:50 +11:00
|
|
|
contentTopic: message.contentTopic,
|
2021-07-20 13:55:01 +10:00
|
|
|
timestamp,
|
2021-03-12 10:35:50 +11:00
|
|
|
};
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
return this.rpcCall<boolean>("post_waku_v2_relay_v1_message", [
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
|
2021-03-12 10:35:50 +11:00
|
|
|
rpcMessage,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 15:52:38 +10:00
|
|
|
async messages(): Promise<WakuMessage[]> {
|
2021-03-12 10:35:50 +11:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
2021-07-07 11:23:56 +10:00
|
|
|
const isDefined = (msg: WakuMessage | undefined): msg is WakuMessage => {
|
|
|
|
|
return !!msg;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const protoMsgs = await this.rpcCall<proto.WakuMessage[]>(
|
2022-02-04 14:12:00 +11:00
|
|
|
"get_waku_v2_relay_v1_messages",
|
2021-08-19 14:12:24 +10:00
|
|
|
[DefaultPubSubTopic]
|
2021-07-07 11:23:56 +10:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const msgs = await Promise.all(
|
|
|
|
|
protoMsgs.map(async (protoMsg) => await WakuMessage.decodeProto(protoMsg))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return msgs.filter(isDefined);
|
2021-03-12 10:35:50 +11:00
|
|
|
}
|
|
|
|
|
|
2021-07-09 10:07:39 +10:00
|
|
|
async getAsymmetricKeyPair(): Promise<KeyPair> {
|
|
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
const { seckey, pubkey } = await this.rpcCall<{
|
|
|
|
|
seckey: string;
|
|
|
|
|
pubkey: string;
|
2022-02-04 14:12:00 +11:00
|
|
|
}>("get_waku_v2_private_v1_asymmetric_keypair", []);
|
2021-07-09 10:07:39 +10:00
|
|
|
|
|
|
|
|
return { privateKey: seckey, publicKey: pubkey };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async postAsymmetricMessage(
|
|
|
|
|
message: WakuRelayMessage,
|
|
|
|
|
publicKey: Uint8Array,
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic?: string
|
2021-07-09 10:07:39 +10:00
|
|
|
): Promise<boolean> {
|
|
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
if (!message.payload) {
|
2022-02-04 14:12:00 +11:00
|
|
|
throw "Attempting to send empty message";
|
2021-07-09 10:07:39 +10:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
return this.rpcCall<boolean>("post_waku_v2_private_v1_asymmetric_message", [
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
|
2021-07-09 10:07:39 +10:00
|
|
|
message,
|
2022-02-04 14:12:00 +11:00
|
|
|
"0x" + bufToHex(publicKey),
|
2021-07-09 10:07:39 +10:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-09 10:37:29 +10:00
|
|
|
async getAsymmetricMessages(
|
|
|
|
|
privateKey: Uint8Array,
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic?: string
|
2021-07-09 10:37:29 +10:00
|
|
|
): Promise<WakuRelayMessage[]> {
|
|
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
return await this.rpcCall<WakuRelayMessage[]>(
|
2022-02-04 14:12:00 +11:00
|
|
|
"get_waku_v2_private_v1_asymmetric_messages",
|
2021-07-09 10:37:29 +10:00
|
|
|
[
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
|
2022-02-04 14:12:00 +11:00
|
|
|
"0x" + bufToHex(privateKey),
|
2021-07-09 10:37:29 +10:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 12:12:43 +10:00
|
|
|
async getSymmetricKey(): Promise<Buffer> {
|
|
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
return this.rpcCall<string>(
|
2022-02-04 14:12:00 +11:00
|
|
|
"get_waku_v2_private_v1_symmetric_key",
|
2021-07-15 12:12:43 +10:00
|
|
|
[]
|
|
|
|
|
).then(hexToBuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async postSymmetricMessage(
|
|
|
|
|
message: WakuRelayMessage,
|
|
|
|
|
symKey: Uint8Array,
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic?: string
|
2021-07-15 12:12:43 +10:00
|
|
|
): Promise<boolean> {
|
|
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
if (!message.payload) {
|
2022-02-04 14:12:00 +11:00
|
|
|
throw "Attempting to send empty message";
|
2021-07-15 12:12:43 +10:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
return this.rpcCall<boolean>("post_waku_v2_private_v1_symmetric_message", [
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
|
2021-07-15 12:12:43 +10:00
|
|
|
message,
|
2022-02-04 14:12:00 +11:00
|
|
|
"0x" + bufToHex(symKey),
|
2021-07-15 12:12:43 +10:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getSymmetricMessages(
|
|
|
|
|
symKey: Uint8Array,
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic?: string
|
2021-07-15 12:12:43 +10:00
|
|
|
): Promise<WakuRelayMessage[]> {
|
|
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
return await this.rpcCall<WakuRelayMessage[]>(
|
2022-02-04 14:12:00 +11:00
|
|
|
"get_waku_v2_private_v1_symmetric_messages",
|
|
|
|
|
[pubSubTopic ? pubSubTopic : DefaultPubSubTopic, "0x" + bufToHex(symKey)]
|
2021-07-15 12:12:43 +10:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
async getPeerId(): Promise<PeerId> {
|
2022-01-19 15:43:45 +11:00
|
|
|
return await this._getPeerId().then((res) => res.peerId);
|
2021-03-23 11:14:51 +11:00
|
|
|
}
|
2021-03-12 17:08:42 +11:00
|
|
|
|
2021-03-23 11:14:51 +11:00
|
|
|
async getMultiaddrWithId(): Promise<Multiaddr> {
|
2022-01-19 15:43:45 +11:00
|
|
|
return await this._getPeerId().then((res) => res.multiaddrWithId);
|
2021-03-23 11:14:51 +11:00
|
|
|
}
|
2021-03-12 17:08:42 +11:00
|
|
|
|
2022-01-19 15:43:45 +11:00
|
|
|
private async _getPeerId(): Promise<{
|
2021-03-23 11:14:51 +11:00
|
|
|
peerId: PeerId;
|
|
|
|
|
multiaddrWithId: Multiaddr;
|
|
|
|
|
}> {
|
|
|
|
|
if (this.peerId && this.multiaddrWithId) {
|
|
|
|
|
return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId };
|
|
|
|
|
}
|
|
|
|
|
const res = await this.info();
|
2022-02-14 09:26:22 +11:00
|
|
|
console.log(res);
|
2022-01-20 13:00:58 +11:00
|
|
|
this.multiaddrWithId = res.listenAddresses
|
|
|
|
|
.map((ma) => multiaddr(ma))
|
2022-02-04 14:12:00 +11:00
|
|
|
.find((ma) => ma.protoNames().includes("ws"));
|
|
|
|
|
if (!this.multiaddrWithId) throw "Nim-waku did not return a ws multiaddr";
|
2021-03-23 11:14:51 +11:00
|
|
|
const peerIdStr = this.multiaddrWithId.getPeerId();
|
2022-02-04 14:12:00 +11:00
|
|
|
if (!peerIdStr) throw "Nim-waku multiaddr does not contain peerId";
|
2021-03-23 11:14:51 +11:00
|
|
|
this.peerId = PeerId.createFromB58String(peerIdStr);
|
|
|
|
|
return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId };
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
get rpcUrl(): string {
|
2022-02-01 12:54:54 +11:00
|
|
|
return `http://localhost:${this.rpcPort}/`;
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
2021-05-03 15:52:38 +10:00
|
|
|
private async rpcCall<T>(
|
2021-04-13 15:22:29 +10:00
|
|
|
method: string,
|
|
|
|
|
params: Array<string | number | unknown>
|
2021-05-03 15:54:40 +10:00
|
|
|
): Promise<T> {
|
2022-02-14 09:26:22 +11:00
|
|
|
const res = await fetch(this.rpcUrl, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify({
|
2022-02-04 14:12:00 +11:00
|
|
|
jsonrpc: "2.0",
|
2021-03-10 17:39:53 +11:00
|
|
|
id: 1,
|
|
|
|
|
method,
|
|
|
|
|
params,
|
2022-02-14 09:26:22 +11:00
|
|
|
}),
|
|
|
|
|
headers: new Headers({ "Content-Type": "application/json" }),
|
|
|
|
|
});
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2022-02-14 09:26:22 +11:00
|
|
|
const json = await res.json();
|
|
|
|
|
return json.result;
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
2021-05-03 15:52:38 +10:00
|
|
|
private checkProcess(): void {
|
2021-03-10 17:39:53 +11:00
|
|
|
if (!this.process) {
|
|
|
|
|
throw "Nim Waku isn't started";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-11 10:54:35 +11:00
|
|
|
|
|
|
|
|
export function argsToArray(args: Args): Array<string> {
|
|
|
|
|
const array = [];
|
|
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(args)) {
|
|
|
|
|
// Change the key from camelCase to kebab-case
|
|
|
|
|
const kebabKey = key.replace(/([A-Z])/, (_, capital) => {
|
2022-02-04 14:12:00 +11:00
|
|
|
return "-" + capital.toLowerCase();
|
2021-03-11 10:54:35 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const arg = `--${kebabKey}=${value}`;
|
|
|
|
|
array.push(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
export function defaultArgs(): Args {
|
2021-03-11 10:54:35 +11:00
|
|
|
return {
|
2022-02-04 14:12:00 +11:00
|
|
|
nat: "none",
|
|
|
|
|
listenAddress: "127.0.0.1",
|
2021-03-11 10:54:35 +11:00
|
|
|
relay: true,
|
|
|
|
|
rpc: true,
|
|
|
|
|
rpcAdmin: true,
|
2022-01-20 13:00:58 +11:00
|
|
|
websocketSupport: true,
|
2021-03-11 10:54:35 +11:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 10:44:47 +11:00
|
|
|
export function strToHex(str: string): string {
|
2021-03-12 10:35:50 +11:00
|
|
|
let hex: string;
|
|
|
|
|
try {
|
|
|
|
|
hex = unescape(encodeURIComponent(str))
|
2022-02-04 14:12:00 +11:00
|
|
|
.split("")
|
2021-03-12 10:35:50 +11:00
|
|
|
.map(function (v) {
|
2021-03-12 10:44:47 +11:00
|
|
|
return v.charCodeAt(0).toString(16);
|
2021-03-12 10:35:50 +11:00
|
|
|
})
|
2022-02-04 14:12:00 +11:00
|
|
|
.join("");
|
2021-03-12 10:35:50 +11:00
|
|
|
} catch (e) {
|
|
|
|
|
hex = str;
|
2022-02-04 14:12:00 +11:00
|
|
|
console.log("invalid text input: " + str);
|
2021-03-12 10:35:50 +11:00
|
|
|
}
|
2021-03-29 14:25:03 +11:00
|
|
|
return hex;
|
2021-03-12 10:35:50 +11:00
|
|
|
}
|
|
|
|
|
|
2021-05-03 15:52:38 +10:00
|
|
|
export function bufToHex(buffer: Uint8Array): string {
|
2021-03-29 14:25:03 +11:00
|
|
|
return Array.prototype.map
|
2022-02-04 14:12:00 +11:00
|
|
|
.call(buffer, (x) => ("00" + x.toString(16)).slice(-2))
|
|
|
|
|
.join("");
|
2021-03-12 10:35:50 +11:00
|
|
|
}
|
2021-03-12 17:08:42 +11:00
|
|
|
|
|
|
|
|
interface RpcInfoResponse {
|
2022-01-19 15:43:45 +11:00
|
|
|
// multiaddrs including peer id.
|
|
|
|
|
listenAddresses: string[];
|
2021-03-12 17:08:42 +11:00
|
|
|
}
|