*Initial version
This commit is contained in:
parent
c363ae665b
commit
d0e6d91d11
|
@ -0,0 +1,11 @@
|
||||||
|
import esbuild from "esbuild";
|
||||||
|
|
||||||
|
esbuild.buildSync({
|
||||||
|
entryPoints: ["src/index.ts"],
|
||||||
|
outfile: "dist/index.js",
|
||||||
|
format: "iife",
|
||||||
|
bundle: true,
|
||||||
|
define: {
|
||||||
|
global: "self",
|
||||||
|
},
|
||||||
|
});
|
|
@ -0,0 +1,41 @@
|
||||||
|
{
|
||||||
|
"name": "@lumeweb/kernel-discovery-irc",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"test": "jest",
|
||||||
|
"build-script": "tsc && tsc --project tsconfig.build.json && mv dist-build/build.js dist-build/build.mjs",
|
||||||
|
"compile": "pnpm run build-script && node build.js && rm -rf build/",
|
||||||
|
"build": "pnpm run compile && node ./dist-build/build.mjs dev"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@ctrl/irc": "2.1.0",
|
||||||
|
"libkmodule": "^0.2.46",
|
||||||
|
"streamx": "^2.13.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@scure/bip39": "^1.1.0",
|
||||||
|
"@skynetlabs/skynet-nodejs": "^2.9.0",
|
||||||
|
"@types/node": "^18.11.18",
|
||||||
|
"@types/read": "^0.0.29",
|
||||||
|
"@types/streamx": "^2.9.1",
|
||||||
|
"buffer": "^6.0.3",
|
||||||
|
"cli-progress": "^3.11.2",
|
||||||
|
"crypto-browserify": "^3.12.0",
|
||||||
|
"esbuild": "^0.17.0",
|
||||||
|
"events": "^3.3.0",
|
||||||
|
"read": "^2.0.0",
|
||||||
|
"string_decoder": "^1.3.0",
|
||||||
|
"typescript": "^4.9.4"
|
||||||
|
},
|
||||||
|
"browser": {
|
||||||
|
"crypto": false,
|
||||||
|
"net": "./build/net.js",
|
||||||
|
"tls": "./build/net.js"
|
||||||
|
},
|
||||||
|
"pnpm": {
|
||||||
|
"patchedDependencies": {
|
||||||
|
"@ctrl/irc@2.1.0": "patches/@ctrl__irc@2.1.0.patch"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
diff --git a/dist/src/irc.js b/dist/src/irc.js
|
||||||
|
index fd7a55d6bb09a961921e58e6bff1a7028c61bb47..c96cbca5e904b5ba0312d7ae273e537e66e22fb9 100644
|
||||||
|
--- a/dist/src/irc.js
|
||||||
|
+++ b/dist/src/irc.js
|
||||||
|
@@ -9,6 +9,7 @@ import defaultsdeep from 'lodash.defaultsdeep';
|
||||||
|
import { TypedEmitter } from 'tiny-typed-emitter';
|
||||||
|
import { CyclingPingTimer } from './cyclingPingTimer.js';
|
||||||
|
import { parseMessage } from './parseMessage.js';
|
||||||
|
+import { Buffer } from 'buffer';
|
||||||
|
const log = debug('irc');
|
||||||
|
const lineDelimiter = new RegExp(/\r\n|\r|\n/);
|
||||||
|
const defaultOptions = {
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,218 @@
|
||||||
|
// This is the standard build script for a kernel module.
|
||||||
|
|
||||||
|
import * as fs from "fs";
|
||||||
|
import read from "read";
|
||||||
|
import * as bip39 from "@scure/bip39";
|
||||||
|
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
||||||
|
//@ts-ignore
|
||||||
|
import { SkynetClient } from "@skynetlabs/skynet-nodejs";
|
||||||
|
|
||||||
|
// Helper variables to make it easier to return empty values alongside errors.
|
||||||
|
const nu8 = new Uint8Array(0);
|
||||||
|
const nkp = {
|
||||||
|
publicKey: nu8,
|
||||||
|
secretKey: nu8,
|
||||||
|
};
|
||||||
|
|
||||||
|
// readFile is a wrapper for fs.readFileSync that handles the try-catch for the
|
||||||
|
// caller.
|
||||||
|
function readFile(fileName: string): [string, string | null] {
|
||||||
|
try {
|
||||||
|
let data = fs.readFileSync(fileName, "utf8");
|
||||||
|
return [data, null];
|
||||||
|
} catch (err) {
|
||||||
|
return ["", "unable to read file: " + JSON.stringify(err)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// readFileBinary is a wrapper for fs.readFileSync that handles the try-catch
|
||||||
|
// for the caller.
|
||||||
|
function readFileBinary(fileName: string): [Uint8Array, string | null] {
|
||||||
|
try {
|
||||||
|
let data = fs.readFileSync(fileName, null);
|
||||||
|
return [data, null];
|
||||||
|
} catch (err) {
|
||||||
|
return [nu8, "unable to read file: " + JSON.stringify(err)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeFile is a wrapper for fs.writeFileSync which handles the try-catch in a
|
||||||
|
// non-exception way.
|
||||||
|
function writeFile(fileName: string, fileData: string): string | null {
|
||||||
|
try {
|
||||||
|
fs.writeFileSync(fileName, fileData);
|
||||||
|
return null;
|
||||||
|
} catch (err) {
|
||||||
|
return "unable to write file: " + JSON.stringify(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handlePass handles all portions of the script that occur after the password
|
||||||
|
// has been requested. If no password needs to be requested, handlePass will be
|
||||||
|
// called with a null input. We need to structure the code this way because the
|
||||||
|
// password reader is async and we can only access the password when using a
|
||||||
|
// callback.
|
||||||
|
function handlePass(password: string) {
|
||||||
|
try {
|
||||||
|
// If we are running prod and the seed file does not exist, we
|
||||||
|
// need to confirm the password and also warn the user to use a
|
||||||
|
// secure password.
|
||||||
|
if (!fs.existsSync(seedFile) && process.argv[2] === "prod") {
|
||||||
|
// The file does not exist, we need to confirm the
|
||||||
|
// password.
|
||||||
|
console.log();
|
||||||
|
console.log(
|
||||||
|
"No production entry found for module. Creating new production module..."
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
"If someone can guess the password, they can push arbitrary changes to your module."
|
||||||
|
);
|
||||||
|
console.log("Please use a secure password.");
|
||||||
|
console.log();
|
||||||
|
read(
|
||||||
|
{ prompt: "Confirm Password: ", silent: true },
|
||||||
|
function (err: any, confirmPassword: string) {
|
||||||
|
if (err) {
|
||||||
|
console.error("unable to fetch password:", err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
if (password !== confirmPassword) {
|
||||||
|
console.error("passwords do not match");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
handlePassConfirm(moduleSalt, password);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// If the seed file does exist, or if we are using dev,
|
||||||
|
// there's no need to confirm the password but we do
|
||||||
|
// need to pass the logic off to the handlePassConfirm
|
||||||
|
// callback.
|
||||||
|
handlePassConfirm(moduleSalt, password);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Unable to read seedFile:", err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handlePassConfirm handles the full script after the confirmation password
|
||||||
|
// has been provided. If not confirmation password is needed, this function
|
||||||
|
// will be called anyway using the unconfirmed password as input.
|
||||||
|
function handlePassConfirm(seed: string, password: string) {
|
||||||
|
// Create the seedFile if it does not exist. For dev we just save the
|
||||||
|
// seed to disk outright, because this is a dev build and therefore not
|
||||||
|
// security sensitive. Also the dev seed does not get pushed to the
|
||||||
|
// github repo.
|
||||||
|
//
|
||||||
|
// For prod, we use the seed to create a new seed (called the shield)
|
||||||
|
// which allows us to verify that the developer has provided the right
|
||||||
|
// password when deploying the module. The shield does get pushed to
|
||||||
|
// the github repo so that the production module is the same on all
|
||||||
|
// devices.
|
||||||
|
if (!fs.existsSync(seedFile) && process.argv[2] !== "prod") {
|
||||||
|
// Generate the seed phrase and write it to the file.
|
||||||
|
let seedPhrase = bip39.generateMnemonic(wordlist);
|
||||||
|
let errWF = writeFile(seedFile, seedPhrase);
|
||||||
|
if (errWF !== null) {
|
||||||
|
console.error("unable to write file:", errWF);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
} else if (!fs.existsSync(seedFile) && process.argv[2] === "prod") {
|
||||||
|
// Generate the seed phrase.
|
||||||
|
let seedPhrase = bip39.generateMnemonic(wordlist);
|
||||||
|
// Write the registry link to the file.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load or verify the seed. If this is prod, the password is used to
|
||||||
|
// create and verify the seed. If this is dev, we just load the seed
|
||||||
|
// with no password.
|
||||||
|
let seedPhrase: string;
|
||||||
|
let registryLink: string;
|
||||||
|
if (process.argv[2] === "prod") {
|
||||||
|
// Generate the seed phrase from the password.
|
||||||
|
seedPhrase = bip39.generateMnemonic(wordlist);
|
||||||
|
} else {
|
||||||
|
let [sp, errRF] = readFile(seedFile);
|
||||||
|
if (errRF !== null) {
|
||||||
|
console.error("unable to read seed phrase for dev command from disk");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
seedPhrase = sp;
|
||||||
|
}
|
||||||
|
|
||||||
|
let metadata = {
|
||||||
|
Filename: "index.js",
|
||||||
|
};
|
||||||
|
const client = new SkynetClient("https://web3portal.com");
|
||||||
|
client
|
||||||
|
.uploadFile("dist/index.js")
|
||||||
|
.then((result: any) => {
|
||||||
|
console.log("Immutable Link for module:", result);
|
||||||
|
})
|
||||||
|
.catch((err: any) => {
|
||||||
|
console.error("unable to upload file", err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a newline for readability.
|
||||||
|
console.log();
|
||||||
|
|
||||||
|
// Check for a 'dev' or 'prod' input to the script.
|
||||||
|
if (process.argv.length !== 3) {
|
||||||
|
console.error("need to provide either 'dev' or 'prod' as an input");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the build folder if it does not exist.
|
||||||
|
if (!fs.existsSync("build")) {
|
||||||
|
fs.mkdirSync("build");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine the seed file.
|
||||||
|
let seedFile: string;
|
||||||
|
if (process.argv[2] === "prod") {
|
||||||
|
seedFile = "module-skylink";
|
||||||
|
} else if (process.argv[2] === "dev") {
|
||||||
|
seedFile = "build/dev-seed";
|
||||||
|
} else {
|
||||||
|
console.error("need to provide either 'dev' or 'prod' as an input");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If doing a prod deployment, check whether the salt file exists. If it does
|
||||||
|
// not, create it.
|
||||||
|
let moduleSalt: string;
|
||||||
|
if (!fs.existsSync(".module-salt")) {
|
||||||
|
moduleSalt = bip39.generateMnemonic(wordlist);
|
||||||
|
let errWF = writeFile(".module-salt", moduleSalt);
|
||||||
|
if (errWF !== null) {
|
||||||
|
console.error("unable to write module salt file:", errWF);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let [ms, errRF] = readFile(".module-salt");
|
||||||
|
if (errRF !== null) {
|
||||||
|
console.error("unable to read moduleSalt");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
ms = ms.replace(/\n$/, "");
|
||||||
|
moduleSalt = ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Need to get a password if this is a prod build.
|
||||||
|
if (process.argv[2] === "prod") {
|
||||||
|
read(
|
||||||
|
{ prompt: "Password: ", silent: true },
|
||||||
|
function (err: any, password: string) {
|
||||||
|
if (err) {
|
||||||
|
console.error("unable to fetch password:", err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
handlePass(password);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
handlePass("");
|
||||||
|
}
|
|
@ -0,0 +1,60 @@
|
||||||
|
import type { ActiveQuery } from "libkmodule";
|
||||||
|
import { addHandler, callModule } from "libkmodule";
|
||||||
|
import DiscoveryIRC from "@lumeweb/peer-discovery-irc";
|
||||||
|
import { handleMessage } from "libkmodule";
|
||||||
|
|
||||||
|
onmessage = handleMessage;
|
||||||
|
|
||||||
|
async function handleRegister(aq: ActiveQuery) {
|
||||||
|
let [ret, err] = await callModule(
|
||||||
|
"FAAT5HPzSHOQCbEdxC9R2-_FbMHpTFhAWzumKLWREkGPdQ",
|
||||||
|
"register"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
aq.reject(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
aq.respond(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleName(aq: ActiveQuery): Promise<void> {
|
||||||
|
aq.respond("irc");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDiscover(aq: ActiveQuery): Promise<void> {
|
||||||
|
if (!("pubkey" in aq.callerInput)) {
|
||||||
|
aq.reject("pubkey required");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (aq.callerInput.pubkey.length !== 32) {
|
||||||
|
aq.reject("pubkey must be 32 bytes");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!("pubkey" in aq.callerInput)) {
|
||||||
|
aq.reject("pubkey required");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
"options" in aq.callerInput &&
|
||||||
|
typeof aq.callerInput.options !== "object"
|
||||||
|
) {
|
||||||
|
aq.reject(`options must be an object`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ret = await DiscoveryIRC(
|
||||||
|
aq.callerInput.pubkey,
|
||||||
|
aq.callerInput?.options
|
||||||
|
);
|
||||||
|
|
||||||
|
aq.respond(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
addHandler("register", handleRegister);
|
||||||
|
addHandler("name", handleName);
|
||||||
|
addHandler("discover", handleDiscover);
|
|
@ -0,0 +1,48 @@
|
||||||
|
import { Duplex, Callback } from "streamx";
|
||||||
|
interface SocketOptions {
|
||||||
|
allowHalfOpen?: boolean;
|
||||||
|
remoteAddress?: string;
|
||||||
|
remotePort?: number;
|
||||||
|
cb?: Function;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class Socket extends Duplex {
|
||||||
|
private _ws: WebSocket;
|
||||||
|
|
||||||
|
constructor({ remoteAddress, remotePort, cb }: SocketOptions = {}) {
|
||||||
|
super();
|
||||||
|
this._ws = new WebSocket(`wss://${remoteAddress}/webirc`);
|
||||||
|
|
||||||
|
["message", "end", "close", "error"].forEach((event) => {
|
||||||
|
this._ws.addEventListener(event, (...args) => {
|
||||||
|
let sendEvent = event;
|
||||||
|
let sendData = args;
|
||||||
|
|
||||||
|
if (event === "message") {
|
||||||
|
sendEvent = "data";
|
||||||
|
sendData[0] = (args[0] as MessageEvent).data + "\n" as any;
|
||||||
|
}
|
||||||
|
// @ts-ignore
|
||||||
|
return this.emit(sendEvent, ...sendData);
|
||||||
|
});
|
||||||
|
this._ws.addEventListener("open", cb as any);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_write(data: any, cb: Callback) {
|
||||||
|
this._ws.send(data);
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
setEncoding() {}
|
||||||
|
}
|
||||||
|
export function connect(
|
||||||
|
port: number,
|
||||||
|
host: string,
|
||||||
|
opts = {},
|
||||||
|
cb: Function
|
||||||
|
): Socket {
|
||||||
|
return new Socket({
|
||||||
|
remotePort: port,
|
||||||
|
remoteAddress: host,
|
||||||
|
cb,
|
||||||
|
});
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "esnext",
|
||||||
|
"module": "esnext",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"declaration": true,
|
||||||
|
"outDir": "./dist-build",
|
||||||
|
"strict": true,
|
||||||
|
"esModuleInterop": true
|
||||||
|
},
|
||||||
|
"include": ["src-build"],
|
||||||
|
"exclude": ["node_modules", "**/__tests__/*"]
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "esnext",
|
||||||
|
"declaration": true,
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"outDir": "./build",
|
||||||
|
"strict": true,
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"esModuleInterop": true
|
||||||
|
},
|
||||||
|
"include": ["src"],
|
||||||
|
"exclude": ["node_modules", "**/__tests__/*"]
|
||||||
|
}
|
Loading…
Reference in New Issue