@nextera.one/axis-server-sdk 0.2.0 → 0.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/dist/index.d.mts +634 -2
- package/dist/index.d.ts +634 -2
- package/dist/index.js +1345 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1313 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
4
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
5
|
+
}) : x)(function(x) {
|
|
6
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
7
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
+
});
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
3
13
|
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
14
|
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
15
|
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
@@ -242,17 +252,17 @@ var ERR_CONTRACT_VIOLATION = "CONTRACT_VIOLATION";
|
|
|
242
252
|
// src/core/varint.ts
|
|
243
253
|
function encodeVarint(value) {
|
|
244
254
|
if (value < 0) throw new Error("Varint must be unsigned");
|
|
245
|
-
const
|
|
255
|
+
const bytes2 = [];
|
|
246
256
|
while (true) {
|
|
247
257
|
const byte = value & 127;
|
|
248
258
|
value >>>= 7;
|
|
249
259
|
if (value === 0) {
|
|
250
|
-
|
|
260
|
+
bytes2.push(byte);
|
|
251
261
|
break;
|
|
252
262
|
}
|
|
253
|
-
|
|
263
|
+
bytes2.push(byte | 128);
|
|
254
264
|
}
|
|
255
|
-
return new Uint8Array(
|
|
265
|
+
return new Uint8Array(bytes2);
|
|
256
266
|
}
|
|
257
267
|
function decodeVarint(buf, offset = 0) {
|
|
258
268
|
let value = 0;
|
|
@@ -353,21 +363,21 @@ function decodeTLVs(buf) {
|
|
|
353
363
|
}
|
|
354
364
|
return map2;
|
|
355
365
|
}
|
|
356
|
-
function decodeObject(
|
|
366
|
+
function decodeObject(bytes2, depth = 0, limits = { maxDepth: 8, maxItems: 128 }) {
|
|
357
367
|
if (depth > limits.maxDepth) {
|
|
358
368
|
throw new Error("OBJECT_DEPTH_EXCEEDED");
|
|
359
369
|
}
|
|
360
|
-
const map2 = decodeTLVs(
|
|
370
|
+
const map2 = decodeTLVs(bytes2);
|
|
361
371
|
return map2;
|
|
362
372
|
}
|
|
363
|
-
function decodeArray(
|
|
364
|
-
const list = decodeTLVsList(
|
|
373
|
+
function decodeArray(bytes2, itemType, maxItems = 256) {
|
|
374
|
+
const list = decodeTLVsList(bytes2, maxItems);
|
|
365
375
|
const items = [];
|
|
366
|
-
for (const
|
|
367
|
-
if (
|
|
368
|
-
throw new Error(`INVALID_ARRAY_ITEM:${
|
|
376
|
+
for (const tlv2 of list) {
|
|
377
|
+
if (tlv2.type !== itemType) {
|
|
378
|
+
throw new Error(`INVALID_ARRAY_ITEM:${tlv2.type}`);
|
|
369
379
|
}
|
|
370
|
-
items.push(
|
|
380
|
+
items.push(tlv2.value);
|
|
371
381
|
}
|
|
372
382
|
return items;
|
|
373
383
|
}
|
|
@@ -586,14 +596,1272 @@ function computeReceiptHash(receiptBytes, prevHash) {
|
|
|
586
596
|
}
|
|
587
597
|
return hasher.digest();
|
|
588
598
|
}
|
|
599
|
+
|
|
600
|
+
// src/codec/ats1.constants.ts
|
|
601
|
+
var ATS1_HDR = {
|
|
602
|
+
INTENT_ID: 1,
|
|
603
|
+
// uvarint
|
|
604
|
+
ACTOR_KEY_ID: 2,
|
|
605
|
+
// bytes (key fingerprint / credentialId hash)
|
|
606
|
+
CAPSULE_ID: 3,
|
|
607
|
+
// bytes or varint
|
|
608
|
+
NONCE: 4,
|
|
609
|
+
// 16 bytes
|
|
610
|
+
TS_MS: 5,
|
|
611
|
+
// u64be (8)
|
|
612
|
+
SCHEMA_ID: 6,
|
|
613
|
+
// uvarint
|
|
614
|
+
BODY_HASH: 7,
|
|
615
|
+
// 32 bytes (sha256)
|
|
616
|
+
TRACE_ID: 8
|
|
617
|
+
// 16 bytes
|
|
618
|
+
};
|
|
619
|
+
var ATS1_SCHEMA = {
|
|
620
|
+
PASSKEY_LOGIN_OPTIONS_REQ: 2001,
|
|
621
|
+
PASSKEY_LOGIN_OPTIONS_RES: 2002,
|
|
622
|
+
PASSKEY_LOGIN_VERIFY_REQ: 2011,
|
|
623
|
+
PASSKEY_LOGIN_VERIFY_RES: 2012,
|
|
624
|
+
PASSKEY_REGISTER_OPTIONS_REQ: 2021,
|
|
625
|
+
PASSKEY_REGISTER_OPTIONS_RES: 2022,
|
|
626
|
+
PASSKEY_REGISTER_VERIFY_REQ: 2031,
|
|
627
|
+
PASSKEY_REGISTER_VERIFY_RES: 2032
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
// src/codec/ats1.ts
|
|
631
|
+
var ats1_exports = {};
|
|
632
|
+
__export(ats1_exports, {
|
|
633
|
+
DEFAULT_LIMITS: () => DEFAULT_LIMITS,
|
|
634
|
+
HDR_TAGS: () => HDR_TAGS,
|
|
635
|
+
Schema2001_PasskeyLoginOptionsReq: () => Schema2001_PasskeyLoginOptionsReq,
|
|
636
|
+
Schema3100_DeviceContext: () => Schema3100_DeviceContext,
|
|
637
|
+
Schema4001_LoginWithDeviceReq: () => Schema4001_LoginWithDeviceReq,
|
|
638
|
+
decodeAxisHeaderFromTLVs: () => decodeAxisHeaderFromTLVs,
|
|
639
|
+
decodeAxisRequestBinary: () => decodeAxisRequestBinary,
|
|
640
|
+
decodeTLVStream: () => decodeTLVStream,
|
|
641
|
+
decodeU64BE: () => decodeU64BE,
|
|
642
|
+
decodeUVarint: () => decodeUVarint,
|
|
643
|
+
encodeAxisHeaderToTLVs: () => encodeAxisHeaderToTLVs,
|
|
644
|
+
encodeAxisRequestBinary: () => encodeAxisRequestBinary,
|
|
645
|
+
encodeTLV: () => encodeTLV,
|
|
646
|
+
encodeTLVStreamCanonical: () => encodeTLVStreamCanonical,
|
|
647
|
+
encodeU64BE: () => encodeU64BE,
|
|
648
|
+
encodeUVarint: () => encodeUVarint,
|
|
649
|
+
logicalBodyToTLVs: () => logicalBodyToTLVs,
|
|
650
|
+
sha256: () => sha2562,
|
|
651
|
+
tlvsToLogicalBody: () => tlvsToLogicalBody,
|
|
652
|
+
tlvsToMap: () => tlvsToMap,
|
|
653
|
+
validateTLVsAgainstSchema: () => validateTLVsAgainstSchema
|
|
654
|
+
});
|
|
655
|
+
import { createHash as createHash2 } from "crypto";
|
|
656
|
+
var DEFAULT_LIMITS = {
|
|
657
|
+
maxVarintBytes: 10,
|
|
658
|
+
maxTlvCount: 512,
|
|
659
|
+
maxValueBytes: 1048576,
|
|
660
|
+
// 1 MiB
|
|
661
|
+
maxNestingDepth: 4
|
|
662
|
+
};
|
|
663
|
+
function encodeUVarint(n) {
|
|
664
|
+
let x = typeof n === "bigint" ? n : BigInt(n);
|
|
665
|
+
if (x < 0n) throw new Error("encodeUVarint: negative not allowed");
|
|
666
|
+
const out = [];
|
|
667
|
+
while (x >= 0x80n) {
|
|
668
|
+
out.push(Number(x & 0x7fn | 0x80n));
|
|
669
|
+
x >>= 7n;
|
|
670
|
+
}
|
|
671
|
+
out.push(Number(x));
|
|
672
|
+
return Buffer.from(out);
|
|
673
|
+
}
|
|
674
|
+
function decodeUVarint(buf, offset, limits = DEFAULT_LIMITS) {
|
|
675
|
+
let x = 0n;
|
|
676
|
+
let shift = 0n;
|
|
677
|
+
const start = offset;
|
|
678
|
+
for (let i = 0; i < limits.maxVarintBytes; i++) {
|
|
679
|
+
if (offset >= buf.length) throw new Error("decodeUVarint: truncated");
|
|
680
|
+
const b = buf[offset++];
|
|
681
|
+
x |= BigInt(b & 127) << shift;
|
|
682
|
+
if ((b & 128) === 0) {
|
|
683
|
+
const bytesRead = offset - start;
|
|
684
|
+
const re = encodeUVarint(x);
|
|
685
|
+
const original = buf.subarray(start, offset);
|
|
686
|
+
if (!re.equals(original))
|
|
687
|
+
throw new Error("decodeUVarint: non-minimal varint");
|
|
688
|
+
return { value: x, offset, bytesRead };
|
|
689
|
+
}
|
|
690
|
+
shift += 7n;
|
|
691
|
+
}
|
|
692
|
+
throw new Error("decodeUVarint: too long");
|
|
693
|
+
}
|
|
694
|
+
function encodeU64BE(n) {
|
|
695
|
+
if (n < 0n) throw new Error("encodeU64BE: negative not allowed");
|
|
696
|
+
const b = Buffer.alloc(8);
|
|
697
|
+
b.writeBigUInt64BE(n, 0);
|
|
698
|
+
return b;
|
|
699
|
+
}
|
|
700
|
+
function decodeU64BE(buf) {
|
|
701
|
+
if (buf.length !== 8) throw new Error("decodeU64BE: length must be 8");
|
|
702
|
+
return buf.readBigUInt64BE(0);
|
|
703
|
+
}
|
|
704
|
+
function sha2562(data) {
|
|
705
|
+
return createHash2("sha256").update(data).digest();
|
|
706
|
+
}
|
|
707
|
+
function encodeTLV(tag, value) {
|
|
708
|
+
if (!Number.isInteger(tag) || tag <= 0)
|
|
709
|
+
throw new Error("encodeTLV: tag must be positive int");
|
|
710
|
+
const t = encodeUVarint(tag);
|
|
711
|
+
const l = encodeUVarint(value.length);
|
|
712
|
+
return Buffer.concat([t, l, value]);
|
|
713
|
+
}
|
|
714
|
+
function encodeTLVStreamCanonical(entries) {
|
|
715
|
+
const sorted = [...entries].sort((a, b) => a.tag - b.tag);
|
|
716
|
+
const parts = [];
|
|
717
|
+
for (const e of sorted) parts.push(encodeTLV(e.tag, e.value));
|
|
718
|
+
return Buffer.concat(parts);
|
|
719
|
+
}
|
|
720
|
+
function decodeTLVStream(stream, limits = DEFAULT_LIMITS) {
|
|
721
|
+
const out = [];
|
|
722
|
+
let off = 0;
|
|
723
|
+
while (off < stream.length) {
|
|
724
|
+
if (out.length >= limits.maxTlvCount)
|
|
725
|
+
throw new Error("decodeTLVStream: too many TLVs");
|
|
726
|
+
const tagRes = decodeUVarint(stream, off, limits);
|
|
727
|
+
const tag = Number(tagRes.value);
|
|
728
|
+
off = tagRes.offset;
|
|
729
|
+
const lenRes = decodeUVarint(stream, off, limits);
|
|
730
|
+
const len = Number(lenRes.value);
|
|
731
|
+
off = lenRes.offset;
|
|
732
|
+
if (len < 0) throw new Error("decodeTLVStream: negative length");
|
|
733
|
+
if (len > limits.maxValueBytes)
|
|
734
|
+
throw new Error("decodeTLVStream: value too large");
|
|
735
|
+
if (off + len > stream.length)
|
|
736
|
+
throw new Error("decodeTLVStream: truncated value");
|
|
737
|
+
const value = stream.subarray(off, off + len);
|
|
738
|
+
off += len;
|
|
739
|
+
out.push({ tag, value: Buffer.from(value) });
|
|
740
|
+
}
|
|
741
|
+
for (let i = 1; i < out.length; i++) {
|
|
742
|
+
if (out[i].tag < out[i - 1].tag)
|
|
743
|
+
throw new Error("decodeTLVStream: non-canonical tag order");
|
|
744
|
+
}
|
|
745
|
+
return out;
|
|
746
|
+
}
|
|
747
|
+
function tlvsToMap(entries) {
|
|
748
|
+
const m = /* @__PURE__ */ new Map();
|
|
749
|
+
for (const e of entries) {
|
|
750
|
+
const arr = m.get(e.tag) ?? [];
|
|
751
|
+
arr.push(e.value);
|
|
752
|
+
m.set(e.tag, arr);
|
|
753
|
+
}
|
|
754
|
+
return m;
|
|
755
|
+
}
|
|
756
|
+
function validateTLVsAgainstSchema(schema, tlvs, depth = 0, limits = DEFAULT_LIMITS) {
|
|
757
|
+
if (depth > Math.min(schema.maxNestingDepth, limits.maxNestingDepth)) {
|
|
758
|
+
throw new Error("validateTLVsAgainstSchema: nesting depth exceeded");
|
|
759
|
+
}
|
|
760
|
+
if (schema.maxBodyBytes && tlvsBytes(tlvs) > schema.maxBodyBytes) {
|
|
761
|
+
throw new Error("validateTLVsAgainstSchema: body too large");
|
|
762
|
+
}
|
|
763
|
+
const byTag = /* @__PURE__ */ new Map();
|
|
764
|
+
for (const t of tlvs) {
|
|
765
|
+
if (!byTag.has(t.tag)) byTag.set(t.tag, []);
|
|
766
|
+
byTag.get(t.tag).push(t);
|
|
767
|
+
}
|
|
768
|
+
const fieldByTag = new Map(schema.fields.map((f) => [f.tag, f]));
|
|
769
|
+
if (schema.strict) {
|
|
770
|
+
for (const tag of byTag.keys()) {
|
|
771
|
+
if (!fieldByTag.has(tag))
|
|
772
|
+
throw new Error(`validateTLVsAgainstSchema: unknown tag ${tag}`);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
for (const f of schema.fields) {
|
|
776
|
+
const vals = byTag.get(f.tag) ?? [];
|
|
777
|
+
if (f.required && vals.length === 0)
|
|
778
|
+
throw new Error(`validateTLVsAgainstSchema: missing ${f.name}`);
|
|
779
|
+
if (!f.repeated && vals.length > 1) {
|
|
780
|
+
throw new Error(
|
|
781
|
+
`validateTLVsAgainstSchema: duplicate tag not allowed for ${f.name}`
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
if (typeof f.maxLen === "number") {
|
|
785
|
+
for (const v of vals) {
|
|
786
|
+
if (v.value.length > f.maxLen)
|
|
787
|
+
throw new Error(`validateTLVsAgainstSchema: ${f.name} too long`);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
for (const v of vals) {
|
|
791
|
+
switch (f.type) {
|
|
792
|
+
case "u64be":
|
|
793
|
+
if (v.value.length !== 8)
|
|
794
|
+
throw new Error(
|
|
795
|
+
`validateTLVsAgainstSchema: ${f.name} u64be must be 8 bytes`
|
|
796
|
+
);
|
|
797
|
+
break;
|
|
798
|
+
case "nested": {
|
|
799
|
+
if (!f.nestedSchema)
|
|
800
|
+
throw new Error(
|
|
801
|
+
`validateTLVsAgainstSchema: ${f.name} missing nestedSchema`
|
|
802
|
+
);
|
|
803
|
+
const nestedTlvs = decodeTLVStream(v.value, limits);
|
|
804
|
+
validateTLVsAgainstSchema(
|
|
805
|
+
f.nestedSchema,
|
|
806
|
+
nestedTlvs,
|
|
807
|
+
depth + 1,
|
|
808
|
+
limits
|
|
809
|
+
);
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
default:
|
|
813
|
+
break;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
function tlvsBytes(tlvs) {
|
|
819
|
+
let n = 0;
|
|
820
|
+
for (const t of tlvs) {
|
|
821
|
+
n += encodeUVarint(t.tag).length + encodeUVarint(t.value.length).length + t.value.length;
|
|
822
|
+
}
|
|
823
|
+
return n;
|
|
824
|
+
}
|
|
825
|
+
function logicalBodyToTLVs(schema, body, limits = DEFAULT_LIMITS) {
|
|
826
|
+
if (body.schemaId !== schema.schemaId)
|
|
827
|
+
throw new Error("logicalBodyToTLVs: schemaId mismatch");
|
|
828
|
+
const fieldsByName = new Map(schema.fields.map((f) => [f.name, f]));
|
|
829
|
+
const tlvs = [];
|
|
830
|
+
for (const [name, val] of Object.entries(body.fields ?? {})) {
|
|
831
|
+
const f = fieldsByName.get(name);
|
|
832
|
+
if (!f) {
|
|
833
|
+
if (schema.strict)
|
|
834
|
+
throw new Error(`logicalBodyToTLVs: unknown field ${name}`);
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
const pushOne = (v) => {
|
|
838
|
+
const valueBuf = encodeFieldValue(f, v, limits);
|
|
839
|
+
if (valueBuf.length > limits.maxValueBytes)
|
|
840
|
+
throw new Error("logicalBodyToTLVs: value too large");
|
|
841
|
+
tlvs.push({ tag: f.tag, value: valueBuf });
|
|
842
|
+
};
|
|
843
|
+
if (f.repeated) {
|
|
844
|
+
if (!Array.isArray(val))
|
|
845
|
+
throw new Error(
|
|
846
|
+
`logicalBodyToTLVs: repeated field ${name} must be array`
|
|
847
|
+
);
|
|
848
|
+
for (const item of val) pushOne(item);
|
|
849
|
+
} else {
|
|
850
|
+
pushOne(val);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
validateTLVsAgainstSchema(schema, tlvs, 0, limits);
|
|
854
|
+
return tlvs;
|
|
855
|
+
}
|
|
856
|
+
function encodeFieldValue(f, val, limits) {
|
|
857
|
+
switch (f.type) {
|
|
858
|
+
case "bytes":
|
|
859
|
+
if (Buffer.isBuffer(val)) return Buffer.from(val);
|
|
860
|
+
if (val instanceof Uint8Array) return Buffer.from(val);
|
|
861
|
+
throw new Error(`encodeFieldValue: ${f.name} expects bytes`);
|
|
862
|
+
case "utf8":
|
|
863
|
+
if (typeof val !== "string")
|
|
864
|
+
throw new Error(`encodeFieldValue: ${f.name} expects string`);
|
|
865
|
+
return Buffer.from(val, "utf8");
|
|
866
|
+
case "uvarint":
|
|
867
|
+
if (typeof val !== "number" && typeof val !== "bigint")
|
|
868
|
+
throw new Error(`encodeFieldValue: ${f.name} expects number/bigint`);
|
|
869
|
+
return encodeUVarint(val);
|
|
870
|
+
case "u64be":
|
|
871
|
+
if (typeof val !== "bigint")
|
|
872
|
+
throw new Error(`encodeFieldValue: ${f.name} expects bigint`);
|
|
873
|
+
return encodeU64BE(val);
|
|
874
|
+
case "nested": {
|
|
875
|
+
if (!f.nestedSchema)
|
|
876
|
+
throw new Error(`encodeFieldValue: ${f.name} missing nestedSchema`);
|
|
877
|
+
const nestedFields = val && typeof val === "object" && "fields" in val ? val.fields : val;
|
|
878
|
+
if (!nestedFields || typeof nestedFields !== "object")
|
|
879
|
+
throw new Error(`encodeFieldValue: ${f.name} expects object`);
|
|
880
|
+
const nestedBody = {
|
|
881
|
+
schemaId: f.nestedSchema.schemaId,
|
|
882
|
+
fields: nestedFields
|
|
883
|
+
};
|
|
884
|
+
const nestedTlvs = logicalBodyToTLVs(f.nestedSchema, nestedBody, limits);
|
|
885
|
+
const nestedBytes = encodeTLVStreamCanonical(nestedTlvs);
|
|
886
|
+
const re = decodeTLVStream(nestedBytes, limits);
|
|
887
|
+
validateTLVsAgainstSchema(f.nestedSchema, re, 1, limits);
|
|
888
|
+
return nestedBytes;
|
|
889
|
+
}
|
|
890
|
+
default:
|
|
891
|
+
throw new Error(`encodeFieldValue: unsupported type ${f.type}`);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
function tlvsToLogicalBody(schema, tlvs, limits = DEFAULT_LIMITS) {
|
|
895
|
+
validateTLVsAgainstSchema(schema, tlvs, 0, limits);
|
|
896
|
+
const fields = {};
|
|
897
|
+
const fieldByTag = new Map(schema.fields.map((f) => [f.tag, f]));
|
|
898
|
+
for (const t of tlvs) {
|
|
899
|
+
const f = fieldByTag.get(t.tag);
|
|
900
|
+
if (!f) {
|
|
901
|
+
if (schema.strict)
|
|
902
|
+
throw new Error(`tlvsToLogicalBody: unknown tag ${t.tag}`);
|
|
903
|
+
continue;
|
|
904
|
+
}
|
|
905
|
+
const decoded = decodeFieldValue(f, t.value, limits);
|
|
906
|
+
if (f.repeated) {
|
|
907
|
+
if (!Array.isArray(fields[f.name])) fields[f.name] = [];
|
|
908
|
+
fields[f.name].push(decoded);
|
|
909
|
+
} else {
|
|
910
|
+
fields[f.name] = decoded;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return { schemaId: schema.schemaId, fields };
|
|
914
|
+
}
|
|
915
|
+
function decodeFieldValue(f, value, limits) {
|
|
916
|
+
switch (f.type) {
|
|
917
|
+
case "bytes":
|
|
918
|
+
return Buffer.from(value);
|
|
919
|
+
case "utf8":
|
|
920
|
+
return value.toString("utf8");
|
|
921
|
+
case "uvarint": {
|
|
922
|
+
const r = decodeUVarint(value, 0, limits);
|
|
923
|
+
if (r.offset !== value.length)
|
|
924
|
+
throw new Error(
|
|
925
|
+
`decodeFieldValue: ${f.name} uvarint has trailing bytes`
|
|
926
|
+
);
|
|
927
|
+
const asNum = Number(r.value);
|
|
928
|
+
return Number.isSafeInteger(asNum) ? asNum : r.value;
|
|
929
|
+
}
|
|
930
|
+
case "u64be":
|
|
931
|
+
return decodeU64BE(value);
|
|
932
|
+
case "nested": {
|
|
933
|
+
if (!f.nestedSchema)
|
|
934
|
+
throw new Error(`decodeFieldValue: ${f.name} missing nestedSchema`);
|
|
935
|
+
const nestedTlvs = decodeTLVStream(value, limits);
|
|
936
|
+
const nestedBody = tlvsToLogicalBody(f.nestedSchema, nestedTlvs, limits);
|
|
937
|
+
return nestedBody.fields;
|
|
938
|
+
}
|
|
939
|
+
default:
|
|
940
|
+
throw new Error(`decodeFieldValue: unsupported type ${f.type}`);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
var HDR_TAGS = {
|
|
944
|
+
intent_id: 1,
|
|
945
|
+
actor_key_id: 2,
|
|
946
|
+
capsule_id: 3,
|
|
947
|
+
nonce: 4,
|
|
948
|
+
ts_ms: 5,
|
|
949
|
+
schema_id: 6,
|
|
950
|
+
body_hash: 7,
|
|
951
|
+
trace_id: 8
|
|
952
|
+
};
|
|
953
|
+
function encodeAxisHeaderToTLVs(hdr) {
|
|
954
|
+
if (hdr.nonce.byteLength !== 16)
|
|
955
|
+
throw new Error("encodeAxisHeaderToTLVs: nonce must be 16 bytes");
|
|
956
|
+
if (hdr.bodyHash.byteLength !== 32)
|
|
957
|
+
throw new Error("encodeAxisHeaderToTLVs: bodyHash must be 32 bytes");
|
|
958
|
+
if (hdr.traceId && hdr.traceId.byteLength !== 16)
|
|
959
|
+
throw new Error("encodeAxisHeaderToTLVs: traceId must be 16 bytes");
|
|
960
|
+
const tlvs = [
|
|
961
|
+
{ tag: HDR_TAGS.intent_id, value: encodeUVarint(hdr.intentId) },
|
|
962
|
+
{ tag: HDR_TAGS.actor_key_id, value: Buffer.from(hdr.actorKeyId) },
|
|
963
|
+
{ tag: HDR_TAGS.nonce, value: Buffer.from(hdr.nonce) },
|
|
964
|
+
{ tag: HDR_TAGS.ts_ms, value: encodeU64BE(hdr.tsMs) },
|
|
965
|
+
{ tag: HDR_TAGS.schema_id, value: encodeUVarint(hdr.schemaId) },
|
|
966
|
+
{ tag: HDR_TAGS.body_hash, value: Buffer.from(hdr.bodyHash) }
|
|
967
|
+
];
|
|
968
|
+
if (hdr.capsuleId)
|
|
969
|
+
tlvs.push({ tag: HDR_TAGS.capsule_id, value: Buffer.from(hdr.capsuleId) });
|
|
970
|
+
if (hdr.traceId)
|
|
971
|
+
tlvs.push({ tag: HDR_TAGS.trace_id, value: Buffer.from(hdr.traceId) });
|
|
972
|
+
return tlvs;
|
|
973
|
+
}
|
|
974
|
+
function decodeAxisHeaderFromTLVs(hdrTlvs, limits = DEFAULT_LIMITS) {
|
|
975
|
+
const m = tlvsToMap(hdrTlvs);
|
|
976
|
+
const get1 = (tag) => {
|
|
977
|
+
const arr = m.get(tag);
|
|
978
|
+
if (!arr || arr.length !== 1)
|
|
979
|
+
throw new Error(
|
|
980
|
+
`decodeAxisHeaderFromTLVs: missing/dup header tag ${tag}`
|
|
981
|
+
);
|
|
982
|
+
return arr[0];
|
|
983
|
+
};
|
|
984
|
+
const getOpt1 = (tag) => {
|
|
985
|
+
const arr = m.get(tag);
|
|
986
|
+
if (!arr) return void 0;
|
|
987
|
+
if (arr.length !== 1)
|
|
988
|
+
throw new Error(`decodeAxisHeaderFromTLVs: dup header tag ${tag}`);
|
|
989
|
+
return arr[0];
|
|
990
|
+
};
|
|
991
|
+
const intentIdVar = decodeUVarint(get1(HDR_TAGS.intent_id), 0, limits);
|
|
992
|
+
if (intentIdVar.offset !== get1(HDR_TAGS.intent_id).length)
|
|
993
|
+
throw new Error("decodeAxisHeaderFromTLVs: intent_id trailing bytes");
|
|
994
|
+
const schemaIdVar = decodeUVarint(get1(HDR_TAGS.schema_id), 0, limits);
|
|
995
|
+
if (schemaIdVar.offset !== get1(HDR_TAGS.schema_id).length)
|
|
996
|
+
throw new Error("decodeAxisHeaderFromTLVs: schema_id trailing bytes");
|
|
997
|
+
const ts = decodeU64BE(get1(HDR_TAGS.ts_ms));
|
|
998
|
+
const nonce = get1(HDR_TAGS.nonce);
|
|
999
|
+
if (nonce.length !== 16)
|
|
1000
|
+
throw new Error("decodeAxisHeaderFromTLVs: nonce must be 16 bytes");
|
|
1001
|
+
const bodyHash = get1(HDR_TAGS.body_hash);
|
|
1002
|
+
if (bodyHash.length !== 32)
|
|
1003
|
+
throw new Error("decodeAxisHeaderFromTLVs: body_hash must be 32 bytes");
|
|
1004
|
+
const trace = getOpt1(HDR_TAGS.trace_id);
|
|
1005
|
+
if (trace && trace.length !== 16)
|
|
1006
|
+
throw new Error("decodeAxisHeaderFromTLVs: trace_id must be 16 bytes");
|
|
1007
|
+
return {
|
|
1008
|
+
intentId: Number(intentIdVar.value),
|
|
1009
|
+
actorKeyId: Buffer.from(get1(HDR_TAGS.actor_key_id)),
|
|
1010
|
+
capsuleId: getOpt1(HDR_TAGS.capsule_id) ? Buffer.from(getOpt1(HDR_TAGS.capsule_id)) : void 0,
|
|
1011
|
+
nonce: Buffer.from(nonce),
|
|
1012
|
+
tsMs: ts,
|
|
1013
|
+
schemaId: Number(schemaIdVar.value),
|
|
1014
|
+
bodyHash: Buffer.from(bodyHash),
|
|
1015
|
+
traceId: trace ? Buffer.from(trace) : void 0
|
|
1016
|
+
};
|
|
1017
|
+
}
|
|
1018
|
+
function encodeAxisRequestBinary(schema, req, limits = DEFAULT_LIMITS) {
|
|
1019
|
+
const bodyTlvs = logicalBodyToTLVs(schema, req.body, limits);
|
|
1020
|
+
const bodyBytes = encodeTLVStreamCanonical(bodyTlvs);
|
|
1021
|
+
const bodyHash = sha2562(bodyBytes);
|
|
1022
|
+
const hdr = {
|
|
1023
|
+
...req.hdr,
|
|
1024
|
+
schemaId: schema.schemaId,
|
|
1025
|
+
bodyHash
|
|
1026
|
+
};
|
|
1027
|
+
const hdrTlvs = encodeAxisHeaderToTLVs(hdr);
|
|
1028
|
+
const hdrBytes = encodeTLVStreamCanonical(hdrTlvs);
|
|
1029
|
+
return { hdrBytes, bodyBytes, bodyHash };
|
|
1030
|
+
}
|
|
1031
|
+
function decodeAxisRequestBinary(schema, hdrBytes, bodyBytes, limits = DEFAULT_LIMITS) {
|
|
1032
|
+
const hdrTlvs = decodeTLVStream(hdrBytes, limits);
|
|
1033
|
+
const bodyTlvs = decodeTLVStream(bodyBytes, limits);
|
|
1034
|
+
const hdr = decodeAxisHeaderFromTLVs(hdrTlvs, limits);
|
|
1035
|
+
if (hdr.schemaId !== schema.schemaId)
|
|
1036
|
+
throw new Error("decodeAxisRequestBinary: schemaId mismatch");
|
|
1037
|
+
const bh = sha2562(bodyBytes);
|
|
1038
|
+
if (!Buffer.from(hdr.bodyHash).equals(bh))
|
|
1039
|
+
throw new Error("decodeAxisRequestBinary: body_hash mismatch");
|
|
1040
|
+
const body = tlvsToLogicalBody(schema, bodyTlvs, limits);
|
|
1041
|
+
const sensorInput = {
|
|
1042
|
+
hdrTLVs: tlvsToMap(hdrTlvs),
|
|
1043
|
+
bodyTLVs: tlvsToMap(bodyTlvs),
|
|
1044
|
+
schemaId: hdr.schemaId,
|
|
1045
|
+
intentId: hdr.intentId
|
|
1046
|
+
};
|
|
1047
|
+
return { hdr, body, sensorInput };
|
|
1048
|
+
}
|
|
1049
|
+
var Schema3100_DeviceContext = {
|
|
1050
|
+
schemaId: 3100,
|
|
1051
|
+
name: "device.context",
|
|
1052
|
+
strict: true,
|
|
1053
|
+
maxNestingDepth: 4,
|
|
1054
|
+
fields: [
|
|
1055
|
+
{ tag: 1, name: "deviceId", type: "bytes", required: true, maxLen: 128 },
|
|
1056
|
+
{ tag: 2, name: "os", type: "utf8", required: true, maxLen: 64 },
|
|
1057
|
+
{ tag: 3, name: "hw", type: "utf8", required: true, maxLen: 64 }
|
|
1058
|
+
]
|
|
1059
|
+
};
|
|
1060
|
+
var Schema2001_PasskeyLoginOptionsReq = {
|
|
1061
|
+
schemaId: 2001,
|
|
1062
|
+
name: "axis.auth.passkey.login.options.req",
|
|
1063
|
+
strict: true,
|
|
1064
|
+
maxNestingDepth: 4,
|
|
1065
|
+
fields: [
|
|
1066
|
+
{ tag: 1, name: "username", type: "utf8", required: true, maxLen: 128 }
|
|
1067
|
+
]
|
|
1068
|
+
};
|
|
1069
|
+
var Schema4001_LoginWithDeviceReq = {
|
|
1070
|
+
schemaId: 4001,
|
|
1071
|
+
name: "axis.auth.login.with_device.req",
|
|
1072
|
+
strict: true,
|
|
1073
|
+
maxNestingDepth: 4,
|
|
1074
|
+
fields: [
|
|
1075
|
+
{ tag: 1, name: "username", type: "utf8", required: true, maxLen: 128 },
|
|
1076
|
+
{
|
|
1077
|
+
tag: 2,
|
|
1078
|
+
name: "device",
|
|
1079
|
+
type: "nested",
|
|
1080
|
+
required: true,
|
|
1081
|
+
nestedSchema: Schema3100_DeviceContext
|
|
1082
|
+
}
|
|
1083
|
+
]
|
|
1084
|
+
};
|
|
1085
|
+
|
|
1086
|
+
// src/codec/ats1.passkey.schemas.ts
|
|
1087
|
+
function buildAts1Hdr(params) {
|
|
1088
|
+
const hdr = {
|
|
1089
|
+
intentId: params.intentId,
|
|
1090
|
+
schemaId: params.schemaId,
|
|
1091
|
+
actorKeyId: params.actorKeyId ?? Buffer.alloc(0),
|
|
1092
|
+
capsuleId: params.capsuleId,
|
|
1093
|
+
nonce: params.nonce ?? __require("crypto").randomBytes(16),
|
|
1094
|
+
tsMs: params.tsMs ?? BigInt(Date.now()),
|
|
1095
|
+
bodyHash: params.bodyHash ?? Buffer.alloc(32),
|
|
1096
|
+
traceId: params.traceId
|
|
1097
|
+
};
|
|
1098
|
+
const tlvs = encodeAxisHeaderToTLVs(hdr);
|
|
1099
|
+
return encodeTLVStreamCanonical(tlvs);
|
|
1100
|
+
}
|
|
1101
|
+
function packPasskeyLoginOptionsReq(params) {
|
|
1102
|
+
const bodyTlvs = logicalBodyToTLVs(
|
|
1103
|
+
Schema2001_PasskeyLoginOptionsReq,
|
|
1104
|
+
{
|
|
1105
|
+
schemaId: ATS1_SCHEMA.PASSKEY_LOGIN_OPTIONS_REQ,
|
|
1106
|
+
fields: { username: params.username }
|
|
1107
|
+
}
|
|
1108
|
+
);
|
|
1109
|
+
const body = encodeTLVStreamCanonical(bodyTlvs);
|
|
1110
|
+
const bodyHash = sha2562(body);
|
|
1111
|
+
const hdr = buildAts1Hdr({
|
|
1112
|
+
intentId: params.intentId,
|
|
1113
|
+
schemaId: ATS1_SCHEMA.PASSKEY_LOGIN_OPTIONS_REQ,
|
|
1114
|
+
actorKeyId: params.actorKeyId,
|
|
1115
|
+
capsuleId: params.capsuleId,
|
|
1116
|
+
traceId: params.traceId,
|
|
1117
|
+
bodyHash
|
|
1118
|
+
});
|
|
1119
|
+
return { hdr, body };
|
|
1120
|
+
}
|
|
1121
|
+
function unpackPasskeyLoginOptionsReq(body) {
|
|
1122
|
+
const tlvs = decodeTLVStream(body);
|
|
1123
|
+
const decoded = tlvsToLogicalBody(
|
|
1124
|
+
Schema2001_PasskeyLoginOptionsReq,
|
|
1125
|
+
tlvs
|
|
1126
|
+
);
|
|
1127
|
+
return { username: decoded.fields.username };
|
|
1128
|
+
}
|
|
1129
|
+
var Schema2021_PasskeyRegisterOptionsReq = {
|
|
1130
|
+
schemaId: ATS1_SCHEMA.PASSKEY_REGISTER_OPTIONS_REQ,
|
|
1131
|
+
name: "axis.auth.passkey.register.options.req",
|
|
1132
|
+
strict: true,
|
|
1133
|
+
maxNestingDepth: 4,
|
|
1134
|
+
fields: [
|
|
1135
|
+
{ tag: 1, name: "username", type: "utf8", required: true, maxLen: 128 }
|
|
1136
|
+
]
|
|
1137
|
+
};
|
|
1138
|
+
var Schema2011_PasskeyLoginVerifyReq = {
|
|
1139
|
+
schemaId: ATS1_SCHEMA.PASSKEY_LOGIN_VERIFY_REQ,
|
|
1140
|
+
name: "axis.auth.passkey.login.verify.req",
|
|
1141
|
+
strict: true,
|
|
1142
|
+
maxNestingDepth: 4,
|
|
1143
|
+
fields: [
|
|
1144
|
+
{ tag: 1, name: "username", type: "utf8", required: true, maxLen: 128 },
|
|
1145
|
+
{
|
|
1146
|
+
tag: 2,
|
|
1147
|
+
name: "credentialId",
|
|
1148
|
+
type: "bytes",
|
|
1149
|
+
required: true,
|
|
1150
|
+
maxLen: 1024
|
|
1151
|
+
},
|
|
1152
|
+
{
|
|
1153
|
+
tag: 3,
|
|
1154
|
+
name: "clientDataJSON",
|
|
1155
|
+
type: "bytes",
|
|
1156
|
+
required: true,
|
|
1157
|
+
maxLen: 4096
|
|
1158
|
+
},
|
|
1159
|
+
{
|
|
1160
|
+
tag: 4,
|
|
1161
|
+
name: "authenticatorData",
|
|
1162
|
+
type: "bytes",
|
|
1163
|
+
required: true,
|
|
1164
|
+
maxLen: 1024
|
|
1165
|
+
},
|
|
1166
|
+
{ tag: 5, name: "signature", type: "bytes", required: true, maxLen: 1024 },
|
|
1167
|
+
{ tag: 6, name: "userHandle", type: "bytes", required: false, maxLen: 128 }
|
|
1168
|
+
]
|
|
1169
|
+
};
|
|
1170
|
+
function packPasskeyRegisterOptionsReq(params) {
|
|
1171
|
+
const bodyTlvs = logicalBodyToTLVs(
|
|
1172
|
+
Schema2021_PasskeyRegisterOptionsReq,
|
|
1173
|
+
{
|
|
1174
|
+
schemaId: ATS1_SCHEMA.PASSKEY_REGISTER_OPTIONS_REQ,
|
|
1175
|
+
fields: { username: params.username }
|
|
1176
|
+
}
|
|
1177
|
+
);
|
|
1178
|
+
const body = encodeTLVStreamCanonical(bodyTlvs);
|
|
1179
|
+
const bodyHash = sha2562(body);
|
|
1180
|
+
const hdr = buildAts1Hdr({
|
|
1181
|
+
intentId: params.intentId,
|
|
1182
|
+
schemaId: ATS1_SCHEMA.PASSKEY_REGISTER_OPTIONS_REQ,
|
|
1183
|
+
actorKeyId: params.actorKeyId,
|
|
1184
|
+
traceId: params.traceId,
|
|
1185
|
+
bodyHash
|
|
1186
|
+
});
|
|
1187
|
+
return { hdr, body };
|
|
1188
|
+
}
|
|
1189
|
+
function unpackPasskeyRegisterOptionsReq(body) {
|
|
1190
|
+
const tlvs = decodeTLVStream(body);
|
|
1191
|
+
const decoded = tlvsToLogicalBody(
|
|
1192
|
+
Schema2021_PasskeyRegisterOptionsReq,
|
|
1193
|
+
tlvs
|
|
1194
|
+
);
|
|
1195
|
+
return { username: decoded.fields.username };
|
|
1196
|
+
}
|
|
1197
|
+
function packPasskeyLoginVerifyReq(params) {
|
|
1198
|
+
const bodyTlvs = logicalBodyToTLVs(Schema2011_PasskeyLoginVerifyReq, {
|
|
1199
|
+
schemaId: ATS1_SCHEMA.PASSKEY_LOGIN_VERIFY_REQ,
|
|
1200
|
+
fields: {
|
|
1201
|
+
username: params.username,
|
|
1202
|
+
credentialId: params.credentialId,
|
|
1203
|
+
clientDataJSON: params.clientDataJSON,
|
|
1204
|
+
authenticatorData: params.authenticatorData,
|
|
1205
|
+
signature: params.signature,
|
|
1206
|
+
userHandle: params.userHandle
|
|
1207
|
+
}
|
|
1208
|
+
});
|
|
1209
|
+
const body = encodeTLVStreamCanonical(bodyTlvs);
|
|
1210
|
+
const bodyHash = sha2562(body);
|
|
1211
|
+
const hdr = buildAts1Hdr({
|
|
1212
|
+
intentId: params.intentId,
|
|
1213
|
+
schemaId: ATS1_SCHEMA.PASSKEY_LOGIN_VERIFY_REQ,
|
|
1214
|
+
actorKeyId: params.actorKeyId,
|
|
1215
|
+
traceId: params.traceId,
|
|
1216
|
+
bodyHash
|
|
1217
|
+
});
|
|
1218
|
+
return { hdr, body };
|
|
1219
|
+
}
|
|
1220
|
+
function unpackPasskeyLoginVerifyReq(body) {
|
|
1221
|
+
const tlvs = decodeTLVStream(body);
|
|
1222
|
+
const decoded = tlvsToLogicalBody(
|
|
1223
|
+
Schema2011_PasskeyLoginVerifyReq,
|
|
1224
|
+
tlvs
|
|
1225
|
+
);
|
|
1226
|
+
const f = decoded.fields;
|
|
1227
|
+
return {
|
|
1228
|
+
username: f.username,
|
|
1229
|
+
credentialId: f.credentialId,
|
|
1230
|
+
clientDataJSON: f.clientDataJSON,
|
|
1231
|
+
authenticatorData: f.authenticatorData,
|
|
1232
|
+
signature: f.signature,
|
|
1233
|
+
userHandle: f.userHandle
|
|
1234
|
+
};
|
|
1235
|
+
}
|
|
1236
|
+
var Schema2002_PasskeyLoginOptionsRes = {
|
|
1237
|
+
schemaId: ATS1_SCHEMA.PASSKEY_LOGIN_OPTIONS_RES,
|
|
1238
|
+
name: "axis.auth.passkey.login.options.res",
|
|
1239
|
+
strict: false,
|
|
1240
|
+
// allow extra fields from WebAuthn library
|
|
1241
|
+
maxNestingDepth: 4,
|
|
1242
|
+
fields: [
|
|
1243
|
+
{ tag: 1, name: "challenge", type: "utf8", required: true },
|
|
1244
|
+
// base64url string
|
|
1245
|
+
{ tag: 2, name: "timeout", type: "uvarint", required: false },
|
|
1246
|
+
{ tag: 3, name: "rpId", type: "utf8", required: false },
|
|
1247
|
+
{ tag: 4, name: "userVerification", type: "utf8", required: false },
|
|
1248
|
+
{ tag: 5, name: "allowCredentialsJson", type: "utf8", required: false }
|
|
1249
|
+
// JSON array for simplicity
|
|
1250
|
+
]
|
|
1251
|
+
};
|
|
1252
|
+
function packPasskeyLoginOptionsRes(params) {
|
|
1253
|
+
const fields = {
|
|
1254
|
+
challenge: params.challenge
|
|
1255
|
+
};
|
|
1256
|
+
if (params.timeout !== void 0) fields.timeout = params.timeout;
|
|
1257
|
+
if (params.rpId) fields.rpId = params.rpId;
|
|
1258
|
+
if (params.userVerification)
|
|
1259
|
+
fields.userVerification = params.userVerification;
|
|
1260
|
+
if (params.allowCredentials)
|
|
1261
|
+
fields.allowCredentialsJson = JSON.stringify(params.allowCredentials);
|
|
1262
|
+
const bodyTlvs = logicalBodyToTLVs(Schema2002_PasskeyLoginOptionsRes, {
|
|
1263
|
+
schemaId: ATS1_SCHEMA.PASSKEY_LOGIN_OPTIONS_RES,
|
|
1264
|
+
fields
|
|
1265
|
+
});
|
|
1266
|
+
return encodeTLVStreamCanonical(bodyTlvs);
|
|
1267
|
+
}
|
|
1268
|
+
var Schema2012_PasskeyLoginVerifyRes = {
|
|
1269
|
+
schemaId: ATS1_SCHEMA.PASSKEY_LOGIN_VERIFY_RES,
|
|
1270
|
+
name: "axis.auth.passkey.login.verify.res",
|
|
1271
|
+
strict: true,
|
|
1272
|
+
maxNestingDepth: 4,
|
|
1273
|
+
fields: [
|
|
1274
|
+
{ tag: 1, name: "actorId", type: "utf8", required: true, maxLen: 256 },
|
|
1275
|
+
{ tag: 2, name: "keyId", type: "utf8", required: true, maxLen: 256 },
|
|
1276
|
+
{ tag: 3, name: "capsule", type: "bytes", required: true, maxLen: 4096 },
|
|
1277
|
+
{ tag: 4, name: "expiresAt", type: "u64be", required: true }
|
|
1278
|
+
]
|
|
1279
|
+
};
|
|
1280
|
+
function packPasskeyLoginVerifyRes(params) {
|
|
1281
|
+
const bodyTlvs = logicalBodyToTLVs(Schema2012_PasskeyLoginVerifyRes, {
|
|
1282
|
+
schemaId: ATS1_SCHEMA.PASSKEY_LOGIN_VERIFY_RES,
|
|
1283
|
+
fields: {
|
|
1284
|
+
actorId: params.actorId,
|
|
1285
|
+
keyId: params.keyId,
|
|
1286
|
+
capsule: params.capsule,
|
|
1287
|
+
expiresAt: params.expiresAt
|
|
1288
|
+
}
|
|
1289
|
+
});
|
|
1290
|
+
return encodeTLVStreamCanonical(bodyTlvs);
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
// src/codec/tlv.encode.ts
|
|
1294
|
+
import { randomBytes as randomBytes2 } from "crypto";
|
|
1295
|
+
function encVarint(x) {
|
|
1296
|
+
if (x < 0n) throw new Error("VARINT_NEG");
|
|
1297
|
+
const out = [];
|
|
1298
|
+
while (x >= 0x80n) {
|
|
1299
|
+
out.push(Number(x & 0x7fn | 0x80n));
|
|
1300
|
+
x >>= 7n;
|
|
1301
|
+
}
|
|
1302
|
+
out.push(Number(x));
|
|
1303
|
+
return Buffer.from(out);
|
|
1304
|
+
}
|
|
1305
|
+
function varintU(x) {
|
|
1306
|
+
const v = typeof x === "number" ? BigInt(x) : x;
|
|
1307
|
+
return encVarint(v);
|
|
1308
|
+
}
|
|
1309
|
+
function u64be(x) {
|
|
1310
|
+
if (x < 0n) throw new Error("U64_NEG");
|
|
1311
|
+
const b = Buffer.alloc(8);
|
|
1312
|
+
b.writeBigUInt64BE(x, 0);
|
|
1313
|
+
return b;
|
|
1314
|
+
}
|
|
1315
|
+
function utf8(s) {
|
|
1316
|
+
return Buffer.from(s, "utf8");
|
|
1317
|
+
}
|
|
1318
|
+
function bytes(b) {
|
|
1319
|
+
return Buffer.isBuffer(b) ? b : Buffer.from(b);
|
|
1320
|
+
}
|
|
1321
|
+
function nonce16() {
|
|
1322
|
+
return randomBytes2(16);
|
|
1323
|
+
}
|
|
1324
|
+
function tlv(type, value) {
|
|
1325
|
+
if (!Number.isSafeInteger(type) || type < 0) throw new Error("TLV_BAD_TYPE");
|
|
1326
|
+
return Buffer.concat([
|
|
1327
|
+
encVarint(BigInt(type)),
|
|
1328
|
+
encVarint(BigInt(value.length)),
|
|
1329
|
+
value
|
|
1330
|
+
]);
|
|
1331
|
+
}
|
|
1332
|
+
function buildTLVs(items, opts) {
|
|
1333
|
+
const allow = opts?.allowDupTypes ?? /* @__PURE__ */ new Set();
|
|
1334
|
+
const sorted = [...items].sort((a, b) => a.type - b.type);
|
|
1335
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
1336
|
+
if (sorted[i].type === sorted[i - 1].type && !allow.has(sorted[i].type)) {
|
|
1337
|
+
throw new Error(`TLV_DUP_TYPE_${sorted[i].type}`);
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
return Buffer.concat(sorted.map((it) => tlv(it.type, it.value)));
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
// src/codec/axis1.encode.ts
|
|
1344
|
+
var MAGIC = Buffer.from("AXIS1", "ascii");
|
|
1345
|
+
function encodeAxis1Frame(f) {
|
|
1346
|
+
if (!Buffer.isBuffer(f.hdr) || !Buffer.isBuffer(f.body) || !Buffer.isBuffer(f.sig)) {
|
|
1347
|
+
throw new Error("AXIS1_BAD_BUFFERS");
|
|
1348
|
+
}
|
|
1349
|
+
if (f.ver !== 1) throw new Error("AXIS1_BAD_VER");
|
|
1350
|
+
const hdrLen = encVarint(BigInt(f.hdr.length));
|
|
1351
|
+
const bodyLen = encVarint(BigInt(f.body.length));
|
|
1352
|
+
const sigLen = encVarint(BigInt(f.sig.length));
|
|
1353
|
+
return Buffer.concat([
|
|
1354
|
+
MAGIC,
|
|
1355
|
+
Buffer.from([f.ver & 255]),
|
|
1356
|
+
Buffer.from([f.flags & 255]),
|
|
1357
|
+
hdrLen,
|
|
1358
|
+
bodyLen,
|
|
1359
|
+
sigLen,
|
|
1360
|
+
f.hdr,
|
|
1361
|
+
f.body,
|
|
1362
|
+
f.sig
|
|
1363
|
+
]);
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
// src/codec/axis1.signing.ts
|
|
1367
|
+
var MAGIC2 = Buffer.from("AXIS1", "ascii");
|
|
1368
|
+
function axis1SigningBytes(params) {
|
|
1369
|
+
if (params.ver !== 1) throw new Error("AXIS1_BAD_VER");
|
|
1370
|
+
const hdrLen = encVarint(BigInt(params.hdr.length));
|
|
1371
|
+
const bodyLen = encVarint(BigInt(params.body.length));
|
|
1372
|
+
const sigLenZero = encVarint(0n);
|
|
1373
|
+
return Buffer.concat([
|
|
1374
|
+
MAGIC2,
|
|
1375
|
+
Buffer.from([params.ver & 255]),
|
|
1376
|
+
Buffer.from([params.flags & 255]),
|
|
1377
|
+
hdrLen,
|
|
1378
|
+
bodyLen,
|
|
1379
|
+
sigLenZero,
|
|
1380
|
+
params.hdr,
|
|
1381
|
+
params.body
|
|
1382
|
+
]);
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
// src/crypto/b64url.ts
|
|
1386
|
+
function b64urlEncode(buf) {
|
|
1387
|
+
return buf.toString("base64").replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
1388
|
+
}
|
|
1389
|
+
function b64urlDecode(str) {
|
|
1390
|
+
const pad = str.length % 4 ? "=".repeat(4 - str.length % 4) : "";
|
|
1391
|
+
const base64 = (str + pad).replace(/-/g, "+").replace(/_/g, "/");
|
|
1392
|
+
return Buffer.from(base64, "base64");
|
|
1393
|
+
}
|
|
1394
|
+
function b64urlEncodeString(str, encoding = "utf8") {
|
|
1395
|
+
return b64urlEncode(Buffer.from(str, encoding));
|
|
1396
|
+
}
|
|
1397
|
+
function b64urlDecodeString(str, encoding = "utf8") {
|
|
1398
|
+
return b64urlDecode(str).toString(encoding);
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
// src/crypto/canonical-json.ts
|
|
1402
|
+
function sortRec(value) {
|
|
1403
|
+
if (value === null) {
|
|
1404
|
+
return null;
|
|
1405
|
+
}
|
|
1406
|
+
if (value === void 0) {
|
|
1407
|
+
return void 0;
|
|
1408
|
+
}
|
|
1409
|
+
if (Array.isArray(value)) {
|
|
1410
|
+
return value.map(sortRec);
|
|
1411
|
+
}
|
|
1412
|
+
if (typeof value === "object") {
|
|
1413
|
+
const sorted = {};
|
|
1414
|
+
const keys = Object.keys(value).sort();
|
|
1415
|
+
for (const key of keys) {
|
|
1416
|
+
const sortedValue = sortRec(value[key]);
|
|
1417
|
+
if (sortedValue !== void 0) {
|
|
1418
|
+
sorted[key] = sortedValue;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
return sorted;
|
|
1422
|
+
}
|
|
1423
|
+
return value;
|
|
1424
|
+
}
|
|
1425
|
+
function canonicalJson(value) {
|
|
1426
|
+
return JSON.stringify(sortRec(value));
|
|
1427
|
+
}
|
|
1428
|
+
function canonicalJsonExcluding(obj, exclude) {
|
|
1429
|
+
const filtered = {};
|
|
1430
|
+
for (const key in obj) {
|
|
1431
|
+
if (!exclude.includes(key) && obj[key] !== void 0) {
|
|
1432
|
+
filtered[key] = obj[key];
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
return canonicalJson(filtered);
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
// src/contract/execution-meter.ts
|
|
1439
|
+
var ContractViolationError = class extends Error {
|
|
1440
|
+
constructor(code, message) {
|
|
1441
|
+
super(message);
|
|
1442
|
+
this.code = code;
|
|
1443
|
+
this.name = "ContractViolationError";
|
|
1444
|
+
}
|
|
1445
|
+
};
|
|
1446
|
+
var ExecutionMeter = class {
|
|
1447
|
+
// ExecutionContract
|
|
1448
|
+
constructor(contract) {
|
|
1449
|
+
this.dbWrites = 0;
|
|
1450
|
+
this.dbReads = 0;
|
|
1451
|
+
this.externalCalls = 0;
|
|
1452
|
+
this.contract = contract;
|
|
1453
|
+
this.startTime = Date.now();
|
|
1454
|
+
}
|
|
1455
|
+
recordDbWrite() {
|
|
1456
|
+
this.dbWrites++;
|
|
1457
|
+
if (this.dbWrites > this.contract.maxDbWrites) {
|
|
1458
|
+
throw new ContractViolationError(
|
|
1459
|
+
"MAX_DB_WRITES_EXCEEDED",
|
|
1460
|
+
`DB writes exceeded: ${this.dbWrites}/${this.contract.maxDbWrites}`
|
|
1461
|
+
);
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
recordDbRead() {
|
|
1465
|
+
this.dbReads++;
|
|
1466
|
+
if (this.contract.maxDbReads && this.dbReads > this.contract.maxDbReads) {
|
|
1467
|
+
throw new ContractViolationError(
|
|
1468
|
+
"MAX_DB_READS_EXCEEDED",
|
|
1469
|
+
`DB reads exceeded: ${this.dbReads}/${this.contract.maxDbReads}`
|
|
1470
|
+
);
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
recordExternalCall() {
|
|
1474
|
+
this.externalCalls++;
|
|
1475
|
+
if (this.externalCalls > this.contract.maxExternalCalls) {
|
|
1476
|
+
throw new ContractViolationError(
|
|
1477
|
+
"MAX_EXTERNAL_CALLS_EXCEEDED",
|
|
1478
|
+
`External calls exceeded: ${this.externalCalls}/${this.contract.maxExternalCalls}`
|
|
1479
|
+
);
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
checkTime() {
|
|
1483
|
+
const elapsed = Date.now() - this.startTime;
|
|
1484
|
+
if (elapsed > this.contract.maxTimeMs) {
|
|
1485
|
+
throw new ContractViolationError(
|
|
1486
|
+
"MAX_TIME_EXCEEDED",
|
|
1487
|
+
`Execution time exceeded: ${elapsed}ms/${this.contract.maxTimeMs}ms`
|
|
1488
|
+
);
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
validateEffect(effect) {
|
|
1492
|
+
if (this.contract.allowedEffects.includes("*")) {
|
|
1493
|
+
return;
|
|
1494
|
+
}
|
|
1495
|
+
if (!this.contract.allowedEffects.includes(effect)) {
|
|
1496
|
+
throw new ContractViolationError(
|
|
1497
|
+
"INVALID_EFFECT",
|
|
1498
|
+
`Effect '${effect}' not allowed. Allowed: ${this.contract.allowedEffects.join(", ")}`
|
|
1499
|
+
);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
getMetrics() {
|
|
1503
|
+
return {
|
|
1504
|
+
dbWrites: this.dbWrites,
|
|
1505
|
+
dbReads: this.dbReads,
|
|
1506
|
+
externalCalls: this.externalCalls,
|
|
1507
|
+
elapsedMs: Date.now() - this.startTime
|
|
1508
|
+
};
|
|
1509
|
+
}
|
|
1510
|
+
getContract() {
|
|
1511
|
+
return this.contract;
|
|
1512
|
+
}
|
|
1513
|
+
};
|
|
1514
|
+
|
|
1515
|
+
// src/contract/contract.interface.ts
|
|
1516
|
+
var DEFAULT_CONTRACTS = {
|
|
1517
|
+
// System intents
|
|
1518
|
+
"system.ping": {
|
|
1519
|
+
maxDbWrites: 0,
|
|
1520
|
+
maxExternalCalls: 0,
|
|
1521
|
+
maxTimeMs: 100,
|
|
1522
|
+
allowedEffects: ["system.pong"]
|
|
1523
|
+
},
|
|
1524
|
+
// Catalog intents
|
|
1525
|
+
"catalog.list": {
|
|
1526
|
+
maxDbWrites: 0,
|
|
1527
|
+
maxExternalCalls: 0,
|
|
1528
|
+
maxTimeMs: 200,
|
|
1529
|
+
allowedEffects: ["catalog.listed"]
|
|
1530
|
+
},
|
|
1531
|
+
"catalog.search": {
|
|
1532
|
+
maxDbWrites: 0,
|
|
1533
|
+
maxExternalCalls: 0,
|
|
1534
|
+
maxTimeMs: 300,
|
|
1535
|
+
allowedEffects: ["catalog.searched"]
|
|
1536
|
+
},
|
|
1537
|
+
// Passport intents
|
|
1538
|
+
"passport.issue": {
|
|
1539
|
+
maxDbWrites: 10,
|
|
1540
|
+
maxExternalCalls: 0,
|
|
1541
|
+
maxTimeMs: 500,
|
|
1542
|
+
allowedEffects: ["passport.issued", "passport.rejected"]
|
|
1543
|
+
},
|
|
1544
|
+
"passport.revoke": {
|
|
1545
|
+
maxDbWrites: 5,
|
|
1546
|
+
maxExternalCalls: 0,
|
|
1547
|
+
maxTimeMs: 300,
|
|
1548
|
+
allowedEffects: ["passport.revoked", "passport.revoke_failed"]
|
|
1549
|
+
},
|
|
1550
|
+
// File intents
|
|
1551
|
+
"file.init": {
|
|
1552
|
+
maxDbWrites: 2,
|
|
1553
|
+
maxExternalCalls: 0,
|
|
1554
|
+
maxTimeMs: 200,
|
|
1555
|
+
allowedEffects: ["file.initialized"]
|
|
1556
|
+
},
|
|
1557
|
+
"file.chunk": {
|
|
1558
|
+
maxDbWrites: 2,
|
|
1559
|
+
maxExternalCalls: 0,
|
|
1560
|
+
maxTimeMs: 1e3,
|
|
1561
|
+
allowedEffects: ["file.chunk.stored"]
|
|
1562
|
+
},
|
|
1563
|
+
"file.finalize": {
|
|
1564
|
+
maxDbWrites: 2,
|
|
1565
|
+
maxExternalCalls: 0,
|
|
1566
|
+
maxTimeMs: 500,
|
|
1567
|
+
allowedEffects: ["file.finalized"]
|
|
1568
|
+
},
|
|
1569
|
+
// Stream intents
|
|
1570
|
+
"stream.publish": {
|
|
1571
|
+
maxDbWrites: 1,
|
|
1572
|
+
maxExternalCalls: 0,
|
|
1573
|
+
maxTimeMs: 200,
|
|
1574
|
+
allowedEffects: ["stream.published"]
|
|
1575
|
+
},
|
|
1576
|
+
"stream.read": {
|
|
1577
|
+
maxDbWrites: 0,
|
|
1578
|
+
maxExternalCalls: 0,
|
|
1579
|
+
maxTimeMs: 300,
|
|
1580
|
+
allowedEffects: ["stream.data"]
|
|
1581
|
+
},
|
|
1582
|
+
// Mail intents
|
|
1583
|
+
"mail.send": {
|
|
1584
|
+
maxDbWrites: 3,
|
|
1585
|
+
maxExternalCalls: 1,
|
|
1586
|
+
// Email service
|
|
1587
|
+
maxTimeMs: 2e3,
|
|
1588
|
+
allowedEffects: ["mail.sent", "mail.failed"]
|
|
1589
|
+
}
|
|
1590
|
+
};
|
|
1591
|
+
var FALLBACK_CONTRACT = {
|
|
1592
|
+
maxDbWrites: 10,
|
|
1593
|
+
maxExternalCalls: 0,
|
|
1594
|
+
maxTimeMs: 1e3,
|
|
1595
|
+
allowedEffects: ["*"]
|
|
1596
|
+
// Allow any effect
|
|
1597
|
+
};
|
|
1598
|
+
|
|
1599
|
+
// src/types/tlv.ts
|
|
1600
|
+
function decVarint(buf, off) {
|
|
1601
|
+
let shift = 0n;
|
|
1602
|
+
let x = 0n;
|
|
1603
|
+
while (true) {
|
|
1604
|
+
if (off >= buf.length) throw new Error("varint overflow");
|
|
1605
|
+
const b = BigInt(buf[off++]);
|
|
1606
|
+
x |= (b & 0x7fn) << shift;
|
|
1607
|
+
if ((b & 0x80n) === 0n) break;
|
|
1608
|
+
shift += 7n;
|
|
1609
|
+
if (shift > 63n) throw new Error("varint too large");
|
|
1610
|
+
}
|
|
1611
|
+
return { val: x, off };
|
|
1612
|
+
}
|
|
1613
|
+
function parseTLVs(buf, maxItems = 512) {
|
|
1614
|
+
const out = [];
|
|
1615
|
+
let off = 0;
|
|
1616
|
+
while (off < buf.length) {
|
|
1617
|
+
if (out.length >= maxItems) throw new Error("TLV_TOO_MANY_ITEMS");
|
|
1618
|
+
const t1 = decVarint(buf, off);
|
|
1619
|
+
off = t1.off;
|
|
1620
|
+
const t2 = decVarint(buf, off);
|
|
1621
|
+
off = t2.off;
|
|
1622
|
+
const type = Number(t1.val);
|
|
1623
|
+
const len = Number(t2.val);
|
|
1624
|
+
if (len < 0 || off + len > buf.length) {
|
|
1625
|
+
throw new Error("TLV_LEN_INVALID");
|
|
1626
|
+
}
|
|
1627
|
+
const value = buf.subarray(off, off + len);
|
|
1628
|
+
off += len;
|
|
1629
|
+
out.push({ type, value });
|
|
1630
|
+
}
|
|
1631
|
+
return out;
|
|
1632
|
+
}
|
|
1633
|
+
function tlvMap(buf) {
|
|
1634
|
+
const m = /* @__PURE__ */ new Map();
|
|
1635
|
+
for (const it of parseTLVs(buf)) {
|
|
1636
|
+
const arr = m.get(it.type) ?? [];
|
|
1637
|
+
arr.push(it.value);
|
|
1638
|
+
m.set(it.type, arr);
|
|
1639
|
+
}
|
|
1640
|
+
return m;
|
|
1641
|
+
}
|
|
1642
|
+
function asUtf8(b) {
|
|
1643
|
+
if (!b) return void 0;
|
|
1644
|
+
return b.toString("utf8");
|
|
1645
|
+
}
|
|
1646
|
+
function asBigintVarint(b) {
|
|
1647
|
+
if (!b) return void 0;
|
|
1648
|
+
const { val, off } = decVarint(b, 0);
|
|
1649
|
+
if (off !== b.length) throw new Error("VARINT_TRAILING_BYTES");
|
|
1650
|
+
return val;
|
|
1651
|
+
}
|
|
1652
|
+
function asBigint64BE(b) {
|
|
1653
|
+
if (!b) return void 0;
|
|
1654
|
+
if (b.length !== 8) throw new Error("Expected 8 bytes for u64");
|
|
1655
|
+
return b.readBigUInt64BE(0);
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
// src/types/frame.ts
|
|
1659
|
+
var MAGIC3 = Buffer.from("AXIS1", "ascii");
|
|
1660
|
+
function decodeAxis1Frame(buf) {
|
|
1661
|
+
let off = 0;
|
|
1662
|
+
const magic = buf.subarray(off, off + 5);
|
|
1663
|
+
off += 5;
|
|
1664
|
+
if (magic.length !== 5 || !magic.equals(MAGIC3))
|
|
1665
|
+
throw new Error("AXIS1_BAD_MAGIC");
|
|
1666
|
+
if (off + 2 > buf.length) throw new Error("AXIS1_TRUNCATED");
|
|
1667
|
+
const ver = buf[off++];
|
|
1668
|
+
const flags = buf[off++];
|
|
1669
|
+
const h1 = decVarint(buf, off);
|
|
1670
|
+
off = h1.off;
|
|
1671
|
+
const b1 = decVarint(buf, off);
|
|
1672
|
+
off = b1.off;
|
|
1673
|
+
const s1 = decVarint(buf, off);
|
|
1674
|
+
off = s1.off;
|
|
1675
|
+
const hdrLen = Number(h1.val);
|
|
1676
|
+
const bodyLen = Number(b1.val);
|
|
1677
|
+
const sigLen = Number(s1.val);
|
|
1678
|
+
if (hdrLen < 0 || bodyLen < 0 || sigLen < 0) throw new Error("AXIS1_LEN_NEG");
|
|
1679
|
+
if (off + hdrLen + bodyLen + sigLen > buf.length)
|
|
1680
|
+
throw new Error("AXIS1_TRUNCATED_PAYLOAD");
|
|
1681
|
+
const hdr = buf.subarray(off, off + hdrLen);
|
|
1682
|
+
off += hdrLen;
|
|
1683
|
+
const body = buf.subarray(off, off + bodyLen);
|
|
1684
|
+
off += bodyLen;
|
|
1685
|
+
const sig = buf.subarray(off, off + sigLen);
|
|
1686
|
+
off += sigLen;
|
|
1687
|
+
if (off !== buf.length) throw new Error("AXIS1_TRAILING_BYTES");
|
|
1688
|
+
return { ver, flags, hdr, body, sig, frameSize: buf.length };
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
// src/types/packet.ts
|
|
1692
|
+
var T = {
|
|
1693
|
+
/** The specific intent or action (e.g., 'vault.create') */
|
|
1694
|
+
INTENT: TLV_INTENT,
|
|
1695
|
+
/** Package identifier / ID */
|
|
1696
|
+
PID: TLV_PID,
|
|
1697
|
+
/** Versioning of the intent schema */
|
|
1698
|
+
INTENT_VERSION: 10,
|
|
1699
|
+
// Defaulting to TRACE_ID for now or a new tag if available
|
|
1700
|
+
/** Unique identifier for the requesting actor */
|
|
1701
|
+
ACTOR_ID: TLV_ACTOR_ID,
|
|
1702
|
+
/** Optional Capability Token identifier (16 bytes) */
|
|
1703
|
+
CAPSULE_ID: TLV_PROOF_REF,
|
|
1704
|
+
/** Unique session/request identifier (16 bytes) */
|
|
1705
|
+
NONCE: TLV_NONCE,
|
|
1706
|
+
/** High-precision Unix timestamp in milliseconds */
|
|
1707
|
+
TS_MS: TLV_TS,
|
|
1708
|
+
/** Proof type */
|
|
1709
|
+
PROOF_TYPE: TLV_PROOF_TYPE,
|
|
1710
|
+
/** Standard binary body tag */
|
|
1711
|
+
BODY: 100,
|
|
1712
|
+
/** Standard JSON-encoded body tag */
|
|
1713
|
+
JSON: 200
|
|
1714
|
+
};
|
|
1715
|
+
function buildPacket(hdr, body, sig, flags = 0) {
|
|
1716
|
+
const hm = tlvMap(hdr);
|
|
1717
|
+
const BODY_IS_TLV = 1;
|
|
1718
|
+
const bm = flags & BODY_IS_TLV ? tlvMap(body) : /* @__PURE__ */ new Map();
|
|
1719
|
+
const intent = asUtf8(hm.get(T.INTENT)?.[0]);
|
|
1720
|
+
const intentVerRaw = hm.get(T.INTENT_VERSION)?.[0];
|
|
1721
|
+
const intentVer = intentVerRaw ? Number(asBigintVarint(intentVerRaw)) : 1;
|
|
1722
|
+
const actorIdRaw = hm.get(T.ACTOR_ID)?.[0];
|
|
1723
|
+
const actorId = actorIdRaw ? actorIdRaw.toString("hex") : void 0;
|
|
1724
|
+
const capsuleId = hm.get(T.CAPSULE_ID)?.[0];
|
|
1725
|
+
const pid = hm.get(T.PID)?.[0] || hm.get(T.NONCE)?.[0];
|
|
1726
|
+
const nonce = hm.get(T.NONCE)?.[0];
|
|
1727
|
+
const tsMs = asBigint64BE(hm.get(T.TS_MS)?.[0]);
|
|
1728
|
+
if (!intent) throw new Error("PACKET_MISSING_INTENT");
|
|
1729
|
+
if (!actorId) throw new Error("PACKET_MISSING_ACTOR_ID");
|
|
1730
|
+
if (!nonce || nonce.length < 16 || nonce.length > 32)
|
|
1731
|
+
throw new Error("PACKET_BAD_NONCE");
|
|
1732
|
+
if (!pid) throw new Error("PACKET_MISSING_PID");
|
|
1733
|
+
if (!tsMs) throw new Error("PACKET_MISSING_TS");
|
|
1734
|
+
return {
|
|
1735
|
+
intent,
|
|
1736
|
+
intentVersion: intentVer,
|
|
1737
|
+
actorId,
|
|
1738
|
+
capsuleId,
|
|
1739
|
+
pid,
|
|
1740
|
+
nonce,
|
|
1741
|
+
tsMs,
|
|
1742
|
+
headersMap: hm,
|
|
1743
|
+
bodyMap: bm,
|
|
1744
|
+
hdrBytes: hdr,
|
|
1745
|
+
bodyBytes: body,
|
|
1746
|
+
sig
|
|
1747
|
+
};
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
// src/sensor/axis-sensor.ts
|
|
1751
|
+
var Decision = /* @__PURE__ */ ((Decision2) => {
|
|
1752
|
+
Decision2["ALLOW"] = "ALLOW";
|
|
1753
|
+
Decision2["DENY"] = "DENY";
|
|
1754
|
+
Decision2["THROTTLE"] = "THROTTLE";
|
|
1755
|
+
Decision2["FLAG"] = "FLAG";
|
|
1756
|
+
return Decision2;
|
|
1757
|
+
})(Decision || {});
|
|
1758
|
+
function normalizeSensorDecision(sensorDecision) {
|
|
1759
|
+
if ("action" in sensorDecision) {
|
|
1760
|
+
switch (sensorDecision.action) {
|
|
1761
|
+
case "ALLOW":
|
|
1762
|
+
return {
|
|
1763
|
+
allow: true,
|
|
1764
|
+
riskScore: 0,
|
|
1765
|
+
reasons: [],
|
|
1766
|
+
meta: sensorDecision.meta
|
|
1767
|
+
};
|
|
1768
|
+
case "DENY":
|
|
1769
|
+
return {
|
|
1770
|
+
allow: false,
|
|
1771
|
+
riskScore: 100,
|
|
1772
|
+
reasons: [sensorDecision.code, sensorDecision.reason].filter(
|
|
1773
|
+
Boolean
|
|
1774
|
+
),
|
|
1775
|
+
meta: sensorDecision.meta,
|
|
1776
|
+
retryAfterMs: sensorDecision.retryAfterMs
|
|
1777
|
+
};
|
|
1778
|
+
case "THROTTLE":
|
|
1779
|
+
return {
|
|
1780
|
+
allow: false,
|
|
1781
|
+
riskScore: 50,
|
|
1782
|
+
reasons: ["RATE_LIMIT"],
|
|
1783
|
+
retryAfterMs: sensorDecision.retryAfterMs,
|
|
1784
|
+
meta: sensorDecision.meta
|
|
1785
|
+
};
|
|
1786
|
+
case "FLAG":
|
|
1787
|
+
return {
|
|
1788
|
+
allow: true,
|
|
1789
|
+
riskScore: sensorDecision.scoreDelta,
|
|
1790
|
+
reasons: sensorDecision.reasons,
|
|
1791
|
+
meta: sensorDecision.meta
|
|
1792
|
+
};
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
return {
|
|
1796
|
+
allow: sensorDecision.allow,
|
|
1797
|
+
riskScore: sensorDecision.riskScore,
|
|
1798
|
+
reasons: sensorDecision.reasons,
|
|
1799
|
+
tags: sensorDecision.tags,
|
|
1800
|
+
meta: sensorDecision.meta,
|
|
1801
|
+
tighten: sensorDecision.tighten,
|
|
1802
|
+
retryAfterMs: sensorDecision.retryAfterMs
|
|
1803
|
+
};
|
|
1804
|
+
}
|
|
1805
|
+
var SensorDecisions = {
|
|
1806
|
+
allow(meta, tags) {
|
|
1807
|
+
return {
|
|
1808
|
+
decision: "ALLOW" /* ALLOW */,
|
|
1809
|
+
allow: true,
|
|
1810
|
+
riskScore: 0,
|
|
1811
|
+
reasons: [],
|
|
1812
|
+
tags,
|
|
1813
|
+
meta
|
|
1814
|
+
};
|
|
1815
|
+
},
|
|
1816
|
+
deny(code, reason, meta) {
|
|
1817
|
+
return {
|
|
1818
|
+
decision: "DENY" /* DENY */,
|
|
1819
|
+
allow: false,
|
|
1820
|
+
riskScore: 100,
|
|
1821
|
+
code,
|
|
1822
|
+
reasons: [code, reason].filter(Boolean),
|
|
1823
|
+
meta
|
|
1824
|
+
};
|
|
1825
|
+
},
|
|
1826
|
+
throttle(retryAfterMs, meta) {
|
|
1827
|
+
return {
|
|
1828
|
+
decision: "THROTTLE" /* THROTTLE */,
|
|
1829
|
+
allow: false,
|
|
1830
|
+
riskScore: 50,
|
|
1831
|
+
retryAfterMs,
|
|
1832
|
+
code: "RATE_LIMIT",
|
|
1833
|
+
reasons: ["RATE_LIMIT"],
|
|
1834
|
+
meta
|
|
1835
|
+
};
|
|
1836
|
+
},
|
|
1837
|
+
flag(scoreDelta, reasons, meta) {
|
|
1838
|
+
return {
|
|
1839
|
+
decision: "FLAG" /* FLAG */,
|
|
1840
|
+
allow: true,
|
|
1841
|
+
riskScore: scoreDelta,
|
|
1842
|
+
scoreDelta,
|
|
1843
|
+
reasons,
|
|
1844
|
+
meta
|
|
1845
|
+
};
|
|
1846
|
+
}
|
|
1847
|
+
};
|
|
589
1848
|
export {
|
|
1849
|
+
ATS1_HDR,
|
|
1850
|
+
ATS1_SCHEMA,
|
|
590
1851
|
AXIS_MAGIC,
|
|
591
1852
|
AXIS_VERSION,
|
|
1853
|
+
ats1_exports as Ats1Codec,
|
|
592
1854
|
AxisFrameZ,
|
|
1855
|
+
T as AxisPacketTags,
|
|
1856
|
+
ContractViolationError,
|
|
1857
|
+
DEFAULT_CONTRACTS,
|
|
1858
|
+
Decision,
|
|
593
1859
|
ERR_BAD_SIGNATURE,
|
|
594
1860
|
ERR_CONTRACT_VIOLATION,
|
|
595
1861
|
ERR_INVALID_PACKET,
|
|
596
1862
|
ERR_REPLAY_DETECTED,
|
|
1863
|
+
ExecutionMeter,
|
|
1864
|
+
FALLBACK_CONTRACT,
|
|
597
1865
|
FLAG_BODY_TLV,
|
|
598
1866
|
FLAG_CHAIN_REQ,
|
|
599
1867
|
FLAG_HAS_WITNESS,
|
|
@@ -610,6 +1878,11 @@ export {
|
|
|
610
1878
|
PROOF_JWT,
|
|
611
1879
|
PROOF_LOOM,
|
|
612
1880
|
PROOF_MTLS,
|
|
1881
|
+
Schema2002_PasskeyLoginOptionsRes,
|
|
1882
|
+
Schema2011_PasskeyLoginVerifyReq,
|
|
1883
|
+
Schema2012_PasskeyLoginVerifyRes,
|
|
1884
|
+
Schema2021_PasskeyRegisterOptionsReq,
|
|
1885
|
+
SensorDecisions,
|
|
613
1886
|
TLV_ACTOR_ID,
|
|
614
1887
|
TLV_AUD,
|
|
615
1888
|
TLV_EFFECT,
|
|
@@ -633,22 +1906,50 @@ export {
|
|
|
633
1906
|
TLV_RID,
|
|
634
1907
|
TLV_TRACE_ID,
|
|
635
1908
|
TLV_TS,
|
|
1909
|
+
axis1SigningBytes,
|
|
1910
|
+
b64urlDecode,
|
|
1911
|
+
b64urlDecodeString,
|
|
1912
|
+
b64urlEncode,
|
|
1913
|
+
b64urlEncodeString,
|
|
1914
|
+
buildAts1Hdr,
|
|
1915
|
+
buildPacket,
|
|
1916
|
+
buildTLVs,
|
|
1917
|
+
bytes,
|
|
1918
|
+
canonicalJson,
|
|
1919
|
+
canonicalJsonExcluding,
|
|
636
1920
|
computeReceiptHash,
|
|
637
1921
|
computeSignaturePayload,
|
|
638
1922
|
decodeArray,
|
|
1923
|
+
decodeAxis1Frame,
|
|
639
1924
|
decodeFrame,
|
|
640
1925
|
decodeObject,
|
|
641
1926
|
decodeTLVs,
|
|
642
1927
|
decodeTLVsList,
|
|
643
1928
|
decodeVarint,
|
|
1929
|
+
encVarint,
|
|
1930
|
+
encodeAxis1Frame,
|
|
644
1931
|
encodeFrame,
|
|
645
1932
|
encodeTLVs,
|
|
646
1933
|
encodeVarint,
|
|
647
1934
|
generateEd25519KeyPair,
|
|
648
1935
|
getSignTarget,
|
|
1936
|
+
nonce16,
|
|
1937
|
+
normalizeSensorDecision,
|
|
1938
|
+
packPasskeyLoginOptionsReq,
|
|
1939
|
+
packPasskeyLoginOptionsRes,
|
|
1940
|
+
packPasskeyLoginVerifyReq,
|
|
1941
|
+
packPasskeyLoginVerifyRes,
|
|
1942
|
+
packPasskeyRegisterOptionsReq,
|
|
649
1943
|
sha256,
|
|
650
1944
|
signFrame,
|
|
1945
|
+
tlv,
|
|
1946
|
+
u64be,
|
|
1947
|
+
unpackPasskeyLoginOptionsReq,
|
|
1948
|
+
unpackPasskeyLoginVerifyReq,
|
|
1949
|
+
unpackPasskeyRegisterOptionsReq,
|
|
1950
|
+
utf8,
|
|
651
1951
|
varintLength,
|
|
1952
|
+
varintU,
|
|
652
1953
|
verifyFrameSignature
|
|
653
1954
|
};
|
|
654
1955
|
//# sourceMappingURL=index.mjs.map
|