@ziplayer/net 0.0.0-stage → 0.0.1

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.
Files changed (56) hide show
  1. package/README.md +136 -2
  2. package/dist/NetNode.d.ts +124 -0
  3. package/dist/NetNode.js +593 -0
  4. package/dist/NetNode.js.map +1 -0
  5. package/dist/ZiNet.d.ts +165 -0
  6. package/dist/ZiNet.js +599 -0
  7. package/dist/ZiNet.js.map +1 -0
  8. package/dist/crypto/handshake.d.ts +84 -0
  9. package/dist/crypto/handshake.js +204 -0
  10. package/dist/crypto/handshake.js.map +1 -0
  11. package/dist/crypto/identity.d.ts +44 -0
  12. package/dist/crypto/identity.js +92 -0
  13. package/dist/crypto/identity.js.map +1 -0
  14. package/dist/crypto/session.d.ts +56 -0
  15. package/dist/crypto/session.js +148 -0
  16. package/dist/crypto/session.js.map +1 -0
  17. package/dist/debug.d.ts +56 -0
  18. package/dist/debug.js +51 -0
  19. package/dist/debug.js.map +1 -0
  20. package/dist/ext/NetExtension.d.ts +15 -0
  21. package/dist/ext/NetExtension.js +39 -0
  22. package/dist/ext/NetExtension.js.map +1 -0
  23. package/dist/index.d.ts +28 -0
  24. package/dist/index.js +52 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/protocol/frames.d.ts +22 -0
  27. package/dist/protocol/frames.js +45 -0
  28. package/dist/protocol/frames.js.map +1 -0
  29. package/dist/router.d.ts +38 -0
  30. package/dist/router.js +51 -0
  31. package/dist/router.js.map +1 -0
  32. package/dist/search.d.ts +72 -0
  33. package/dist/search.js +94 -0
  34. package/dist/search.js.map +1 -0
  35. package/dist/stream/StreamImporter.d.ts +37 -0
  36. package/dist/stream/StreamImporter.js +138 -0
  37. package/dist/stream/StreamImporter.js.map +1 -0
  38. package/dist/stream/StreamProvider.d.ts +49 -0
  39. package/dist/stream/StreamProvider.js +157 -0
  40. package/dist/stream/StreamProvider.js.map +1 -0
  41. package/dist/transport/MemoryTransport.d.ts +36 -0
  42. package/dist/transport/MemoryTransport.js +84 -0
  43. package/dist/transport/MemoryTransport.js.map +1 -0
  44. package/dist/transport/MqttTransport.d.ts +26 -0
  45. package/dist/transport/MqttTransport.js +104 -0
  46. package/dist/transport/MqttTransport.js.map +1 -0
  47. package/dist/transport/Transport.d.ts +21 -0
  48. package/dist/transport/Transport.js +18 -0
  49. package/dist/transport/Transport.js.map +1 -0
  50. package/dist/voice/Gateway.d.ts +16 -0
  51. package/dist/voice/Gateway.js +35 -0
  52. package/dist/voice/Gateway.js.map +1 -0
  53. package/dist/voice/RemoteVoice.d.ts +29 -0
  54. package/dist/voice/RemoteVoice.js +80 -0
  55. package/dist/voice/RemoteVoice.js.map +1 -0
  56. package/package.json +53 -5
@@ -0,0 +1,84 @@
1
+ import type { NodeIdentity, TrustStore } from "./identity";
2
+ /**
3
+ * Three-message authenticated key exchange (SIGMA-I style, built only from node:crypto):
4
+ *
5
+ * 1. hs1 A -> B ephemeral X25519 key + nonce (plaintext, reveals nothing secret)
6
+ * 2. hs2 B -> A B's ephemeral key + nonce + Ed25519 signature (signature encrypted under a handshake key)
7
+ * 3. hs3 A -> B A's Ed25519 signature over the full transcript (encrypted)
8
+ *
9
+ * Both signatures cover the whole transcript, so each side proves it owns the pinned long-term key AND
10
+ * took part in this exact exchange. Session keys come from HKDF over the ephemeral ECDH secret, which gives
11
+ * forward secrecy. Node identities are pinned in a {@link TrustStore}; unknown nodes are rejected.
12
+ *
13
+ * This is a small custom protocol on standard primitives. Get it reviewed before trusting it with anything
14
+ * valuable, and consider swapping in a Noise XX implementation behind the same interface.
15
+ */
16
+ export declare const PROTOCOL_VERSION = 1;
17
+ export declare class HandshakeError extends Error {
18
+ constructor(message: string);
19
+ }
20
+ export interface Hs1 {
21
+ t: "hs1";
22
+ v: number;
23
+ net: string;
24
+ from: string;
25
+ to: string;
26
+ sid: string;
27
+ eA: string;
28
+ nA: string;
29
+ }
30
+ export interface Hs2 {
31
+ t: "hs2";
32
+ sid: string;
33
+ eB: string;
34
+ nB: string;
35
+ enc: string;
36
+ }
37
+ export interface Hs3 {
38
+ t: "hs3";
39
+ sid: string;
40
+ enc: string;
41
+ }
42
+ export interface HandshakeResult {
43
+ sessionId: string;
44
+ peerId: string;
45
+ /** Key for frames this node sends. */
46
+ sendKey: Buffer;
47
+ /** Key for frames this node receives. */
48
+ recvKey: Buffer;
49
+ initiator: boolean;
50
+ }
51
+ export interface HandshakeContext {
52
+ networkId: string;
53
+ selfId: string;
54
+ identity: NodeIdentity;
55
+ trust: TrustStore;
56
+ }
57
+ export declare class HandshakeInitiator {
58
+ private readonly ctx;
59
+ private readonly peerId;
60
+ private readonly ephemeral;
61
+ private readonly hs1;
62
+ private done;
63
+ constructor(ctx: HandshakeContext, peerId: string);
64
+ get sessionId(): string;
65
+ hello(): Hs1;
66
+ /** Verifies B, returns hs3 plus the session keys. */
67
+ finish(hs2: Hs2): {
68
+ hs3: Hs3;
69
+ result: HandshakeResult;
70
+ };
71
+ }
72
+ export declare class HandshakeResponder {
73
+ private readonly ctx;
74
+ private readonly hs1;
75
+ private readonly th;
76
+ private readonly keys;
77
+ private readonly sigB;
78
+ private done;
79
+ readonly hs2: Hs2;
80
+ constructor(ctx: HandshakeContext, hs1: Hs1);
81
+ get peerId(): string;
82
+ /** Verifies A's signature and returns the session keys. */
83
+ finish(hs3: Hs3): HandshakeResult;
84
+ }
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HandshakeResponder = exports.HandshakeInitiator = exports.HandshakeError = exports.PROTOCOL_VERSION = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ /**
6
+ * Three-message authenticated key exchange (SIGMA-I style, built only from node:crypto):
7
+ *
8
+ * 1. hs1 A -> B ephemeral X25519 key + nonce (plaintext, reveals nothing secret)
9
+ * 2. hs2 B -> A B's ephemeral key + nonce + Ed25519 signature (signature encrypted under a handshake key)
10
+ * 3. hs3 A -> B A's Ed25519 signature over the full transcript (encrypted)
11
+ *
12
+ * Both signatures cover the whole transcript, so each side proves it owns the pinned long-term key AND
13
+ * took part in this exact exchange. Session keys come from HKDF over the ephemeral ECDH secret, which gives
14
+ * forward secrecy. Node identities are pinned in a {@link TrustStore}; unknown nodes are rejected.
15
+ *
16
+ * This is a small custom protocol on standard primitives. Get it reviewed before trusting it with anything
17
+ * valuable, and consider swapping in a Noise XX implementation behind the same interface.
18
+ */
19
+ exports.PROTOCOL_VERSION = 1;
20
+ const LABEL = Buffer.from("zinet/1");
21
+ class HandshakeError extends Error {
22
+ constructor(message) {
23
+ super(message);
24
+ this.name = "HandshakeError";
25
+ }
26
+ }
27
+ exports.HandshakeError = HandshakeError;
28
+ const SPKI_X25519_PREFIX = Buffer.from("302a300506032b656e032100", "hex");
29
+ function rawPublic(key) {
30
+ return key.export({ type: "spki", format: "der" }).subarray(-32);
31
+ }
32
+ function importRawPublic(raw) {
33
+ if (raw.length !== 32)
34
+ throw new HandshakeError("Invalid ephemeral key length");
35
+ return (0, node_crypto_1.createPublicKey)({ key: Buffer.concat([SPKI_X25519_PREFIX, raw]), format: "der", type: "spki" });
36
+ }
37
+ function lp(...parts) {
38
+ const buffers = parts.map((p) => (typeof p === "string" ? Buffer.from(p, "utf8") : p));
39
+ const out = [];
40
+ for (const b of buffers) {
41
+ const len = Buffer.alloc(2);
42
+ len.writeUInt16BE(b.length);
43
+ out.push(len, b);
44
+ }
45
+ return Buffer.concat(out);
46
+ }
47
+ function b64(buf) {
48
+ return buf.toString("base64");
49
+ }
50
+ function unb64(value, label, length) {
51
+ if (typeof value !== "string")
52
+ throw new HandshakeError(`Missing ${label}`);
53
+ const buf = Buffer.from(value, "base64");
54
+ if (length !== undefined && buf.length !== length)
55
+ throw new HandshakeError(`Invalid ${label}`);
56
+ return buf;
57
+ }
58
+ function transcript(hs1, eB, nB) {
59
+ const base = lp(LABEL, hs1.net, hs1.from, hs1.to, hs1.sid, Buffer.from(hs1.eA, "base64"), Buffer.from(hs1.nA, "base64"));
60
+ return eB && nB ? Buffer.concat([base, lp(eB, nB)]) : base;
61
+ }
62
+ function deriveKeys(dh, nA, nB, th) {
63
+ const salt = Buffer.concat([nA, nB]);
64
+ const expand = (label) => Buffer.from((0, node_crypto_1.hkdfSync)("sha256", dh, salt, Buffer.concat([Buffer.from(label), th]), 32));
65
+ return { hs: expand("zinet/hs"), a2b: expand("zinet/a2b"), b2a: expand("zinet/b2a") };
66
+ }
67
+ function hsNonce(step) {
68
+ const n = Buffer.alloc(12);
69
+ n[11] = step;
70
+ return n;
71
+ }
72
+ function seal(key, nonce, aad, plaintext) {
73
+ const cipher = (0, node_crypto_1.createCipheriv)("chacha20-poly1305", key, nonce, { authTagLength: 16 });
74
+ cipher.setAAD(aad, { plaintextLength: plaintext.length });
75
+ const ct = Buffer.concat([cipher.update(plaintext), cipher.final()]);
76
+ return Buffer.concat([ct, cipher.getAuthTag()]);
77
+ }
78
+ function open(key, nonce, aad, sealed) {
79
+ if (sealed.length < 16)
80
+ throw new HandshakeError("Handshake payload too short");
81
+ try {
82
+ const decipher = (0, node_crypto_1.createDecipheriv)("chacha20-poly1305", key, nonce, { authTagLength: 16 });
83
+ decipher.setAAD(aad, { plaintextLength: sealed.length - 16 });
84
+ decipher.setAuthTag(sealed.subarray(sealed.length - 16));
85
+ return Buffer.concat([decipher.update(sealed.subarray(0, sealed.length - 16)), decipher.final()]);
86
+ }
87
+ catch {
88
+ throw new HandshakeError("Handshake authentication failed");
89
+ }
90
+ }
91
+ const sigDigest = (tag, th, extra) => (0, node_crypto_1.createHash)("sha256")
92
+ .update(tag)
93
+ .update(th)
94
+ .update(extra ?? Buffer.alloc(0))
95
+ .digest();
96
+ class HandshakeInitiator {
97
+ ctx;
98
+ peerId;
99
+ ephemeral = (0, node_crypto_1.generateKeyPairSync)("x25519");
100
+ hs1;
101
+ done = false;
102
+ constructor(ctx, peerId) {
103
+ this.ctx = ctx;
104
+ this.peerId = peerId;
105
+ if (!ctx.trust.has(peerId))
106
+ throw new HandshakeError(`Peer "${peerId}" is not trusted`);
107
+ this.hs1 = {
108
+ t: "hs1",
109
+ v: exports.PROTOCOL_VERSION,
110
+ net: ctx.networkId,
111
+ from: ctx.selfId,
112
+ to: peerId,
113
+ sid: (0, node_crypto_1.randomBytes)(8).toString("hex"),
114
+ eA: b64(rawPublic(this.ephemeral.publicKey)),
115
+ nA: b64((0, node_crypto_1.randomBytes)(16)),
116
+ };
117
+ }
118
+ get sessionId() {
119
+ return this.hs1.sid;
120
+ }
121
+ hello() {
122
+ return this.hs1;
123
+ }
124
+ /** Verifies B, returns hs3 plus the session keys. */
125
+ finish(hs2) {
126
+ if (this.done)
127
+ throw new HandshakeError("Handshake already completed");
128
+ if (hs2.t !== "hs2" || hs2.sid !== this.hs1.sid)
129
+ throw new HandshakeError("Unexpected handshake reply");
130
+ const eB = unb64(hs2.eB, "eB", 32);
131
+ const nB = unb64(hs2.nB, "nB", 16);
132
+ const th = (0, node_crypto_1.createHash)("sha256")
133
+ .update(transcript(this.hs1, eB, nB))
134
+ .digest();
135
+ const dh = (0, node_crypto_1.diffieHellman)({ privateKey: this.ephemeral.privateKey, publicKey: importRawPublic(eB) });
136
+ const keys = deriveKeys(dh, Buffer.from(this.hs1.nA, "base64"), nB, th);
137
+ const sigB = open(keys.hs, hsNonce(2), th, unb64(hs2.enc, "enc"));
138
+ const peerKey = this.ctx.trust.publicKey(this.peerId);
139
+ if (!peerKey || !(0, node_crypto_1.verify)(null, sigDigest("resp", th), peerKey, sigB))
140
+ throw new HandshakeError("Responder signature invalid");
141
+ const sigA = (0, node_crypto_1.sign)(null, sigDigest("init", th, sigB), this.ctx.identity.privateKey);
142
+ const hs3 = { t: "hs3", sid: this.hs1.sid, enc: b64(seal(keys.hs, hsNonce(3), th, sigA)) };
143
+ this.done = true;
144
+ return {
145
+ hs3,
146
+ result: { sessionId: this.hs1.sid, peerId: this.peerId, sendKey: keys.a2b, recvKey: keys.b2a, initiator: true },
147
+ };
148
+ }
149
+ }
150
+ exports.HandshakeInitiator = HandshakeInitiator;
151
+ class HandshakeResponder {
152
+ ctx;
153
+ hs1;
154
+ th;
155
+ keys;
156
+ sigB;
157
+ done = false;
158
+ hs2;
159
+ constructor(ctx, hs1) {
160
+ this.ctx = ctx;
161
+ this.hs1 = hs1;
162
+ if (hs1.t !== "hs1" || hs1.v !== exports.PROTOCOL_VERSION)
163
+ throw new HandshakeError("Unsupported handshake version");
164
+ if (hs1.net !== ctx.networkId)
165
+ throw new HandshakeError("Wrong network");
166
+ if (hs1.to !== ctx.selfId)
167
+ throw new HandshakeError("Handshake not addressed to this node");
168
+ if (!ctx.trust.has(hs1.from))
169
+ throw new HandshakeError(`Peer "${hs1.from}" is not trusted`);
170
+ if (!/^[0-9a-f]{16}$/.test(hs1.sid))
171
+ throw new HandshakeError("Invalid session id");
172
+ const eA = unb64(hs1.eA, "eA", 32);
173
+ const nA = unb64(hs1.nA, "nA", 16);
174
+ const ephemeral = (0, node_crypto_1.generateKeyPairSync)("x25519");
175
+ const eB = rawPublic(ephemeral.publicKey);
176
+ const nB = (0, node_crypto_1.randomBytes)(16);
177
+ this.th = (0, node_crypto_1.createHash)("sha256")
178
+ .update(transcript(hs1, eB, nB))
179
+ .digest();
180
+ const dh = (0, node_crypto_1.diffieHellman)({ privateKey: ephemeral.privateKey, publicKey: importRawPublic(eA) });
181
+ this.keys = deriveKeys(dh, nA, nB, this.th);
182
+ this.sigB = (0, node_crypto_1.sign)(null, sigDigest("resp", this.th), ctx.identity.privateKey);
183
+ this.hs2 = { t: "hs2", sid: hs1.sid, eB: b64(eB), nB: b64(nB), enc: b64(seal(this.keys.hs, hsNonce(2), this.th, this.sigB)) };
184
+ }
185
+ get peerId() {
186
+ return this.hs1.from;
187
+ }
188
+ /** Verifies A's signature and returns the session keys. */
189
+ finish(hs3) {
190
+ if (this.done)
191
+ throw new HandshakeError("Handshake already completed");
192
+ if (hs3.t !== "hs3" || hs3.sid !== this.hs1.sid)
193
+ throw new HandshakeError("Unexpected handshake message");
194
+ const sigA = open(this.keys.hs, hsNonce(3), this.th, unb64(hs3.enc, "enc"));
195
+ const peerKey = this.ctx.trust.publicKey(this.hs1.from);
196
+ if (!peerKey || !(0, node_crypto_1.verify)(null, sigDigest("init", this.th, this.sigB), peerKey, sigA)) {
197
+ throw new HandshakeError("Initiator signature invalid");
198
+ }
199
+ this.done = true;
200
+ return { sessionId: this.hs1.sid, peerId: this.hs1.from, sendKey: this.keys.b2a, recvKey: this.keys.a2b, initiator: false };
201
+ }
202
+ }
203
+ exports.HandshakeResponder = HandshakeResponder;
204
+ //# sourceMappingURL=handshake.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handshake.js","sourceRoot":"","sources":["../../src/crypto/handshake.ts"],"names":[],"mappings":";;;AAAA,6CAYqB;AAGrB;;;;;;;;;;;;;GAaG;AAEU,QAAA,gBAAgB,GAAG,CAAC,CAAC;AAClC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AAErC,MAAa,cAAe,SAAQ,KAAK;IACxC,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC9B,CAAC;CACD;AALD,wCAKC;AA0CD,MAAM,kBAAkB,GAAG,MAAM,CAAC,IAAI,CAAC,0BAA0B,EAAE,KAAK,CAAC,CAAC;AAE1E,SAAS,SAAS,CAAC,GAAc;IAChC,OAAQ,GAAG,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAY,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,eAAe,CAAC,GAAW;IACnC,IAAI,GAAG,CAAC,MAAM,KAAK,EAAE;QAAE,MAAM,IAAI,cAAc,CAAC,8BAA8B,CAAC,CAAC;IAChF,OAAO,IAAA,6BAAe,EAAC,EAAE,GAAG,EAAE,MAAM,CAAC,MAA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,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACxD,IAAI,CAAC,OAAO,IAAI,CAAC,IAAA,oBAAM,EAAC,IAAI,EAAE,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,CAAC;YACrF,MAAM,IAAI,cAAc,CAAC,6BAA6B,CAAC,CAAC;QACzD,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,OAAO,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAC7H,CAAC;CACD;AA9CD,gDA8CC"}
@@ -0,0 +1,44 @@
1
+ import { type KeyObject } from "node:crypto";
2
+ /** Long-term Ed25519 identity of a node. Used only to sign handshake transcripts. */
3
+ export interface NodeIdentity {
4
+ privateKey: KeyObject;
5
+ publicKey: KeyObject;
6
+ }
7
+ export interface GeneratedIdentity {
8
+ identity: NodeIdentity;
9
+ privateKeyPem: string;
10
+ publicKeyPem: string;
11
+ }
12
+ export declare function generateIdentity(): GeneratedIdentity;
13
+ export declare function loadIdentity(privateKeyPem: string | KeyObject): NodeIdentity;
14
+ /**
15
+ * Loads the node identity from `keyFile`, or creates and stores a new one (mode 0600, plus `<file>.pub`)
16
+ * when the file does not exist yet.
17
+ */
18
+ export declare function loadOrCreateIdentity(keyFile: string): GeneratedIdentity & {
19
+ created: boolean;
20
+ };
21
+ export declare function parsePublicKey(key: string | KeyObject): KeyObject;
22
+ /** Short human-comparable fingerprint of a public key (SHA-256 of its DER encoding). */
23
+ export declare function fingerprint(key: KeyObject): string;
24
+ export type NetRole = "provide-stream" | "provide-search" | "accept-voice" | "delegate-voice" | "*";
25
+ export interface TrustedPeer {
26
+ nodeId: string;
27
+ /** Ed25519 public key (PEM or KeyObject). Pinned: a different key for this nodeId is rejected. */
28
+ publicKey: string | KeyObject;
29
+ /**
30
+ * What this peer is allowed to ask of us. Defaults to `["*"]` because listing a peer here is already an
31
+ * explicit decision; narrow it for least privilege.
32
+ */
33
+ roles?: NetRole[];
34
+ }
35
+ export declare class TrustStore {
36
+ private readonly peers;
37
+ constructor(peers?: TrustedPeer[]);
38
+ add(peer: TrustedPeer): void;
39
+ remove(nodeId: string): boolean;
40
+ has(nodeId: string): boolean;
41
+ publicKey(nodeId: string): KeyObject | undefined;
42
+ hasRole(nodeId: string, role: NetRole): boolean;
43
+ ids(): string[];
44
+ }
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TrustStore = void 0;
4
+ exports.generateIdentity = generateIdentity;
5
+ exports.loadIdentity = loadIdentity;
6
+ exports.loadOrCreateIdentity = loadOrCreateIdentity;
7
+ exports.parsePublicKey = parsePublicKey;
8
+ exports.fingerprint = fingerprint;
9
+ const node_crypto_1 = require("node:crypto");
10
+ const node_fs_1 = require("node:fs");
11
+ const node_path_1 = require("node:path");
12
+ function generateIdentity() {
13
+ const { privateKey, publicKey } = (0, node_crypto_1.generateKeyPairSync)("ed25519");
14
+ return {
15
+ identity: { privateKey, publicKey },
16
+ privateKeyPem: privateKey.export({ type: "pkcs8", format: "pem" }).toString(),
17
+ publicKeyPem: publicKey.export({ type: "spki", format: "pem" }).toString(),
18
+ };
19
+ }
20
+ function loadIdentity(privateKeyPem) {
21
+ const privateKey = typeof privateKeyPem === "string" ? (0, node_crypto_1.createPrivateKey)(privateKeyPem) : privateKeyPem;
22
+ if (privateKey.asymmetricKeyType !== "ed25519")
23
+ throw new TypeError("Node identity must be an Ed25519 key");
24
+ return { privateKey, publicKey: (0, node_crypto_1.createPublicKey)(privateKey) };
25
+ }
26
+ /**
27
+ * Loads the node identity from `keyFile`, or creates and stores a new one (mode 0600, plus `<file>.pub`)
28
+ * when the file does not exist yet.
29
+ */
30
+ function loadOrCreateIdentity(keyFile) {
31
+ if ((0, node_fs_1.existsSync)(keyFile)) {
32
+ const privateKeyPem = (0, node_fs_1.readFileSync)(keyFile, "utf8");
33
+ const identity = loadIdentity(privateKeyPem);
34
+ return {
35
+ identity,
36
+ privateKeyPem,
37
+ publicKeyPem: identity.publicKey.export({ type: "spki", format: "pem" }).toString(),
38
+ created: false,
39
+ };
40
+ }
41
+ const generated = generateIdentity();
42
+ (0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(keyFile), { recursive: true });
43
+ (0, node_fs_1.writeFileSync)(keyFile, generated.privateKeyPem, { mode: 0o600 });
44
+ try {
45
+ (0, node_fs_1.chmodSync)(keyFile, 0o600);
46
+ }
47
+ catch { }
48
+ (0, node_fs_1.writeFileSync)(`${keyFile}.pub`, generated.publicKeyPem);
49
+ return { ...generated, created: true };
50
+ }
51
+ function parsePublicKey(key) {
52
+ const publicKey = typeof key === "string" ? (0, node_crypto_1.createPublicKey)(key) : key;
53
+ if (publicKey.asymmetricKeyType !== "ed25519")
54
+ throw new TypeError("Trusted peer key must be an Ed25519 public key");
55
+ return publicKey;
56
+ }
57
+ /** Short human-comparable fingerprint of a public key (SHA-256 of its DER encoding). */
58
+ function fingerprint(key) {
59
+ return (0, node_crypto_1.createHash)("sha256")
60
+ .update(key.export({ type: "spki", format: "der" }))
61
+ .digest("hex");
62
+ }
63
+ class TrustStore {
64
+ peers = new Map();
65
+ constructor(peers = []) {
66
+ for (const peer of peers)
67
+ this.add(peer);
68
+ }
69
+ add(peer) {
70
+ if (!peer.nodeId)
71
+ throw new TypeError("Trusted peer needs a nodeId");
72
+ this.peers.set(peer.nodeId, { publicKey: parsePublicKey(peer.publicKey), roles: new Set(peer.roles ?? ["*"]) });
73
+ }
74
+ remove(nodeId) {
75
+ return this.peers.delete(nodeId);
76
+ }
77
+ has(nodeId) {
78
+ return this.peers.has(nodeId);
79
+ }
80
+ publicKey(nodeId) {
81
+ return this.peers.get(nodeId)?.publicKey;
82
+ }
83
+ hasRole(nodeId, role) {
84
+ const roles = this.peers.get(nodeId)?.roles;
85
+ return Boolean(roles && (roles.has("*") || roles.has(role)));
86
+ }
87
+ ids() {
88
+ return [...this.peers.keys()];
89
+ }
90
+ }
91
+ exports.TrustStore = TrustStore;
92
+ //# sourceMappingURL=identity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.js","sourceRoot":"","sources":["../../src/crypto/identity.ts"],"names":[],"mappings":";;;AAgBA,4CAOC;AAED,oCAIC;AAMD,oDAmBC;AAED,wCAIC;AAGD,kCAIC;AAnED,6CAAiH;AACjH,qCAAwF;AACxF,yCAAoC;AAcpC,SAAgB,gBAAgB;IAC/B,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,IAAA,iCAAmB,EAAC,SAAS,CAAC,CAAC;IACjE,OAAO;QACN,QAAQ,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE;QACnC,aAAa,EAAE,UAAU,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,EAAE;QAC7E,YAAY,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,EAAE;KAC1E,CAAC;AACH,CAAC;AAED,SAAgB,YAAY,CAAC,aAAiC;IAC7D,MAAM,UAAU,GAAG,OAAO,aAAa,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAA,8BAAgB,EAAC,aAAa,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC;IACvG,IAAI,UAAU,CAAC,iBAAiB,KAAK,SAAS;QAAE,MAAM,IAAI,SAAS,CAAC,sCAAsC,CAAC,CAAC;IAC5G,OAAO,EAAE,UAAU,E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pC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC;QAC5C,OAAO,OAAO,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC9D,CAAC;IAED,GAAG;QACF,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAC/B,CAAC;CACD;AAhCD,gCAgCC"}
@@ -0,0 +1,56 @@
1
+ export declare class SessionError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ /** Wire kinds (first byte of every message). */
5
+ export declare const KIND: {
6
+ readonly HANDSHAKE: 1;
7
+ readonly CONTROL: 2;
8
+ readonly AUDIO: 3;
9
+ readonly AUDIO_ENCRYPTED: 4;
10
+ };
11
+ /** Accepts each counter at most once inside a sliding window; tolerates reordering, rejects replays. */
12
+ export declare class ReplayWindow {
13
+ private readonly size;
14
+ private max;
15
+ private readonly seen;
16
+ constructor(size?: number);
17
+ accept(counter: number): boolean;
18
+ }
19
+ export interface SecureChannelOptions {
20
+ /** Session lifetime; after this the node performs a fresh handshake (new keys). Default 6 h. */
21
+ maxAgeMs?: number;
22
+ }
23
+ /**
24
+ * Directional AEAD channel (ChaCha20-Poly1305). Header bytes are authenticated as AAD, nonces are
25
+ * `direction | channel | 64-bit counter` and never reused, and a replay window rejects duplicates.
26
+ *
27
+ * Control frame: `[KIND.CONTROL][sid 8][counter 8][ciphertext+tag]`
28
+ * Audio frame: `[KIND.AUDIO_ENCRYPTED][sid 8][counter 8][streamId 4][seq 4][flags 1][ciphertext+tag]`
29
+ */
30
+ export declare class SecureChannel {
31
+ readonly sessionId: string;
32
+ readonly peerId: string;
33
+ private readonly sendKey;
34
+ private readonly recvKey;
35
+ private sendCounter;
36
+ private audioSendCounter;
37
+ private readonly controlWindow;
38
+ private readonly audioWindow;
39
+ readonly createdAt: number;
40
+ readonly maxAgeMs: number;
41
+ private readonly sid;
42
+ private readonly sendDir;
43
+ private readonly recvDir;
44
+ closed: boolean;
45
+ constructor(sessionId: string, peerId: string, initiator: boolean, sendKey: Buffer, recvKey: Buffer, options?: SecureChannelOptions);
46
+ get expired(): boolean;
47
+ seal(plaintext: Buffer): Buffer;
48
+ open(frame: Buffer): Buffer;
49
+ sealAudio(streamId: number, seq: number, flags: number, payload: Buffer): Buffer;
50
+ openAudio(frame: Buffer): {
51
+ streamId: number;
52
+ seq: number;
53
+ flags: number;
54
+ payload: Buffer;
55
+ };
56
+ }
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SecureChannel = exports.ReplayWindow = exports.KIND = exports.SessionError = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ class SessionError extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ this.name = "SessionError";
9
+ }
10
+ }
11
+ exports.SessionError = SessionError;
12
+ /** Wire kinds (first byte of every message). */
13
+ exports.KIND = { HANDSHAKE: 1, CONTROL: 2, AUDIO: 3, AUDIO_ENCRYPTED: 4 };
14
+ const TAG = 16;
15
+ const MAX_COUNTER = 2 ** 48;
16
+ /** Accepts each counter at most once inside a sliding window; tolerates reordering, rejects replays. */
17
+ class ReplayWindow {
18
+ size;
19
+ max = -1;
20
+ seen = new Set();
21
+ constructor(size = 1024) {
22
+ this.size = size;
23
+ }
24
+ accept(counter) {
25
+ if (counter <= this.max - this.size || this.seen.has(counter))
26
+ return false;
27
+ this.seen.add(counter);
28
+ if (counter > this.max) {
29
+ this.max = counter;
30
+ for (const old of this.seen)
31
+ if (old <= this.max - this.size)
32
+ this.seen.delete(old);
33
+ }
34
+ return true;
35
+ }
36
+ }
37
+ exports.ReplayWindow = ReplayWindow;
38
+ function nonce(direction, channel, counter) {
39
+ const n = Buffer.alloc(12);
40
+ n[0] = direction;
41
+ n[1] = channel;
42
+ n.writeBigUInt64BE(BigInt(counter), 4);
43
+ return n;
44
+ }
45
+ function aead(key, n, aad, data) {
46
+ const c = (0, node_crypto_1.createCipheriv)("chacha20-poly1305", key, n, { authTagLength: TAG });
47
+ c.setAAD(aad, { plaintextLength: data.length });
48
+ return Buffer.concat([c.update(data), c.final(), c.getAuthTag()]);
49
+ }
50
+ function unaead(key, n, aad, sealed) {
51
+ if (sealed.length < TAG)
52
+ throw new SessionError("Message too short");
53
+ try {
54
+ const d = (0, node_crypto_1.createDecipheriv)("chacha20-poly1305", key, n, { authTagLength: TAG });
55
+ d.setAAD(aad, { plaintextLength: sealed.length - TAG });
56
+ d.setAuthTag(sealed.subarray(sealed.length - TAG));
57
+ return Buffer.concat([d.update(sealed.subarray(0, sealed.length - TAG)), d.final()]);
58
+ }
59
+ catch {
60
+ throw new SessionError("Message authentication failed");
61
+ }
62
+ }
63
+ /**
64
+ * Directional AEAD channel (ChaCha20-Poly1305). Header bytes are authenticated as AAD, nonces are
65
+ * `direction | channel | 64-bit counter` and never reused, and a replay window rejects duplicates.
66
+ *
67
+ * Control frame: `[KIND.CONTROL][sid 8][counter 8][ciphertext+tag]`
68
+ * Audio frame: `[KIND.AUDIO_ENCRYPTED][sid 8][counter 8][streamId 4][seq 4][flags 1][ciphertext+tag]`
69
+ */
70
+ class SecureChannel {
71
+ sessionId;
72
+ peerId;
73
+ sendKey;
74
+ recvKey;
75
+ sendCounter = 0;
76
+ audioSendCounter = 0;
77
+ controlWindow = new ReplayWindow();
78
+ audioWindow = new ReplayWindow(4096);
79
+ createdAt = Date.now();
80
+ maxAgeMs;
81
+ sid;
82
+ sendDir;
83
+ recvDir;
84
+ closed = false;
85
+ constructor(sessionId, peerId, initiator, sendKey, recvKey, options = {}) {
86
+ this.sessionId = sessionId;
87
+ this.peerId = peerId;
88
+ this.sendKey = sendKey;
89
+ this.recvKey = recvKey;
90
+ this.sid = Buffer.from(sessionId, "hex");
91
+ this.sendDir = initiator ? 1 : 2;
92
+ this.recvDir = initiator ? 2 : 1;
93
+ this.maxAgeMs = options.maxAgeMs ?? 6 * 60 * 60 * 1000;
94
+ }
95
+ get expired() {
96
+ return this.closed || Date.now() - this.createdAt > this.maxAgeMs;
97
+ }
98
+ seal(plaintext) {
99
+ if (this.sendCounter >= MAX_COUNTER)
100
+ throw new SessionError("Session exhausted; re-handshake required");
101
+ const counter = this.sendCounter++;
102
+ const header = Buffer.alloc(17);
103
+ header[0] = exports.KIND.CONTROL;
104
+ this.sid.copy(header, 1);
105
+ header.writeBigUInt64BE(BigInt(counter), 9);
106
+ return Buffer.concat([header, aead(this.sendKey, nonce(this.sendDir, 0, counter), header, plaintext)]);
107
+ }
108
+ open(frame) {
109
+ if (frame.length < 17 + TAG || frame[0] !== exports.KIND.CONTROL)
110
+ throw new SessionError("Malformed control frame");
111
+ if (!frame.subarray(1, 9).equals(this.sid))
112
+ throw new SessionError("Session id mismatch");
113
+ const counter = Number(frame.readBigUInt64BE(9));
114
+ const header = frame.subarray(0, 17);
115
+ const plaintext = unaead(this.recvKey, nonce(this.recvDir, 0, counter), header, frame.subarray(17));
116
+ // Check the window only AFTER authentication so forged frames cannot burn counters.
117
+ if (!this.controlWindow.accept(counter))
118
+ throw new SessionError("Replayed control frame");
119
+ return plaintext;
120
+ }
121
+ sealAudio(streamId, seq, flags, payload) {
122
+ if (this.audioSendCounter >= MAX_COUNTER)
123
+ throw new SessionError("Session exhausted; re-handshake required");
124
+ const counter = this.audioSendCounter++;
125
+ const header = Buffer.alloc(26);
126
+ header[0] = exports.KIND.AUDIO_ENCRYPTED;
127
+ this.sid.copy(header, 1);
128
+ header.writeBigUInt64BE(BigInt(counter), 9);
129
+ header.writeUInt32BE(streamId >>> 0, 17);
130
+ header.writeUInt32BE(seq >>> 0, 21);
131
+ header[25] = flags & 0xff;
132
+ return Buffer.concat([header, aead(this.sendKey, nonce(this.sendDir, 1, counter), header, payload)]);
133
+ }
134
+ openAudio(frame) {
135
+ if (frame.length < 26 + TAG || frame[0] !== exports.KIND.AUDIO_ENCRYPTED)
136
+ throw new SessionError("Malformed audio frame");
137
+ if (!frame.subarray(1, 9).equals(this.sid))
138
+ throw new SessionError("Session id mismatch");
139
+ const counter = Number(frame.readBigUInt64BE(9));
140
+ const header = frame.subarray(0, 26);
141
+ const payload = unaead(this.recvKey, nonce(this.recvDir, 1, counter), header, frame.subarray(26));
142
+ if (!this.audioWindow.accept(counter))
143
+ throw new SessionError("Replayed audio frame");
144
+ return { streamId: header.readUInt32BE(17), seq: header.readUInt32BE(21), flags: header[25], payload };
145
+ }
146
+ }
147
+ exports.SecureChannel = SecureChannel;
148
+ //# sourceMappingURL=session.js.map