Compare commits

...

11 Commits

Author SHA1 Message Date
Derrick Hammer 336c1996e4
*Refactor to use new MultiSocketProxy from libhyperproxy 2023-04-16 20:18:20 -04:00
Derrick Hammer 48810569d8
*Need to use kernel protomux client 2023-04-09 12:48:44 -04:00
Derrick Hammer a41797a29a
*update deps 2023-04-04 11:36:55 -04:00
Derrick Hammer bc8fa993bc
*update deps 2023-04-04 06:38:54 -04:00
Derrick Hammer c54367823a
*Fix wrong dep url 2023-03-16 10:49:05 -04:00
Derrick Hammer 61d385897a
*If we have no peers, hook on when we have a peer and recurse
*If we have a peer, loop over the peer list, and see if we have any syncable peers. If not, loop again, and hook on the peers open event
2023-02-20 17:33:40 -05:00
Derrick Hammer f3edce42fa
*Need to call resolveWithPeers with resolve callback handler 2023-02-20 11:16:45 -05:00
Derrick Hammer e77eaac0a0
*Check pool.peers head 2023-02-20 11:16:22 -05:00
Derrick Hammer eebc09bee5
*Check and wait on peers on ready method
*Check peer count on query method
2023-02-20 07:39:23 -05:00
Derrick Hammer 3cffb47069
*Update deps 2023-02-19 16:54:36 -05:00
Derrick Hammer 2f063e722a
*Initial version 2023-02-18 10:40:05 -05:00
8 changed files with 586 additions and 0 deletions

14
build.js Normal file
View File

@ -0,0 +1,14 @@
import esbuild from "esbuild";
esbuild.buildSync({
entryPoints: ["src/index.ts"],
outfile: "dist/index.js",
format: "iife",
bundle: true,
legalComments: "external",
// minify: true
define: {
global: "self",
},
external: ['dns']
});

65
package.json Normal file
View File

@ -0,0 +1,65 @@
{
"name": "@lumeweb/kernel-handshake-node",
"types": "module",
"author": {
"name": "Hammer Technologies LLC",
"email": "contact@lumeweb.com"
},
"scripts": {
"test": "jest",
"format": "prettier -w src",
"build-script": "tsc --project tsconfig.build.json && mv dist-build/build.js dist-build/build.mjs",
"compile": "npm run build-script && tsc && rollup -c rollup.config.js",
"build": "npm run compile && node ./dist-build/build.mjs dev"
},
"type": "module",
"dependencies": {
"@lumeweb/kernel-swarm-client": "git+https://git.lumeweb.com/LumeWeb/kernel-swarm-client.git",
"@lumeweb/libhyperproxy": "git+https://git.lumeweb.com/LumeWeb/libhyperproxy.git",
"hsd": "github:handshake-org/hsd",
"libkmodule": "^0.2.53",
"p-defer": "^4.0.0"
},
"devDependencies": {
"@i2labs/dns": "^1.0.1",
"@rollup/plugin-alias": "^4.0.4",
"@rollup/plugin-commonjs": "^24.1.0",
"@rollup/plugin-inject": "^5.0.3",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-typescript": "^11.1.0",
"@screamingvoid/sodium-universal": "^0.1.1",
"@scure/bip39": "^1.2.0",
"@skynetlabs/skynet-nodejs": "^2.9.0",
"@types/node": "^18.15.11",
"@types/read": "^0.0.29",
"@types/streamx": "^2.9.1",
"cli-progress": "^3.12.0",
"esbuild": "^0.17.17",
"eventemitter3": "^5.0.0",
"node-stdlib-browser": "^1.2.0",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"prettier": "^2.8.7",
"read": "^2.1.0",
"rollup": "^3.20.3",
"rollup-plugin-polyfill": "^4.0.0",
"rollup-plugin-polyfill-inject": "^1.0.4",
"rollup-plugin-polyfill-node": "^0.12.0",
"setimmediate": "^1.0.5",
"tslib": "^2.5.0",
"typescript": "^4.9.5",
"util": "^0.12.5",
"vite": "^4.2.1",
"vite-plugin-node-polyfills": "^0.7.0",
"vite-plugin-node-stdlib-browser": "^0.1.1"
},
"pnpm": {
"overrides": {
"protomux": "git+https://git.lumeweb.com/LumeWeb/kernel-protomux-client.git"
},
"patchedDependencies": {
"b4a@1.6.3": "patches/b4a@1.6.3.patch"
}
}
}

36
patches/b4a@1.6.3.patch Normal file
View File

@ -0,0 +1,36 @@
diff --git a/browser.js b/browser.js
index e07f78d17b7b4a2963e4f0062047cc96e6025f9e..a9f266a2644f3f8ebd0f6684b3e11a7519059851 100644
--- a/browser.js
+++ b/browser.js
@@ -3,6 +3,7 @@ const base64 = require('./lib/base64')
const hex = require('./lib/hex')
const utf8 = require('./lib/utf8')
const utf16le = require('./lib/utf16le')
+const Buffer = require('buffer').Buffer
const LE = new Uint8Array(Uint16Array.of(0xff).buffer)[0] === 0xff
diff --git a/index.js b/index.js
index a751d6b4d27c7dd903efbfdc87a98f61044a62b8..8bd32640afdcfbc475168d458c51f8b9162d1656 100644
--- a/index.js
+++ b/index.js
@@ -1,3 +1,5 @@
+const Buffer = require('buffer').Buffer
+
function isBuffer (value) {
return Buffer.isBuffer(value) || value instanceof Uint8Array
}
diff --git a/package.json b/package.json
index eb0f792709b27e66f26658054781b4099507b5ed..69c4ff39c48b0bdd5f0bc7ebbade517e7c5d76e4 100644
--- a/package.json
+++ b/package.json
@@ -8,9 +8,6 @@
"index.js",
"lib"
],
- "browser": {
- "./index.js": "./browser.js"
- },
"scripts": {
"test": "standard && brittle test/*.mjs"
},

37
rollup.config.js Normal file
View File

@ -0,0 +1,37 @@
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import stdLibBrowser from "node-stdlib-browser";
import { handleCircularDependancyWarning } from "node-stdlib-browser/helpers/rollup/plugin";
import alias from "@rollup/plugin-alias";
import inject from "@rollup/plugin-inject";
import { defineConfig } from "rollup";
import * as fs from "fs";
import path from "path";
export default defineConfig({
input: "build/index.js",
output: {
file: "dist/index.js",
format: "iife",
inlineDynamicImports: true,
banner: fs.readFileSync(
path.resolve("node_modules/setimmediate/setImmediate.js")
),
},
plugins: [
json(),
alias({
entries: stdLibBrowser,
}),
resolve({ browser: true, preferBuiltins: true, dedupe: ["libkmodule"] }),
commonjs({ transformMixedEsModules: true }),
inject({
process: stdLibBrowser.process,
Buffer: [stdLibBrowser.buffer, "Buffer"],
}),
],
onwarn: (warning, rollupWarn) => {
handleCircularDependancyWarning(warning, rollupWarn);
},
});

218
src-build/build.ts Normal file
View File

@ -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 kernel:", 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("");
}

190
src/index.ts Normal file
View File

@ -0,0 +1,190 @@
import type { ActiveQuery } from "libkmodule";
import { addHandler, handleMessage } from "libkmodule";
import { createClient } from "@lumeweb/kernel-swarm-client";
import {
createServer,
DummySocket,
MultiSocketProxy,
} from "@lumeweb/libhyperproxy";
// @ts-ignore
import { SPVNode } from "hsd/lib/node";
import defer from "p-defer";
import dns from "@i2labs/dns";
import assert from "assert";
const PROTOCOL = "lumeweb.proxy.handshake";
onmessage = handleMessage;
let moduleLoadedResolve: Function;
let moduleLoaded: Promise<void> = new Promise((resolve) => {
moduleLoadedResolve = resolve;
});
addHandler("presentSeed", handlePresentSeed);
addHandler("ready", handleReady);
addHandler("query", handleQuery);
let swarm;
let proxy: MultiSocketProxy;
let node: SPVNode;
function resolveWithPeers(resolve: Function) {
if (!node.pool.peers.head()) {
node.pool.on("peer", () => {
resolveWithPeers(resolve);
});
return;
}
let syncable = false;
for (let peer = node.pool.peers.head(); peer; peer = peer.next) {
if (node.pool.isSyncable(peer)) {
syncable = true;
break;
}
}
if (!syncable) {
for (let peer = node.pool.peers.head(); peer; peer = peer.next) {
const listener = () => {
peer.off("open", listener);
resolve();
};
peer.on("open", listener);
}
return;
}
return resolve(null);
}
async function handlePresentSeed(aq: ActiveQuery) {
swarm = createClient();
const peerConnected = defer();
node = new SPVNode({
config: false,
argv: false,
env: false,
noDns: true,
memory: false,
logFile: false,
logConsole: true,
logLevel: "info",
workers: true,
network: "main",
createServer,
createSocket: (port: number, host: string) => {
const socket = proxy.createSocket({
host,
port,
}) as unknown as DummySocket;
socket.connect();
return socket;
},
});
node.pool.hosts.resolve = async (host: any, family?: any) => {
if (family == null) family = null;
assert(family === null || family === 4 || family === 6);
const stub = new dns.promises.Resolver();
stub.setServers([
// Cloudflare
"1.1.1.1",
// Google
"8.8.8.8",
"8.8.4.4",
// OpenDNS
"208.67.222.222",
"208.67.220.220",
"208.67.222.220",
"208.67.220.222",
]);
const out = [];
const types = [];
if (family == null || family === 4) types.push("A");
if (family == null || family === 6) types.push("AAAA");
for (const type of types) {
let addrs;
try {
addrs = await stub.resolve(host, type as any);
} catch (e) {
continue;
}
// @ts-ignore
out.push(...addrs);
}
if (out.length === 0) throw new Error("No DNS results.");
return out;
};
if (node?.http?.http?.listen) {
node.http.http.listen = (port: number, host: string, cb: Function) => cb();
}
proxy = new MultiSocketProxy({
protocol: PROTOCOL,
swarm,
server: false,
autostart: true,
listen: true,
});
proxy.on("peerChannelOpen", () => {
peerConnected.resolve();
});
swarm.join(PROTOCOL);
await swarm.start();
await peerConnected.promise;
await node.open();
await node.connect();
await node.startSync();
moduleLoadedResolve();
}
async function handleReady(aq: ActiveQuery) {
await moduleLoaded;
await new Promise((resolve): void => {
if (node.chain.synced) {
return resolveWithPeers(resolve);
}
node.pool.once("full", () => {
resolveWithPeers(resolve);
});
});
aq.respond();
}
async function handleQuery(aq: ActiveQuery) {
if (!node.chain.synced || !node.pool.peers.head()) {
aq.reject("not ready");
return;
}
try {
aq.respond(await node.rpc.call(aq.callerInput));
} catch (e) {
aq.reject((e as Error).message);
}
}

14
tsconfig.build.json Normal file
View File

@ -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__/*"]
}

12
tsconfig.json Normal file
View File

@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "esnext",
"declaration": true,
"moduleResolution": "node",
"outDir": "./build",
"strict": true,
"allowSyntheticDefaultImports": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}