cubyz-node-client 1.2.0 → 1.3.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 AMerkuri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,20 @@
1
+ import { Buffer } from "node:buffer";
2
+ export interface KeySet {
3
+ ed25519PrivKey: Buffer;
4
+ ed25519PubKey: Buffer;
5
+ p256PrivKey: Buffer;
6
+ p256PubKey: Buffer;
7
+ mlDsa44PrivKey: Uint8Array;
8
+ mlDsa44PubKey: Buffer;
9
+ }
10
+ export declare function derToCompact(derSig: Buffer): Buffer;
11
+ export declare function signEd25519(seed: Buffer, message: Buffer): Buffer;
12
+ export declare function signP256(seed: Buffer, message: Buffer): Buffer;
13
+ export declare function signMlDsa44(secretKey: Uint8Array, message: Buffer): Promise<Buffer>;
14
+ export declare function deriveKeys(accountCodeText: string): Promise<KeySet>;
15
+ export interface Identity {
16
+ accountCode: string;
17
+ keys: KeySet;
18
+ }
19
+ export declare function loadOrCreateIdentity(filePath: string): Promise<Identity>;
20
+ export declare function buildPublicKeysZon(keys: KeySet): string;
@@ -0,0 +1,150 @@
1
+ import { Buffer } from "node:buffer";
2
+ import { createHash, createPrivateKey, createPublicKey, randomBytes, sign, } from "node:crypto";
3
+ import { readFile, writeFile } from "node:fs/promises";
4
+ import { WORDLIST } from "./wordlist.js";
5
+ // Key salts from authentication.zig lines 59-63
6
+ const KEY_SALTS = [
7
+ "n59zw0qz53q05b73q9a50vmso",
8
+ "4t7z3592a09p85z4piotfh7z",
9
+ "u89564epogz1qi9up5zc94309",
10
+ ];
11
+ // PKCS8 DER prefix for Ed25519 private key (32-byte seed appended after)
12
+ const ED25519_PKCS8_PREFIX = Buffer.from("302e020100300506032b657004220420", "hex");
13
+ // PKCS8 DER prefix for P-256 private key (32-byte seed appended at offset 36)
14
+ const P256_PKCS8_PREFIX = Buffer.from("3041020100301306072a8648ce3d020106082a8648ce3d030107042730250201010420", "hex");
15
+ function deriveKeyMaterial(accountCodeText, saltIndex) {
16
+ const salt = KEY_SALTS[saltIndex];
17
+ let hashedResult = Buffer.alloc(64);
18
+ for (let j = 0; j < 2048; j++) {
19
+ const input = j === 0 ? Buffer.from(accountCodeText + salt, "utf8") : hashedResult;
20
+ hashedResult = createHash("sha512").update(input).digest();
21
+ }
22
+ return hashedResult.slice(0, 32);
23
+ }
24
+ function deriveEd25519(seed) {
25
+ const der = Buffer.concat([ED25519_PKCS8_PREFIX, seed]);
26
+ const privKey = createPrivateKey({ key: der, format: "der", type: "pkcs8" });
27
+ const pubDer = createPublicKey(privKey)
28
+ .export({ format: "der", type: "spki" })
29
+ .slice(-32);
30
+ return { privKey: seed, pubKey: Buffer.from(pubDer) };
31
+ }
32
+ function deriveP256(seed) {
33
+ const der = Buffer.concat([P256_PKCS8_PREFIX, seed]);
34
+ const privKey = createPrivateKey({ key: der, format: "der", type: "pkcs8" });
35
+ const pubDer = createPublicKey(privKey)
36
+ .export({ format: "der", type: "spki" })
37
+ .slice(-65);
38
+ return { privKey: seed, pubKey: Buffer.from(pubDer) };
39
+ }
40
+ export function derToCompact(derSig) {
41
+ // Parse DER SEQUENCE { INTEGER r, INTEGER s }
42
+ // Structure: 30 <total-len> 02 <r-len> <r-bytes> 02 <s-len> <s-bytes>
43
+ let offset = 0;
44
+ if (derSig[offset++] !== 0x30) {
45
+ throw new Error("Expected SEQUENCE tag 0x30");
46
+ }
47
+ // Skip length (may be 1 or 2 bytes)
48
+ if (derSig[offset] & 0x80) {
49
+ offset += (derSig[offset] & 0x7f) + 1;
50
+ }
51
+ else {
52
+ offset += 1;
53
+ }
54
+ if (derSig[offset++] !== 0x02) {
55
+ throw new Error("Expected INTEGER tag 0x02 for r");
56
+ }
57
+ const rLen = derSig[offset++];
58
+ const rBytes = derSig.slice(offset, offset + rLen);
59
+ offset += rLen;
60
+ if (derSig[offset++] !== 0x02) {
61
+ throw new Error("Expected INTEGER tag 0x02 for s");
62
+ }
63
+ const sLen = derSig[offset++];
64
+ const sBytes = derSig.slice(offset, offset + sLen);
65
+ // Strip leading zero padding and left-pad to 32 bytes
66
+ const rStripped = rBytes[0] === 0 ? rBytes.slice(1) : rBytes;
67
+ const sStripped = sBytes[0] === 0 ? sBytes.slice(1) : sBytes;
68
+ const compact = Buffer.alloc(64, 0);
69
+ rStripped.copy(compact, 32 - rStripped.length);
70
+ sStripped.copy(compact, 64 - sStripped.length);
71
+ return compact;
72
+ }
73
+ export function signEd25519(seed, message) {
74
+ const der = Buffer.concat([ED25519_PKCS8_PREFIX, seed]);
75
+ const privKey = createPrivateKey({ key: der, format: "der", type: "pkcs8" });
76
+ return sign(null, message, privKey);
77
+ }
78
+ export function signP256(seed, message) {
79
+ const der = Buffer.concat([P256_PKCS8_PREFIX, seed]);
80
+ const privKey = createPrivateKey({ key: der, format: "der", type: "pkcs8" });
81
+ const derSig = sign("sha256", message, privKey);
82
+ return derToCompact(derSig);
83
+ }
84
+ export async function signMlDsa44(secretKey, message) {
85
+ // Lazy import to avoid top-level dependency issue
86
+ const { ml_dsa44 } = await import("@noble/post-quantum/ml-dsa");
87
+ const sig = ml_dsa44.sign(secretKey, message);
88
+ return Buffer.from(sig);
89
+ }
90
+ export async function deriveKeys(accountCodeText) {
91
+ const { ml_dsa44 } = await import("@noble/post-quantum/ml-dsa");
92
+ const ed25519Seed = deriveKeyMaterial(accountCodeText, 0);
93
+ const p256Seed = deriveKeyMaterial(accountCodeText, 1);
94
+ const mldsaSeed = deriveKeyMaterial(accountCodeText, 2);
95
+ const ed25519 = deriveEd25519(ed25519Seed);
96
+ const p256 = deriveP256(p256Seed);
97
+ // ML-DSA-44: generateDeterministic from 32-byte seed
98
+ const mldsaKeyPair = ml_dsa44.keygen(mldsaSeed);
99
+ return {
100
+ ed25519PrivKey: ed25519.privKey,
101
+ ed25519PubKey: ed25519.pubKey,
102
+ p256PrivKey: p256.privKey,
103
+ p256PubKey: p256.pubKey,
104
+ mlDsa44PrivKey: mldsaKeyPair.secretKey,
105
+ mlDsa44PubKey: Buffer.from(mldsaKeyPair.publicKey),
106
+ };
107
+ }
108
+ function generateAccountCode() {
109
+ // Generate 20 random bytes + checksum byte = 21 bytes
110
+ const bits = Buffer.alloc(21, 0);
111
+ randomBytes(20).copy(bits, 0);
112
+ const sha256 = createHash("sha256").update(bits.slice(0, 20)).digest();
113
+ bits[20] = sha256[0];
114
+ const words = [];
115
+ for (let i = 0; i < 15; i++) {
116
+ const bitIndex = i * 11;
117
+ const byteIndex = Math.floor(bitIndex / 8);
118
+ const b0 = bits[byteIndex];
119
+ const b1 = bits[byteIndex + 1];
120
+ const b2 = byteIndex + 2 < bits.length ? bits[byteIndex + 2] : 0;
121
+ const containingRegion = (b0 << 16) | (b1 << 8) | b2;
122
+ const shift = 24 - 11 - (bitIndex % 8);
123
+ const wordIndex = (containingRegion >> shift) & 0x7ff;
124
+ words.push(WORDLIST[wordIndex]);
125
+ }
126
+ return words.join(" ");
127
+ }
128
+ export async function loadOrCreateIdentity(filePath) {
129
+ let accountCode = null;
130
+ try {
131
+ const contents = await readFile(filePath, "utf8");
132
+ accountCode = contents.trim();
133
+ }
134
+ catch {
135
+ // File does not exist; generate a new identity
136
+ }
137
+ if (!accountCode) {
138
+ accountCode = generateAccountCode();
139
+ await writeFile(filePath, `${accountCode}\n`, "utf8");
140
+ }
141
+ const keys = await deriveKeys(accountCode);
142
+ return { accountCode, keys };
143
+ }
144
+ export function buildPublicKeysZon(keys) {
145
+ const ed25519B64 = keys.ed25519PubKey.toString("base64");
146
+ const p256B64 = keys.p256PubKey.toString("base64");
147
+ const mldsaB64 = keys.mlDsa44PubKey.toString("base64");
148
+ return `{.ed25519 = "${ed25519B64}", .ecdsaP256Sha256 = "${p256B64}", .mldsa44 = "${mldsaB64}"}`;
149
+ }
150
+ //# sourceMappingURL=authentication.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authentication.js","sourceRoot":"","sources":["../src/authentication.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,WAAW,EACX,IAAI,GACL,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,gDAAgD;AAChD,MAAM,SAAS,GAAG;IAChB,2BAA2B;IAC3B,0BAA0B;IAC1B,2BAA2B;CACnB,CAAC;AAEX,yEAAyE;AACzE,MAAM,oBAAoB,GAAG,MAAM,CAAC,IAAI,CACtC,kCAAkC,EAClC,KAAK,CACN,CAAC;AAEF,8EAA8E;AAC9E,MAAM,iBAAiB,GAAG,MAAM,CAAC,IAAI,CACnC,wEAAwE,EACxE,KAAK,CACN,CAAC;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U,0BAA0B,OAAO,kBAAkB,QAAQ,IAAI,CAAC;AACnG,CAAC"}
package/dist/binary.js CHANGED
@@ -10,7 +10,7 @@ export function encodeVarInt(value) {
10
10
  return Buffer.from([0]);
11
11
  }
12
12
  // Calculate number of bits needed
13
- const bits = val === 0 ? 1 : Math.floor(Math.log2(val)) + 1;
13
+ const bits = Math.floor(Math.log2(val)) + 1;
14
14
  // Calculate number of 7-bit chunks needed
15
15
  const numBytes = Math.ceil(bits / 7);
16
16
  // Encode big-endian (most significant byte first)
@@ -1 +1 @@
1
- {"version":3,"file":"binary.js","sourceRoot":"","sources":["../src/binary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,UAAU,YAAY,CAAC,KAAa;IACxC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACd,MAAM,IAAI,UAAU,CAAC,sCAAsC,CAAC,CAAC;IAC/D,CAAC;IACD,oDAAoD;IACpD,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,MAAM,GAAG,GAAG,KAAK,KAAK,CAAC,CAAC;IAExB,IAAI,GAAG,KAAK,CAAC,EAAE,CAAC;QACd,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1B,CAAC;IAED,kCAAkC;IAClC,MAAM,IAAI,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IAC5D,0CAA0C;IAC1C,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;IAErC,kDAAkD;IAClD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,EAAE,CAAC,EAAE,EAAE,CAAC;QAClC,MAAM,KAAK,GAAG,CAAC,GAAG,C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from "node:events";
2
2
  import { type CloseOptions, type CubyzConnectionEvents, type CubyzConnectionOptions, type EntitySnapshot, type ItemSnapshot, type PlayerData } from "./connectionTypes.js";
3
- export type { BiomeUpdate, BlockUpdate, CloseOptions, CubyzConnectionLogger, CubyzConnectionOptions, DisconnectEvent, EntityPositionPacket, EntitySnapshot, Gamemode, GamemodeUpdate, GenericUpdate, ItemSnapshot, LogLevel, PlayerData, PlayerState, PlayersEvent, ProtocolEvent, TeleportUpdate, TimeUpdate, Vector3, WorldEditPosUpdate, } from "./connectionTypes.js";
3
+ export type { BiomeUpdate, BlockUpdate, ClearUpdate, CloseOptions, CubyzConnectionLogger, CubyzConnectionOptions, DisconnectEvent, EntityPositionPacket, EntitySnapshot, Gamemode, GamemodeUpdate, GenericUpdate, ItemSnapshot, LogLevel, ParticlesUpdate, PlayerData, PlayerState, PlayersEvent, ProtocolEvent, TeleportUpdate, TimeUpdate, Vector3, WorldEditPosUpdate, } from "./connectionTypes.js";
4
4
  export { GAMEMODE } from "./connectionTypes.js";
5
5
  export declare class CubyzConnection extends EventEmitter {
6
6
  readonly host: string;
@@ -30,9 +30,11 @@ export declare class CubyzConnection extends EventEmitter {
30
30
  private disconnectSent;
31
31
  private disconnectEmitted;
32
32
  private initSent;
33
- private handshakeQueued;
34
33
  private awaitingServerSince;
35
- constructor({ host, port, name, version, logger, logLevel, }: CubyzConnectionOptions);
34
+ private readonly identityFile;
35
+ private identity;
36
+ private secureChannel;
37
+ constructor({ host, port, name, version, logger, logLevel, identityFile, }: CubyzConnectionOptions);
36
38
  private log;
37
39
  private emitDisconnect;
38
40
  on<K extends keyof CubyzConnectionEvents>(event: K, listener: (...args: CubyzConnectionEvents[K]) => void): this;
@@ -49,13 +51,14 @@ export declare class CubyzConnection extends EventEmitter {
49
51
  private sendInit;
50
52
  private sendInitAck;
51
53
  private ensureReceiveChannels;
54
+ private setupSecureChannel;
52
55
  private handlePacket;
53
56
  private handleInitPacket;
54
- private queueHandshake;
55
57
  private handleSequencedPacket;
56
58
  private handleConfirmation;
57
59
  private handleProtocol;
58
60
  private handleHandshake;
61
+ private handleSignatureRequest;
59
62
  private handleEntityUpdate;
60
63
  private handleEntityPosition;
61
64
  private handleBlockUpdate;
@@ -2,6 +2,7 @@ import { Buffer } from "node:buffer";
2
2
  import { randomInt } from "node:crypto";
3
3
  import dgram from "node:dgram";
4
4
  import { EventEmitter } from "node:events";
5
+ import { loadOrCreateIdentity, signEd25519, signMlDsa44, signP256, } from "./authentication.js";
5
6
  import { decodeVarInt, readInt32BE, writeInt32BE } from "./binary.js";
6
7
  import { prepareChatMessage } from "./chatFormat.js";
7
8
  import { DEG_TO_RAD, GENERIC_UPDATE_TYPE, LOG_LEVEL_ORDER, WORLD_EDIT_POSITION, } from "./connectionTypes.js";
@@ -9,6 +10,7 @@ import { AWAITING_SERVER_TIMEOUT_MS, CHANNEL, CONFIRMATION_BATCH_SIZE, DEFAULT_V
9
10
  import { parseEntityPositionPacket } from "./entityParser.js";
10
11
  import { buildHandshakePayload, parseHandshake, randomSequence, } from "./handshakeUtils.js";
11
12
  import { parseChannelPacket, ReceiveChannel } from "./receiveChannel.js";
13
+ import { SecureChannelHandler } from "./secureChannel.js";
12
14
  import { SendChannel } from "./sendChannel.js";
13
15
  import { parseZon } from "./zon.js";
14
16
  export { GAMEMODE } from "./connectionTypes.js";
@@ -44,9 +46,11 @@ export class CubyzConnection extends EventEmitter {
44
46
  disconnectSent = false;
45
47
  disconnectEmitted = false;
46
48
  initSent = false;
47
- handshakeQueued = false;
48
49
  awaitingServerSince = null;
49
- constructor({ host, port, name, version = DEFAULT_VERSION, logger = console, logLevel = "error", }) {
50
+ identityFile;
51
+ identity = null;
52
+ secureChannel = null;
53
+ constructor({ host, port, name, version = DEFAULT_VERSION, logger = console, logLevel = "error", identityFile = "./cubyz-identity.txt", }) {
50
54
  super();
51
55
  this.host = host;
52
56
  this.port = port;
@@ -54,11 +58,12 @@ export class CubyzConnection extends EventEmitter {
54
58
  this.version = version;
55
59
  this.baseLogger = logger ?? console;
56
60
  this.logLevel = (logLevel in LOG_LEVEL_ORDER ? logLevel : "error");
61
+ this.identityFile = identityFile;
57
62
  this.socket = dgram.createSocket("udp4");
58
63
  this.connectionId = BigInt.asIntN(64, (BigInt(Date.now()) << 20n) | BigInt(randomInt(0, 0xfffff)));
59
64
  this.sendChannels = {
60
65
  [CHANNEL.LOSSY]: new SendChannel(CHANNEL.LOSSY, randomSequence()),
61
- [CHANNEL.FAST]: new SendChannel(CHANNEL.FAST, randomSequence()),
66
+ [CHANNEL.SECURE]: new SendChannel(CHANNEL.SECURE, randomSequence()),
62
67
  [CHANNEL.SLOW]: new SendChannel(CHANNEL.SLOW, randomSequence()),
63
68
  };
64
69
  this.socket.on("message", (msg) => {
@@ -114,6 +119,8 @@ export class CubyzConnection extends EventEmitter {
114
119
  return super.emit(event, ...args);
115
120
  }
116
121
  async start() {
122
+ this.identity = await loadOrCreateIdentity(this.identityFile);
123
+ this.log("info", `Loaded identity from ${this.identityFile}`);
117
124
  await new Promise((resolve, reject) => {
118
125
  const onError = (err) => {
119
126
  this.socket.off("listening", onListening);
@@ -193,6 +200,10 @@ export class CubyzConnection extends EventEmitter {
193
200
  }
194
201
  flushSendQueues(now) {
195
202
  for (const channel of Object.values(this.sendChannels)) {
203
+ // The SECURE channel is driven by SecureChannelHandler; skip normal queuing.
204
+ if (channel.channelId === CHANNEL.SECURE) {
205
+ continue;
206
+ }
196
207
  if (!channel.hasWork()) {
197
208
  continue;
198
209
  }
@@ -242,28 +253,69 @@ export class CubyzConnection extends EventEmitter {
242
253
  payload[0] = CHANNEL.INIT;
243
254
  payload.writeBigInt64BE(this.connectionId, 1);
244
255
  writeInt32BE(payload, 9, this.sendChannels[CHANNEL.LOSSY].initialSequence);
245
- writeInt32BE(payload, 13, this.sendChannels[CHANNEL.FAST].initialSequence);
256
+ writeInt32BE(payload, 13, this.sendChannels[CHANNEL.SECURE].initialSequence);
246
257
  writeInt32BE(payload, 17, this.sendChannels[CHANNEL.SLOW].initialSequence);
247
258
  this.socket.send(payload, this.port, this.host);
248
259
  this.initSent = true;
249
260
  }
250
- sendInitAck() {
261
+ sendInitAck(onSent) {
251
262
  const buffer = Buffer.alloc(1 + 8);
252
263
  buffer[0] = CHANNEL.INIT;
253
264
  buffer.writeBigInt64BE(this.connectionId, 1);
254
- this.socket.send(buffer, this.port, this.host);
265
+ this.socket.send(buffer, this.port, this.host, (err) => {
266
+ if (err) {
267
+ this.log("error", "Failed to send init ack:", err);
268
+ }
269
+ onSent?.();
270
+ });
255
271
  }
256
- ensureReceiveChannels(lossyStart, fastStart, slowStart) {
272
+ ensureReceiveChannels(lossyStart, secureStart, slowStart) {
257
273
  if (!this.receiveChannels.has(CHANNEL.LOSSY)) {
258
274
  this.receiveChannels.set(CHANNEL.LOSSY, new ReceiveChannel(CHANNEL.LOSSY, lossyStart));
259
275
  }
260
- if (!this.receiveChannels.has(CHANNEL.FAST)) {
261
- this.receiveChannels.set(CHANNEL.FAST, new ReceiveChannel(CHANNEL.FAST, fastStart));
276
+ if (!this.receiveChannels.has(CHANNEL.SECURE)) {
277
+ const secureRecv = new ReceiveChannel(CHANNEL.SECURE, secureStart);
278
+ // Route raw bytes from the SECURE channel into the TLS handler.
279
+ secureRecv.rawBytesCallback = (data) => {
280
+ this.secureChannel?.feedRawBytes(data);
281
+ };
282
+ this.receiveChannels.set(CHANNEL.SECURE, secureRecv);
262
283
  }
263
284
  if (!this.receiveChannels.has(CHANNEL.SLOW)) {
264
285
  this.receiveChannels.set(CHANNEL.SLOW, new ReceiveChannel(CHANNEL.SLOW, slowStart));
265
286
  }
266
287
  }
288
+ setupSecureChannel() {
289
+ if (this.secureChannel !== null) {
290
+ return;
291
+ }
292
+ const handler = new SecureChannelHandler({
293
+ socket: this.socket,
294
+ host: this.host,
295
+ port: this.port,
296
+ channelId: CHANNEL.SECURE,
297
+ mtu: 548,
298
+ initialSendSeq: this.sendChannels[CHANNEL.SECURE].initialSequence,
299
+ });
300
+ handler.onError = (err) => {
301
+ this.log("error", "Secure channel error:", err);
302
+ };
303
+ handler.onSecureConnect = (_verificationData) => {
304
+ this.log("debug", "TLS handshake complete, sending userData");
305
+ if (!this.identity) {
306
+ this.log("error", "No identity loaded before TLS handshake completed");
307
+ return;
308
+ }
309
+ const payload = buildHandshakePayload(this.name, this.version, this.identity);
310
+ handler.sendMessage(PROTOCOL.HANDSHAKE, payload);
311
+ };
312
+ handler.onMessage = (msg) => {
313
+ this.handleProtocol(CHANNEL.SECURE, msg.protocolId, msg.payload).catch((err) => {
314
+ this.log("error", `Secure protocol ${msg.protocolId} failed:`, err);
315
+ });
316
+ };
317
+ this.secureChannel = handler;
318
+ }
267
319
  handlePacket(buffer) {
268
320
  if (!buffer || buffer.length === 0) {
269
321
  return;
@@ -302,27 +354,24 @@ export class CubyzConnection extends EventEmitter {
302
354
  const remoteId = buffer.readBigInt64BE(1);
303
355
  this.remoteConnectionId = remoteId;
304
356
  const lossyStart = readInt32BE(buffer, 9);
305
- const fastStart = readInt32BE(buffer, 13);
357
+ const secureStart = readInt32BE(buffer, 13);
306
358
  const slowStart = readInt32BE(buffer, 17);
307
- this.ensureReceiveChannels(lossyStart, fastStart, slowStart);
359
+ this.ensureReceiveChannels(lossyStart, secureStart, slowStart);
308
360
  if (this.state !== "connected") {
309
361
  this.state = "connected";
310
362
  this.lastInbound = Date.now();
311
363
  this.awaitingServerSince = null;
312
364
  this.log("info", "Channel handshake completed with server");
313
- this.sendInitAck();
314
- this.queueHandshake();
365
+ this.setupSecureChannel();
366
+ // Send the init-ACK and only start the TLS handshake once the packet
367
+ // has been handed to the OS, guaranteeing the server transitions to
368
+ // .connected before it receives the TLS ClientHello.
369
+ this.sendInitAck(() => {
370
+ this.secureChannel?.startHandshake();
371
+ });
315
372
  this.emit("connected");
316
373
  }
317
374
  }
318
- queueHandshake() {
319
- if (this.handshakeQueued) {
320
- return;
321
- }
322
- const payload = buildHandshakePayload(this.name, this.version);
323
- this.sendChannels[CHANNEL.FAST].queue(PROTOCOL.HANDSHAKE, payload);
324
- this.handshakeQueued = true;
325
- }
326
375
  async handleSequencedPacket(buffer) {
327
376
  const parsed = parseChannelPacket(buffer);
328
377
  const channel = this.receiveChannels.get(parsed.channelId);
@@ -389,6 +438,10 @@ export class CubyzConnection extends EventEmitter {
389
438
  async handleHandshake(payload) {
390
439
  const { state, data } = parseHandshake(payload);
391
440
  switch (state) {
441
+ case HANDSHAKE_STATE.SIGNATURE_REQUEST: {
442
+ await this.handleSignatureRequest(data);
443
+ break;
444
+ }
392
445
  case HANDSHAKE_STATE.ASSETS: {
393
446
  // Assets are compressed with zlib's raw DEFLATE
394
447
  // Skipping asset storage for brevity
@@ -489,6 +542,63 @@ export class CubyzConnection extends EventEmitter {
489
542
  this.log("debug", `Unhandled handshake state ${state}`);
490
543
  }
491
544
  }
545
+ async handleSignatureRequest(data) {
546
+ if (!this.identity) {
547
+ this.log("error", "No identity available for signature request");
548
+ return;
549
+ }
550
+ if (!this.secureChannel) {
551
+ this.log("error", "No secure channel for signature request");
552
+ return;
553
+ }
554
+ const verificationData = this.secureChannel.verificationDataBuffer ?? Buffer.alloc(0);
555
+ // Parse signatureRequest:
556
+ // [varint: len of algo1 name][algo1 name bytes][varint: len of algo2 name (0 if none)][algo2 name bytes if present]
557
+ let offset = 0;
558
+ const algo1LenResult = decodeVarInt(data, offset);
559
+ offset += algo1LenResult.consumed;
560
+ const algo1Name = data
561
+ .slice(offset, offset + algo1LenResult.value)
562
+ .toString("utf8");
563
+ offset += algo1LenResult.value;
564
+ let algo2Name = "";
565
+ if (offset < data.length) {
566
+ const algo2LenResult = decodeVarInt(data, offset);
567
+ offset += algo2LenResult.consumed;
568
+ if (algo2LenResult.value > 0) {
569
+ algo2Name = data
570
+ .slice(offset, offset + algo2LenResult.value)
571
+ .toString("utf8");
572
+ }
573
+ }
574
+ this.log("debug", "Signature request: algo1=", algo1Name, "algo2=", algo2Name);
575
+ const signatureBuffers = [];
576
+ const identity = this.identity;
577
+ const signOne = async (algoName) => {
578
+ switch (algoName) {
579
+ case "ed25519":
580
+ signatureBuffers.push(signEd25519(identity.keys.ed25519PrivKey, verificationData));
581
+ break;
582
+ case "ecdsaP256Sha256":
583
+ signatureBuffers.push(signP256(identity.keys.p256PrivKey, verificationData));
584
+ break;
585
+ case "mldsa44":
586
+ signatureBuffers.push(await signMlDsa44(identity.keys.mlDsa44PrivKey, verificationData));
587
+ break;
588
+ default:
589
+ this.log("warn", "Unknown signature algorithm requested:", algoName);
590
+ }
591
+ };
592
+ await signOne(algo1Name);
593
+ if (algo2Name) {
594
+ await signOne(algo2Name);
595
+ }
596
+ // signatureResponse format: [state byte = 3][sig1_bytes][optional sig2_bytes]
597
+ const statePrefix = Buffer.from([HANDSHAKE_STATE.SIGNATURE_RESPONSE]);
598
+ const responsePayload = Buffer.concat([statePrefix, ...signatureBuffers]);
599
+ this.secureChannel.sendMessage(PROTOCOL.HANDSHAKE, responsePayload);
600
+ this.log("debug", "Sent signature response");
601
+ }
492
602
  handleEntityUpdate(payload) {
493
603
  const text = payload.toString("utf8");
494
604
  let parsed;
@@ -505,8 +615,11 @@ export class CubyzConnection extends EventEmitter {
505
615
  return;
506
616
  }
507
617
  let changed = false;
508
- for (const entry of parsed) {
618
+ let nullIndex = -1;
619
+ for (let i = 0; i < parsed.length; i++) {
620
+ const entry = parsed[i];
509
621
  if (entry === null) {
622
+ nullIndex = i;
510
623
  break;
511
624
  }
512
625
  if (typeof entry === "number") {
@@ -549,24 +662,33 @@ export class CubyzConnection extends EventEmitter {
549
662
  }
550
663
  this.emitPlayers();
551
664
  }
665
+ // Process item drops after the null sentinel.
666
+ // Number entries are removals (by u16 index); object entries are additions
667
+ // or updates whose positions will arrive via the entityPositions protocol.
668
+ if (nullIndex >= 0) {
669
+ for (let i = nullIndex + 1; i < parsed.length; i++) {
670
+ const entry = parsed[i];
671
+ if (typeof entry === "number") {
672
+ this.itemStates.delete(entry);
673
+ }
674
+ // Object entries (add/update) don't carry position data here;
675
+ // their positions are delivered via protocol 6 (ENTITY_POSITION).
676
+ }
677
+ }
552
678
  }
553
679
  handleEntityPosition(payload) {
554
680
  const result = parseEntityPositionPacket(payload, this.log.bind(this));
555
681
  if (!result) {
556
682
  return;
557
683
  }
558
- // Update internal state with parsed entities
559
684
  this.entityStates.clear();
560
- const entityStatesMap = result._entityStates;
561
- for (const [id, state] of entityStatesMap) {
685
+ for (const [id, state] of result.entityStates) {
562
686
  this.entityStates.set(id, state);
563
687
  }
564
688
  this.itemStates.clear();
565
- const itemStatesMap = result._itemStates;
566
- for (const [index, state] of itemStatesMap) {
689
+ for (const [index, state] of result.itemStates) {
567
690
  this.itemStates.set(index, state);
568
691
  }
569
- // Emit the packet without internal state maps
570
692
  const packet = {
571
693
  timestamp: result.timestamp,
572
694
  basePosition: result.basePosition,
@@ -587,9 +709,13 @@ export class CubyzConnection extends EventEmitter {
587
709
  offset += 4;
588
710
  const block = payload.readUInt32BE(offset);
589
711
  offset += 4;
590
- // Read block entity data length (usize - platform dependent, use varint)
591
- const { value: blockEntityDataLen, consumed } = decodeVarInt(payload, offset);
592
- offset += consumed;
712
+ // blockEntityData length is written as usize (8 bytes, big-endian) by the Zig server.
713
+ if (offset + 8 > payload.length) {
714
+ this.log("warn", "Block update payload truncated (no room for length)");
715
+ break;
716
+ }
717
+ const blockEntityDataLen = Number(payload.readBigUInt64BE(offset));
718
+ offset += 8;
593
719
  if (offset + blockEntityDataLen > payload.length) {
594
720
  this.log("warn", "Block update payload truncated");
595
721
  break;
@@ -697,6 +823,70 @@ export class CubyzConnection extends EventEmitter {
697
823
  });
698
824
  break;
699
825
  }
826
+ case GENERIC_UPDATE_TYPE.PARTICLES: {
827
+ // [varint u16: particleIdLen][particleId][f64 x][f64 y][f64 z][u8 collides][varint u32: count][varint usize: spawnZonLen][spawnZon]
828
+ const particleIdLenResult = decodeVarInt(payload, offset);
829
+ offset += particleIdLenResult.consumed;
830
+ const particleIdLen = particleIdLenResult.value;
831
+ if (offset + particleIdLen > payload.length) {
832
+ this.log("warn", "Particles update payload truncated (particleId)");
833
+ return;
834
+ }
835
+ const particleId = payload
836
+ .slice(offset, offset + particleIdLen)
837
+ .toString("utf8");
838
+ offset += particleIdLen;
839
+ if (offset + 24 > payload.length) {
840
+ this.log("warn", "Particles update payload truncated (position)");
841
+ return;
842
+ }
843
+ const px = payload.readDoubleBE(offset);
844
+ offset += 8;
845
+ const py = payload.readDoubleBE(offset);
846
+ offset += 8;
847
+ const pz = payload.readDoubleBE(offset);
848
+ offset += 8;
849
+ if (offset + 1 > payload.length) {
850
+ this.log("warn", "Particles update payload truncated (collides)");
851
+ return;
852
+ }
853
+ const collides = payload.readUInt8(offset) !== 0;
854
+ offset += 1;
855
+ const countResult = decodeVarInt(payload, offset);
856
+ offset += countResult.consumed;
857
+ const count = countResult.value;
858
+ const spawnZonLenResult = decodeVarInt(payload, offset);
859
+ offset += spawnZonLenResult.consumed;
860
+ const spawnZonLen = spawnZonLenResult.value;
861
+ if (offset + spawnZonLen > payload.length) {
862
+ this.log("warn", "Particles update payload truncated (spawnZon)");
863
+ return;
864
+ }
865
+ const spawnZon = payload
866
+ .slice(offset, offset + spawnZonLen)
867
+ .toString("utf8");
868
+ this.emit("genericUpdate", {
869
+ type: "particles",
870
+ particleId,
871
+ position: { x: px, y: py, z: pz },
872
+ collides,
873
+ count,
874
+ spawnZon,
875
+ });
876
+ break;
877
+ }
878
+ case GENERIC_UPDATE_TYPE.CLEAR: {
879
+ // [u8 clearType: 0=chat]
880
+ if (offset + 1 > payload.length) {
881
+ this.log("warn", "Clear update payload too short");
882
+ return;
883
+ }
884
+ const clearTypeByte = payload.readUInt8(offset);
885
+ // Only clearType 0 (chat) is defined by the server.
886
+ const clearType = clearTypeByte === 0 ? "chat" : "chat";
887
+ this.emit("genericUpdate", { type: "clear", clearType });
888
+ break;
889
+ }
700
890
  default:
701
891
  this.log("debug", `Unknown generic update type: ${updateType}`);
702
892
  }