@chaincraft/core 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -6
- package/dist/btNode.d.ts +1 -0
- package/dist/btNode.js +4 -2
- package/dist/discovery.js +19 -15
- package/dist/features.d.ts +2 -2
- package/dist/features.js +1 -1
- package/dist/mesh.d.ts +6 -1
- package/dist/mesh.js +14 -1
- package/dist/node.d.ts +1 -0
- package/dist/node.js +4 -2
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -1,27 +1,64 @@
|
|
|
1
1
|
# @chaincraft/core
|
|
2
2
|
|
|
3
3
|
TypeScript implementation of the Chaincraft wire protocol and object semantics.
|
|
4
|
+
Release **[1.1.0](https://www.npmjs.com/package/@chaincraft/core)** on npm.
|
|
5
|
+
|
|
6
|
+
Open-source toolkit for peer-to-peer protocols like blockchains, chat rooms,
|
|
7
|
+
BitTorrent swarms, and content-addressed stores.
|
|
4
8
|
|
|
5
9
|
## Install
|
|
6
10
|
|
|
7
11
|
```bash
|
|
8
12
|
npm install @chaincraft/core
|
|
13
|
+
npm install --global @chaincraft/core
|
|
9
14
|
```
|
|
10
15
|
|
|
16
|
+
The node binary is `chaincraft-ts-node`. Default listen is `0.0.0.0:9735`.
|
|
17
|
+
|
|
11
18
|
The unscoped npm name `chaincraft` belongs to another project. This package is
|
|
12
19
|
`@chaincraft/core`, alongside the `chaincraft` crate and the `chaincraft` Python
|
|
13
20
|
package.
|
|
14
21
|
|
|
15
|
-
##
|
|
22
|
+
## Hello World
|
|
23
|
+
|
|
24
|
+
Two TCP nodes in one process. A listens on `9735`, B on `9736`, B dials A and
|
|
25
|
+
gossips `app.hello`. Save as `hello.mjs` and run `node hello.mjs`.
|
|
16
26
|
|
|
17
|
-
|
|
27
|
+
```js
|
|
28
|
+
import { Mesh } from "@chaincraft/core";
|
|
18
29
|
|
|
19
|
-
|
|
20
|
-
|
|
30
|
+
const a = new Mesh();
|
|
31
|
+
const b = new Mesh();
|
|
32
|
+
a.onApp = (_t, data) => {
|
|
33
|
+
console.log("Received:", data.message);
|
|
34
|
+
};
|
|
21
35
|
|
|
22
|
-
|
|
23
|
-
|
|
36
|
+
console.log("listening", await a.start("127.0.0.1", 9735));
|
|
37
|
+
console.log("listening", await b.start("127.0.0.1", 9736));
|
|
38
|
+
await b.connect(a.listenAddr);
|
|
39
|
+
const id = b.broadcast("app.hello", { message: "Hello, Chaincraft!" });
|
|
40
|
+
await new Promise((r) => setTimeout(r, 400));
|
|
41
|
+
console.log(id);
|
|
42
|
+
a.close();
|
|
43
|
+
b.close();
|
|
24
44
|
```
|
|
25
45
|
|
|
46
|
+
CLI, three languages on one loopback mesh:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
python3 -m chaincraft.node
|
|
50
|
+
chaincraft-node 127.0.0.1:9736
|
|
51
|
+
chaincraft-ts-node 127.0.0.1:9737
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Then `{"cmd":"connect","addr":"127.0.0.1:9735"}` on the last two, and
|
|
55
|
+
`{"cmd":"send","type":"app.hello","data":{"message":"Hello, Chaincraft!"}}` on
|
|
56
|
+
Python.
|
|
57
|
+
|
|
58
|
+
Further local nodes take unused ports and connect. Remote hosts dial this
|
|
59
|
+
machine's unicast IPv4 or DNS name on 9735. `connect` allows loopback,
|
|
60
|
+
RFC1918, public unicast IPv4, `localhost`, `.local`, and ordinary DNS names.
|
|
61
|
+
Each mesh keeps at most 256 peers.
|
|
62
|
+
|
|
26
63
|
Specification and the other implementations live in the
|
|
27
64
|
[monorepo](https://github.com/jose-compu/chaincraft-monorepo).
|
package/dist/btNode.d.ts
CHANGED
package/dist/btNode.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
/**
|
|
2
3
|
* BitTorrent mesh client — Swarm on top of the Level 0 mesh.
|
|
3
4
|
*
|
|
@@ -5,7 +6,7 @@
|
|
|
5
6
|
*/
|
|
6
7
|
import * as readline from 'node:readline';
|
|
7
8
|
import { BitTorrentClient, IMPL, ProtocolError } from "./index.js";
|
|
8
|
-
import { Mesh } from "./mesh.js";
|
|
9
|
+
import { Mesh, parseListen } from "./mesh.js";
|
|
9
10
|
const MAX_STDIN_LINE = 1_048_576;
|
|
10
11
|
function emit(event) {
|
|
11
12
|
process.stdout.write(`${JSON.stringify(event)}\n`);
|
|
@@ -13,7 +14,8 @@ function emit(event) {
|
|
|
13
14
|
const mesh = new Mesh(IMPL, emit);
|
|
14
15
|
const client = new BitTorrentClient((typeName, data) => mesh.broadcast(typeName, data));
|
|
15
16
|
mesh.onApp = (typeName, data) => client.onApp(typeName, data);
|
|
16
|
-
|
|
17
|
+
const listen = parseListen(process.argv[2]);
|
|
18
|
+
await mesh.start(listen.host, listen.port);
|
|
17
19
|
emit({ event: 'ready', addr: mesh.listenAddr, impl: IMPL });
|
|
18
20
|
const rl = readline.createInterface({ input: process.stdin });
|
|
19
21
|
for await (const line of rl) {
|
package/dist/discovery.js
CHANGED
|
@@ -8,17 +8,20 @@ export function isResolvedIpAllowed(ip) {
|
|
|
8
8
|
const labels = ip.split('.');
|
|
9
9
|
if (labels.length !== 4 || !labels.every((label) => /^[0-9]+$/.test(label)))
|
|
10
10
|
return false;
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
if (
|
|
14
|
-
return
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
const first = labels[0];
|
|
12
|
+
const second = labels[1];
|
|
13
|
+
if (first === undefined || second === undefined)
|
|
14
|
+
return false;
|
|
15
|
+
const octets = labels.map((label) => Number(label));
|
|
16
|
+
if (octets.some((octet) => !Number.isSafeInteger(octet) || octet > 255))
|
|
17
|
+
return false;
|
|
18
|
+
const a = Number(first);
|
|
19
|
+
const b = Number(second);
|
|
20
|
+
if (a === 0 || a >= 224)
|
|
21
|
+
return false;
|
|
22
|
+
if (a === 169 && b === 254)
|
|
23
|
+
return false;
|
|
24
|
+
return true;
|
|
22
25
|
}
|
|
23
26
|
export function isConnectAllowed(address) {
|
|
24
27
|
if (!isValidAddress(address))
|
|
@@ -26,10 +29,11 @@ export function isConnectAllowed(address) {
|
|
|
26
29
|
const split = splitHostPort(address);
|
|
27
30
|
if (!split)
|
|
28
31
|
return false;
|
|
29
|
-
const
|
|
30
|
-
if (
|
|
31
|
-
return
|
|
32
|
-
|
|
32
|
+
const labels = split.host.split('.');
|
|
33
|
+
if (labels.length === 4 && labels.every((label) => /^[0-9]+$/.test(label))) {
|
|
34
|
+
return isResolvedIpAllowed(split.host);
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
33
37
|
}
|
|
34
38
|
export function isValidAddress(address) {
|
|
35
39
|
const split = splitHostPort(address);
|
package/dist/features.d.ts
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
*
|
|
8
8
|
* Add an ID here only when the feature is implemented and its vectors pass.
|
|
9
9
|
*/
|
|
10
|
-
export declare const VERSION = "1.
|
|
10
|
+
export declare const VERSION = "1.1.0";
|
|
11
11
|
export declare const WIRE_VERSION = 1;
|
|
12
|
-
export declare const IMPL = "ts-chaincraft/1.
|
|
12
|
+
export declare const IMPL = "ts-chaincraft/1.1.0";
|
|
13
13
|
export declare const LANGUAGE = "typescript";
|
|
14
14
|
export declare const FEATURES: readonly string[];
|
|
15
15
|
export interface Manifest {
|
package/dist/features.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* Add an ID here only when the feature is implemented and its vectors pass.
|
|
9
9
|
*/
|
|
10
|
-
export const VERSION = '1.
|
|
10
|
+
export const VERSION = '1.1.0';
|
|
11
11
|
export const WIRE_VERSION = 1;
|
|
12
12
|
export const IMPL = `ts-chaincraft/${VERSION}`;
|
|
13
13
|
export const LANGUAGE = 'typescript';
|
package/dist/mesh.d.ts
CHANGED
|
@@ -6,10 +6,15 @@
|
|
|
6
6
|
import * as net from 'node:net';
|
|
7
7
|
import { type JsonValue } from './canonical.ts';
|
|
8
8
|
import { Deduplicator } from './dedup.ts';
|
|
9
|
-
export declare const MAX_PEERS =
|
|
9
|
+
export declare const MAX_PEERS = 256;
|
|
10
10
|
export declare const MAX_RECEIVED = 4096;
|
|
11
11
|
export declare const CONNECT_TIMEOUT_MS = 5000;
|
|
12
12
|
export declare const NODE_MAX_FRAME = 262144;
|
|
13
|
+
export declare const DEFAULT_LISTEN = "0.0.0.0:9735";
|
|
14
|
+
export declare function parseListen(raw?: string): {
|
|
15
|
+
host: string;
|
|
16
|
+
port: number;
|
|
17
|
+
};
|
|
13
18
|
export type MeshEvent = Record<string, unknown>;
|
|
14
19
|
export type AppHandler = (typeName: string, data: JsonValue) => void;
|
|
15
20
|
export type LinkHandler = (addr: string, typeName: string, data: JsonValue) => void;
|
package/dist/mesh.js
CHANGED
|
@@ -13,10 +13,23 @@ import { Envelope } from "./envelope.js";
|
|
|
13
13
|
import { ProtocolError } from "./errors.js";
|
|
14
14
|
import { IMPL } from "./features.js";
|
|
15
15
|
import { FrameReader, encodeFrame } from "./framing.js";
|
|
16
|
-
export const MAX_PEERS =
|
|
16
|
+
export const MAX_PEERS = 256;
|
|
17
17
|
export const MAX_RECEIVED = 4096;
|
|
18
18
|
export const CONNECT_TIMEOUT_MS = 5000;
|
|
19
19
|
export const NODE_MAX_FRAME = 262_144;
|
|
20
|
+
export const DEFAULT_LISTEN = '0.0.0.0:9735';
|
|
21
|
+
export function parseListen(raw) {
|
|
22
|
+
const text = raw && raw.length > 0 ? raw : DEFAULT_LISTEN;
|
|
23
|
+
const idx = text.lastIndexOf(':');
|
|
24
|
+
if (idx <= 0)
|
|
25
|
+
throw new Error('listen must be host:port');
|
|
26
|
+
const host = text.slice(0, idx);
|
|
27
|
+
const port = Number(text.slice(idx + 1));
|
|
28
|
+
if (!host || !Number.isInteger(port) || port < 0 || port > 65535) {
|
|
29
|
+
throw new Error('invalid listen');
|
|
30
|
+
}
|
|
31
|
+
return { host, port };
|
|
32
|
+
}
|
|
20
33
|
export class Mesh {
|
|
21
34
|
impl;
|
|
22
35
|
dedup = new Deduplicator();
|
package/dist/node.d.ts
CHANGED
package/dist/node.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
/**
|
|
2
3
|
* Minimal TCP mesh node — SPEC/wire-v1.md §4, §5, §7, §8.
|
|
3
4
|
*
|
|
@@ -5,13 +6,14 @@
|
|
|
5
6
|
*/
|
|
6
7
|
import * as readline from 'node:readline';
|
|
7
8
|
import { IMPL, ProtocolError } from "./index.js";
|
|
8
|
-
import { Mesh } from "./mesh.js";
|
|
9
|
+
import { Mesh, parseListen } from "./mesh.js";
|
|
9
10
|
const MAX_STDIN_LINE = 1_048_576;
|
|
10
11
|
function emit(event) {
|
|
11
12
|
process.stdout.write(`${JSON.stringify(event)}\n`);
|
|
12
13
|
}
|
|
13
14
|
const mesh = new Mesh(IMPL, emit);
|
|
14
|
-
|
|
15
|
+
const listen = parseListen(process.argv[2]);
|
|
16
|
+
await mesh.start(listen.host, listen.port);
|
|
15
17
|
emit({ event: 'ready', addr: mesh.listenAddr, impl: IMPL });
|
|
16
18
|
const rl = readline.createInterface({ input: process.stdin });
|
|
17
19
|
for await (const line of rl) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chaincraft/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A platform for decentralized protocols — wire, objects, and pedagogical consensus",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
"default": "./dist/index.js"
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
|
+
"bin": {
|
|
29
|
+
"chaincraft-ts-node": "dist/node.js",
|
|
30
|
+
"chaincraft-ts-bt-node": "dist/btNode.js"
|
|
31
|
+
},
|
|
28
32
|
"files": [
|
|
29
33
|
"dist",
|
|
30
34
|
"README.md",
|