@nextera.one/axis-server-sdk 0.1.0 → 0.2.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.
@@ -0,0 +1,85 @@
1
+ import * as z from 'zod';
2
+
3
+ declare const AxisFrameZ: z.ZodObject<{
4
+ flags: z.ZodNumber;
5
+ headers: z.ZodMap<z.ZodNumber, z.ZodCustom<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>>;
6
+ body: z.ZodCustom<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>;
7
+ sig: z.ZodCustom<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>;
8
+ }, z.z.core.$strip>;
9
+ type AxisFrame = z.infer<typeof AxisFrameZ>;
10
+ declare function encodeFrame(frame: AxisFrame): Uint8Array;
11
+ declare function decodeFrame(buf: Uint8Array): AxisFrame;
12
+ declare function getSignTarget(frame: AxisFrame): Uint8Array;
13
+
14
+ declare const AXIS_MAGIC: Uint8Array<ArrayBuffer>;
15
+ declare const AXIS_VERSION = 1;
16
+ declare const MAX_HDR_LEN = 2048;
17
+ declare const MAX_BODY_LEN = 65536;
18
+ declare const MAX_SIG_LEN = 128;
19
+ declare const MAX_FRAME_LEN: number;
20
+ declare const FLAG_BODY_TLV = 1;
21
+ declare const FLAG_CHAIN_REQ = 2;
22
+ declare const FLAG_HAS_WITNESS = 4;
23
+ declare const TLV_PID = 1;
24
+ declare const TLV_TS = 2;
25
+ declare const TLV_INTENT = 3;
26
+ declare const TLV_ACTOR_ID = 4;
27
+ declare const TLV_PROOF_TYPE = 5;
28
+ declare const TLV_PROOF_REF = 6;
29
+ declare const TLV_NONCE = 7;
30
+ declare const TLV_AUD = 8;
31
+ declare const TLV_NODE = 9;
32
+ declare const TLV_TRACE_ID = 10;
33
+ declare const TLV_KID = 11;
34
+ declare const TLV_RID = 15;
35
+ declare const TLV_OK = 16;
36
+ declare const TLV_EFFECT = 17;
37
+ declare const TLV_ERROR_CODE = 18;
38
+ declare const TLV_ERROR_MSG = 19;
39
+ declare const TLV_PREV_HASH = 20;
40
+ declare const TLV_RECEIPT_HASH = 21;
41
+ declare const TLV_NODE_KID = 30;
42
+ declare const TLV_NODE_CERT_HASH = 34;
43
+ declare const TLV_LOOM_PRESENCE_ID = 91;
44
+ declare const TLV_LOOM_WRIT = 92;
45
+ declare const TLV_LOOM_THREAD_HASH = 93;
46
+ declare const PROOF_CAPSULE = 1;
47
+ declare const PROOF_JWT = 2;
48
+ declare const PROOF_MTLS = 3;
49
+ declare const PROOF_LOOM = 4;
50
+ declare const ERR_INVALID_PACKET = "INVALID_PACKET";
51
+ declare const ERR_BAD_SIGNATURE = "BAD_SIGNATURE";
52
+ declare const ERR_REPLAY_DETECTED = "REPLAY_DETECTED";
53
+ declare const ERR_CONTRACT_VIOLATION = "CONTRACT_VIOLATION";
54
+
55
+ declare function encodeVarint(value: number): Uint8Array;
56
+ declare function decodeVarint(buf: Uint8Array, offset?: number): {
57
+ value: number;
58
+ length: number;
59
+ };
60
+ declare function varintLength(value: number): number;
61
+
62
+ interface TLV {
63
+ type: number;
64
+ value: Uint8Array;
65
+ }
66
+ declare function encodeTLVs(tlvs: TLV[]): Uint8Array;
67
+ declare function decodeTLVsList(buf: Uint8Array, maxItems?: number): TLV[];
68
+ declare function decodeTLVs(buf: Uint8Array): Map<number, Uint8Array>;
69
+ declare function decodeObject(bytes: Uint8Array, depth?: number, limits?: {
70
+ maxDepth: number;
71
+ maxItems: number;
72
+ }): Map<number, any>;
73
+ declare function decodeArray(bytes: Uint8Array, itemType: number, maxItems?: number): Uint8Array[];
74
+
75
+ declare function computeSignaturePayload(frame: AxisFrame): Buffer;
76
+ declare function signFrame(frame: AxisFrame, privateKey: Buffer): Buffer;
77
+ declare function verifyFrameSignature(frame: AxisFrame, publicKey: Buffer): boolean;
78
+ declare function generateEd25519KeyPair(): {
79
+ privateKey: Buffer;
80
+ publicKey: Buffer;
81
+ };
82
+ declare function sha256(data: Buffer | Uint8Array): Buffer;
83
+ declare function computeReceiptHash(receiptBytes: Buffer | Uint8Array, prevHash?: Buffer | Uint8Array): Buffer;
84
+
85
+ export { AXIS_MAGIC, AXIS_VERSION, type AxisFrame, AxisFrameZ, ERR_BAD_SIGNATURE, ERR_CONTRACT_VIOLATION, ERR_INVALID_PACKET, ERR_REPLAY_DETECTED, FLAG_BODY_TLV, FLAG_CHAIN_REQ, FLAG_HAS_WITNESS, MAX_BODY_LEN, MAX_FRAME_LEN, MAX_HDR_LEN, MAX_SIG_LEN, PROOF_CAPSULE, PROOF_JWT, PROOF_LOOM, PROOF_MTLS, type TLV, TLV_ACTOR_ID, TLV_AUD, TLV_EFFECT, TLV_ERROR_CODE, TLV_ERROR_MSG, TLV_INTENT, TLV_KID, TLV_LOOM_PRESENCE_ID, TLV_LOOM_THREAD_HASH, TLV_LOOM_WRIT, TLV_NODE, TLV_NODE_CERT_HASH, TLV_NODE_KID, TLV_NONCE, TLV_OK, TLV_PID, TLV_PREV_HASH, TLV_PROOF_REF, TLV_PROOF_TYPE, TLV_RECEIPT_HASH, TLV_RID, TLV_TRACE_ID, TLV_TS, computeReceiptHash, computeSignaturePayload, decodeArray, decodeFrame, decodeObject, decodeTLVs, decodeTLVsList, decodeVarint, encodeFrame, encodeTLVs, encodeVarint, generateEd25519KeyPair, getSignTarget, sha256, signFrame, varintLength, verifyFrameSignature };
@@ -0,0 +1,85 @@
1
+ import * as z from 'zod';
2
+
3
+ declare const AxisFrameZ: z.ZodObject<{
4
+ flags: z.ZodNumber;
5
+ headers: z.ZodMap<z.ZodNumber, z.ZodCustom<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>>;
6
+ body: z.ZodCustom<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>;
7
+ sig: z.ZodCustom<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>;
8
+ }, z.z.core.$strip>;
9
+ type AxisFrame = z.infer<typeof AxisFrameZ>;
10
+ declare function encodeFrame(frame: AxisFrame): Uint8Array;
11
+ declare function decodeFrame(buf: Uint8Array): AxisFrame;
12
+ declare function getSignTarget(frame: AxisFrame): Uint8Array;
13
+
14
+ declare const AXIS_MAGIC: Uint8Array<ArrayBuffer>;
15
+ declare const AXIS_VERSION = 1;
16
+ declare const MAX_HDR_LEN = 2048;
17
+ declare const MAX_BODY_LEN = 65536;
18
+ declare const MAX_SIG_LEN = 128;
19
+ declare const MAX_FRAME_LEN: number;
20
+ declare const FLAG_BODY_TLV = 1;
21
+ declare const FLAG_CHAIN_REQ = 2;
22
+ declare const FLAG_HAS_WITNESS = 4;
23
+ declare const TLV_PID = 1;
24
+ declare const TLV_TS = 2;
25
+ declare const TLV_INTENT = 3;
26
+ declare const TLV_ACTOR_ID = 4;
27
+ declare const TLV_PROOF_TYPE = 5;
28
+ declare const TLV_PROOF_REF = 6;
29
+ declare const TLV_NONCE = 7;
30
+ declare const TLV_AUD = 8;
31
+ declare const TLV_NODE = 9;
32
+ declare const TLV_TRACE_ID = 10;
33
+ declare const TLV_KID = 11;
34
+ declare const TLV_RID = 15;
35
+ declare const TLV_OK = 16;
36
+ declare const TLV_EFFECT = 17;
37
+ declare const TLV_ERROR_CODE = 18;
38
+ declare const TLV_ERROR_MSG = 19;
39
+ declare const TLV_PREV_HASH = 20;
40
+ declare const TLV_RECEIPT_HASH = 21;
41
+ declare const TLV_NODE_KID = 30;
42
+ declare const TLV_NODE_CERT_HASH = 34;
43
+ declare const TLV_LOOM_PRESENCE_ID = 91;
44
+ declare const TLV_LOOM_WRIT = 92;
45
+ declare const TLV_LOOM_THREAD_HASH = 93;
46
+ declare const PROOF_CAPSULE = 1;
47
+ declare const PROOF_JWT = 2;
48
+ declare const PROOF_MTLS = 3;
49
+ declare const PROOF_LOOM = 4;
50
+ declare const ERR_INVALID_PACKET = "INVALID_PACKET";
51
+ declare const ERR_BAD_SIGNATURE = "BAD_SIGNATURE";
52
+ declare const ERR_REPLAY_DETECTED = "REPLAY_DETECTED";
53
+ declare const ERR_CONTRACT_VIOLATION = "CONTRACT_VIOLATION";
54
+
55
+ declare function encodeVarint(value: number): Uint8Array;
56
+ declare function decodeVarint(buf: Uint8Array, offset?: number): {
57
+ value: number;
58
+ length: number;
59
+ };
60
+ declare function varintLength(value: number): number;
61
+
62
+ interface TLV {
63
+ type: number;
64
+ value: Uint8Array;
65
+ }
66
+ declare function encodeTLVs(tlvs: TLV[]): Uint8Array;
67
+ declare function decodeTLVsList(buf: Uint8Array, maxItems?: number): TLV[];
68
+ declare function decodeTLVs(buf: Uint8Array): Map<number, Uint8Array>;
69
+ declare function decodeObject(bytes: Uint8Array, depth?: number, limits?: {
70
+ maxDepth: number;
71
+ maxItems: number;
72
+ }): Map<number, any>;
73
+ declare function decodeArray(bytes: Uint8Array, itemType: number, maxItems?: number): Uint8Array[];
74
+
75
+ declare function computeSignaturePayload(frame: AxisFrame): Buffer;
76
+ declare function signFrame(frame: AxisFrame, privateKey: Buffer): Buffer;
77
+ declare function verifyFrameSignature(frame: AxisFrame, publicKey: Buffer): boolean;
78
+ declare function generateEd25519KeyPair(): {
79
+ privateKey: Buffer;
80
+ publicKey: Buffer;
81
+ };
82
+ declare function sha256(data: Buffer | Uint8Array): Buffer;
83
+ declare function computeReceiptHash(receiptBytes: Buffer | Uint8Array, prevHash?: Buffer | Uint8Array): Buffer;
84
+
85
+ export { AXIS_MAGIC, AXIS_VERSION, type AxisFrame, AxisFrameZ, ERR_BAD_SIGNATURE, ERR_CONTRACT_VIOLATION, ERR_INVALID_PACKET, ERR_REPLAY_DETECTED, FLAG_BODY_TLV, FLAG_CHAIN_REQ, FLAG_HAS_WITNESS, MAX_BODY_LEN, MAX_FRAME_LEN, MAX_HDR_LEN, MAX_SIG_LEN, PROOF_CAPSULE, PROOF_JWT, PROOF_LOOM, PROOF_MTLS, type TLV, TLV_ACTOR_ID, TLV_AUD, TLV_EFFECT, TLV_ERROR_CODE, TLV_ERROR_MSG, TLV_INTENT, TLV_KID, TLV_LOOM_PRESENCE_ID, TLV_LOOM_THREAD_HASH, TLV_LOOM_WRIT, TLV_NODE, TLV_NODE_CERT_HASH, TLV_NODE_KID, TLV_NONCE, TLV_OK, TLV_PID, TLV_PREV_HASH, TLV_PROOF_REF, TLV_PROOF_TYPE, TLV_RECEIPT_HASH, TLV_RID, TLV_TRACE_ID, TLV_TS, computeReceiptHash, computeSignaturePayload, decodeArray, decodeFrame, decodeObject, decodeTLVs, decodeTLVsList, decodeVarint, encodeFrame, encodeTLVs, encodeVarint, generateEd25519KeyPair, getSignTarget, sha256, signFrame, varintLength, verifyFrameSignature };
@@ -0,0 +1,543 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/core/index.ts
30
+ var core_exports = {};
31
+ __export(core_exports, {
32
+ AXIS_MAGIC: () => AXIS_MAGIC,
33
+ AXIS_VERSION: () => AXIS_VERSION,
34
+ AxisFrameZ: () => AxisFrameZ,
35
+ ERR_BAD_SIGNATURE: () => ERR_BAD_SIGNATURE,
36
+ ERR_CONTRACT_VIOLATION: () => ERR_CONTRACT_VIOLATION,
37
+ ERR_INVALID_PACKET: () => ERR_INVALID_PACKET,
38
+ ERR_REPLAY_DETECTED: () => ERR_REPLAY_DETECTED,
39
+ FLAG_BODY_TLV: () => FLAG_BODY_TLV,
40
+ FLAG_CHAIN_REQ: () => FLAG_CHAIN_REQ,
41
+ FLAG_HAS_WITNESS: () => FLAG_HAS_WITNESS,
42
+ MAX_BODY_LEN: () => MAX_BODY_LEN,
43
+ MAX_FRAME_LEN: () => MAX_FRAME_LEN,
44
+ MAX_HDR_LEN: () => MAX_HDR_LEN,
45
+ MAX_SIG_LEN: () => MAX_SIG_LEN,
46
+ PROOF_CAPSULE: () => PROOF_CAPSULE,
47
+ PROOF_JWT: () => PROOF_JWT,
48
+ PROOF_LOOM: () => PROOF_LOOM,
49
+ PROOF_MTLS: () => PROOF_MTLS,
50
+ TLV_ACTOR_ID: () => TLV_ACTOR_ID,
51
+ TLV_AUD: () => TLV_AUD,
52
+ TLV_EFFECT: () => TLV_EFFECT,
53
+ TLV_ERROR_CODE: () => TLV_ERROR_CODE,
54
+ TLV_ERROR_MSG: () => TLV_ERROR_MSG,
55
+ TLV_INTENT: () => TLV_INTENT,
56
+ TLV_KID: () => TLV_KID,
57
+ TLV_LOOM_PRESENCE_ID: () => TLV_LOOM_PRESENCE_ID,
58
+ TLV_LOOM_THREAD_HASH: () => TLV_LOOM_THREAD_HASH,
59
+ TLV_LOOM_WRIT: () => TLV_LOOM_WRIT,
60
+ TLV_NODE: () => TLV_NODE,
61
+ TLV_NODE_CERT_HASH: () => TLV_NODE_CERT_HASH,
62
+ TLV_NODE_KID: () => TLV_NODE_KID,
63
+ TLV_NONCE: () => TLV_NONCE,
64
+ TLV_OK: () => TLV_OK,
65
+ TLV_PID: () => TLV_PID,
66
+ TLV_PREV_HASH: () => TLV_PREV_HASH,
67
+ TLV_PROOF_REF: () => TLV_PROOF_REF,
68
+ TLV_PROOF_TYPE: () => TLV_PROOF_TYPE,
69
+ TLV_RECEIPT_HASH: () => TLV_RECEIPT_HASH,
70
+ TLV_RID: () => TLV_RID,
71
+ TLV_TRACE_ID: () => TLV_TRACE_ID,
72
+ TLV_TS: () => TLV_TS,
73
+ computeReceiptHash: () => computeReceiptHash,
74
+ computeSignaturePayload: () => computeSignaturePayload,
75
+ decodeArray: () => decodeArray,
76
+ decodeFrame: () => decodeFrame,
77
+ decodeObject: () => decodeObject,
78
+ decodeTLVs: () => decodeTLVs,
79
+ decodeTLVsList: () => decodeTLVsList,
80
+ decodeVarint: () => decodeVarint,
81
+ encodeFrame: () => encodeFrame,
82
+ encodeTLVs: () => encodeTLVs,
83
+ encodeVarint: () => encodeVarint,
84
+ generateEd25519KeyPair: () => generateEd25519KeyPair,
85
+ getSignTarget: () => getSignTarget,
86
+ sha256: () => sha256,
87
+ signFrame: () => signFrame,
88
+ varintLength: () => varintLength,
89
+ verifyFrameSignature: () => verifyFrameSignature
90
+ });
91
+ module.exports = __toCommonJS(core_exports);
92
+
93
+ // src/core/constants.ts
94
+ var AXIS_MAGIC = new Uint8Array([65, 88, 73, 83, 49]);
95
+ var AXIS_VERSION = 1;
96
+ var MAX_HDR_LEN = 2048;
97
+ var MAX_BODY_LEN = 65536;
98
+ var MAX_SIG_LEN = 128;
99
+ var MAX_FRAME_LEN = 70 * 1024;
100
+ var FLAG_BODY_TLV = 1;
101
+ var FLAG_CHAIN_REQ = 2;
102
+ var FLAG_HAS_WITNESS = 4;
103
+ var TLV_PID = 1;
104
+ var TLV_TS = 2;
105
+ var TLV_INTENT = 3;
106
+ var TLV_ACTOR_ID = 4;
107
+ var TLV_PROOF_TYPE = 5;
108
+ var TLV_PROOF_REF = 6;
109
+ var TLV_NONCE = 7;
110
+ var TLV_AUD = 8;
111
+ var TLV_NODE = 9;
112
+ var TLV_TRACE_ID = 10;
113
+ var TLV_KID = 11;
114
+ var TLV_RID = 15;
115
+ var TLV_OK = 16;
116
+ var TLV_EFFECT = 17;
117
+ var TLV_ERROR_CODE = 18;
118
+ var TLV_ERROR_MSG = 19;
119
+ var TLV_PREV_HASH = 20;
120
+ var TLV_RECEIPT_HASH = 21;
121
+ var TLV_NODE_KID = 30;
122
+ var TLV_NODE_CERT_HASH = 34;
123
+ var TLV_LOOM_PRESENCE_ID = 91;
124
+ var TLV_LOOM_WRIT = 92;
125
+ var TLV_LOOM_THREAD_HASH = 93;
126
+ var PROOF_CAPSULE = 1;
127
+ var PROOF_JWT = 2;
128
+ var PROOF_MTLS = 3;
129
+ var PROOF_LOOM = 4;
130
+ var ERR_INVALID_PACKET = "INVALID_PACKET";
131
+ var ERR_BAD_SIGNATURE = "BAD_SIGNATURE";
132
+ var ERR_REPLAY_DETECTED = "REPLAY_DETECTED";
133
+ var ERR_CONTRACT_VIOLATION = "CONTRACT_VIOLATION";
134
+
135
+ // src/core/varint.ts
136
+ function encodeVarint(value) {
137
+ if (value < 0) throw new Error("Varint must be unsigned");
138
+ const bytes = [];
139
+ while (true) {
140
+ const byte = value & 127;
141
+ value >>>= 7;
142
+ if (value === 0) {
143
+ bytes.push(byte);
144
+ break;
145
+ }
146
+ bytes.push(byte | 128);
147
+ }
148
+ return new Uint8Array(bytes);
149
+ }
150
+ function decodeVarint(buf, offset = 0) {
151
+ let value = 0;
152
+ let shift = 0;
153
+ let length = 0;
154
+ while (true) {
155
+ if (offset + length >= buf.length) {
156
+ throw new Error("Varint decode out of bounds");
157
+ }
158
+ const byte = buf[offset + length];
159
+ value += (byte & 127) * Math.pow(2, shift);
160
+ length++;
161
+ shift += 7;
162
+ if ((byte & 128) === 0) {
163
+ break;
164
+ }
165
+ if (length > 8) throw new Error("Varint too large");
166
+ }
167
+ return { value, length };
168
+ }
169
+ function varintLength(value) {
170
+ if (value < 0) throw new Error("Varint must be unsigned");
171
+ let len = 0;
172
+ do {
173
+ value >>>= 7;
174
+ len++;
175
+ } while (value !== 0);
176
+ return len;
177
+ }
178
+
179
+ // src/core/tlv.ts
180
+ function encodeTLVs(tlvs) {
181
+ const sorted = [...tlvs].sort((a, b) => a.type - b.type);
182
+ for (let i = 0; i < sorted.length - 1; i++) {
183
+ if (sorted[i].type === sorted[i + 1].type) {
184
+ throw new Error(`Duplicate TLV type: ${sorted[i].type}`);
185
+ }
186
+ }
187
+ let totalSize = 0;
188
+ for (const t of sorted) {
189
+ totalSize += varintLength(t.type);
190
+ totalSize += varintLength(t.value.length);
191
+ totalSize += t.value.length;
192
+ }
193
+ const buf = new Uint8Array(totalSize);
194
+ let offset = 0;
195
+ for (const t of sorted) {
196
+ const typeBytes = encodeVarint(t.type);
197
+ buf.set(typeBytes, offset);
198
+ offset += typeBytes.length;
199
+ const lenBytes = encodeVarint(t.value.length);
200
+ buf.set(lenBytes, offset);
201
+ offset += lenBytes.length;
202
+ buf.set(t.value, offset);
203
+ offset += t.value.length;
204
+ }
205
+ return buf;
206
+ }
207
+ function decodeTLVsList(buf, maxItems = 1024) {
208
+ const list = [];
209
+ let offset = 0;
210
+ while (offset < buf.length) {
211
+ if (list.length >= maxItems) throw new Error("TLV_LIMIT");
212
+ const { value: type, length: typeLen } = decodeVarint(buf, offset);
213
+ offset += typeLen;
214
+ const { value: len, length: lenLen } = decodeVarint(buf, offset);
215
+ offset += lenLen;
216
+ if (offset + len > buf.length) {
217
+ throw new Error(`TLV violation: Length ${len} exceeds buffer`);
218
+ }
219
+ const value = buf.slice(offset, offset + len);
220
+ list.push({ type, value });
221
+ offset += len;
222
+ }
223
+ return list;
224
+ }
225
+ function decodeTLVs(buf) {
226
+ const map2 = /* @__PURE__ */ new Map();
227
+ let offset = 0;
228
+ let lastType = -1;
229
+ while (offset < buf.length) {
230
+ const { value: type, length: typeLen } = decodeVarint(buf, offset);
231
+ offset += typeLen;
232
+ if (type <= lastType) {
233
+ throw new Error(
234
+ `TLV violation: Unsorted or duplicate type ${type} after ${lastType}`
235
+ );
236
+ }
237
+ lastType = type;
238
+ const { value: len, length: lenLen } = decodeVarint(buf, offset);
239
+ offset += lenLen;
240
+ if (offset + len > buf.length) {
241
+ throw new Error(`TLV violation: Length ${len} exceeds buffer`);
242
+ }
243
+ const value = buf.slice(offset, offset + len);
244
+ map2.set(type, value);
245
+ offset += len;
246
+ }
247
+ return map2;
248
+ }
249
+ function decodeObject(bytes, depth = 0, limits = { maxDepth: 8, maxItems: 128 }) {
250
+ if (depth > limits.maxDepth) {
251
+ throw new Error("OBJECT_DEPTH_EXCEEDED");
252
+ }
253
+ const map2 = decodeTLVs(bytes);
254
+ return map2;
255
+ }
256
+ function decodeArray(bytes, itemType, maxItems = 256) {
257
+ const list = decodeTLVsList(bytes, maxItems);
258
+ const items = [];
259
+ for (const tlv of list) {
260
+ if (tlv.type !== itemType) {
261
+ throw new Error(`INVALID_ARRAY_ITEM:${tlv.type}`);
262
+ }
263
+ items.push(tlv.value);
264
+ }
265
+ return items;
266
+ }
267
+
268
+ // src/core/axis-bin.ts
269
+ var z = __toESM(require("zod"));
270
+ var AxisFrameZ = z.object({
271
+ /** Flag bits for protocol control (e.g., encryption, compression) */
272
+ flags: z.number().int().nonnegative(),
273
+ /** A map of TLV headers where key=Tag and value=BinaryData */
274
+ headers: z.map(
275
+ z.number(),
276
+ z.custom((v) => v instanceof Uint8Array)
277
+ ),
278
+ /** The main payload of the frame */
279
+ body: z.custom((v) => v instanceof Uint8Array),
280
+ /** The cryptographic signature covering the frame (except the signature itself) */
281
+ sig: z.custom((v) => v instanceof Uint8Array)
282
+ });
283
+ function encodeFrame(frame) {
284
+ const hdrBytes = encodeTLVs(
285
+ Array.from(frame.headers.entries()).map(([t, v]) => ({
286
+ type: t,
287
+ value: v
288
+ }))
289
+ );
290
+ if (hdrBytes.length > MAX_HDR_LEN) throw new Error("Header too large");
291
+ if (frame.body.length > MAX_BODY_LEN) throw new Error("Body too large");
292
+ if (frame.sig.length > MAX_SIG_LEN) throw new Error("Signature too large");
293
+ const hdrLenBytes = encodeVarint(hdrBytes.length);
294
+ const bodyLenBytes = encodeVarint(frame.body.length);
295
+ const sigLenBytes = encodeVarint(frame.sig.length);
296
+ const totalLen = 5 + // Magic (AXIS1)
297
+ 1 + // Version
298
+ 1 + // Flags
299
+ hdrLenBytes.length + bodyLenBytes.length + sigLenBytes.length + hdrBytes.length + frame.body.length + frame.sig.length;
300
+ if (totalLen > MAX_FRAME_LEN) throw new Error("Total frame too large");
301
+ const buf = new Uint8Array(totalLen);
302
+ let offset = 0;
303
+ buf.set(AXIS_MAGIC, offset);
304
+ offset += 5;
305
+ buf[offset++] = AXIS_VERSION;
306
+ buf[offset++] = frame.flags;
307
+ buf.set(hdrLenBytes, offset);
308
+ offset += hdrLenBytes.length;
309
+ buf.set(bodyLenBytes, offset);
310
+ offset += bodyLenBytes.length;
311
+ buf.set(sigLenBytes, offset);
312
+ offset += sigLenBytes.length;
313
+ buf.set(hdrBytes, offset);
314
+ offset += hdrBytes.length;
315
+ buf.set(frame.body, offset);
316
+ offset += frame.body.length;
317
+ buf.set(frame.sig, offset);
318
+ offset += frame.sig.length;
319
+ return buf;
320
+ }
321
+ function decodeFrame(buf) {
322
+ let offset = 0;
323
+ if (offset + 5 > buf.length) throw new Error("Packet too short");
324
+ for (let i = 0; i < 5; i++) {
325
+ if (buf[offset + i] !== AXIS_MAGIC[i]) throw new Error("Invalid Magic");
326
+ }
327
+ offset += 5;
328
+ const ver = buf[offset++];
329
+ if (ver !== AXIS_VERSION) throw new Error(`Unsupported version: ${ver}`);
330
+ const flags = buf[offset++];
331
+ const { value: hdrLen, length: hlLen } = decodeVarint(buf, offset);
332
+ offset += hlLen;
333
+ if (hdrLen > MAX_HDR_LEN) throw new Error("Header limit exceeded");
334
+ const { value: bodyLen, length: blLen } = decodeVarint(buf, offset);
335
+ offset += blLen;
336
+ if (bodyLen > MAX_BODY_LEN) throw new Error("Body limit exceeded");
337
+ const { value: sigLen, length: slLen } = decodeVarint(buf, offset);
338
+ offset += slLen;
339
+ if (sigLen > MAX_SIG_LEN) throw new Error("Signature limit exceeded");
340
+ if (offset + hdrLen + bodyLen + sigLen > buf.length) {
341
+ throw new Error("Frame truncated");
342
+ }
343
+ const hdrBytes = buf.slice(offset, offset + hdrLen);
344
+ offset += hdrLen;
345
+ const bodyBytes = buf.slice(offset, offset + bodyLen);
346
+ offset += bodyLen;
347
+ const sigBytes = buf.slice(offset, offset + sigLen);
348
+ offset += sigLen;
349
+ const headers = decodeTLVs(hdrBytes);
350
+ return {
351
+ flags,
352
+ headers,
353
+ body: bodyBytes,
354
+ sig: sigBytes
355
+ };
356
+ }
357
+ function getSignTarget(frame) {
358
+ return encodeFrame({
359
+ ...frame,
360
+ sig: new Uint8Array(0)
361
+ });
362
+ }
363
+
364
+ // src/core/signature.ts
365
+ var crypto = __toESM(require("crypto"));
366
+ function computeSignaturePayload(frame) {
367
+ const frameWithoutSig = {
368
+ ...frame,
369
+ sig: new Uint8Array(0)
370
+ };
371
+ const encoded = encodeFrame(frameWithoutSig);
372
+ return Buffer.from(encoded);
373
+ }
374
+ function signFrame(frame, privateKey) {
375
+ const payload = computeSignaturePayload(frame);
376
+ let keyObject;
377
+ if (privateKey.length === 32) {
378
+ const pkcs8Prefix = Buffer.from([
379
+ 48,
380
+ 46,
381
+ 2,
382
+ 1,
383
+ 0,
384
+ 48,
385
+ 5,
386
+ 6,
387
+ 3,
388
+ 43,
389
+ 101,
390
+ 112,
391
+ 4,
392
+ 34,
393
+ 4,
394
+ 32
395
+ ]);
396
+ const pkcs8Key = Buffer.concat([pkcs8Prefix, privateKey]);
397
+ keyObject = crypto.createPrivateKey({
398
+ key: pkcs8Key,
399
+ format: "der",
400
+ type: "pkcs8"
401
+ });
402
+ } else {
403
+ keyObject = crypto.createPrivateKey({
404
+ key: privateKey,
405
+ format: "der",
406
+ type: "pkcs8"
407
+ });
408
+ }
409
+ const signature = crypto.sign(null, payload, keyObject);
410
+ if (signature.length !== 64) {
411
+ throw new Error("Ed25519 signature must be 64 bytes");
412
+ }
413
+ return signature;
414
+ }
415
+ function verifyFrameSignature(frame, publicKey) {
416
+ if (frame.sig.length === 0) {
417
+ return false;
418
+ }
419
+ if (frame.sig.length !== 64) {
420
+ throw new Error("Ed25519 signature must be 64 bytes");
421
+ }
422
+ const payload = computeSignaturePayload(frame);
423
+ try {
424
+ let keyObject;
425
+ if (publicKey.length === 32) {
426
+ const spkiPrefix = Buffer.from([
427
+ 48,
428
+ 42,
429
+ 48,
430
+ 5,
431
+ 6,
432
+ 3,
433
+ 43,
434
+ 101,
435
+ 112,
436
+ 3,
437
+ 33,
438
+ 0
439
+ ]);
440
+ const spkiKey = Buffer.concat([spkiPrefix, publicKey]);
441
+ keyObject = crypto.createPublicKey({
442
+ key: spkiKey,
443
+ format: "der",
444
+ type: "spki"
445
+ });
446
+ } else {
447
+ keyObject = crypto.createPublicKey({
448
+ key: publicKey,
449
+ format: "der",
450
+ type: "spki"
451
+ });
452
+ }
453
+ const valid = crypto.verify(
454
+ null,
455
+ payload,
456
+ keyObject,
457
+ Buffer.from(frame.sig)
458
+ );
459
+ return valid;
460
+ } catch (error) {
461
+ return false;
462
+ }
463
+ }
464
+ function generateEd25519KeyPair() {
465
+ const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519");
466
+ return {
467
+ privateKey: privateKey.export({ type: "pkcs8", format: "der" }),
468
+ publicKey: publicKey.export({ type: "spki", format: "der" })
469
+ };
470
+ }
471
+ function sha256(data) {
472
+ return crypto.createHash("sha256").update(data).digest();
473
+ }
474
+ function computeReceiptHash(receiptBytes, prevHash) {
475
+ const hasher = crypto.createHash("sha256");
476
+ hasher.update(receiptBytes);
477
+ if (prevHash && prevHash.length > 0) {
478
+ hasher.update(prevHash);
479
+ }
480
+ return hasher.digest();
481
+ }
482
+ // Annotate the CommonJS export names for ESM import in node:
483
+ 0 && (module.exports = {
484
+ AXIS_MAGIC,
485
+ AXIS_VERSION,
486
+ AxisFrameZ,
487
+ ERR_BAD_SIGNATURE,
488
+ ERR_CONTRACT_VIOLATION,
489
+ ERR_INVALID_PACKET,
490
+ ERR_REPLAY_DETECTED,
491
+ FLAG_BODY_TLV,
492
+ FLAG_CHAIN_REQ,
493
+ FLAG_HAS_WITNESS,
494
+ MAX_BODY_LEN,
495
+ MAX_FRAME_LEN,
496
+ MAX_HDR_LEN,
497
+ MAX_SIG_LEN,
498
+ PROOF_CAPSULE,
499
+ PROOF_JWT,
500
+ PROOF_LOOM,
501
+ PROOF_MTLS,
502
+ TLV_ACTOR_ID,
503
+ TLV_AUD,
504
+ TLV_EFFECT,
505
+ TLV_ERROR_CODE,
506
+ TLV_ERROR_MSG,
507
+ TLV_INTENT,
508
+ TLV_KID,
509
+ TLV_LOOM_PRESENCE_ID,
510
+ TLV_LOOM_THREAD_HASH,
511
+ TLV_LOOM_WRIT,
512
+ TLV_NODE,
513
+ TLV_NODE_CERT_HASH,
514
+ TLV_NODE_KID,
515
+ TLV_NONCE,
516
+ TLV_OK,
517
+ TLV_PID,
518
+ TLV_PREV_HASH,
519
+ TLV_PROOF_REF,
520
+ TLV_PROOF_TYPE,
521
+ TLV_RECEIPT_HASH,
522
+ TLV_RID,
523
+ TLV_TRACE_ID,
524
+ TLV_TS,
525
+ computeReceiptHash,
526
+ computeSignaturePayload,
527
+ decodeArray,
528
+ decodeFrame,
529
+ decodeObject,
530
+ decodeTLVs,
531
+ decodeTLVsList,
532
+ decodeVarint,
533
+ encodeFrame,
534
+ encodeTLVs,
535
+ encodeVarint,
536
+ generateEd25519KeyPair,
537
+ getSignTarget,
538
+ sha256,
539
+ signFrame,
540
+ varintLength,
541
+ verifyFrameSignature
542
+ });
543
+ //# sourceMappingURL=index.js.map