2022-06-20 16:48:30 +10:00
|
|
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
|
|
|
|
import { peerIdFromString } from "@libp2p/peer-id";
|
|
|
|
|
import { Multiaddr, multiaddr } from "@multiformats/multiaddr";
|
2022-11-01 21:31:53 +11:00
|
|
|
import { DefaultPubSubTopic } from "@waku/core";
|
2023-03-16 12:22:14 +11:00
|
|
|
import { isDefined } from "@waku/utils";
|
2023-03-14 10:10:38 +05:30
|
|
|
import { bytesToHex, hexToBytes } from "@waku/utils/bytes";
|
2022-02-04 14:12:00 +11:00
|
|
|
import debug from "debug";
|
|
|
|
|
import portfinder from "portfinder";
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2023-05-19 01:28:49 +05:30
|
|
|
import { existsAsync, mkdirAsync, openAsync } from "../async_fs.js";
|
|
|
|
|
import { delay } from "../delay.js";
|
|
|
|
|
import waitForLine from "../log_file.js";
|
|
|
|
|
|
|
|
|
|
import Dockerode from "./dockerode.js";
|
|
|
|
|
import {
|
|
|
|
|
Args,
|
|
|
|
|
KeyPair,
|
|
|
|
|
LogLevel,
|
|
|
|
|
MessageRpcQuery,
|
|
|
|
|
MessageRpcResponse,
|
|
|
|
|
} from "./interfaces.js";
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2023-05-19 01:28:49 +05:30
|
|
|
const log = debug("waku:node");
|
2021-04-06 11:00:40 +10:00
|
|
|
|
2022-08-24 07:43:04 +10:00
|
|
|
const WAKU_SERVICE_NODE_PARAMS =
|
|
|
|
|
process.env.WAKU_SERVICE_NODE_PARAMS ?? undefined;
|
2022-05-26 15:49:39 +10:00
|
|
|
const NODE_READY_LOG_LINE = "Node setup complete";
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2023-04-17 10:29:36 +05:30
|
|
|
const DOCKER_IMAGE_NAME =
|
2023-05-19 01:52:06 +05:30
|
|
|
process.env.WAKUNODE_IMAGE || "statusteam/nim-waku:v0.17.0";
|
2023-04-17 10:29:36 +05:30
|
|
|
|
|
|
|
|
const isGoWaku = DOCKER_IMAGE_NAME.includes("go-waku");
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const LOG_DIR = "./log";
|
2021-03-15 13:38:36 +11:00
|
|
|
|
2022-09-11 00:57:10 +10:00
|
|
|
const OneMillion = BigInt(1_000_000);
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
BigInt.prototype.toJSON = function toJSON() {
|
|
|
|
|
return Number(this);
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-19 01:28:49 +05:30
|
|
|
export class NimGoNode {
|
|
|
|
|
private docker?: Dockerode;
|
2021-03-12 17:08:42 +11:00
|
|
|
private peerId?: PeerId;
|
2021-03-23 11:14:51 +11:00
|
|
|
private multiaddrWithId?: Multiaddr;
|
2023-01-27 15:37:57 +11:00
|
|
|
private websocketPort?: number;
|
2022-02-01 12:54:54 +11:00
|
|
|
private readonly logPath: string;
|
|
|
|
|
private rpcPort?: number;
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2022-03-09 12:59:47 +11:00
|
|
|
/**
|
|
|
|
|
* Convert a [[WakuMessage]] to a [[WakuRelayMessage]]. The latter is used
|
|
|
|
|
* by the nwaku JSON-RPC API.
|
|
|
|
|
*/
|
2022-09-11 00:57:10 +10:00
|
|
|
static toMessageRpcQuery(message: {
|
|
|
|
|
payload: Uint8Array;
|
|
|
|
|
contentTopic: string;
|
|
|
|
|
timestamp?: Date;
|
|
|
|
|
}): MessageRpcQuery {
|
2022-03-09 12:59:47 +11:00
|
|
|
if (!message.payload) {
|
|
|
|
|
throw "Attempting to convert empty message";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let timestamp;
|
2022-09-11 00:57:10 +10:00
|
|
|
if (message.timestamp) {
|
|
|
|
|
timestamp = BigInt(message.timestamp.valueOf()) * OneMillion;
|
2022-03-09 12:59:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2023-03-21 12:10:56 +11:00
|
|
|
payload: Buffer.from(message.payload).toString("base64"),
|
2022-03-09 12:59:47 +11:00
|
|
|
contentTopic: message.contentTopic,
|
|
|
|
|
timestamp,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 15:49:07 +11:00
|
|
|
constructor(logName: string) {
|
2023-05-19 01:28:49 +05:30
|
|
|
this.logPath = `${LOG_DIR}/wakunode_${logName}.log`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type(): "go-waku" | "nwaku" {
|
|
|
|
|
return isGoWaku ? "go-waku" : "nwaku";
|
2021-03-15 13:25:14 +11:00
|
|
|
}
|
2021-03-11 11:11:37 +11:00
|
|
|
|
2023-05-23 16:06:46 +05:30
|
|
|
get nodeType(): "go-waku" | "nwaku" {
|
|
|
|
|
return isGoWaku ? "go-waku" : "nwaku";
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 14:58:27 +11:00
|
|
|
async start(args: Args = {}): Promise<void> {
|
2023-05-19 01:28:49 +05:30
|
|
|
this.docker = await Dockerode.createInstance(DOCKER_IMAGE_NAME);
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 10:29:36 +05:30
|
|
|
await openAsync(this.logPath, "w");
|
2021-03-11 10:54:35 +11:00
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
const mergedArgs = defaultArgs();
|
|
|
|
|
|
2023-05-19 01:28:49 +05:30
|
|
|
// waku nodes takes some time to bind port so to decrease chances of conflict
|
2023-01-20 16:43:22 +11:00
|
|
|
// we also randomize the first port that portfinder will try
|
|
|
|
|
const startPort = Math.floor(Math.random() * (65535 - 1025) + 1025);
|
|
|
|
|
|
2022-02-01 12:54:54 +11:00
|
|
|
const ports: number[] = await new Promise((resolve, reject) => {
|
2023-02-17 01:07:44 +05:30
|
|
|
portfinder.getPorts(4, { port: startPort }, (err, ports) => {
|
2022-02-01 12:54:54 +11:00
|
|
|
if (err) reject(err);
|
|
|
|
|
resolve(ports);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-20 14:58:27 +11:00
|
|
|
if (isGoWaku && !args.logLevel) {
|
|
|
|
|
args.logLevel = LogLevel.Debug;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 10:29:36 +05:30
|
|
|
const [rpcPort, tcpPort, websocketPort, discv5UdpPort] = ports;
|
|
|
|
|
this.rpcPort = rpcPort;
|
|
|
|
|
this.websocketPort = websocketPort;
|
|
|
|
|
|
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
|
|
|
{
|
2023-04-17 10:29:36 +05:30
|
|
|
rpcPort,
|
|
|
|
|
tcpPort,
|
|
|
|
|
websocketPort,
|
|
|
|
|
...(args?.peerExchange && { discv5UdpPort }),
|
2023-05-23 16:06:46 +05:30
|
|
|
...(isGoWaku && { minRelayPeersToPublish: 0 }),
|
2022-02-01 12:54:54 +11:00
|
|
|
},
|
2023-05-19 01:28:49 +05:30
|
|
|
{ rpcAddress: "0.0.0.0" },
|
2021-04-09 11:23:00 +10:00
|
|
|
args
|
|
|
|
|
);
|
2021-03-12 17:08:42 +11:00
|
|
|
|
2022-11-17 14:28:01 +11:00
|
|
|
process.env.WAKUNODE2_STORE_MESSAGE_DB_URL = "";
|
|
|
|
|
|
2023-05-19 01:28:49 +05:30
|
|
|
if (this.docker.container) {
|
|
|
|
|
await this.docker.stop();
|
2022-08-24 07:43:04 +10:00
|
|
|
}
|
2022-12-07 11:35:30 +05:30
|
|
|
|
2023-05-19 01:28:49 +05:30
|
|
|
await this.docker.startContainer(
|
|
|
|
|
ports,
|
|
|
|
|
mergedArgs,
|
|
|
|
|
this.logPath,
|
|
|
|
|
WAKU_SERVICE_NODE_PARAMS
|
|
|
|
|
);
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2023-04-17 10:29:36 +05:30
|
|
|
try {
|
2023-05-19 01:28:49 +05:30
|
|
|
log(`Waiting to see '${NODE_READY_LOG_LINE}' in ${this.type} logs`);
|
2023-04-17 10:29:36 +05:30
|
|
|
await this.waitForLog(NODE_READY_LOG_LINE, 15000);
|
|
|
|
|
if (process.env.CI) await delay(100);
|
2023-05-19 01:28:49 +05:30
|
|
|
log(`${this.type} node has been started`);
|
2023-04-17 10:29:36 +05:30
|
|
|
} catch (error) {
|
2023-05-19 01:28:49 +05:30
|
|
|
log(`Error starting ${this.type}: ${error}`);
|
|
|
|
|
if (this.docker.container) await this.docker.stop();
|
2023-04-17 10:29:36 +05:30
|
|
|
throw error;
|
|
|
|
|
}
|
2021-03-15 13:25:14 +11:00
|
|
|
}
|
|
|
|
|
|
2023-04-17 10:29:36 +05:30
|
|
|
public async stop(): Promise<void> {
|
2023-05-19 01:28:49 +05:30
|
|
|
this.docker?.stop();
|
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
|
|
|
}
|
|
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
/** Calls nwaku JSON-RPC API `get_waku_v2_admin_v1_peers` to check
|
2021-03-10 17:39:53 +11:00
|
|
|
* for known peers
|
2023-05-19 01:28:49 +05:30
|
|
|
* @throws if WakuNode isn't started.
|
2021-03-10 17:39:53 +11:00
|
|
|
*/
|
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(
|
2022-09-11 00:57:10 +10:00
|
|
|
message: MessageRpcQuery,
|
2022-08-24 19:04:31 +10:00
|
|
|
pubSubTopic: string = DefaultPubSubTopic
|
2021-06-09 12:25:56 +10:00
|
|
|
): Promise<boolean> {
|
2021-03-12 10:35:50 +11:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
2022-09-11 00:57:10 +10:00
|
|
|
if (typeof message.timestamp === "undefined") {
|
|
|
|
|
message.timestamp = BigInt(new Date().valueOf()) * OneMillion;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
return this.rpcCall<boolean>("post_waku_v2_relay_v1_message", [
|
2022-08-24 19:04:31 +10:00
|
|
|
pubSubTopic,
|
2022-03-09 12:59:47 +11:00
|
|
|
message,
|
2021-03-12 10:35:50 +11:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 11:12:06 +10:00
|
|
|
async messages(
|
|
|
|
|
pubsubTopic: string = DefaultPubSubTopic
|
2022-09-11 00:57:10 +10:00
|
|
|
): Promise<MessageRpcResponse[]> {
|
2021-03-12 10:35:50 +11:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
2022-09-11 00:57:10 +10:00
|
|
|
const msgs = await this.rpcCall<MessageRpcResponse[]>(
|
2022-02-04 14:12:00 +11:00
|
|
|
"get_waku_v2_relay_v1_messages",
|
2022-06-15 11:12:06 +10:00
|
|
|
[pubsubTopic]
|
2021-07-07 11:23:56 +10:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
2022-06-15 14:22:10 +10:00
|
|
|
const { privateKey, publicKey, seckey, pubkey } = await this.rpcCall<{
|
2021-07-09 10:07:39 +10:00
|
|
|
seckey: string;
|
|
|
|
|
pubkey: string;
|
2022-06-15 14:22:10 +10:00
|
|
|
privateKey: string;
|
|
|
|
|
publicKey: string;
|
2022-02-04 14:12:00 +11:00
|
|
|
}>("get_waku_v2_private_v1_asymmetric_keypair", []);
|
2021-07-09 10:07:39 +10:00
|
|
|
|
2022-06-15 14:22:10 +10:00
|
|
|
// To be removed once https://github.com/vacp2p/rfc/issues/507 is fixed
|
|
|
|
|
if (seckey) {
|
|
|
|
|
return { privateKey: seckey, publicKey: pubkey };
|
|
|
|
|
} else {
|
|
|
|
|
return { privateKey, publicKey };
|
|
|
|
|
}
|
2021-07-09 10:07:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async postAsymmetricMessage(
|
2022-09-11 00:57:10 +10:00
|
|
|
message: MessageRpcQuery,
|
2021-07-09 10:07:39 +10:00
|
|
|
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-14 10:50:02 +11:00
|
|
|
"0x" + bytesToHex(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
|
2023-02-16 13:27:39 +11:00
|
|
|
): Promise<MessageRpcResponse[]> {
|
2021-07-09 10:37:29 +10:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
2023-02-16 13:27:39 +11:00
|
|
|
return await this.rpcCall<MessageRpcResponse[]>(
|
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-14 10:50:02 +11:00
|
|
|
"0x" + bytesToHex(privateKey),
|
2021-07-09 10:37:29 +10:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 10:50:02 +11:00
|
|
|
async getSymmetricKey(): Promise<Uint8Array> {
|
2021-07-15 12:12:43 +10:00
|
|
|
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
|
|
|
[]
|
2022-02-14 10:50:02 +11:00
|
|
|
).then(hexToBytes);
|
2021-07-15 12:12:43 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async postSymmetricMessage(
|
2022-09-11 00:57:10 +10:00
|
|
|
message: MessageRpcQuery,
|
2021-07-15 12:12:43 +10:00
|
|
|
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-14 10:50:02 +11:00
|
|
|
"0x" + bytesToHex(symKey),
|
2021-07-15 12:12:43 +10:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getSymmetricMessages(
|
|
|
|
|
symKey: Uint8Array,
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic?: string
|
2023-02-16 13:27:39 +11:00
|
|
|
): Promise<MessageRpcResponse[]> {
|
2021-07-15 12:12:43 +10:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
2023-02-16 13:27:39 +11:00
|
|
|
return await this.rpcCall<MessageRpcResponse[]>(
|
2022-02-04 14:12:00 +11:00
|
|
|
"get_waku_v2_private_v1_symmetric_messages",
|
2022-02-14 10:50:02 +11:00
|
|
|
[
|
|
|
|
|
pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
|
|
|
|
|
"0x" + bytesToHex(symKey),
|
|
|
|
|
]
|
2021-07-15 12:12:43 +10:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
async getPeerId(): Promise<PeerId> {
|
2023-01-27 15:37:57 +11:00
|
|
|
if (this.peerId) return this.peerId;
|
|
|
|
|
this.peerId = await this._getPeerId();
|
|
|
|
|
return this.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> {
|
2023-01-27 15:37:57 +11:00
|
|
|
if (this.multiaddrWithId) return this.multiaddrWithId;
|
|
|
|
|
|
|
|
|
|
const peerId = await this.getPeerId();
|
|
|
|
|
|
|
|
|
|
this.multiaddrWithId = multiaddr(
|
|
|
|
|
`/ip4/127.0.0.1/tcp/${this.websocketPort}/ws/p2p/${peerId.toString()}`
|
|
|
|
|
);
|
|
|
|
|
return this.multiaddrWithId;
|
2021-03-23 11:14:51 +11:00
|
|
|
}
|
2021-03-12 17:08:42 +11:00
|
|
|
|
2023-01-27 15:37:57 +11:00
|
|
|
private async _getPeerId(): Promise<PeerId> {
|
|
|
|
|
if (this.peerId) {
|
|
|
|
|
return this.peerId;
|
2021-03-23 11:14:51 +11:00
|
|
|
}
|
|
|
|
|
const res = await this.info();
|
2023-01-27 15:37:57 +11:00
|
|
|
const multiaddrWithId = res.listenAddresses
|
2022-01-20 13:00:58 +11:00
|
|
|
.map((ma) => multiaddr(ma))
|
2022-02-04 14:12:00 +11:00
|
|
|
.find((ma) => ma.protoNames().includes("ws"));
|
2023-05-19 01:28:49 +05:30
|
|
|
if (!multiaddrWithId) throw `${this.type} did not return a ws multiaddr`;
|
2023-01-27 15:37:57 +11:00
|
|
|
const peerIdStr = multiaddrWithId.getPeerId();
|
2023-05-19 01:28:49 +05:30
|
|
|
if (!peerIdStr) throw `${this.type} multiaddr does not contain peerId`;
|
2022-06-20 16:48:30 +10:00
|
|
|
this.peerId = peerIdFromString(peerIdStr);
|
2023-01-27 15:37:57 +11:00
|
|
|
|
|
|
|
|
return this.peerId;
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
get rpcUrl(): string {
|
2022-12-12 10:52:17 +05:30
|
|
|
return `http://127.0.0.1:${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-09-05 21:30:29 +10:00
|
|
|
log("RPC Query: ", method, params);
|
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" }),
|
|
|
|
|
});
|
|
|
|
|
const json = await res.json();
|
2022-11-15 14:15:29 +11:00
|
|
|
log(`RPC Response: `, JSON.stringify(json));
|
2022-02-14 09:26:22 +11:00
|
|
|
return json.result;
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
2021-05-03 15:52:38 +10:00
|
|
|
private checkProcess(): void {
|
2023-05-19 01:28:49 +05:30
|
|
|
if (!this.docker?.container) {
|
|
|
|
|
throw `${this.type} container hasn't started`;
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-11 10:54:35 +11:00
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
export function defaultArgs(): Args {
|
2021-03-11 10:54:35 +11:00
|
|
|
return {
|
2023-04-17 10:29:36 +05:30
|
|
|
listenAddress: "0.0.0.0",
|
2021-03-11 10:54:35 +11:00
|
|
|
rpc: true,
|
2022-12-12 11:08:08 +05:30
|
|
|
relay: false,
|
2021-03-11 10:54:35 +11:00
|
|
|
rpcAdmin: true,
|
2022-01-20 13:00:58 +11:00
|
|
|
websocketSupport: true,
|
2022-11-17 11:02:04 +11:00
|
|
|
logLevel: LogLevel.Trace,
|
2021-03-11 10:54:35 +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[];
|
2022-05-04 20:07:21 +10:00
|
|
|
enrUri?: string;
|
2021-03-12 17:08:42 +11:00
|
|
|
}
|
2023-02-16 13:27:39 +11:00
|
|
|
|
|
|
|
|
export function base64ToUtf8(b64: string): string {
|
|
|
|
|
return Buffer.from(b64, "base64").toString("utf-8");
|
|
|
|
|
}
|