@bcts/envelope 1.0.0-alpha.12 → 1.0.0-alpha.13
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.cjs +2209 -519
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +839 -283
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +839 -283
- package/dist/index.d.mts.map +1 -1
- package/dist/index.iife.js +2218 -530
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +2104 -524
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -8
- package/src/base/assertions.ts +3 -0
- package/src/base/digest.ts +28 -156
- package/src/base/elide.ts +9 -0
- package/src/base/envelope-decodable.ts +179 -1
- package/src/base/envelope-encodable.ts +5 -1
- package/src/base/envelope.ts +146 -10
- package/src/base/error.ts +15 -2
- package/src/base/index.ts +9 -0
- package/src/base/leaf.ts +57 -0
- package/src/base/queries.ts +93 -0
- package/src/base/ur.ts +43 -0
- package/src/base/walk.ts +73 -0
- package/src/extension/attachment.ts +65 -0
- package/src/extension/encrypt.ts +54 -102
- package/src/extension/event.ts +242 -0
- package/src/extension/expression.ts +529 -82
- package/src/extension/index.ts +68 -0
- package/src/extension/recipient.ts +8 -3
- package/src/extension/request.ts +289 -0
- package/src/extension/response.ts +385 -0
- package/src/extension/salt.ts +90 -0
- package/src/extension/secret.ts +159 -0
- package/src/extension/signature.ts +253 -322
- package/src/extension/sskr.ts +240 -0
- package/src/format/envelope-summary.ts +167 -0
- package/src/format/format-context.ts +202 -0
- package/src/format/index.ts +50 -2
- package/src/format/mermaid.ts +331 -0
- package/src/format/notation.ts +478 -0
- package/src/format/tree.ts +36 -3
- package/src/index.ts +9 -1
- package/src/seal.ts +101 -0
package/dist/index.cjs
CHANGED
|
@@ -25,60 +25,19 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
25
25
|
}) : target, mod));
|
|
26
26
|
|
|
27
27
|
//#endregion
|
|
28
|
-
let
|
|
28
|
+
let _bcts_components = require("@bcts/components");
|
|
29
29
|
let _bcts_dcbor = require("@bcts/dcbor");
|
|
30
30
|
let _bcts_known_values = require("@bcts/known-values");
|
|
31
|
-
let _bcts_components = require("@bcts/components");
|
|
32
31
|
let pako = require("pako");
|
|
33
32
|
pako = __toESM(pako);
|
|
33
|
+
let _bcts_crypto = require("@bcts/crypto");
|
|
34
34
|
let _bcts_rand = require("@bcts/rand");
|
|
35
|
+
let _bcts_uniform_resources = require("@bcts/uniform-resources");
|
|
36
|
+
let _bcts_tags = require("@bcts/tags");
|
|
35
37
|
|
|
36
38
|
//#region src/base/digest.ts
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
constructor(data) {
|
|
40
|
-
if (data.length !== 32) throw new Error(`Digest must be exactly 32 bytes, got ${data.length} bytes`);
|
|
41
|
-
this.#data = data;
|
|
42
|
-
}
|
|
43
|
-
data() {
|
|
44
|
-
return this.#data;
|
|
45
|
-
}
|
|
46
|
-
static fromImage(image) {
|
|
47
|
-
return new Digest((0, _bcts_crypto.sha256)(image));
|
|
48
|
-
}
|
|
49
|
-
static fromDigests(digests) {
|
|
50
|
-
const totalLength = digests.length * 32;
|
|
51
|
-
const combined = new Uint8Array(totalLength);
|
|
52
|
-
let offset = 0;
|
|
53
|
-
for (const digest of digests) {
|
|
54
|
-
combined.set(digest.data(), offset);
|
|
55
|
-
offset += 32;
|
|
56
|
-
}
|
|
57
|
-
return Digest.fromImage(combined);
|
|
58
|
-
}
|
|
59
|
-
hex() {
|
|
60
|
-
return Array.from(this.#data).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
61
|
-
}
|
|
62
|
-
short() {
|
|
63
|
-
return this.hex().substring(0, 7);
|
|
64
|
-
}
|
|
65
|
-
static fromHex(hex) {
|
|
66
|
-
if (hex.length !== 64) throw new Error(`Hex string must be exactly 64 characters, got ${hex.length}`);
|
|
67
|
-
const data = new Uint8Array(32);
|
|
68
|
-
for (let i = 0; i < 32; i++) data[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);
|
|
69
|
-
return new Digest(data);
|
|
70
|
-
}
|
|
71
|
-
equals(other) {
|
|
72
|
-
if (this.#data.length !== other.#data.length) return false;
|
|
73
|
-
for (let i = 0; i < this.#data.length; i++) if (this.#data[i] !== other.#data[i]) return false;
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
toString() {
|
|
77
|
-
return this.short();
|
|
78
|
-
}
|
|
79
|
-
clone() {
|
|
80
|
-
return new Digest(new Uint8Array(this.#data));
|
|
81
|
-
}
|
|
39
|
+
_bcts_components.Digest.prototype.short = function() {
|
|
40
|
+
return this.hex().slice(0, 7);
|
|
82
41
|
};
|
|
83
42
|
|
|
84
43
|
//#endregion
|
|
@@ -113,6 +72,7 @@ let ErrorCode = /* @__PURE__ */ function(ErrorCode$1) {
|
|
|
113
72
|
ErrorCode$1["SSKR"] = "SSKR";
|
|
114
73
|
ErrorCode$1["INVALID_TYPE"] = "INVALID_TYPE";
|
|
115
74
|
ErrorCode$1["AMBIGUOUS_TYPE"] = "AMBIGUOUS_TYPE";
|
|
75
|
+
ErrorCode$1["SUBJECT_NOT_UNIT"] = "SUBJECT_NOT_UNIT";
|
|
116
76
|
ErrorCode$1["UNEXPECTED_RESPONSE_ID"] = "UNEXPECTED_RESPONSE_ID";
|
|
117
77
|
ErrorCode$1["INVALID_RESPONSE"] = "INVALID_RESPONSE";
|
|
118
78
|
ErrorCode$1["CBOR"] = "CBOR";
|
|
@@ -159,8 +119,8 @@ var EnvelopeError = class EnvelopeError extends Error {
|
|
|
159
119
|
static invalidAssertion() {
|
|
160
120
|
return new EnvelopeError(ErrorCode.INVALID_ASSERTION, "assertion must be a map with exactly one element");
|
|
161
121
|
}
|
|
162
|
-
static invalidAttachment() {
|
|
163
|
-
return new EnvelopeError(ErrorCode.INVALID_ATTACHMENT, "invalid attachment");
|
|
122
|
+
static invalidAttachment(message) {
|
|
123
|
+
return new EnvelopeError(ErrorCode.INVALID_ATTACHMENT, message !== void 0 ? `invalid attachment: ${message}` : "invalid attachment");
|
|
164
124
|
}
|
|
165
125
|
static nonexistentAttachment() {
|
|
166
126
|
return new EnvelopeError(ErrorCode.NONEXISTENT_ATTACHMENT, "nonexistent attachment");
|
|
@@ -216,6 +176,9 @@ var EnvelopeError = class EnvelopeError extends Error {
|
|
|
216
176
|
static ambiguousType() {
|
|
217
177
|
return new EnvelopeError(ErrorCode.AMBIGUOUS_TYPE, "ambiguous type");
|
|
218
178
|
}
|
|
179
|
+
static subjectNotUnit() {
|
|
180
|
+
return new EnvelopeError(ErrorCode.SUBJECT_NOT_UNIT, "subject is not the unit value");
|
|
181
|
+
}
|
|
219
182
|
static unexpectedResponseId() {
|
|
220
183
|
return new EnvelopeError(ErrorCode.UNEXPECTED_RESPONSE_ID, "unexpected response ID");
|
|
221
184
|
}
|
|
@@ -245,7 +208,7 @@ var Assertion = class Assertion {
|
|
|
245
208
|
constructor(predicate, object) {
|
|
246
209
|
this.#predicate = predicate instanceof Envelope ? predicate : Envelope.new(predicate);
|
|
247
210
|
this.#object = object instanceof Envelope ? object : Envelope.new(object);
|
|
248
|
-
this.#digest = Digest.fromDigests([this.#predicate.digest(), this.#object.digest()]);
|
|
211
|
+
this.#digest = _bcts_components.Digest.fromDigests([this.#predicate.digest(), this.#object.digest()]);
|
|
249
212
|
}
|
|
250
213
|
predicate() {
|
|
251
214
|
return this.#predicate;
|
|
@@ -352,41 +315,30 @@ registerCompressExtension();
|
|
|
352
315
|
|
|
353
316
|
//#endregion
|
|
354
317
|
//#region src/extension/encrypt.ts
|
|
355
|
-
|
|
356
|
-
|
|
318
|
+
/**
|
|
319
|
+
* Encrypts plaintext with a symmetric key using the given digest as AAD.
|
|
320
|
+
* This is an envelope-specific helper function that returns an envelope EncryptedMessage.
|
|
321
|
+
*/
|
|
322
|
+
function encryptWithDigest(key, plaintext, digest) {
|
|
323
|
+
const nonce = (0, _bcts_rand.rngRandomData)(new _bcts_rand.SecureRandomNumberGenerator(), _bcts_crypto.SYMMETRIC_NONCE_SIZE);
|
|
324
|
+
const aad = digest.data();
|
|
325
|
+
const [ciphertext, authTag] = (0, _bcts_crypto.aeadChaCha20Poly1305EncryptWithAad)(plaintext, key.data(), nonce, aad);
|
|
326
|
+
return new EncryptedMessage(ciphertext, nonce, authTag, digest);
|
|
357
327
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
data() {
|
|
371
|
-
return this.#key;
|
|
372
|
-
}
|
|
373
|
-
encrypt(plaintext, digest) {
|
|
374
|
-
const nonce = (0, _bcts_rand.rngRandomData)(createSecureRng$1(), _bcts_crypto.SYMMETRIC_NONCE_SIZE);
|
|
375
|
-
const aad = digest.data();
|
|
376
|
-
const [ciphertext, authTag] = (0, _bcts_crypto.aeadChaCha20Poly1305EncryptWithAad)(plaintext, this.#key, nonce, aad);
|
|
377
|
-
return new EncryptedMessage(ciphertext, nonce, authTag, digest);
|
|
378
|
-
}
|
|
379
|
-
decrypt(message) {
|
|
380
|
-
const digest = message.aadDigest();
|
|
381
|
-
if (digest === void 0) throw EnvelopeError.general("Missing digest in encrypted message");
|
|
382
|
-
const aad = digest.data();
|
|
383
|
-
try {
|
|
384
|
-
return (0, _bcts_crypto.aeadChaCha20Poly1305DecryptWithAad)(message.ciphertext(), this.#key, message.nonce(), aad, message.authTag());
|
|
385
|
-
} catch (_error) {
|
|
386
|
-
throw EnvelopeError.general("Decryption failed: invalid key or corrupted data");
|
|
387
|
-
}
|
|
328
|
+
/**
|
|
329
|
+
* Decrypts an envelope EncryptedMessage with a symmetric key.
|
|
330
|
+
* This is an envelope-specific helper function.
|
|
331
|
+
*/
|
|
332
|
+
function decryptWithDigest(key, message) {
|
|
333
|
+
const digest = message.aadDigest();
|
|
334
|
+
if (digest === void 0) throw EnvelopeError.general("Missing digest in encrypted message");
|
|
335
|
+
const aad = digest.data();
|
|
336
|
+
try {
|
|
337
|
+
return (0, _bcts_crypto.aeadChaCha20Poly1305DecryptWithAad)(message.ciphertext(), key.data(), message.nonce(), aad, message.authTag());
|
|
338
|
+
} catch (_error) {
|
|
339
|
+
throw EnvelopeError.general("Decryption failed: invalid key or corrupted data");
|
|
388
340
|
}
|
|
389
|
-
}
|
|
341
|
+
}
|
|
390
342
|
var EncryptedMessage = class {
|
|
391
343
|
#ciphertext;
|
|
392
344
|
#nonce;
|
|
@@ -424,18 +376,14 @@ function registerEncryptExtension() {
|
|
|
424
376
|
if (c.type === "elided") throw EnvelopeError.general("Cannot encrypt elided envelope");
|
|
425
377
|
if (c.type === "node") {
|
|
426
378
|
if (c.subject.isEncrypted()) throw EnvelopeError.general("Subject is already encrypted");
|
|
427
|
-
const
|
|
428
|
-
const subjectDigest = c.subject.digest();
|
|
429
|
-
const encryptedMessage$1 = key.encrypt(encodedCbor$1, subjectDigest);
|
|
379
|
+
const encryptedMessage$1 = encryptWithDigest(key, (0, _bcts_dcbor.cborData)(c.subject.taggedCbor()), c.subject.digest());
|
|
430
380
|
const encryptedSubject = Envelope.fromCase({
|
|
431
381
|
type: "encrypted",
|
|
432
382
|
message: encryptedMessage$1
|
|
433
383
|
});
|
|
434
384
|
return Envelope.newWithAssertions(encryptedSubject, c.assertions);
|
|
435
385
|
}
|
|
436
|
-
const
|
|
437
|
-
const digest = this.digest();
|
|
438
|
-
const encryptedMessage = key.encrypt(encodedCbor, digest);
|
|
386
|
+
const encryptedMessage = encryptWithDigest(key, (0, _bcts_dcbor.cborData)(this.taggedCbor()), this.digest());
|
|
439
387
|
return Envelope.fromCase({
|
|
440
388
|
type: "encrypted",
|
|
441
389
|
message: encryptedMessage
|
|
@@ -447,7 +395,7 @@ function registerEncryptExtension() {
|
|
|
447
395
|
const message = subjectCase.message;
|
|
448
396
|
const subjectDigest = message.aadDigest();
|
|
449
397
|
if (subjectDigest === void 0) throw EnvelopeError.general("Missing digest in encrypted message");
|
|
450
|
-
const cbor$1 = (0, _bcts_dcbor.decodeCbor)(key
|
|
398
|
+
const cbor$1 = (0, _bcts_dcbor.decodeCbor)(decryptWithDigest(key, message));
|
|
451
399
|
const resultSubject = Envelope.fromTaggedCbor(cbor$1);
|
|
452
400
|
if (!resultSubject.digest().equals(subjectDigest)) throw EnvelopeError.general("Invalid digest after decryption");
|
|
453
401
|
const c = this.case();
|
|
@@ -486,6 +434,7 @@ var Envelope = class Envelope {
|
|
|
486
434
|
}
|
|
487
435
|
static new(subject) {
|
|
488
436
|
if (subject instanceof Envelope) return subject;
|
|
437
|
+
if (subject instanceof _bcts_known_values.KnownValue) return Envelope.newWithKnownValue(subject);
|
|
489
438
|
return Envelope.newLeaf(subject);
|
|
490
439
|
}
|
|
491
440
|
static newOrNull(subject) {
|
|
@@ -519,7 +468,7 @@ var Envelope = class Envelope {
|
|
|
519
468
|
type: "node",
|
|
520
469
|
subject,
|
|
521
470
|
assertions: sortedAssertions,
|
|
522
|
-
digest: Digest.fromDigests(digests)
|
|
471
|
+
digest: _bcts_components.Digest.fromDigests(digests)
|
|
523
472
|
});
|
|
524
473
|
}
|
|
525
474
|
static newWithAssertions(subject, assertions) {
|
|
@@ -537,7 +486,7 @@ var Envelope = class Envelope {
|
|
|
537
486
|
return new Envelope({
|
|
538
487
|
type: "knownValue",
|
|
539
488
|
value: knownValue,
|
|
540
|
-
digest: Digest.fromImage(knownValue.toCborData())
|
|
489
|
+
digest: _bcts_components.Digest.fromImage(knownValue.toCborData())
|
|
541
490
|
});
|
|
542
491
|
}
|
|
543
492
|
static newWithEncrypted(encryptedMessage) {
|
|
@@ -564,14 +513,14 @@ var Envelope = class Envelope {
|
|
|
564
513
|
return new Envelope({
|
|
565
514
|
type: "leaf",
|
|
566
515
|
cbor: cbor$1,
|
|
567
|
-
digest: Digest.fromImage(cborBytes)
|
|
516
|
+
digest: _bcts_components.Digest.fromImage(cborBytes)
|
|
568
517
|
});
|
|
569
518
|
}
|
|
570
519
|
static newWrapped(envelope) {
|
|
571
520
|
return new Envelope({
|
|
572
521
|
type: "wrapped",
|
|
573
522
|
envelope,
|
|
574
|
-
digest: Digest.fromDigests([envelope.digest()])
|
|
523
|
+
digest: _bcts_components.Digest.fromDigests([envelope.digest()])
|
|
575
524
|
});
|
|
576
525
|
}
|
|
577
526
|
digest() {
|
|
@@ -678,7 +627,7 @@ var Envelope = class Envelope {
|
|
|
678
627
|
if (compressedData === void 0) throw EnvelopeError.cbor("compressed data must be byte string");
|
|
679
628
|
const digestBytes = arr.length === 2 ? (0, _bcts_dcbor.asByteString)(arr.get(1)) : void 0;
|
|
680
629
|
if (arr.length === 2 && digestBytes === void 0) throw EnvelopeError.cbor("digest must be byte string");
|
|
681
|
-
const compressed = new Compressed(compressedData, digestBytes !== void 0 ?
|
|
630
|
+
const compressed = new Compressed(compressedData, digestBytes !== void 0 ? _bcts_components.Digest.fromData(digestBytes) : void 0);
|
|
682
631
|
return Envelope.fromCase({
|
|
683
632
|
type: "compressed",
|
|
684
633
|
value: compressed
|
|
@@ -693,7 +642,7 @@ var Envelope = class Envelope {
|
|
|
693
642
|
if (ciphertext === void 0 || nonce === void 0 || authTag === void 0) throw EnvelopeError.cbor("ciphertext, nonce, and auth must be byte strings");
|
|
694
643
|
const digestBytes = arr.length === 4 ? (0, _bcts_dcbor.asByteString)(arr.get(3)) : void 0;
|
|
695
644
|
if (arr.length === 4 && digestBytes === void 0) throw EnvelopeError.cbor("aad digest must be byte string");
|
|
696
|
-
const message = new EncryptedMessage(ciphertext, nonce, authTag, digestBytes !== void 0 ?
|
|
645
|
+
const message = new EncryptedMessage(ciphertext, nonce, authTag, digestBytes !== void 0 ? _bcts_components.Digest.fromData(digestBytes) : void 0);
|
|
697
646
|
return Envelope.fromCase({
|
|
698
647
|
type: "encrypted",
|
|
699
648
|
message
|
|
@@ -705,7 +654,7 @@ var Envelope = class Envelope {
|
|
|
705
654
|
const bytes = (0, _bcts_dcbor.asByteString)(cbor$1);
|
|
706
655
|
if (bytes !== void 0) {
|
|
707
656
|
if (bytes.length !== 32) throw EnvelopeError.cbor("elided digest must be 32 bytes");
|
|
708
|
-
return Envelope.newElided(
|
|
657
|
+
return Envelope.newElided(_bcts_components.Digest.fromData(bytes));
|
|
709
658
|
}
|
|
710
659
|
const array = (0, _bcts_dcbor.asCborArray)(cbor$1);
|
|
711
660
|
if (array !== void 0) {
|
|
@@ -902,6 +851,54 @@ Envelope.prototype.extractBytes = function() {
|
|
|
902
851
|
Envelope.prototype.extractNull = function() {
|
|
903
852
|
return extractNull(this);
|
|
904
853
|
};
|
|
854
|
+
function extractSubject(envelope, decoder) {
|
|
855
|
+
const cbor$1 = envelope.subject().tryLeaf();
|
|
856
|
+
try {
|
|
857
|
+
return decoder(cbor$1);
|
|
858
|
+
} catch (error) {
|
|
859
|
+
throw EnvelopeError.cbor("failed to decode subject", error instanceof Error ? error : void 0);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
Envelope.prototype.extractSubject = function(decoder) {
|
|
863
|
+
return extractSubject(this, decoder);
|
|
864
|
+
};
|
|
865
|
+
function tryObjectForPredicate(envelope, predicate, decoder) {
|
|
866
|
+
return extractSubject(envelope.objectForPredicate(predicate), decoder);
|
|
867
|
+
}
|
|
868
|
+
Envelope.prototype.tryObjectForPredicate = function(predicate, decoder) {
|
|
869
|
+
return tryObjectForPredicate(this, predicate, decoder);
|
|
870
|
+
};
|
|
871
|
+
function tryOptionalObjectForPredicate(envelope, predicate, decoder) {
|
|
872
|
+
const obj = envelope.optionalObjectForPredicate(predicate);
|
|
873
|
+
if (obj === void 0) return;
|
|
874
|
+
return extractSubject(obj, decoder);
|
|
875
|
+
}
|
|
876
|
+
Envelope.prototype.tryOptionalObjectForPredicate = function(predicate, decoder) {
|
|
877
|
+
return tryOptionalObjectForPredicate(this, predicate, decoder);
|
|
878
|
+
};
|
|
879
|
+
function extractObjectForPredicateWithDefault(envelope, predicate, decoder, defaultValue) {
|
|
880
|
+
return tryOptionalObjectForPredicate(envelope, predicate, decoder) ?? defaultValue;
|
|
881
|
+
}
|
|
882
|
+
Envelope.prototype.extractObjectForPredicateWithDefault = function(predicate, decoder, defaultValue) {
|
|
883
|
+
return extractObjectForPredicateWithDefault(this, predicate, decoder, defaultValue);
|
|
884
|
+
};
|
|
885
|
+
function extractObjectsForPredicate(envelope, predicate, decoder) {
|
|
886
|
+
return envelope.objectsForPredicate(predicate).map((obj) => extractSubject(obj, decoder));
|
|
887
|
+
}
|
|
888
|
+
Envelope.prototype.extractObjectsForPredicate = function(predicate, decoder) {
|
|
889
|
+
return extractObjectsForPredicate(this, predicate, decoder);
|
|
890
|
+
};
|
|
891
|
+
function tryObjectsForPredicate(envelope, predicate, decoder) {
|
|
892
|
+
try {
|
|
893
|
+
return extractObjectsForPredicate(envelope, predicate, decoder);
|
|
894
|
+
} catch (error) {
|
|
895
|
+
if (error instanceof EnvelopeError && error.code === ErrorCode.NONEXISTENT_PREDICATE) return [];
|
|
896
|
+
throw error;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
Envelope.prototype.tryObjectsForPredicate = function(predicate, decoder) {
|
|
900
|
+
return tryObjectsForPredicate(this, predicate, decoder);
|
|
901
|
+
};
|
|
905
902
|
|
|
906
903
|
//#endregion
|
|
907
904
|
//#region src/base/elide.ts
|
|
@@ -942,6 +939,9 @@ function elideSetWithAction(envelope, target, isRevealing, action) {
|
|
|
942
939
|
Envelope.prototype.elideRemovingSetWithAction = function(target, action) {
|
|
943
940
|
return elideSetWithAction(this, target, false, action);
|
|
944
941
|
};
|
|
942
|
+
Envelope.prototype.elideSetWithAction = function(target, action) {
|
|
943
|
+
return elideSetWithAction(this, target, true, action);
|
|
944
|
+
};
|
|
945
945
|
Envelope.prototype.elideRemovingSet = function(target) {
|
|
946
946
|
return elideSetWithAction(this, target, false, elideAction());
|
|
947
947
|
};
|
|
@@ -1148,6 +1148,39 @@ function walkTree(envelope, level, incomingEdge, state, visit) {
|
|
|
1148
1148
|
}
|
|
1149
1149
|
return currentState;
|
|
1150
1150
|
}
|
|
1151
|
+
Envelope.prototype.digests = function(levelLimit) {
|
|
1152
|
+
const result = /* @__PURE__ */ new Set();
|
|
1153
|
+
const visitor = (envelope, level, _incomingEdge, _state) => {
|
|
1154
|
+
if (level < levelLimit) {
|
|
1155
|
+
result.add(envelope.digest());
|
|
1156
|
+
result.add(envelope.subject().digest());
|
|
1157
|
+
}
|
|
1158
|
+
return [void 0, false];
|
|
1159
|
+
};
|
|
1160
|
+
this.walk(false, void 0, visitor);
|
|
1161
|
+
return result;
|
|
1162
|
+
};
|
|
1163
|
+
Envelope.prototype.deepDigests = function() {
|
|
1164
|
+
return this.digests(Number.MAX_SAFE_INTEGER);
|
|
1165
|
+
};
|
|
1166
|
+
Envelope.prototype.shallowDigests = function() {
|
|
1167
|
+
return this.digests(2);
|
|
1168
|
+
};
|
|
1169
|
+
Envelope.prototype.object = function() {
|
|
1170
|
+
return this.tryObject();
|
|
1171
|
+
};
|
|
1172
|
+
Envelope.prototype.predicate = function() {
|
|
1173
|
+
return this.tryPredicate();
|
|
1174
|
+
};
|
|
1175
|
+
Envelope.prototype.toCbor = function() {
|
|
1176
|
+
return this.taggedCbor();
|
|
1177
|
+
};
|
|
1178
|
+
Envelope.prototype.expectLeaf = function() {
|
|
1179
|
+
return this.tryLeaf();
|
|
1180
|
+
};
|
|
1181
|
+
Envelope.prototype.checkTypeValue = function(type) {
|
|
1182
|
+
this.checkType(type);
|
|
1183
|
+
};
|
|
1151
1184
|
|
|
1152
1185
|
//#endregion
|
|
1153
1186
|
//#region src/base/assertions.ts
|
|
@@ -1287,6 +1320,30 @@ Envelope.prototype.asLeaf = function() {
|
|
|
1287
1320
|
const c = this.case();
|
|
1288
1321
|
if (c.type === "leaf") return c.cbor;
|
|
1289
1322
|
};
|
|
1323
|
+
Envelope.unit = function() {
|
|
1324
|
+
return Envelope.new(_bcts_known_values.UNIT);
|
|
1325
|
+
};
|
|
1326
|
+
Envelope.prototype.asKnownValue = function() {
|
|
1327
|
+
const c = this.case();
|
|
1328
|
+
if (c.type === "knownValue") return c.value;
|
|
1329
|
+
};
|
|
1330
|
+
Envelope.prototype.tryKnownValue = function() {
|
|
1331
|
+
const kv = this.asKnownValue();
|
|
1332
|
+
if (kv === void 0) throw EnvelopeError.notKnownValue();
|
|
1333
|
+
return kv;
|
|
1334
|
+
};
|
|
1335
|
+
Envelope.prototype.isKnownValue = function() {
|
|
1336
|
+
return this.case().type === "knownValue";
|
|
1337
|
+
};
|
|
1338
|
+
Envelope.prototype.isSubjectUnit = function() {
|
|
1339
|
+
const kv = this.subject().asKnownValue();
|
|
1340
|
+
if (kv === void 0) return false;
|
|
1341
|
+
return kv.equals(_bcts_known_values.UNIT);
|
|
1342
|
+
};
|
|
1343
|
+
Envelope.prototype.checkSubjectUnit = function() {
|
|
1344
|
+
if (this.isSubjectUnit()) return this;
|
|
1345
|
+
throw EnvelopeError.subjectNotUnit();
|
|
1346
|
+
};
|
|
1290
1347
|
|
|
1291
1348
|
//#endregion
|
|
1292
1349
|
//#region src/base/queries.ts
|
|
@@ -1402,6 +1459,38 @@ Envelope.prototype.elementsCount = function() {
|
|
|
1402
1459
|
}
|
|
1403
1460
|
return count;
|
|
1404
1461
|
};
|
|
1462
|
+
Envelope.prototype.isSubjectEncrypted = function() {
|
|
1463
|
+
const c = this.case();
|
|
1464
|
+
if (c.type === "encrypted") return true;
|
|
1465
|
+
if (c.type === "node") return c.subject.isSubjectEncrypted();
|
|
1466
|
+
return false;
|
|
1467
|
+
};
|
|
1468
|
+
Envelope.prototype.isSubjectCompressed = function() {
|
|
1469
|
+
const c = this.case();
|
|
1470
|
+
if (c.type === "compressed") return true;
|
|
1471
|
+
if (c.type === "node") return c.subject.isSubjectCompressed();
|
|
1472
|
+
return false;
|
|
1473
|
+
};
|
|
1474
|
+
Envelope.prototype.isSubjectElided = function() {
|
|
1475
|
+
const c = this.case();
|
|
1476
|
+
if (c.type === "elided") return true;
|
|
1477
|
+
if (c.type === "node") return c.subject.isSubjectElided();
|
|
1478
|
+
return false;
|
|
1479
|
+
};
|
|
1480
|
+
Envelope.prototype.setPosition = function(position) {
|
|
1481
|
+
const positionAssertions = this.assertionsWithPredicate(_bcts_known_values.POSITION);
|
|
1482
|
+
if (positionAssertions.length > 1) throw EnvelopeError.invalidFormat();
|
|
1483
|
+
return (positionAssertions.length === 1 ? this.removeAssertion(positionAssertions[0]) : this).addAssertion(_bcts_known_values.POSITION, position);
|
|
1484
|
+
};
|
|
1485
|
+
Envelope.prototype.position = function() {
|
|
1486
|
+
return this.objectForPredicate(_bcts_known_values.POSITION).extractNumber();
|
|
1487
|
+
};
|
|
1488
|
+
Envelope.prototype.removePosition = function() {
|
|
1489
|
+
const positionAssertions = this.assertionsWithPredicate(_bcts_known_values.POSITION);
|
|
1490
|
+
if (positionAssertions.length > 1) throw EnvelopeError.invalidFormat();
|
|
1491
|
+
if (positionAssertions.length === 1) return this.removeAssertion(positionAssertions[0]);
|
|
1492
|
+
return this;
|
|
1493
|
+
};
|
|
1405
1494
|
|
|
1406
1495
|
//#endregion
|
|
1407
1496
|
//#region src/base/wrap.ts
|
|
@@ -1417,6 +1506,27 @@ Envelope.prototype.unwrap = function() {
|
|
|
1417
1506
|
return this.tryUnwrap();
|
|
1418
1507
|
};
|
|
1419
1508
|
|
|
1509
|
+
//#endregion
|
|
1510
|
+
//#region src/base/ur.ts
|
|
1511
|
+
Envelope.prototype.urString = function() {
|
|
1512
|
+
return _bcts_uniform_resources.UR.new("envelope", this.taggedCbor()).string();
|
|
1513
|
+
};
|
|
1514
|
+
Envelope.prototype.ur = function() {
|
|
1515
|
+
return _bcts_uniform_resources.UR.new("envelope", this.taggedCbor());
|
|
1516
|
+
};
|
|
1517
|
+
Envelope.prototype.taggedCborData = function() {
|
|
1518
|
+
return this.cborBytes();
|
|
1519
|
+
};
|
|
1520
|
+
Envelope.fromUrString = function(urString) {
|
|
1521
|
+
const ur = _bcts_uniform_resources.UR.fromURString(urString);
|
|
1522
|
+
return Envelope.fromUR(ur);
|
|
1523
|
+
};
|
|
1524
|
+
Envelope.fromURString = Envelope.fromUrString;
|
|
1525
|
+
Envelope.fromUR = function(ur) {
|
|
1526
|
+
ur.checkType("envelope");
|
|
1527
|
+
return Envelope.fromTaggedCbor(ur.cbor());
|
|
1528
|
+
};
|
|
1529
|
+
|
|
1420
1530
|
//#endregion
|
|
1421
1531
|
//#region src/extension/types.ts
|
|
1422
1532
|
const IS_A = "isA";
|
|
@@ -1470,6 +1580,7 @@ if (Envelope?.prototype) {
|
|
|
1470
1580
|
const saltBytes = generateRandomBytes(count);
|
|
1471
1581
|
return this.addAssertion(SALT, saltBytes);
|
|
1472
1582
|
};
|
|
1583
|
+
Envelope.prototype.addSaltWithLen = Envelope.prototype.addSaltWithLength;
|
|
1473
1584
|
Envelope.prototype.addSaltBytes = function(saltBytes) {
|
|
1474
1585
|
if (saltBytes.length < MIN_SALT_SIZE) throw EnvelopeError.general(`Salt must be at least ${MIN_SALT_SIZE} bytes, got ${saltBytes.length}`);
|
|
1475
1586
|
return this.addAssertion(SALT, saltBytes);
|
|
@@ -1481,6 +1592,32 @@ if (Envelope?.prototype) {
|
|
|
1481
1592
|
const saltBytes = generateRandomBytes((0, _bcts_rand.rngNextInClosedRangeI32)(rng, min, max), rng);
|
|
1482
1593
|
return this.addAssertion(SALT, saltBytes);
|
|
1483
1594
|
};
|
|
1595
|
+
Envelope.prototype.addAssertionSalted = function(predicate, object, salted) {
|
|
1596
|
+
const assertion = Envelope.newAssertion(predicate, object);
|
|
1597
|
+
if (!salted) return this.addAssertionEnvelope(assertion);
|
|
1598
|
+
const saltedAssertion = assertion.addSalt();
|
|
1599
|
+
const c = this.case();
|
|
1600
|
+
if (c.type === "node") return Envelope.newWithUncheckedAssertions(c.subject, [...c.assertions, saltedAssertion]);
|
|
1601
|
+
return Envelope.newWithUncheckedAssertions(this, [saltedAssertion]);
|
|
1602
|
+
};
|
|
1603
|
+
Envelope.prototype.addAssertionEnvelopeSalted = function(assertionEnvelope, salted) {
|
|
1604
|
+
if (!salted) return this.addAssertionEnvelope(assertionEnvelope);
|
|
1605
|
+
const saltedAssertion = assertionEnvelope.addSalt();
|
|
1606
|
+
const c = this.case();
|
|
1607
|
+
if (c.type === "node") return Envelope.newWithUncheckedAssertions(c.subject, [...c.assertions, saltedAssertion]);
|
|
1608
|
+
return Envelope.newWithUncheckedAssertions(this, [saltedAssertion]);
|
|
1609
|
+
};
|
|
1610
|
+
Envelope.prototype.addOptionalAssertionEnvelopeSalted = function(assertionEnvelope, salted) {
|
|
1611
|
+
if (assertionEnvelope === void 0) return this;
|
|
1612
|
+
if (!salted) return this.addOptionalAssertionEnvelope(assertionEnvelope);
|
|
1613
|
+
const saltedAssertion = assertionEnvelope.addSalt();
|
|
1614
|
+
const c = this.case();
|
|
1615
|
+
if (c.type === "node") {
|
|
1616
|
+
if (c.assertions.some((a) => a.digest().equals(saltedAssertion.digest()))) return this;
|
|
1617
|
+
return Envelope.newWithUncheckedAssertions(c.subject, [...c.assertions, saltedAssertion]);
|
|
1618
|
+
}
|
|
1619
|
+
return Envelope.newWithUncheckedAssertions(this.subject(), [saltedAssertion]);
|
|
1620
|
+
};
|
|
1484
1621
|
}
|
|
1485
1622
|
|
|
1486
1623
|
//#endregion
|
|
@@ -1513,123 +1650,21 @@ const VERIFIED_BY = "verifiedBy";
|
|
|
1513
1650
|
*/
|
|
1514
1651
|
const NOTE = "note";
|
|
1515
1652
|
/**
|
|
1516
|
-
*
|
|
1517
|
-
*/
|
|
1518
|
-
var Signature = class Signature {
|
|
1519
|
-
#data;
|
|
1520
|
-
constructor(data) {
|
|
1521
|
-
this.#data = data;
|
|
1522
|
-
}
|
|
1523
|
-
/**
|
|
1524
|
-
* Returns the raw signature bytes.
|
|
1525
|
-
*/
|
|
1526
|
-
data() {
|
|
1527
|
-
return this.#data;
|
|
1528
|
-
}
|
|
1529
|
-
/**
|
|
1530
|
-
* Returns the hex-encoded signature.
|
|
1531
|
-
*/
|
|
1532
|
-
hex() {
|
|
1533
|
-
return Array.from(this.#data).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1534
|
-
}
|
|
1535
|
-
/**
|
|
1536
|
-
* Creates a Signature from hex string.
|
|
1537
|
-
*/
|
|
1538
|
-
static fromHex(hex) {
|
|
1539
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
1540
|
-
for (let i = 0; i < hex.length; i += 2) bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
1541
|
-
return new Signature(bytes);
|
|
1542
|
-
}
|
|
1543
|
-
};
|
|
1544
|
-
/**
|
|
1545
|
-
* ECDSA signing key using secp256k1 curve.
|
|
1546
|
-
* Uses @bcts/crypto functions.
|
|
1547
|
-
*/
|
|
1548
|
-
var SigningPrivateKey = class SigningPrivateKey {
|
|
1549
|
-
#privateKey;
|
|
1550
|
-
constructor(privateKey) {
|
|
1551
|
-
if (privateKey.length !== _bcts_crypto.ECDSA_PRIVATE_KEY_SIZE) throw new Error(`Private key must be ${_bcts_crypto.ECDSA_PRIVATE_KEY_SIZE} bytes`);
|
|
1552
|
-
this.#privateKey = privateKey;
|
|
1553
|
-
}
|
|
1554
|
-
/**
|
|
1555
|
-
* Generates a new random private key.
|
|
1556
|
-
*/
|
|
1557
|
-
static generate() {
|
|
1558
|
-
return new SigningPrivateKey((0, _bcts_rand.rngRandomData)(new _bcts_rand.SecureRandomNumberGenerator(), _bcts_crypto.ECDSA_PRIVATE_KEY_SIZE));
|
|
1559
|
-
}
|
|
1560
|
-
/**
|
|
1561
|
-
* Creates a private key from hex string.
|
|
1562
|
-
*/
|
|
1563
|
-
static fromHex(hex) {
|
|
1564
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
1565
|
-
for (let i = 0; i < hex.length; i += 2) bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
1566
|
-
return new SigningPrivateKey(bytes);
|
|
1567
|
-
}
|
|
1568
|
-
/**
|
|
1569
|
-
* Returns the corresponding public key.
|
|
1570
|
-
*/
|
|
1571
|
-
publicKey() {
|
|
1572
|
-
return new SigningPublicKey((0, _bcts_crypto.ecdsaPublicKeyFromPrivateKey)(this.#privateKey));
|
|
1573
|
-
}
|
|
1574
|
-
/**
|
|
1575
|
-
* Signs data and returns a Signature.
|
|
1576
|
-
*/
|
|
1577
|
-
sign(data) {
|
|
1578
|
-
return new Signature((0, _bcts_crypto.ecdsaSign)(this.#privateKey, data));
|
|
1579
|
-
}
|
|
1580
|
-
/**
|
|
1581
|
-
* Returns the raw private key bytes.
|
|
1582
|
-
*/
|
|
1583
|
-
data() {
|
|
1584
|
-
return this.#privateKey;
|
|
1585
|
-
}
|
|
1586
|
-
};
|
|
1587
|
-
/**
|
|
1588
|
-
* ECDSA public key for signature verification using secp256k1 curve.
|
|
1589
|
-
* Uses @bcts/crypto functions.
|
|
1653
|
+
* Metadata that can be attached to a signature.
|
|
1590
1654
|
*/
|
|
1591
|
-
var
|
|
1592
|
-
#
|
|
1593
|
-
constructor(publicKey) {
|
|
1594
|
-
if (publicKey.length !== _bcts_crypto.ECDSA_PUBLIC_KEY_SIZE && publicKey.length !== _bcts_crypto.ECDSA_UNCOMPRESSED_PUBLIC_KEY_SIZE) throw new Error(`Public key must be ${_bcts_crypto.ECDSA_PUBLIC_KEY_SIZE} bytes (compressed) or ${_bcts_crypto.ECDSA_UNCOMPRESSED_PUBLIC_KEY_SIZE} bytes (uncompressed)`);
|
|
1595
|
-
this.#publicKey = publicKey;
|
|
1596
|
-
}
|
|
1597
|
-
/**
|
|
1598
|
-
* Creates a public key from hex string.
|
|
1599
|
-
*/
|
|
1600
|
-
static fromHex(hex) {
|
|
1601
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
1602
|
-
for (let i = 0; i < hex.length; i += 2) bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
1603
|
-
return new SigningPublicKey(bytes);
|
|
1604
|
-
}
|
|
1605
|
-
/**
|
|
1606
|
-
* Verifies a signature against the provided data.
|
|
1607
|
-
*/
|
|
1608
|
-
verify(data, signature) {
|
|
1609
|
-
try {
|
|
1610
|
-
return (0, _bcts_crypto.ecdsaVerify)(this.#publicKey, signature.data(), data);
|
|
1611
|
-
} catch {
|
|
1612
|
-
return false;
|
|
1613
|
-
}
|
|
1614
|
-
}
|
|
1655
|
+
var SignatureMetadata = class SignatureMetadata {
|
|
1656
|
+
#assertions = [];
|
|
1615
1657
|
/**
|
|
1616
|
-
*
|
|
1658
|
+
* Creates a new SignatureMetadata instance.
|
|
1659
|
+
* Use the static `new()` method for fluent API style.
|
|
1617
1660
|
*/
|
|
1618
|
-
|
|
1619
|
-
return this.#publicKey;
|
|
1620
|
-
}
|
|
1661
|
+
constructor() {}
|
|
1621
1662
|
/**
|
|
1622
|
-
*
|
|
1663
|
+
* Creates a new empty SignatureMetadata.
|
|
1623
1664
|
*/
|
|
1624
|
-
|
|
1625
|
-
return
|
|
1665
|
+
static new() {
|
|
1666
|
+
return new SignatureMetadata();
|
|
1626
1667
|
}
|
|
1627
|
-
};
|
|
1628
|
-
/**
|
|
1629
|
-
* Metadata that can be attached to a signature.
|
|
1630
|
-
*/
|
|
1631
|
-
var SignatureMetadata = class SignatureMetadata {
|
|
1632
|
-
#assertions = [];
|
|
1633
1668
|
/**
|
|
1634
1669
|
* Adds an assertion to the metadata.
|
|
1635
1670
|
*/
|
|
@@ -1640,114 +1675,110 @@ var SignatureMetadata = class SignatureMetadata {
|
|
|
1640
1675
|
return metadata;
|
|
1641
1676
|
}
|
|
1642
1677
|
/**
|
|
1643
|
-
* Returns all assertions in
|
|
1678
|
+
* Returns all assertions in this metadata.
|
|
1644
1679
|
*/
|
|
1645
1680
|
assertions() {
|
|
1646
1681
|
return this.#assertions;
|
|
1647
1682
|
}
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1683
|
+
};
|
|
1684
|
+
Envelope.prototype.addSignatureWithMetadata = function(signer, metadata) {
|
|
1685
|
+
const digest = this.subject().digest();
|
|
1686
|
+
const signature = signer.sign(digest.data());
|
|
1687
|
+
let signatureEnvelope = Envelope.new(signature);
|
|
1688
|
+
if ("publicKey" in signer && typeof signer.publicKey === "function") {
|
|
1689
|
+
const verifier = signer.publicKey();
|
|
1690
|
+
signatureEnvelope = signatureEnvelope.addAssertion(VERIFIED_BY, verifier);
|
|
1691
|
+
}
|
|
1692
|
+
if (metadata !== void 0) for (const [predicate, object] of metadata.assertions()) signatureEnvelope = signatureEnvelope.addAssertion(predicate, object);
|
|
1693
|
+
return this.addAssertion(SIGNED, signatureEnvelope);
|
|
1694
|
+
};
|
|
1695
|
+
Envelope.prototype.addSignature = function(signer) {
|
|
1696
|
+
return this.addSignatureWithMetadata(signer, void 0);
|
|
1697
|
+
};
|
|
1698
|
+
Envelope.prototype.addSignatureOpt = function(signer, _options, metadata) {
|
|
1699
|
+
return this.addSignatureWithMetadata(signer, metadata);
|
|
1700
|
+
};
|
|
1701
|
+
Envelope.prototype.addSignatures = function(signers) {
|
|
1702
|
+
return signers.reduce((envelope, signer) => envelope.addSignature(signer), this);
|
|
1703
|
+
};
|
|
1704
|
+
Envelope.prototype.addSignaturesWithMetadata = function(signersWithMetadata) {
|
|
1705
|
+
return signersWithMetadata.reduce((envelope, { signer, metadata }) => envelope.addSignatureWithMetadata(signer, metadata), this);
|
|
1706
|
+
};
|
|
1707
|
+
Envelope.prototype.hasSignatureFrom = function(verifier) {
|
|
1708
|
+
const signedAssertions = this.assertionsWithPredicate(SIGNED);
|
|
1709
|
+
for (const assertion of signedAssertions) try {
|
|
1710
|
+
const signatureCbor = assertion.tryObject().subject().tryLeaf();
|
|
1711
|
+
const signature = _bcts_components.Signature.fromTaggedCbor(signatureCbor);
|
|
1712
|
+
const digest = this.subject().digest();
|
|
1713
|
+
if (verifier.verify(signature, digest.data())) return true;
|
|
1714
|
+
} catch {
|
|
1715
|
+
continue;
|
|
1653
1716
|
}
|
|
1717
|
+
return false;
|
|
1654
1718
|
};
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1719
|
+
Envelope.prototype.hasSignaturesFrom = function(verifiers) {
|
|
1720
|
+
return verifiers.every((verifier) => this.hasSignatureFrom(verifier));
|
|
1721
|
+
};
|
|
1722
|
+
Envelope.prototype.hasSignaturesFromThreshold = function(verifiers, threshold) {
|
|
1723
|
+
let count = 0;
|
|
1724
|
+
for (const verifier of verifiers) if (this.hasSignatureFrom(verifier)) {
|
|
1725
|
+
count++;
|
|
1726
|
+
if (count >= threshold) return true;
|
|
1727
|
+
}
|
|
1728
|
+
return false;
|
|
1729
|
+
};
|
|
1730
|
+
Envelope.prototype.sign = function(signer) {
|
|
1731
|
+
return this.wrap().addSignature(signer);
|
|
1732
|
+
};
|
|
1733
|
+
Envelope.prototype.signWithMetadata = function(signer, metadata) {
|
|
1734
|
+
return this.wrap().addSignatureWithMetadata(signer, metadata);
|
|
1735
|
+
};
|
|
1736
|
+
Envelope.prototype.verify = function(verifier) {
|
|
1737
|
+
if (!this.hasSignatureFrom(verifier)) throw EnvelopeError.general("Signature verification failed");
|
|
1738
|
+
return this.unwrap();
|
|
1739
|
+
};
|
|
1740
|
+
Envelope.prototype.verifyReturningMetadata = function(verifier) {
|
|
1741
|
+
const signedAssertions = this.assertionsWithPredicate(SIGNED);
|
|
1742
|
+
for (const assertion of signedAssertions) try {
|
|
1743
|
+
const signatureEnvelope = assertion.tryObject();
|
|
1744
|
+
const signatureCbor = signatureEnvelope.subject().tryLeaf();
|
|
1745
|
+
const signature = _bcts_components.Signature.fromTaggedCbor(signatureCbor);
|
|
1660
1746
|
const digest = this.subject().digest();
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
Envelope.prototype.hasSignatureFrom = function(verifier) {
|
|
1675
|
-
const subjectDigest = this.subject().digest();
|
|
1676
|
-
const signatures = this.signatures();
|
|
1677
|
-
for (const sigEnvelope of signatures) {
|
|
1678
|
-
const c = sigEnvelope.case();
|
|
1679
|
-
if (c.type === "leaf") try {
|
|
1680
|
-
const sigData = sigEnvelope.asByteString();
|
|
1681
|
-
if (sigData !== void 0) {
|
|
1682
|
-
const signature = new Signature(sigData);
|
|
1683
|
-
if (verifier.verify(subjectDigest.data(), signature)) return true;
|
|
1684
|
-
}
|
|
1685
|
-
} catch {
|
|
1686
|
-
continue;
|
|
1687
|
-
}
|
|
1688
|
-
else if (c.type === "node") {
|
|
1689
|
-
const outerSigs = sigEnvelope.assertions().filter((a) => {
|
|
1690
|
-
const aC = a.case();
|
|
1691
|
-
if (aC.type === "assertion") {
|
|
1692
|
-
const pred = aC.assertion.predicate();
|
|
1693
|
-
try {
|
|
1694
|
-
return pred.asText() === SIGNED;
|
|
1695
|
-
} catch {
|
|
1696
|
-
return false;
|
|
1697
|
-
}
|
|
1698
|
-
}
|
|
1699
|
-
return false;
|
|
1700
|
-
});
|
|
1701
|
-
for (const outerSig of outerSigs) {
|
|
1702
|
-
const outerSigCase = outerSig.case();
|
|
1703
|
-
if (outerSigCase.type === "assertion") {
|
|
1704
|
-
const outerSigObj = outerSigCase.assertion.object();
|
|
1705
|
-
try {
|
|
1706
|
-
const outerSigData = outerSigObj.asByteString();
|
|
1707
|
-
if (outerSigData !== void 0) {
|
|
1708
|
-
const outerSignature = new Signature(outerSigData);
|
|
1709
|
-
const nodeSubject = c.subject;
|
|
1710
|
-
const nodeSubjectCase = nodeSubject.case();
|
|
1711
|
-
if (nodeSubjectCase.type === "wrapped" && verifier.verify(nodeSubject.digest().data(), outerSignature)) {
|
|
1712
|
-
const innerSigData = nodeSubjectCase.envelope.subject().asByteString();
|
|
1713
|
-
if (innerSigData !== void 0) {
|
|
1714
|
-
const innerSignature = new Signature(innerSigData);
|
|
1715
|
-
if (verifier.verify(subjectDigest.data(), innerSignature)) return true;
|
|
1716
|
-
}
|
|
1717
|
-
}
|
|
1718
|
-
}
|
|
1719
|
-
} catch {
|
|
1720
|
-
continue;
|
|
1721
|
-
}
|
|
1722
|
-
}
|
|
1723
|
-
}
|
|
1724
|
-
}
|
|
1747
|
+
if (verifier.verify(signature, digest.data())) {
|
|
1748
|
+
const metadata = new SignatureMetadata();
|
|
1749
|
+
for (const metaAssertion of signatureEnvelope.assertions()) try {
|
|
1750
|
+
const pred = metaAssertion.tryPredicate();
|
|
1751
|
+
const obj = metaAssertion.tryObject();
|
|
1752
|
+
const predValue = pred.tryLeaf();
|
|
1753
|
+
const objValue = obj.tryLeaf();
|
|
1754
|
+
if (predValue !== VERIFIED_BY) metadata.withAssertion(predValue, objValue);
|
|
1755
|
+
} catch {}
|
|
1756
|
+
return {
|
|
1757
|
+
envelope: this.unwrap(),
|
|
1758
|
+
metadata
|
|
1759
|
+
};
|
|
1725
1760
|
}
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
throw EnvelopeError.general("Invalid signature assertion");
|
|
1748
|
-
});
|
|
1749
|
-
};
|
|
1750
|
-
}
|
|
1761
|
+
} catch {
|
|
1762
|
+
continue;
|
|
1763
|
+
}
|
|
1764
|
+
throw EnvelopeError.general("Signature verification failed");
|
|
1765
|
+
};
|
|
1766
|
+
Envelope.prototype.signatures = function() {
|
|
1767
|
+
return this.assertionsWithPredicate(SIGNED).map((assertion) => assertion.tryObject());
|
|
1768
|
+
};
|
|
1769
|
+
Envelope.prototype.verifySignatureFrom = function(verifier) {
|
|
1770
|
+
if (!this.hasSignatureFrom(verifier)) throw EnvelopeError.general("Signature verification failed");
|
|
1771
|
+
return this;
|
|
1772
|
+
};
|
|
1773
|
+
Envelope.prototype.verifySignaturesFrom = function(verifiers) {
|
|
1774
|
+
if (!this.hasSignaturesFrom(verifiers)) throw EnvelopeError.general("Signature verification failed");
|
|
1775
|
+
return this;
|
|
1776
|
+
};
|
|
1777
|
+
Envelope.prototype.verifySignaturesFromThreshold = function(verifiers, threshold) {
|
|
1778
|
+
const t = threshold ?? verifiers.length;
|
|
1779
|
+
if (!this.hasSignaturesFromThreshold(verifiers, t)) throw EnvelopeError.general("Signature verification failed - threshold not met");
|
|
1780
|
+
return this;
|
|
1781
|
+
};
|
|
1751
1782
|
|
|
1752
1783
|
//#endregion
|
|
1753
1784
|
//#region src/extension/attachment.ts
|
|
@@ -1931,14 +1962,51 @@ if (Envelope?.prototype) {
|
|
|
1931
1962
|
}
|
|
1932
1963
|
});
|
|
1933
1964
|
};
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1965
|
+
/**
|
|
1966
|
+
* Validates that this envelope is a valid attachment.
|
|
1967
|
+
*
|
|
1968
|
+
* An attachment is valid if:
|
|
1969
|
+
* 1. The envelope is an assertion with 'attachment' as predicate
|
|
1970
|
+
* 2. The object contains a wrapped payload with vendor assertion
|
|
1971
|
+
* 3. Reconstructing the attachment yields an equivalent envelope
|
|
1972
|
+
*
|
|
1973
|
+
* @throws EnvelopeError if the envelope is not a valid attachment
|
|
1974
|
+
*/
|
|
1975
|
+
Envelope.prototype.validateAttachment = function() {
|
|
1976
|
+
const c = this.case();
|
|
1977
|
+
if (c.type !== "assertion") throw EnvelopeError.invalidAttachment("Envelope is not an assertion");
|
|
1978
|
+
if (c.assertion.predicate().asText() !== ATTACHMENT) throw EnvelopeError.invalidAttachment("Assertion predicate is not 'attachment'");
|
|
1979
|
+
const payload = this.attachmentPayload();
|
|
1980
|
+
const vendor = this.attachmentVendor();
|
|
1981
|
+
const conformsTo = this.attachmentConformsTo();
|
|
1982
|
+
const reconstructed = Envelope.newAttachment(payload, vendor, conformsTo);
|
|
1983
|
+
if (!this.digest().equals(reconstructed.digest())) throw EnvelopeError.invalidAttachment("Attachment structure is invalid");
|
|
1984
|
+
};
|
|
1985
|
+
/**
|
|
1986
|
+
* Finds a single attachment matching the given vendor and conformsTo.
|
|
1987
|
+
*
|
|
1988
|
+
* Unlike `attachmentsWithVendorAndConformsTo` which returns an array,
|
|
1989
|
+
* this method requires exactly one attachment to match.
|
|
1990
|
+
*
|
|
1991
|
+
* @param vendor - Optional vendor identifier to match
|
|
1992
|
+
* @param conformsTo - Optional conformsTo URI to match
|
|
1993
|
+
* @returns The matching attachment envelope
|
|
1994
|
+
* @throws EnvelopeError if not exactly one attachment matches
|
|
1995
|
+
*/
|
|
1996
|
+
Envelope.prototype.attachmentWithVendorAndConformsTo = function(vendor, conformsTo) {
|
|
1997
|
+
const matches = this.attachmentsWithVendorAndConformsTo(vendor, conformsTo);
|
|
1998
|
+
if (matches.length === 0) throw EnvelopeError.general("No matching attachment found");
|
|
1999
|
+
if (matches.length > 1) throw EnvelopeError.general(`Expected exactly one attachment, found ${matches.length}`);
|
|
2000
|
+
return matches[0];
|
|
2001
|
+
};
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
//#endregion
|
|
2005
|
+
//#region src/extension/recipient.ts
|
|
2006
|
+
const HAS_RECIPIENT = "hasRecipient";
|
|
2007
|
+
var PublicKeyBase = class PublicKeyBase {
|
|
2008
|
+
#publicKey;
|
|
2009
|
+
constructor(publicKey) {
|
|
1942
2010
|
if (publicKey.length !== _bcts_crypto.X25519_PUBLIC_KEY_SIZE) throw new Error(`Public key must be ${_bcts_crypto.X25519_PUBLIC_KEY_SIZE} bytes`);
|
|
1943
2011
|
this.#publicKey = publicKey;
|
|
1944
2012
|
}
|
|
@@ -1963,7 +2031,11 @@ var PrivateKeyBase = class PrivateKeyBase {
|
|
|
1963
2031
|
this.#publicKey = new PublicKeyBase(publicKey);
|
|
1964
2032
|
}
|
|
1965
2033
|
static generate() {
|
|
1966
|
-
const
|
|
2034
|
+
const rng = new _bcts_rand.SecureRandomNumberGenerator();
|
|
2035
|
+
return PrivateKeyBase.generateUsing(rng);
|
|
2036
|
+
}
|
|
2037
|
+
static generateUsing(rng) {
|
|
2038
|
+
const privateKey = (0, _bcts_crypto.x25519NewPrivateKeyUsing)(rng);
|
|
1967
2039
|
return new PrivateKeyBase(privateKey, (0, _bcts_crypto.x25519PublicKeyFromPrivateKey)(privateKey));
|
|
1968
2040
|
}
|
|
1969
2041
|
static fromBytes(privateKey, publicKey) {
|
|
@@ -1992,7 +2064,7 @@ var PrivateKeyBase = class PrivateKeyBase {
|
|
|
1992
2064
|
unseal(sealedMessage) {
|
|
1993
2065
|
try {
|
|
1994
2066
|
const decrypted = sealedMessage.decrypt(this.#privateKey, this.#publicKey.data());
|
|
1995
|
-
return SymmetricKey.from(decrypted);
|
|
2067
|
+
return _bcts_components.SymmetricKey.from(decrypted);
|
|
1996
2068
|
} catch (_error) {
|
|
1997
2069
|
throw EnvelopeError.general("Failed to unseal message: not a recipient");
|
|
1998
2070
|
}
|
|
@@ -2049,12 +2121,12 @@ var SealedMessage = class SealedMessage {
|
|
|
2049
2121
|
};
|
|
2050
2122
|
if (Envelope?.prototype) {
|
|
2051
2123
|
Envelope.prototype.encryptSubjectToRecipient = function(recipientPublicKey) {
|
|
2052
|
-
const contentKey = SymmetricKey.
|
|
2124
|
+
const contentKey = _bcts_components.SymmetricKey.new();
|
|
2053
2125
|
return this.encryptSubject(contentKey).addRecipient(recipientPublicKey, contentKey);
|
|
2054
2126
|
};
|
|
2055
2127
|
Envelope.prototype.encryptSubjectToRecipients = function(recipients) {
|
|
2056
2128
|
if (recipients.length === 0) throw EnvelopeError.general("Must provide at least one recipient");
|
|
2057
|
-
const contentKey = SymmetricKey.
|
|
2129
|
+
const contentKey = _bcts_components.SymmetricKey.new();
|
|
2058
2130
|
let result = this.encryptSubject(contentKey);
|
|
2059
2131
|
for (const recipient of recipients) result = result.addRecipient(recipient, contentKey);
|
|
2060
2132
|
return result;
|
|
@@ -2144,72 +2216,300 @@ const PARAMETER_IDS = {
|
|
|
2144
2216
|
RHS: 3
|
|
2145
2217
|
};
|
|
2146
2218
|
var Function = class Function {
|
|
2147
|
-
#
|
|
2148
|
-
|
|
2149
|
-
|
|
2219
|
+
#variant;
|
|
2220
|
+
#value;
|
|
2221
|
+
#name;
|
|
2222
|
+
constructor(variant, value, name) {
|
|
2223
|
+
this.#variant = variant;
|
|
2224
|
+
this.#value = value;
|
|
2225
|
+
this.#name = name;
|
|
2226
|
+
}
|
|
2227
|
+
static newKnown(value, name) {
|
|
2228
|
+
return new Function("known", value, name);
|
|
2229
|
+
}
|
|
2230
|
+
static newNamed(name) {
|
|
2231
|
+
return new Function("named", 0, name);
|
|
2232
|
+
}
|
|
2233
|
+
static fromNumeric(id) {
|
|
2234
|
+
return Function.newKnown(id);
|
|
2235
|
+
}
|
|
2236
|
+
static fromString(name) {
|
|
2237
|
+
return Function.newNamed(name);
|
|
2238
|
+
}
|
|
2239
|
+
isKnown() {
|
|
2240
|
+
return this.#variant === "known";
|
|
2241
|
+
}
|
|
2242
|
+
isNamed() {
|
|
2243
|
+
return this.#variant === "named";
|
|
2244
|
+
}
|
|
2245
|
+
value() {
|
|
2246
|
+
return this.#variant === "known" ? this.#value : void 0;
|
|
2150
2247
|
}
|
|
2151
2248
|
id() {
|
|
2152
|
-
return this.#
|
|
2249
|
+
if (this.#variant === "known") return this.#value;
|
|
2250
|
+
if (this.#name === void 0) throw new Error("Invalid named function: missing name");
|
|
2251
|
+
return this.#name;
|
|
2252
|
+
}
|
|
2253
|
+
name() {
|
|
2254
|
+
if (this.#variant === "known") return this.#name ?? this.#value.toString();
|
|
2255
|
+
else return `"${this.#name}"`;
|
|
2256
|
+
}
|
|
2257
|
+
namedName() {
|
|
2258
|
+
return this.#variant === "named" ? this.#name : void 0;
|
|
2259
|
+
}
|
|
2260
|
+
assignedName() {
|
|
2261
|
+
return this.#variant === "known" ? this.#name : void 0;
|
|
2153
2262
|
}
|
|
2154
2263
|
isNumeric() {
|
|
2155
|
-
return
|
|
2264
|
+
return this.#variant === "known";
|
|
2156
2265
|
}
|
|
2157
2266
|
isString() {
|
|
2158
|
-
return
|
|
2267
|
+
return this.#variant === "named";
|
|
2159
2268
|
}
|
|
2160
2269
|
envelope() {
|
|
2161
|
-
const functionStr =
|
|
2270
|
+
const functionStr = this.#variant === "known" ? `«${this.#value}»` : `«"${this.#name}"»`;
|
|
2162
2271
|
return Envelope.new(functionStr);
|
|
2163
2272
|
}
|
|
2164
2273
|
withParameter(param, value) {
|
|
2165
2274
|
return new Expression(this).withParameter(param, value);
|
|
2166
2275
|
}
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2276
|
+
equals(other) {
|
|
2277
|
+
if (this.#variant !== other.#variant) return false;
|
|
2278
|
+
if (this.#variant === "known") return this.#value === other.#value;
|
|
2279
|
+
else return this.#name === other.#name;
|
|
2280
|
+
}
|
|
2281
|
+
hashCode() {
|
|
2282
|
+
if (this.#variant === "known") return this.#value;
|
|
2283
|
+
else {
|
|
2284
|
+
let hash = 0;
|
|
2285
|
+
for (let i = 0; i < (this.#name?.length ?? 0); i++) hash = hash * 31 + (this.#name?.charCodeAt(i) ?? 0) | 0;
|
|
2286
|
+
return hash;
|
|
2287
|
+
}
|
|
2172
2288
|
}
|
|
2173
2289
|
toString() {
|
|
2174
|
-
return
|
|
2290
|
+
return this.#variant === "known" ? `«${this.#value}»` : `«"${this.#name}"»`;
|
|
2291
|
+
}
|
|
2292
|
+
};
|
|
2293
|
+
var FunctionsStore = class {
|
|
2294
|
+
#dict = /* @__PURE__ */ new Map();
|
|
2295
|
+
constructor(functions = []) {
|
|
2296
|
+
for (const func of functions) this.insert(func);
|
|
2297
|
+
}
|
|
2298
|
+
insert(func) {
|
|
2299
|
+
if (func.isKnown()) {
|
|
2300
|
+
const value = func.value();
|
|
2301
|
+
if (value !== void 0) this.#dict.set(value, func);
|
|
2302
|
+
} else {
|
|
2303
|
+
const name = func.namedName();
|
|
2304
|
+
if (name !== void 0) this.#dict.set(name, func);
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
assignedName(func) {
|
|
2308
|
+
let key;
|
|
2309
|
+
if (func.isKnown()) key = func.value();
|
|
2310
|
+
else key = func.namedName();
|
|
2311
|
+
if (key === void 0) return void 0;
|
|
2312
|
+
return this.#dict.get(key)?.assignedName();
|
|
2313
|
+
}
|
|
2314
|
+
name(func) {
|
|
2315
|
+
return this.assignedName(func) ?? func.name();
|
|
2316
|
+
}
|
|
2317
|
+
static nameForFunction(func, store) {
|
|
2318
|
+
if (store !== void 0) {
|
|
2319
|
+
const assigned = store.assignedName(func);
|
|
2320
|
+
if (assigned !== void 0 && assigned !== "") return assigned;
|
|
2321
|
+
}
|
|
2322
|
+
return func.name();
|
|
2175
2323
|
}
|
|
2176
2324
|
};
|
|
2177
2325
|
var Parameter = class Parameter {
|
|
2178
|
-
#
|
|
2326
|
+
#variant;
|
|
2179
2327
|
#value;
|
|
2180
|
-
|
|
2181
|
-
|
|
2328
|
+
#name;
|
|
2329
|
+
#paramValue;
|
|
2330
|
+
constructor(variant, value, name, paramValue) {
|
|
2331
|
+
this.#variant = variant;
|
|
2182
2332
|
this.#value = value;
|
|
2333
|
+
this.#name = name;
|
|
2334
|
+
this.#paramValue = paramValue;
|
|
2183
2335
|
}
|
|
2184
|
-
|
|
2185
|
-
return
|
|
2336
|
+
static newKnown(value, name) {
|
|
2337
|
+
return new Parameter("known", value, name);
|
|
2338
|
+
}
|
|
2339
|
+
static newNamed(name) {
|
|
2340
|
+
return new Parameter("named", 0, name);
|
|
2341
|
+
}
|
|
2342
|
+
static withValue(id, value) {
|
|
2343
|
+
if (typeof id === "number") return new Parameter("known", id, void 0, value);
|
|
2344
|
+
else return new Parameter("named", 0, id, value);
|
|
2345
|
+
}
|
|
2346
|
+
isKnown() {
|
|
2347
|
+
return this.#variant === "known";
|
|
2348
|
+
}
|
|
2349
|
+
isNamed() {
|
|
2350
|
+
return this.#variant === "named";
|
|
2186
2351
|
}
|
|
2187
2352
|
value() {
|
|
2188
|
-
return this.#value;
|
|
2353
|
+
return this.#variant === "known" ? this.#value : void 0;
|
|
2354
|
+
}
|
|
2355
|
+
id() {
|
|
2356
|
+
if (this.#variant === "known") return this.#value;
|
|
2357
|
+
if (this.#name === void 0) throw new Error("Invalid named parameter: missing name");
|
|
2358
|
+
return this.#name;
|
|
2359
|
+
}
|
|
2360
|
+
name() {
|
|
2361
|
+
if (this.#variant === "known") return this.#name ?? this.#value.toString();
|
|
2362
|
+
else return `"${this.#name}"`;
|
|
2363
|
+
}
|
|
2364
|
+
namedName() {
|
|
2365
|
+
return this.#variant === "named" ? this.#name : void 0;
|
|
2366
|
+
}
|
|
2367
|
+
assignedName() {
|
|
2368
|
+
return this.#variant === "known" ? this.#name : void 0;
|
|
2369
|
+
}
|
|
2370
|
+
paramValue() {
|
|
2371
|
+
return this.#paramValue;
|
|
2189
2372
|
}
|
|
2190
2373
|
isNumeric() {
|
|
2191
|
-
return
|
|
2374
|
+
return this.#variant === "known";
|
|
2192
2375
|
}
|
|
2193
2376
|
isString() {
|
|
2194
|
-
return
|
|
2377
|
+
return this.#variant === "named";
|
|
2195
2378
|
}
|
|
2196
2379
|
envelope() {
|
|
2197
|
-
const paramStr =
|
|
2198
|
-
return Envelope.newAssertion(paramStr, this.#
|
|
2380
|
+
const paramStr = this.#variant === "known" ? `❰${this.#value}❱` : `❰"${this.#name}"❱`;
|
|
2381
|
+
if (this.#paramValue !== void 0) return Envelope.newAssertion(paramStr, this.#paramValue);
|
|
2382
|
+
return Envelope.new(paramStr);
|
|
2383
|
+
}
|
|
2384
|
+
equals(other) {
|
|
2385
|
+
if (this.#variant !== other.#variant) return false;
|
|
2386
|
+
if (this.#variant === "known") return this.#value === other.#value;
|
|
2387
|
+
else return this.#name === other.#name;
|
|
2388
|
+
}
|
|
2389
|
+
hashCode() {
|
|
2390
|
+
if (this.#variant === "known") return this.#value;
|
|
2391
|
+
else {
|
|
2392
|
+
let hash = 0;
|
|
2393
|
+
for (let i = 0; i < (this.#name?.length ?? 0); i++) hash = hash * 31 + (this.#name?.charCodeAt(i) ?? 0) | 0;
|
|
2394
|
+
return hash;
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
toString() {
|
|
2398
|
+
const idStr = this.#variant === "known" ? `❰${this.#value}❱` : `❰"${this.#name}"❱`;
|
|
2399
|
+
if (this.#paramValue !== void 0) return `${idStr}: ${this.#paramValue.asText()}`;
|
|
2400
|
+
return idStr;
|
|
2199
2401
|
}
|
|
2200
2402
|
static blank(value) {
|
|
2201
|
-
return
|
|
2403
|
+
return Parameter.withValue(PARAMETER_IDS.BLANK, Envelope.new(value));
|
|
2202
2404
|
}
|
|
2203
2405
|
static lhs(value) {
|
|
2204
|
-
return
|
|
2406
|
+
return Parameter.withValue(PARAMETER_IDS.LHS, Envelope.new(value));
|
|
2205
2407
|
}
|
|
2206
2408
|
static rhs(value) {
|
|
2207
|
-
return
|
|
2409
|
+
return Parameter.withValue(PARAMETER_IDS.RHS, Envelope.new(value));
|
|
2208
2410
|
}
|
|
2209
|
-
|
|
2210
|
-
|
|
2411
|
+
};
|
|
2412
|
+
var ParametersStore = class {
|
|
2413
|
+
#dict = /* @__PURE__ */ new Map();
|
|
2414
|
+
constructor(parameters = []) {
|
|
2415
|
+
for (const param of parameters) this.insert(param);
|
|
2416
|
+
}
|
|
2417
|
+
insert(param) {
|
|
2418
|
+
if (param.isKnown()) {
|
|
2419
|
+
const value = param.value();
|
|
2420
|
+
if (value !== void 0) this.#dict.set(value, param);
|
|
2421
|
+
} else {
|
|
2422
|
+
const name = param.namedName();
|
|
2423
|
+
if (name !== void 0) this.#dict.set(name, param);
|
|
2424
|
+
}
|
|
2425
|
+
}
|
|
2426
|
+
assignedName(param) {
|
|
2427
|
+
let key;
|
|
2428
|
+
if (param.isKnown()) key = param.value();
|
|
2429
|
+
else key = param.namedName();
|
|
2430
|
+
if (key === void 0) return void 0;
|
|
2431
|
+
return this.#dict.get(key)?.assignedName();
|
|
2432
|
+
}
|
|
2433
|
+
name(param) {
|
|
2434
|
+
return this.assignedName(param) ?? param.name();
|
|
2435
|
+
}
|
|
2436
|
+
static nameForParameter(param, store) {
|
|
2437
|
+
if (store !== void 0) {
|
|
2438
|
+
const assigned = store.assignedName(param);
|
|
2439
|
+
if (assigned !== void 0 && assigned !== "") return assigned;
|
|
2440
|
+
}
|
|
2441
|
+
return param.name();
|
|
2442
|
+
}
|
|
2443
|
+
};
|
|
2444
|
+
const ADD = Function.newKnown(FUNCTION_IDS.ADD, "add");
|
|
2445
|
+
const SUB = Function.newKnown(FUNCTION_IDS.SUB, "sub");
|
|
2446
|
+
const MUL = Function.newKnown(FUNCTION_IDS.MUL, "mul");
|
|
2447
|
+
const DIV = Function.newKnown(FUNCTION_IDS.DIV, "div");
|
|
2448
|
+
const NEG = Function.newKnown(FUNCTION_IDS.NEG, "neg");
|
|
2449
|
+
const LT = Function.newKnown(FUNCTION_IDS.LT, "lt");
|
|
2450
|
+
const LE = Function.newKnown(FUNCTION_IDS.LE, "le");
|
|
2451
|
+
const GT = Function.newKnown(FUNCTION_IDS.GT, "gt");
|
|
2452
|
+
const GE = Function.newKnown(FUNCTION_IDS.GE, "ge");
|
|
2453
|
+
const EQ = Function.newKnown(FUNCTION_IDS.EQ, "eq");
|
|
2454
|
+
const NE = Function.newKnown(FUNCTION_IDS.NE, "ne");
|
|
2455
|
+
const AND = Function.newKnown(FUNCTION_IDS.AND, "and");
|
|
2456
|
+
const OR = Function.newKnown(FUNCTION_IDS.OR, "or");
|
|
2457
|
+
const XOR = Function.newKnown(FUNCTION_IDS.XOR, "xor");
|
|
2458
|
+
const NOT = Function.newKnown(FUNCTION_IDS.NOT, "not");
|
|
2459
|
+
const ADD_VALUE = FUNCTION_IDS.ADD;
|
|
2460
|
+
const SUB_VALUE = FUNCTION_IDS.SUB;
|
|
2461
|
+
const MUL_VALUE = FUNCTION_IDS.MUL;
|
|
2462
|
+
const DIV_VALUE = FUNCTION_IDS.DIV;
|
|
2463
|
+
const NEG_VALUE = FUNCTION_IDS.NEG;
|
|
2464
|
+
const LT_VALUE = FUNCTION_IDS.LT;
|
|
2465
|
+
const LE_VALUE = FUNCTION_IDS.LE;
|
|
2466
|
+
const GT_VALUE = FUNCTION_IDS.GT;
|
|
2467
|
+
const GE_VALUE = FUNCTION_IDS.GE;
|
|
2468
|
+
const EQ_VALUE = FUNCTION_IDS.EQ;
|
|
2469
|
+
const NE_VALUE = FUNCTION_IDS.NE;
|
|
2470
|
+
const AND_VALUE = FUNCTION_IDS.AND;
|
|
2471
|
+
const OR_VALUE = FUNCTION_IDS.OR;
|
|
2472
|
+
const XOR_VALUE = FUNCTION_IDS.XOR;
|
|
2473
|
+
const NOT_VALUE = FUNCTION_IDS.NOT;
|
|
2474
|
+
const BLANK = Parameter.newKnown(PARAMETER_IDS.BLANK, "_");
|
|
2475
|
+
const LHS = Parameter.newKnown(PARAMETER_IDS.LHS, "lhs");
|
|
2476
|
+
const RHS = Parameter.newKnown(PARAMETER_IDS.RHS, "rhs");
|
|
2477
|
+
const BLANK_VALUE = PARAMETER_IDS.BLANK;
|
|
2478
|
+
const LHS_VALUE = PARAMETER_IDS.LHS;
|
|
2479
|
+
const RHS_VALUE = PARAMETER_IDS.RHS;
|
|
2480
|
+
var LazyStore = class {
|
|
2481
|
+
#store;
|
|
2482
|
+
#initializer;
|
|
2483
|
+
constructor(initializer) {
|
|
2484
|
+
this.#initializer = initializer;
|
|
2485
|
+
}
|
|
2486
|
+
get() {
|
|
2487
|
+
this.#store ??= this.#initializer();
|
|
2488
|
+
return this.#store;
|
|
2211
2489
|
}
|
|
2212
2490
|
};
|
|
2491
|
+
const GLOBAL_FUNCTIONS = new LazyStore(() => new FunctionsStore([
|
|
2492
|
+
ADD,
|
|
2493
|
+
SUB,
|
|
2494
|
+
MUL,
|
|
2495
|
+
DIV,
|
|
2496
|
+
NEG,
|
|
2497
|
+
LT,
|
|
2498
|
+
LE,
|
|
2499
|
+
GT,
|
|
2500
|
+
GE,
|
|
2501
|
+
EQ,
|
|
2502
|
+
NE,
|
|
2503
|
+
AND,
|
|
2504
|
+
OR,
|
|
2505
|
+
XOR,
|
|
2506
|
+
NOT
|
|
2507
|
+
]));
|
|
2508
|
+
const GLOBAL_PARAMETERS = new LazyStore(() => new ParametersStore([
|
|
2509
|
+
BLANK,
|
|
2510
|
+
LHS,
|
|
2511
|
+
RHS
|
|
2512
|
+
]));
|
|
2213
2513
|
var Expression = class Expression {
|
|
2214
2514
|
#function;
|
|
2215
2515
|
#parameters = /* @__PURE__ */ new Map();
|
|
@@ -2225,7 +2525,7 @@ var Expression = class Expression {
|
|
|
2225
2525
|
}
|
|
2226
2526
|
withParameter(param, value) {
|
|
2227
2527
|
const key = typeof param === "number" ? param.toString() : param;
|
|
2228
|
-
this.#parameters.set(key,
|
|
2528
|
+
this.#parameters.set(key, Parameter.withValue(param, Envelope.new(value)));
|
|
2229
2529
|
this.#envelope = null;
|
|
2230
2530
|
return this;
|
|
2231
2531
|
}
|
|
@@ -2235,7 +2535,7 @@ var Expression = class Expression {
|
|
|
2235
2535
|
}
|
|
2236
2536
|
getParameter(param) {
|
|
2237
2537
|
const key = typeof param === "number" ? param.toString() : param;
|
|
2238
|
-
return this.#parameters.get(key)?.
|
|
2538
|
+
return this.#parameters.get(key)?.paramValue();
|
|
2239
2539
|
}
|
|
2240
2540
|
hasParameter(param) {
|
|
2241
2541
|
const key = typeof param === "number" ? param.toString() : param;
|
|
@@ -2258,13 +2558,13 @@ var Expression = class Expression {
|
|
|
2258
2558
|
static fromEnvelope(envelope) {
|
|
2259
2559
|
const subjectText = envelope.subject().asText();
|
|
2260
2560
|
if (subjectText === void 0) throw EnvelopeError.general("Not a valid function envelope");
|
|
2261
|
-
let
|
|
2561
|
+
let func;
|
|
2262
2562
|
if (subjectText.startsWith("«") && subjectText.endsWith("»")) {
|
|
2263
2563
|
const inner = subjectText.slice(1, -1);
|
|
2264
|
-
if (inner.startsWith("\"") && inner.endsWith("\""))
|
|
2265
|
-
else
|
|
2564
|
+
if (inner.startsWith("\"") && inner.endsWith("\"")) func = Function.newNamed(inner.slice(1, -1));
|
|
2565
|
+
else func = Function.newKnown(parseInt(inner, 10));
|
|
2266
2566
|
} else throw EnvelopeError.general("Not a valid function envelope");
|
|
2267
|
-
const expr = new Expression(
|
|
2567
|
+
const expr = new Expression(func);
|
|
2268
2568
|
for (const assertion of envelope.assertions()) try {
|
|
2269
2569
|
const pred = assertion.subject().asPredicate();
|
|
2270
2570
|
const obj = assertion.subject().asObject();
|
|
@@ -2289,209 +2589,845 @@ var Expression = class Expression {
|
|
|
2289
2589
|
}
|
|
2290
2590
|
};
|
|
2291
2591
|
function add(lhs, rhs) {
|
|
2292
|
-
return
|
|
2592
|
+
return new Expression(ADD).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2293
2593
|
}
|
|
2294
2594
|
function sub(lhs, rhs) {
|
|
2295
|
-
return
|
|
2595
|
+
return new Expression(SUB).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2296
2596
|
}
|
|
2297
2597
|
function mul(lhs, rhs) {
|
|
2298
|
-
return
|
|
2598
|
+
return new Expression(MUL).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2299
2599
|
}
|
|
2300
2600
|
function div(lhs, rhs) {
|
|
2301
|
-
return
|
|
2601
|
+
return new Expression(DIV).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2302
2602
|
}
|
|
2303
2603
|
function neg(value) {
|
|
2304
|
-
return
|
|
2604
|
+
return new Expression(NEG).withParameter(PARAMETER_IDS.BLANK, value);
|
|
2305
2605
|
}
|
|
2306
2606
|
function lt(lhs, rhs) {
|
|
2307
|
-
return
|
|
2607
|
+
return new Expression(LT).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2608
|
+
}
|
|
2609
|
+
function le(lhs, rhs) {
|
|
2610
|
+
return new Expression(LE).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2308
2611
|
}
|
|
2309
2612
|
function gt(lhs, rhs) {
|
|
2310
|
-
return
|
|
2613
|
+
return new Expression(GT).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2614
|
+
}
|
|
2615
|
+
function ge(lhs, rhs) {
|
|
2616
|
+
return new Expression(GE).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2311
2617
|
}
|
|
2312
2618
|
function eq(lhs, rhs) {
|
|
2313
|
-
return
|
|
2619
|
+
return new Expression(EQ).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2620
|
+
}
|
|
2621
|
+
function ne(lhs, rhs) {
|
|
2622
|
+
return new Expression(NE).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2314
2623
|
}
|
|
2315
2624
|
function and(lhs, rhs) {
|
|
2316
|
-
return
|
|
2625
|
+
return new Expression(AND).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2317
2626
|
}
|
|
2318
2627
|
function or(lhs, rhs) {
|
|
2319
|
-
return
|
|
2628
|
+
return new Expression(OR).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2629
|
+
}
|
|
2630
|
+
function xor(lhs, rhs) {
|
|
2631
|
+
return new Expression(XOR).withParameter(PARAMETER_IDS.LHS, lhs).withParameter(PARAMETER_IDS.RHS, rhs);
|
|
2320
2632
|
}
|
|
2321
2633
|
function not(value) {
|
|
2322
|
-
return
|
|
2634
|
+
return new Expression(NOT).withParameter(PARAMETER_IDS.BLANK, value);
|
|
2323
2635
|
}
|
|
2324
2636
|
|
|
2325
2637
|
//#endregion
|
|
2326
|
-
//#region src/extension/
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
if (
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
}
|
|
2346
|
-
function revealSetOfSet(envelope, target) {
|
|
2347
|
-
const result = /* @__PURE__ */ new Set();
|
|
2348
|
-
revealSets(envelope, target, /* @__PURE__ */ new Set(), result);
|
|
2349
|
-
return result;
|
|
2350
|
-
}
|
|
2351
|
-
function revealSets(envelope, target, current, result) {
|
|
2352
|
-
const newCurrent = new Set(current);
|
|
2353
|
-
newCurrent.add(envelope.digest());
|
|
2354
|
-
if (containsDigest(target, envelope.digest())) for (const digest of newCurrent) result.add(digest);
|
|
2355
|
-
const envelopeCase = envelope.case();
|
|
2356
|
-
if (envelopeCase.type === "node") {
|
|
2357
|
-
revealSets(envelopeCase.subject, target, newCurrent, result);
|
|
2358
|
-
for (const assertion of envelopeCase.assertions) revealSets(assertion, target, newCurrent, result);
|
|
2359
|
-
} else if (envelopeCase.type === "wrapped") revealSets(envelopeCase.envelope, target, newCurrent, result);
|
|
2360
|
-
else if (envelopeCase.type === "assertion") {
|
|
2361
|
-
const predicate = envelopeCase.assertion.predicate();
|
|
2362
|
-
const object = envelopeCase.assertion.object();
|
|
2363
|
-
revealSets(predicate, target, newCurrent, result);
|
|
2364
|
-
revealSets(object, target, newCurrent, result);
|
|
2638
|
+
//#region src/extension/secret.ts
|
|
2639
|
+
Envelope.prototype.lockSubject = function(method, secret) {
|
|
2640
|
+
const contentKey = _bcts_components.SymmetricKey.new();
|
|
2641
|
+
const componentsKey = _bcts_components.SymmetricKey.fromData(contentKey.data());
|
|
2642
|
+
const encryptedKey = _bcts_components.EncryptedKey.lock(method, secret, componentsKey);
|
|
2643
|
+
return this.encryptSubject(contentKey).addAssertion(_bcts_known_values.HAS_SECRET, encryptedKey);
|
|
2644
|
+
};
|
|
2645
|
+
Envelope.prototype.unlockSubject = function(secret) {
|
|
2646
|
+
const assertions = this.assertionsWithPredicate(_bcts_known_values.HAS_SECRET);
|
|
2647
|
+
for (const assertion of assertions) {
|
|
2648
|
+
const obj = assertion.asObject();
|
|
2649
|
+
if (obj === void 0) continue;
|
|
2650
|
+
if (obj.isObscured()) continue;
|
|
2651
|
+
try {
|
|
2652
|
+
const componentsKey = obj.extractSubject((cbor$1) => _bcts_components.EncryptedKey.fromTaggedCbor(cbor$1)).unlock(secret);
|
|
2653
|
+
const contentKey = _bcts_components.SymmetricKey.from(componentsKey.data());
|
|
2654
|
+
return this.decryptSubject(contentKey);
|
|
2655
|
+
} catch {
|
|
2656
|
+
continue;
|
|
2657
|
+
}
|
|
2365
2658
|
}
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
removeAllFound(envelopeCase.subject, target);
|
|
2378
|
-
for (const assertion of envelopeCase.assertions) {
|
|
2379
|
-
removeAllFound(assertion, target);
|
|
2380
|
-
if (target.size === 0) break;
|
|
2659
|
+
throw EnvelopeError.unknownSecret();
|
|
2660
|
+
};
|
|
2661
|
+
Envelope.prototype.isLockedWithPassword = function() {
|
|
2662
|
+
const assertions = this.assertionsWithPredicate(_bcts_known_values.HAS_SECRET);
|
|
2663
|
+
for (const assertion of assertions) {
|
|
2664
|
+
const obj = assertion.asObject();
|
|
2665
|
+
if (obj === void 0) continue;
|
|
2666
|
+
try {
|
|
2667
|
+
if (obj.extractSubject((cbor$1) => _bcts_components.EncryptedKey.fromTaggedCbor(cbor$1)).isPasswordBased()) return true;
|
|
2668
|
+
} catch {
|
|
2669
|
+
continue;
|
|
2381
2670
|
}
|
|
2382
|
-
} else if (envelopeCase.type === "wrapped") removeAllFound(envelopeCase.envelope, target);
|
|
2383
|
-
else if (envelopeCase.type === "assertion") {
|
|
2384
|
-
const predicate = envelopeCase.assertion.predicate();
|
|
2385
|
-
const object = envelopeCase.assertion.object();
|
|
2386
|
-
removeAllFound(predicate, target);
|
|
2387
|
-
if (target.size > 0) removeAllFound(object, target);
|
|
2388
2671
|
}
|
|
2389
|
-
}
|
|
2390
|
-
function containsDigest(set, digest) {
|
|
2391
|
-
const hexToFind = digest.hex();
|
|
2392
|
-
for (const d of set) if (d.hex() === hexToFind) return true;
|
|
2393
2672
|
return false;
|
|
2394
|
-
}
|
|
2395
|
-
function
|
|
2396
|
-
const
|
|
2397
|
-
for (const
|
|
2398
|
-
|
|
2399
|
-
|
|
2673
|
+
};
|
|
2674
|
+
Envelope.prototype.isLockedWithSshAgent = function() {
|
|
2675
|
+
const assertions = this.assertionsWithPredicate(_bcts_known_values.HAS_SECRET);
|
|
2676
|
+
for (const assertion of assertions) {
|
|
2677
|
+
const obj = assertion.asObject();
|
|
2678
|
+
if (obj === void 0) continue;
|
|
2679
|
+
try {
|
|
2680
|
+
if (obj.extractSubject((cbor$1) => _bcts_components.EncryptedKey.fromTaggedCbor(cbor$1)).isSshAgent()) return true;
|
|
2681
|
+
} catch {
|
|
2682
|
+
continue;
|
|
2683
|
+
}
|
|
2400
2684
|
}
|
|
2401
|
-
|
|
2402
|
-
function isSubset(subset, superset) {
|
|
2403
|
-
for (const digest of subset) if (!containsDigest(superset, digest)) return false;
|
|
2404
|
-
return true;
|
|
2405
|
-
}
|
|
2406
|
-
|
|
2407
|
-
//#endregion
|
|
2408
|
-
//#region src/format/hex.ts
|
|
2409
|
-
Envelope.prototype.hex = function() {
|
|
2410
|
-
const bytes = this.cborBytes();
|
|
2411
|
-
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
2685
|
+
return false;
|
|
2412
2686
|
};
|
|
2413
|
-
Envelope.prototype.
|
|
2414
|
-
|
|
2687
|
+
Envelope.prototype.addSecret = function(method, secret, contentKey) {
|
|
2688
|
+
const componentsKey = _bcts_components.SymmetricKey.fromData(contentKey.data());
|
|
2689
|
+
const encryptedKey = _bcts_components.EncryptedKey.lock(method, secret, componentsKey);
|
|
2690
|
+
return this.addAssertion(_bcts_known_values.HAS_SECRET, encryptedKey);
|
|
2415
2691
|
};
|
|
2692
|
+
Envelope.prototype.lock = function(method, secret) {
|
|
2693
|
+
return this.wrap().lockSubject(method, secret);
|
|
2694
|
+
};
|
|
2695
|
+
Envelope.prototype.unlock = function(secret) {
|
|
2696
|
+
return this.unlockSubject(secret).tryUnwrap();
|
|
2697
|
+
};
|
|
2698
|
+
const registerSecretExtension = () => {};
|
|
2416
2699
|
|
|
2417
2700
|
//#endregion
|
|
2418
|
-
//#region src/
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2701
|
+
//#region src/extension/sskr.ts
|
|
2702
|
+
const addSskrShare = (envelope, share) => {
|
|
2703
|
+
return envelope.addAssertion(_bcts_known_values.SSKR_SHARE, share);
|
|
2704
|
+
};
|
|
2705
|
+
Envelope.prototype.sskrSplit = function(spec, contentKey) {
|
|
2706
|
+
const shareGroups = (0, _bcts_components.sskrGenerateShares)(spec, _bcts_components.SSKRSecret.new(contentKey.data()));
|
|
2707
|
+
const result = [];
|
|
2708
|
+
for (const group of shareGroups) {
|
|
2709
|
+
const groupResult = [];
|
|
2710
|
+
for (const share of group) {
|
|
2711
|
+
const shareEnvelope = addSskrShare(this, share);
|
|
2712
|
+
groupResult.push(shareEnvelope);
|
|
2713
|
+
}
|
|
2714
|
+
result.push(groupResult);
|
|
2427
2715
|
}
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2716
|
+
return result;
|
|
2717
|
+
};
|
|
2718
|
+
Envelope.prototype.sskrSplitFlattened = function(spec, contentKey) {
|
|
2719
|
+
return this.sskrSplit(spec, contentKey).flat();
|
|
2720
|
+
};
|
|
2721
|
+
Envelope.prototype.sskrSplitUsing = function(spec, contentKey, rng) {
|
|
2722
|
+
const shareGroups = (0, _bcts_components.sskrGenerateUsing)(spec, _bcts_components.SSKRSecret.new(contentKey.data()), rng);
|
|
2723
|
+
const result = [];
|
|
2724
|
+
for (const group of shareGroups) {
|
|
2725
|
+
const groupResult = [];
|
|
2726
|
+
for (const shareData of group) {
|
|
2727
|
+
const share = _bcts_components.SSKRShareCbor.fromData(shareData);
|
|
2728
|
+
const shareEnvelope = addSskrShare(this, share);
|
|
2729
|
+
groupResult.push(shareEnvelope);
|
|
2435
2730
|
}
|
|
2436
|
-
|
|
2731
|
+
result.push(groupResult);
|
|
2437
2732
|
}
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
if (
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2733
|
+
return result;
|
|
2734
|
+
};
|
|
2735
|
+
const extractSskrSharesGrouped = (envelopes) => {
|
|
2736
|
+
const result = /* @__PURE__ */ new Map();
|
|
2737
|
+
for (const envelope of envelopes) {
|
|
2738
|
+
const assertions = envelope.assertionsWithPredicate(_bcts_known_values.SSKR_SHARE);
|
|
2739
|
+
for (const assertion of assertions) {
|
|
2740
|
+
const obj = assertion.asObject();
|
|
2741
|
+
if (obj === void 0) continue;
|
|
2742
|
+
if (obj.isObscured()) continue;
|
|
2743
|
+
try {
|
|
2744
|
+
const share = obj.extractSubject((cbor$1) => _bcts_components.SSKRShareCbor.fromTaggedCbor(cbor$1));
|
|
2745
|
+
const identifier = share.identifier();
|
|
2746
|
+
const existing = result.get(identifier);
|
|
2747
|
+
if (existing !== void 0) existing.push(share);
|
|
2748
|
+
else result.set(identifier, [share]);
|
|
2749
|
+
} catch {
|
|
2750
|
+
continue;
|
|
2456
2751
|
}
|
|
2457
2752
|
}
|
|
2458
2753
|
}
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
if (
|
|
2463
|
-
|
|
2464
|
-
try {
|
|
2465
|
-
|
|
2754
|
+
return result;
|
|
2755
|
+
};
|
|
2756
|
+
Envelope.sskrJoin = function(envelopes) {
|
|
2757
|
+
if (envelopes.length === 0) throw EnvelopeError.invalidShares();
|
|
2758
|
+
const groupedShares = extractSskrSharesGrouped(envelopes);
|
|
2759
|
+
for (const shares of groupedShares.values()) try {
|
|
2760
|
+
const secret = (0, _bcts_components.sskrCombineShares)(shares);
|
|
2761
|
+
const contentKey = _bcts_components.SymmetricKey.from(secret.getData());
|
|
2762
|
+
return envelopes[0].decryptSubject(contentKey).subject();
|
|
2466
2763
|
} catch {
|
|
2467
|
-
|
|
2764
|
+
continue;
|
|
2468
2765
|
}
|
|
2469
|
-
|
|
2470
|
-
Envelope.prototype.diagnostic = function() {
|
|
2471
|
-
return cborToDiagnostic(this.taggedCbor());
|
|
2766
|
+
throw EnvelopeError.invalidShares();
|
|
2472
2767
|
};
|
|
2768
|
+
const registerSskrExtension = () => {};
|
|
2473
2769
|
|
|
2474
2770
|
//#endregion
|
|
2475
|
-
//#region src/
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2771
|
+
//#region src/extension/request.ts
|
|
2772
|
+
/**
|
|
2773
|
+
* Request type for distributed function calls.
|
|
2774
|
+
*
|
|
2775
|
+
* Ported from bc-envelope-rust/src/extension/expressions/request.rs
|
|
2776
|
+
*
|
|
2777
|
+
* A Request represents a message requesting execution of a function with
|
|
2778
|
+
* parameters. Requests are part of the expression system that enables
|
|
2779
|
+
* distributed function calls and communication between systems.
|
|
2780
|
+
*
|
|
2781
|
+
* Each request:
|
|
2782
|
+
* - Contains a body (an Expression) that represents the function to be executed
|
|
2783
|
+
* - Has a unique identifier (ARID) for tracking and correlation
|
|
2784
|
+
* - May include optional metadata like a note and timestamp
|
|
2785
|
+
*
|
|
2786
|
+
* Requests are designed to be paired with Response objects that contain the
|
|
2787
|
+
* results of executing the requested function.
|
|
2788
|
+
*
|
|
2789
|
+
* When serialized to an envelope, requests are tagged with REQUEST tag.
|
|
2790
|
+
*/
|
|
2791
|
+
/**
|
|
2792
|
+
* A Request represents a message requesting execution of a function with parameters.
|
|
2793
|
+
*
|
|
2794
|
+
* @example
|
|
2795
|
+
* ```typescript
|
|
2796
|
+
* import { Request, ARID } from '@bcts/envelope';
|
|
2797
|
+
*
|
|
2798
|
+
* // Create a random request ID
|
|
2799
|
+
* const requestId = ARID.new();
|
|
2800
|
+
*
|
|
2801
|
+
* // Create a request to execute a function with parameters
|
|
2802
|
+
* const request = Request.new("getBalance", requestId)
|
|
2803
|
+
* .withParameter("account", "alice")
|
|
2804
|
+
* .withParameter("currency", "USD")
|
|
2805
|
+
* .withNote("Monthly balance check");
|
|
2806
|
+
*
|
|
2807
|
+
* // Convert to an envelope
|
|
2808
|
+
* const envelope = request.toEnvelope();
|
|
2809
|
+
* ```
|
|
2810
|
+
*/
|
|
2811
|
+
var Request = class Request {
|
|
2812
|
+
#body;
|
|
2813
|
+
#id;
|
|
2814
|
+
#note;
|
|
2815
|
+
#date;
|
|
2816
|
+
constructor(body, id, note = "", date) {
|
|
2817
|
+
this.#body = body;
|
|
2818
|
+
this.#id = id;
|
|
2819
|
+
this.#note = note;
|
|
2820
|
+
this.#date = date;
|
|
2821
|
+
}
|
|
2822
|
+
/**
|
|
2823
|
+
* Creates a new request with the specified expression body and ID.
|
|
2824
|
+
*/
|
|
2825
|
+
static newWithBody(body, id) {
|
|
2826
|
+
return new Request(body, id);
|
|
2827
|
+
}
|
|
2828
|
+
/**
|
|
2829
|
+
* Creates a new request with a function and ID.
|
|
2830
|
+
*
|
|
2831
|
+
* This is a convenience method that creates an expression from the
|
|
2832
|
+
* function and then creates a request with that expression.
|
|
2833
|
+
*/
|
|
2834
|
+
static new(func, id) {
|
|
2835
|
+
const f = typeof func === "string" ? Function.newNamed(func) : typeof func === "number" ? Function.newKnown(func) : func;
|
|
2836
|
+
return Request.newWithBody(new Expression(f), id);
|
|
2837
|
+
}
|
|
2838
|
+
/**
|
|
2839
|
+
* Returns a human-readable summary of the request.
|
|
2840
|
+
*/
|
|
2841
|
+
summary() {
|
|
2842
|
+
return `id: ${this.#id.shortDescription()}, body: ${this.#body.envelope().formatFlat()}`;
|
|
2843
|
+
}
|
|
2844
|
+
withParameter(param, value) {
|
|
2845
|
+
this.#body.withParameter(param, value);
|
|
2846
|
+
return this;
|
|
2847
|
+
}
|
|
2848
|
+
withNote(note) {
|
|
2849
|
+
this.#note = note;
|
|
2850
|
+
return this;
|
|
2851
|
+
}
|
|
2852
|
+
withDate(date) {
|
|
2853
|
+
this.#date = date;
|
|
2854
|
+
return this;
|
|
2855
|
+
}
|
|
2856
|
+
body() {
|
|
2857
|
+
return this.#body;
|
|
2858
|
+
}
|
|
2859
|
+
id() {
|
|
2860
|
+
return this.#id;
|
|
2861
|
+
}
|
|
2862
|
+
note() {
|
|
2863
|
+
return this.#note;
|
|
2864
|
+
}
|
|
2865
|
+
date() {
|
|
2866
|
+
return this.#date;
|
|
2867
|
+
}
|
|
2868
|
+
function() {
|
|
2869
|
+
return this.#body.function();
|
|
2870
|
+
}
|
|
2871
|
+
expressionEnvelope() {
|
|
2872
|
+
return this.#body.envelope();
|
|
2873
|
+
}
|
|
2874
|
+
/**
|
|
2875
|
+
* Converts the request to an envelope.
|
|
2876
|
+
*
|
|
2877
|
+
* The envelope's subject is the request's ID tagged with TAG_REQUEST,
|
|
2878
|
+
* and assertions include the request's body, note (if not empty), and date (if present).
|
|
2879
|
+
*/
|
|
2880
|
+
toEnvelope() {
|
|
2881
|
+
const taggedArid = (0, _bcts_dcbor.toTaggedValue)(_bcts_tags.REQUEST, this.#id.untaggedCbor());
|
|
2882
|
+
let envelope = Envelope.newLeaf(taggedArid).addAssertion(_bcts_known_values.BODY, this.#body.envelope());
|
|
2883
|
+
if (this.#note !== "") envelope = envelope.addAssertion(_bcts_known_values.NOTE, this.#note);
|
|
2884
|
+
if (this.#date !== void 0) envelope = envelope.addAssertion(_bcts_known_values.DATE, this.#date.toISOString());
|
|
2885
|
+
return envelope;
|
|
2886
|
+
}
|
|
2887
|
+
/**
|
|
2888
|
+
* Creates a request from an envelope.
|
|
2889
|
+
*/
|
|
2890
|
+
static fromEnvelope(envelope, expectedFunction) {
|
|
2891
|
+
const bodyEnvelope = envelope.objectForPredicate(_bcts_known_values.BODY);
|
|
2892
|
+
if (bodyEnvelope === void 0) throw EnvelopeError.general("Request envelope missing body");
|
|
2893
|
+
const body = Expression.fromEnvelope(bodyEnvelope);
|
|
2894
|
+
if (expectedFunction !== void 0 && !body.function().equals(expectedFunction)) throw EnvelopeError.general("Request function mismatch");
|
|
2895
|
+
if (envelope.subject().asLeaf() === void 0) throw EnvelopeError.general("Request envelope has invalid subject");
|
|
2896
|
+
const id = _bcts_components.ARID.new();
|
|
2897
|
+
let note = "";
|
|
2898
|
+
try {
|
|
2899
|
+
const noteObj = envelope.objectForPredicate(_bcts_known_values.NOTE);
|
|
2900
|
+
if (noteObj !== void 0) note = noteObj.asText() ?? "";
|
|
2901
|
+
} catch {}
|
|
2902
|
+
let date;
|
|
2903
|
+
try {
|
|
2904
|
+
const dateObj = envelope.objectForPredicate(_bcts_known_values.DATE);
|
|
2905
|
+
if (dateObj !== void 0) {
|
|
2906
|
+
const dateStr = dateObj.asText();
|
|
2907
|
+
if (dateStr !== void 0) date = new Date(dateStr);
|
|
2908
|
+
}
|
|
2909
|
+
} catch {}
|
|
2910
|
+
return new Request(body, id, note, date);
|
|
2911
|
+
}
|
|
2912
|
+
/**
|
|
2913
|
+
* Returns a string representation of the request.
|
|
2914
|
+
*/
|
|
2915
|
+
toString() {
|
|
2916
|
+
return `Request(${this.summary()})`;
|
|
2917
|
+
}
|
|
2918
|
+
/**
|
|
2919
|
+
* Checks equality with another request.
|
|
2920
|
+
*/
|
|
2921
|
+
equals(other) {
|
|
2922
|
+
return this.#id.equals(other.#id) && this.#note === other.#note && this.#date?.getTime() === other.#date?.getTime();
|
|
2923
|
+
}
|
|
2924
|
+
};
|
|
2925
|
+
|
|
2926
|
+
//#endregion
|
|
2927
|
+
//#region src/extension/response.ts
|
|
2928
|
+
/**
|
|
2929
|
+
* Response type for distributed function calls.
|
|
2930
|
+
*
|
|
2931
|
+
* Ported from bc-envelope-rust/src/extension/expressions/response.rs
|
|
2932
|
+
*
|
|
2933
|
+
* A Response represents a reply to a Request containing either a
|
|
2934
|
+
* successful result or an error.
|
|
2935
|
+
*
|
|
2936
|
+
* Responses are part of the expression system that enables distributed
|
|
2937
|
+
* function calls. Each response contains:
|
|
2938
|
+
* - A reference to the original request's ID (ARID) for correlation
|
|
2939
|
+
* - Either a successful result or an error message
|
|
2940
|
+
*
|
|
2941
|
+
* When serialized to an envelope, responses are tagged with RESPONSE tag.
|
|
2942
|
+
*/
|
|
2943
|
+
/**
|
|
2944
|
+
* A Response represents a reply to a Request containing either a
|
|
2945
|
+
* successful result or an error.
|
|
2946
|
+
*
|
|
2947
|
+
* @example
|
|
2948
|
+
* ```typescript
|
|
2949
|
+
* import { Response, ARID } from '@bcts/envelope';
|
|
2950
|
+
*
|
|
2951
|
+
* // Create a request ID (normally this would come from the original request)
|
|
2952
|
+
* const requestId = ARID.new();
|
|
2953
|
+
*
|
|
2954
|
+
* // Create a successful response
|
|
2955
|
+
* const successResponse = Response.newSuccess(requestId)
|
|
2956
|
+
* .withResult("Transaction completed");
|
|
2957
|
+
*
|
|
2958
|
+
* // Create an error response
|
|
2959
|
+
* const errorResponse = Response.newFailure(requestId)
|
|
2960
|
+
* .withError("Insufficient funds");
|
|
2961
|
+
*
|
|
2962
|
+
* // Convert to envelopes
|
|
2963
|
+
* const successEnvelope = successResponse.toEnvelope();
|
|
2964
|
+
* const errorEnvelope = errorResponse.toEnvelope();
|
|
2965
|
+
* ```
|
|
2966
|
+
*/
|
|
2967
|
+
var Response = class Response {
|
|
2968
|
+
#result;
|
|
2969
|
+
constructor(result) {
|
|
2970
|
+
this.#result = result;
|
|
2971
|
+
}
|
|
2972
|
+
/**
|
|
2973
|
+
* Creates a new successful response with the specified request ID.
|
|
2974
|
+
*
|
|
2975
|
+
* By default, the result will be the 'OK' known value. Use `withResult`
|
|
2976
|
+
* to set a specific result value.
|
|
2977
|
+
*/
|
|
2978
|
+
static newSuccess(id) {
|
|
2979
|
+
return new Response({
|
|
2980
|
+
ok: true,
|
|
2981
|
+
id,
|
|
2982
|
+
result: Response.ok()
|
|
2983
|
+
});
|
|
2984
|
+
}
|
|
2985
|
+
/**
|
|
2986
|
+
* Creates a new failure response with the specified request ID.
|
|
2987
|
+
*
|
|
2988
|
+
* By default, the error will be the 'Unknown' known value. Use
|
|
2989
|
+
* `withError` to set a specific error message.
|
|
2990
|
+
*/
|
|
2991
|
+
static newFailure(id) {
|
|
2992
|
+
return new Response({
|
|
2993
|
+
ok: false,
|
|
2994
|
+
id,
|
|
2995
|
+
error: Response.unknown()
|
|
2996
|
+
});
|
|
2997
|
+
}
|
|
2998
|
+
/**
|
|
2999
|
+
* Creates a new early failure response without a request ID.
|
|
3000
|
+
*
|
|
3001
|
+
* An early failure occurs when the error happens before the request
|
|
3002
|
+
* has been fully processed, so the request ID is not known.
|
|
3003
|
+
*/
|
|
3004
|
+
static newEarlyFailure() {
|
|
3005
|
+
return new Response({
|
|
3006
|
+
ok: false,
|
|
3007
|
+
id: void 0,
|
|
3008
|
+
error: Response.unknown()
|
|
3009
|
+
});
|
|
3010
|
+
}
|
|
3011
|
+
/**
|
|
3012
|
+
* Creates an envelope containing the 'Unknown' known value.
|
|
3013
|
+
*/
|
|
3014
|
+
static unknown() {
|
|
3015
|
+
return Envelope.new(_bcts_known_values.UNKNOWN_VALUE);
|
|
3016
|
+
}
|
|
3017
|
+
/**
|
|
3018
|
+
* Creates an envelope containing the 'OK' known value.
|
|
3019
|
+
*/
|
|
3020
|
+
static ok() {
|
|
3021
|
+
return Envelope.new(_bcts_known_values.OK_VALUE);
|
|
3022
|
+
}
|
|
3023
|
+
/**
|
|
3024
|
+
* Returns a human-readable summary of the response.
|
|
3025
|
+
*/
|
|
3026
|
+
summary() {
|
|
3027
|
+
if (this.#result.ok) return `id: ${this.#result.id.shortDescription()}, result: ${this.#result.result.formatFlat()}`;
|
|
3028
|
+
else return `id: ${this.#result.id !== void 0 ? this.#result.id.shortDescription() : "'Unknown'"}, error: ${this.#result.error.formatFlat()}`;
|
|
3029
|
+
}
|
|
3030
|
+
withResult(result) {
|
|
3031
|
+
if (!this.#result.ok) throw new Error("Cannot set result on a failed response");
|
|
3032
|
+
this.#result = {
|
|
3033
|
+
ok: true,
|
|
3034
|
+
id: this.#result.id,
|
|
3035
|
+
result: Envelope.new(result)
|
|
3036
|
+
};
|
|
3037
|
+
return this;
|
|
3038
|
+
}
|
|
3039
|
+
withOptionalResult(result) {
|
|
3040
|
+
if (result !== void 0) return this.withResult(result);
|
|
3041
|
+
return this.withResult(null);
|
|
3042
|
+
}
|
|
3043
|
+
withError(error) {
|
|
3044
|
+
if (this.#result.ok) throw new Error("Cannot set error on a successful response");
|
|
3045
|
+
this.#result = {
|
|
3046
|
+
ok: false,
|
|
3047
|
+
id: this.#result.id,
|
|
3048
|
+
error: Envelope.new(error)
|
|
3049
|
+
};
|
|
3050
|
+
return this;
|
|
3051
|
+
}
|
|
3052
|
+
withOptionalError(error) {
|
|
3053
|
+
if (error !== void 0) return this.withError(error);
|
|
3054
|
+
return this;
|
|
3055
|
+
}
|
|
3056
|
+
isOk() {
|
|
3057
|
+
return this.#result.ok;
|
|
3058
|
+
}
|
|
3059
|
+
isErr() {
|
|
3060
|
+
return !this.#result.ok;
|
|
3061
|
+
}
|
|
3062
|
+
id() {
|
|
3063
|
+
return this.#result.id;
|
|
3064
|
+
}
|
|
3065
|
+
expectId() {
|
|
3066
|
+
const id = this.id();
|
|
3067
|
+
if (id === void 0) throw new Error("Expected an ID");
|
|
3068
|
+
return id;
|
|
3069
|
+
}
|
|
3070
|
+
result() {
|
|
3071
|
+
if (!this.#result.ok) throw EnvelopeError.general("Cannot get result from failed response");
|
|
3072
|
+
return this.#result.result;
|
|
3073
|
+
}
|
|
3074
|
+
error() {
|
|
3075
|
+
if (this.#result.ok) throw EnvelopeError.general("Cannot get error from successful response");
|
|
3076
|
+
return this.#result.error;
|
|
3077
|
+
}
|
|
3078
|
+
/**
|
|
3079
|
+
* Extracts a typed result value from a successful response.
|
|
3080
|
+
*/
|
|
3081
|
+
extractResult(decoder) {
|
|
3082
|
+
return this.result().extractSubject(decoder);
|
|
3083
|
+
}
|
|
3084
|
+
/**
|
|
3085
|
+
* Extracts a typed error value from a failure response.
|
|
3086
|
+
*/
|
|
3087
|
+
extractError(decoder) {
|
|
3088
|
+
return this.error().extractSubject(decoder);
|
|
3089
|
+
}
|
|
3090
|
+
/**
|
|
3091
|
+
* Converts the response to an envelope.
|
|
3092
|
+
*
|
|
3093
|
+
* Successful responses have the request ID as the subject and a 'result'
|
|
3094
|
+
* assertion. Failure responses have the request ID (or 'Unknown' if not known)
|
|
3095
|
+
* as the subject and an 'error' assertion.
|
|
3096
|
+
*/
|
|
3097
|
+
toEnvelope() {
|
|
3098
|
+
if (this.#result.ok) {
|
|
3099
|
+
const taggedArid = (0, _bcts_dcbor.toTaggedValue)(_bcts_tags.RESPONSE, this.#result.id.untaggedCbor());
|
|
3100
|
+
return Envelope.newLeaf(taggedArid).addAssertion(_bcts_known_values.RESULT, this.#result.result);
|
|
3101
|
+
} else {
|
|
3102
|
+
let subject;
|
|
3103
|
+
if (this.#result.id !== void 0) {
|
|
3104
|
+
const taggedArid = (0, _bcts_dcbor.toTaggedValue)(_bcts_tags.RESPONSE, this.#result.id.untaggedCbor());
|
|
3105
|
+
subject = Envelope.newLeaf(taggedArid);
|
|
3106
|
+
} else {
|
|
3107
|
+
const taggedUnknown = (0, _bcts_dcbor.toTaggedValue)(_bcts_tags.RESPONSE, _bcts_known_values.UNKNOWN_VALUE.untaggedCbor());
|
|
3108
|
+
subject = Envelope.newLeaf(taggedUnknown);
|
|
3109
|
+
}
|
|
3110
|
+
return subject.addAssertion(_bcts_known_values.ERROR, this.#result.error);
|
|
3111
|
+
}
|
|
3112
|
+
}
|
|
3113
|
+
/**
|
|
3114
|
+
* Creates a response from an envelope.
|
|
3115
|
+
*/
|
|
3116
|
+
static fromEnvelope(envelope) {
|
|
3117
|
+
let hasResult = false;
|
|
3118
|
+
let hasError = false;
|
|
3119
|
+
try {
|
|
3120
|
+
hasResult = envelope.objectForPredicate(_bcts_known_values.RESULT) !== void 0;
|
|
3121
|
+
} catch {}
|
|
3122
|
+
try {
|
|
3123
|
+
hasError = envelope.objectForPredicate(_bcts_known_values.ERROR) !== void 0;
|
|
3124
|
+
} catch {}
|
|
3125
|
+
if (hasResult === hasError) throw EnvelopeError.invalidResponse();
|
|
3126
|
+
const id = _bcts_components.ARID.new();
|
|
3127
|
+
if (hasResult) return new Response({
|
|
3128
|
+
ok: true,
|
|
3129
|
+
id,
|
|
3130
|
+
result: envelope.objectForPredicate(_bcts_known_values.RESULT) ?? Response.ok()
|
|
3131
|
+
});
|
|
3132
|
+
else return new Response({
|
|
3133
|
+
ok: false,
|
|
3134
|
+
id,
|
|
3135
|
+
error: envelope.objectForPredicate(_bcts_known_values.ERROR) ?? Response.unknown()
|
|
3136
|
+
});
|
|
3137
|
+
}
|
|
3138
|
+
/**
|
|
3139
|
+
* Returns a string representation of the response.
|
|
3140
|
+
*/
|
|
3141
|
+
toString() {
|
|
3142
|
+
return `Response(${this.summary()})`;
|
|
3143
|
+
}
|
|
3144
|
+
/**
|
|
3145
|
+
* Checks equality with another response.
|
|
3146
|
+
*/
|
|
3147
|
+
equals(other) {
|
|
3148
|
+
if (this.#result.ok !== other.#result.ok) return false;
|
|
3149
|
+
if (this.#result.ok && other.#result.ok) return this.#result.id.equals(other.#result.id);
|
|
3150
|
+
if (!this.#result.ok && !other.#result.ok) {
|
|
3151
|
+
if (this.#result.id === void 0 && other.#result.id === void 0) return true;
|
|
3152
|
+
if (this.#result.id !== void 0 && other.#result.id !== void 0) return this.#result.id.equals(other.#result.id);
|
|
3153
|
+
return false;
|
|
3154
|
+
}
|
|
3155
|
+
return false;
|
|
3156
|
+
}
|
|
3157
|
+
};
|
|
3158
|
+
|
|
3159
|
+
//#endregion
|
|
3160
|
+
//#region src/extension/event.ts
|
|
3161
|
+
/**
|
|
3162
|
+
* Event type for notifications and messages.
|
|
3163
|
+
*
|
|
3164
|
+
* Ported from bc-envelope-rust/src/extension/expressions/event.rs
|
|
3165
|
+
*
|
|
3166
|
+
* An Event represents a notification or message that doesn't expect a
|
|
3167
|
+
* response.
|
|
3168
|
+
*
|
|
3169
|
+
* Unlike Request and Response which form a pair, an Event is a
|
|
3170
|
+
* standalone message that can be used for broadcasting information, logging,
|
|
3171
|
+
* or publishing notifications. Events are used when the sender does not expect
|
|
3172
|
+
* or require a response from the recipients.
|
|
3173
|
+
*
|
|
3174
|
+
* Each event contains:
|
|
3175
|
+
* - Content of a generic type that holds the event payload
|
|
3176
|
+
* - A unique identifier (ARID) for tracking and correlation
|
|
3177
|
+
* - Optional metadata like a note and timestamp
|
|
3178
|
+
*
|
|
3179
|
+
* When serialized to an envelope, events are tagged with EVENT tag.
|
|
3180
|
+
*/
|
|
3181
|
+
/**
|
|
3182
|
+
* An Event represents a notification or message that doesn't expect a response.
|
|
3183
|
+
*
|
|
3184
|
+
* @example
|
|
3185
|
+
* ```typescript
|
|
3186
|
+
* import { Event, ARID } from '@bcts/envelope';
|
|
3187
|
+
*
|
|
3188
|
+
* // Create a status update event
|
|
3189
|
+
* const eventId = ARID.new();
|
|
3190
|
+
* const timestamp = new Date("2024-08-15T13:45:30Z");
|
|
3191
|
+
*
|
|
3192
|
+
* const statusEvent = Event.new("System online", eventId)
|
|
3193
|
+
* .withNote("Regular status update")
|
|
3194
|
+
* .withDate(timestamp);
|
|
3195
|
+
*
|
|
3196
|
+
* // Convert to an envelope for transmission
|
|
3197
|
+
* const envelope = statusEvent.toEnvelope();
|
|
3198
|
+
* ```
|
|
3199
|
+
*
|
|
3200
|
+
* @typeParam T - The type of content this event carries
|
|
3201
|
+
*/
|
|
3202
|
+
var Event = class Event {
|
|
3203
|
+
#content;
|
|
3204
|
+
#id;
|
|
3205
|
+
#note;
|
|
3206
|
+
#date;
|
|
3207
|
+
constructor(content, id, note = "", date) {
|
|
3208
|
+
this.#content = content;
|
|
3209
|
+
this.#id = id;
|
|
3210
|
+
this.#note = note;
|
|
3211
|
+
this.#date = date;
|
|
3212
|
+
}
|
|
3213
|
+
/**
|
|
3214
|
+
* Creates a new event with the specified content and ID.
|
|
3215
|
+
*/
|
|
3216
|
+
static new(content, id) {
|
|
3217
|
+
return new Event(content, id);
|
|
3218
|
+
}
|
|
3219
|
+
/**
|
|
3220
|
+
* Returns a human-readable summary of the event.
|
|
3221
|
+
*/
|
|
3222
|
+
summary() {
|
|
3223
|
+
const contentEnvelope = Envelope.new(this.#content);
|
|
3224
|
+
return `id: ${this.#id.shortDescription()}, content: ${contentEnvelope.formatFlat()}`;
|
|
3225
|
+
}
|
|
3226
|
+
withNote(note) {
|
|
3227
|
+
this.#note = note;
|
|
3228
|
+
return this;
|
|
3229
|
+
}
|
|
3230
|
+
withDate(date) {
|
|
3231
|
+
this.#date = date;
|
|
3232
|
+
return this;
|
|
3233
|
+
}
|
|
3234
|
+
content() {
|
|
3235
|
+
return this.#content;
|
|
3236
|
+
}
|
|
3237
|
+
id() {
|
|
3238
|
+
return this.#id;
|
|
3239
|
+
}
|
|
3240
|
+
note() {
|
|
3241
|
+
return this.#note;
|
|
3242
|
+
}
|
|
3243
|
+
date() {
|
|
3244
|
+
return this.#date;
|
|
3245
|
+
}
|
|
3246
|
+
/**
|
|
3247
|
+
* Converts the event to an envelope.
|
|
3248
|
+
*
|
|
3249
|
+
* The envelope's subject is the event's ID tagged with TAG_EVENT,
|
|
3250
|
+
* and assertions include the event's content, note (if not empty), and date
|
|
3251
|
+
* (if present).
|
|
3252
|
+
*/
|
|
3253
|
+
toEnvelope() {
|
|
3254
|
+
const taggedArid = (0, _bcts_dcbor.toTaggedValue)(_bcts_tags.EVENT, this.#id.untaggedCbor());
|
|
3255
|
+
const contentEnvelope = Envelope.new(this.#content);
|
|
3256
|
+
let envelope = Envelope.newLeaf(taggedArid).addAssertion(_bcts_known_values.CONTENT, contentEnvelope);
|
|
3257
|
+
if (this.#note !== "") envelope = envelope.addAssertion(_bcts_known_values.NOTE, this.#note);
|
|
3258
|
+
if (this.#date !== void 0) envelope = envelope.addAssertion(_bcts_known_values.DATE, this.#date.toISOString());
|
|
3259
|
+
return envelope;
|
|
3260
|
+
}
|
|
3261
|
+
/**
|
|
3262
|
+
* Creates an event from an envelope.
|
|
3263
|
+
*
|
|
3264
|
+
* @typeParam T - The type to extract the content as
|
|
3265
|
+
*/
|
|
3266
|
+
static fromEnvelope(envelope, contentExtractor) {
|
|
3267
|
+
const contentEnvelope = envelope.objectForPredicate(_bcts_known_values.CONTENT);
|
|
3268
|
+
if (contentEnvelope === void 0) throw EnvelopeError.general("Event envelope missing content");
|
|
3269
|
+
const content = contentExtractor(contentEnvelope);
|
|
3270
|
+
const id = _bcts_components.ARID.new();
|
|
3271
|
+
let note = "";
|
|
3272
|
+
try {
|
|
3273
|
+
const noteObj = envelope.objectForPredicate(_bcts_known_values.NOTE);
|
|
3274
|
+
if (noteObj !== void 0) note = noteObj.asText() ?? "";
|
|
3275
|
+
} catch {}
|
|
3276
|
+
let date;
|
|
3277
|
+
try {
|
|
3278
|
+
const dateObj = envelope.objectForPredicate(_bcts_known_values.DATE);
|
|
3279
|
+
if (dateObj !== void 0) {
|
|
3280
|
+
const dateStr = dateObj.asText();
|
|
3281
|
+
if (dateStr !== void 0) date = new Date(dateStr);
|
|
3282
|
+
}
|
|
3283
|
+
} catch {}
|
|
3284
|
+
return new Event(content, id, note, date);
|
|
3285
|
+
}
|
|
3286
|
+
/**
|
|
3287
|
+
* Creates a string event from an envelope.
|
|
3288
|
+
*/
|
|
3289
|
+
static stringFromEnvelope(envelope) {
|
|
3290
|
+
return Event.fromEnvelope(envelope, (env) => env.asText() ?? "");
|
|
3291
|
+
}
|
|
3292
|
+
/**
|
|
3293
|
+
* Returns a string representation of the event.
|
|
3294
|
+
*/
|
|
3295
|
+
toString() {
|
|
3296
|
+
return `Event(${this.summary()})`;
|
|
3297
|
+
}
|
|
3298
|
+
/**
|
|
3299
|
+
* Checks equality with another event.
|
|
3300
|
+
*/
|
|
3301
|
+
equals(other) {
|
|
3302
|
+
return this.#id.equals(other.#id) && this.#note === other.#note && this.#date?.getTime() === other.#date?.getTime();
|
|
3303
|
+
}
|
|
3304
|
+
};
|
|
3305
|
+
|
|
3306
|
+
//#endregion
|
|
3307
|
+
//#region src/extension/proof.ts
|
|
3308
|
+
if (Envelope?.prototype) {
|
|
3309
|
+
Envelope.prototype.proofContainsSet = function(target) {
|
|
3310
|
+
const revealSet = revealSetOfSet(this, target);
|
|
3311
|
+
if (!isSubset(target, revealSet)) return;
|
|
3312
|
+
return this.elideRevealingSet(revealSet).elideRemovingSet(target);
|
|
3313
|
+
};
|
|
3314
|
+
Envelope.prototype.proofContainsTarget = function(target) {
|
|
3315
|
+
const targetSet = new Set([target.digest()]);
|
|
3316
|
+
return this.proofContainsSet(targetSet);
|
|
3317
|
+
};
|
|
3318
|
+
Envelope.prototype.confirmContainsSet = function(target, proof) {
|
|
3319
|
+
if (this.digest().hex() !== proof.digest().hex()) return false;
|
|
3320
|
+
return containsAll(proof, target);
|
|
3321
|
+
};
|
|
3322
|
+
Envelope.prototype.confirmContainsTarget = function(target, proof) {
|
|
3323
|
+
const targetSet = new Set([target.digest()]);
|
|
3324
|
+
return this.confirmContainsSet(targetSet, proof);
|
|
3325
|
+
};
|
|
3326
|
+
}
|
|
3327
|
+
function revealSetOfSet(envelope, target) {
|
|
3328
|
+
const result = /* @__PURE__ */ new Set();
|
|
3329
|
+
revealSets(envelope, target, /* @__PURE__ */ new Set(), result);
|
|
3330
|
+
return result;
|
|
3331
|
+
}
|
|
3332
|
+
function revealSets(envelope, target, current, result) {
|
|
3333
|
+
const newCurrent = new Set(current);
|
|
3334
|
+
newCurrent.add(envelope.digest());
|
|
3335
|
+
if (containsDigest$1(target, envelope.digest())) for (const digest of newCurrent) result.add(digest);
|
|
3336
|
+
const envelopeCase = envelope.case();
|
|
3337
|
+
if (envelopeCase.type === "node") {
|
|
3338
|
+
revealSets(envelopeCase.subject, target, newCurrent, result);
|
|
3339
|
+
for (const assertion of envelopeCase.assertions) revealSets(assertion, target, newCurrent, result);
|
|
3340
|
+
} else if (envelopeCase.type === "wrapped") revealSets(envelopeCase.envelope, target, newCurrent, result);
|
|
3341
|
+
else if (envelopeCase.type === "assertion") {
|
|
3342
|
+
const predicate = envelopeCase.assertion.predicate();
|
|
3343
|
+
const object = envelopeCase.assertion.object();
|
|
3344
|
+
revealSets(predicate, target, newCurrent, result);
|
|
3345
|
+
revealSets(object, target, newCurrent, result);
|
|
3346
|
+
}
|
|
3347
|
+
}
|
|
3348
|
+
function containsAll(envelope, target) {
|
|
3349
|
+
const targetCopy = new Set(target);
|
|
3350
|
+
removeAllFound(envelope, targetCopy);
|
|
3351
|
+
return targetCopy.size === 0;
|
|
3352
|
+
}
|
|
3353
|
+
function removeAllFound(envelope, target) {
|
|
3354
|
+
if (containsDigest$1(target, envelope.digest())) removeDigest(target, envelope.digest());
|
|
3355
|
+
if (target.size === 0) return;
|
|
3356
|
+
const envelopeCase = envelope.case();
|
|
3357
|
+
if (envelopeCase.type === "node") {
|
|
3358
|
+
removeAllFound(envelopeCase.subject, target);
|
|
3359
|
+
for (const assertion of envelopeCase.assertions) {
|
|
3360
|
+
removeAllFound(assertion, target);
|
|
3361
|
+
if (target.size === 0) break;
|
|
3362
|
+
}
|
|
3363
|
+
} else if (envelopeCase.type === "wrapped") removeAllFound(envelopeCase.envelope, target);
|
|
3364
|
+
else if (envelopeCase.type === "assertion") {
|
|
3365
|
+
const predicate = envelopeCase.assertion.predicate();
|
|
3366
|
+
const object = envelopeCase.assertion.object();
|
|
3367
|
+
removeAllFound(predicate, target);
|
|
3368
|
+
if (target.size > 0) removeAllFound(object, target);
|
|
3369
|
+
}
|
|
3370
|
+
}
|
|
3371
|
+
function containsDigest$1(set, digest) {
|
|
3372
|
+
const hexToFind = digest.hex();
|
|
3373
|
+
for (const d of set) if (d.hex() === hexToFind) return true;
|
|
3374
|
+
return false;
|
|
3375
|
+
}
|
|
3376
|
+
function removeDigest(set, digest) {
|
|
3377
|
+
const hexToFind = digest.hex();
|
|
3378
|
+
for (const d of set) if (d.hex() === hexToFind) {
|
|
3379
|
+
set.delete(d);
|
|
3380
|
+
return;
|
|
3381
|
+
}
|
|
3382
|
+
}
|
|
3383
|
+
function isSubset(subset, superset) {
|
|
3384
|
+
for (const digest of subset) if (!containsDigest$1(superset, digest)) return false;
|
|
3385
|
+
return true;
|
|
3386
|
+
}
|
|
3387
|
+
|
|
3388
|
+
//#endregion
|
|
3389
|
+
//#region src/format/tree.ts
|
|
3390
|
+
/**
|
|
3391
|
+
* Specifies the format for displaying envelope digests in tree output.
|
|
3392
|
+
*
|
|
3393
|
+
* Ported from bc-envelope-rust/src/format/tree/format/digest.rs
|
|
3394
|
+
*/
|
|
3395
|
+
let DigestDisplayFormat = /* @__PURE__ */ function(DigestDisplayFormat$1) {
|
|
3396
|
+
/**
|
|
3397
|
+
* Short format: first 7 hex characters of the digest.
|
|
3398
|
+
* This is the default format.
|
|
3399
|
+
*/
|
|
3400
|
+
DigestDisplayFormat$1["Short"] = "short";
|
|
3401
|
+
/**
|
|
3402
|
+
* Full format: complete 64 hex character digest.
|
|
3403
|
+
*/
|
|
3404
|
+
DigestDisplayFormat$1["Full"] = "full";
|
|
3405
|
+
/**
|
|
3406
|
+
* UR format: digest encoded as a UR string.
|
|
3407
|
+
*/
|
|
3408
|
+
DigestDisplayFormat$1["UR"] = "ur";
|
|
3409
|
+
return DigestDisplayFormat$1;
|
|
3410
|
+
}({});
|
|
3411
|
+
Envelope.prototype.shortId = function(format = "short") {
|
|
3412
|
+
const digest = this.digest();
|
|
3413
|
+
if (format === "full") return digest.hex();
|
|
3414
|
+
if (format === "ur") return digest.urString();
|
|
3415
|
+
return digest.short();
|
|
3416
|
+
};
|
|
3417
|
+
Envelope.prototype.summary = function(maxLength = 40) {
|
|
3418
|
+
switch (this.case().type) {
|
|
3419
|
+
case "node": return "NODE";
|
|
3420
|
+
case "leaf": {
|
|
3421
|
+
try {
|
|
3422
|
+
const text = this.asText();
|
|
3423
|
+
if (text !== void 0) {
|
|
3424
|
+
const truncated = text.length > maxLength ? `${text.substring(0, maxLength)}...` : text;
|
|
3425
|
+
return JSON.stringify(truncated);
|
|
3426
|
+
}
|
|
3427
|
+
} catch {}
|
|
3428
|
+
try {
|
|
3429
|
+
const num = this.extractNumber();
|
|
3430
|
+
return String(num);
|
|
2495
3431
|
} catch {}
|
|
2496
3432
|
try {
|
|
2497
3433
|
const bool = this.extractBoolean();
|
|
@@ -2540,6 +3476,650 @@ Envelope.prototype.treeFormat = function(options = {}) {
|
|
|
2540
3476
|
}).join("\n");
|
|
2541
3477
|
};
|
|
2542
3478
|
|
|
3479
|
+
//#endregion
|
|
3480
|
+
//#region src/format/format-context.ts
|
|
3481
|
+
const formatContextNone = () => ({ type: "none" });
|
|
3482
|
+
const formatContextGlobal = () => ({ type: "global" });
|
|
3483
|
+
const formatContextCustom = (context) => ({
|
|
3484
|
+
type: "custom",
|
|
3485
|
+
context
|
|
3486
|
+
});
|
|
3487
|
+
var FormatContext = class FormatContext {
|
|
3488
|
+
#tags;
|
|
3489
|
+
#knownValues;
|
|
3490
|
+
constructor(tags, knownValues) {
|
|
3491
|
+
this.#tags = tags ?? new _bcts_dcbor.TagsStore();
|
|
3492
|
+
this.#knownValues = knownValues ?? new _bcts_known_values.KnownValuesStore();
|
|
3493
|
+
}
|
|
3494
|
+
tags() {
|
|
3495
|
+
return this.#tags;
|
|
3496
|
+
}
|
|
3497
|
+
knownValues() {
|
|
3498
|
+
return this.#knownValues;
|
|
3499
|
+
}
|
|
3500
|
+
assignedNameForTag(tag) {
|
|
3501
|
+
return this.#tags.assignedNameForTag(tag);
|
|
3502
|
+
}
|
|
3503
|
+
nameForTag(tag) {
|
|
3504
|
+
return this.#tags.nameForTag(tag);
|
|
3505
|
+
}
|
|
3506
|
+
tagForValue(value) {
|
|
3507
|
+
return this.#tags.tagForValue(value);
|
|
3508
|
+
}
|
|
3509
|
+
tagForName(name) {
|
|
3510
|
+
return this.#tags.tagForName(name);
|
|
3511
|
+
}
|
|
3512
|
+
nameForValue(value) {
|
|
3513
|
+
return this.#tags.nameForValue(value);
|
|
3514
|
+
}
|
|
3515
|
+
summarizer(tag) {
|
|
3516
|
+
return this.#tags.summarizer(tag);
|
|
3517
|
+
}
|
|
3518
|
+
registerTag(value, name) {
|
|
3519
|
+
this.#tags.insert({
|
|
3520
|
+
value: BigInt(value),
|
|
3521
|
+
name
|
|
3522
|
+
});
|
|
3523
|
+
}
|
|
3524
|
+
clone() {
|
|
3525
|
+
return new FormatContext(this.#tags, this.#knownValues);
|
|
3526
|
+
}
|
|
3527
|
+
};
|
|
3528
|
+
let _globalFormatContextInstance;
|
|
3529
|
+
let isInitialized = false;
|
|
3530
|
+
const getGlobalFormatContext = () => {
|
|
3531
|
+
if (!isInitialized) {
|
|
3532
|
+
(0, _bcts_dcbor.registerTags)();
|
|
3533
|
+
_globalFormatContextInstance = new FormatContext((0, _bcts_dcbor.getGlobalTagsStore)(), _bcts_known_values.KNOWN_VALUES.get());
|
|
3534
|
+
isInitialized = true;
|
|
3535
|
+
setupKnownValueSummarizer(_globalFormatContextInstance);
|
|
3536
|
+
}
|
|
3537
|
+
return _globalFormatContextInstance;
|
|
3538
|
+
};
|
|
3539
|
+
const withFormatContext = (action) => {
|
|
3540
|
+
return action(getGlobalFormatContext());
|
|
3541
|
+
};
|
|
3542
|
+
const withFormatContextMut = (action) => {
|
|
3543
|
+
return action(getGlobalFormatContext());
|
|
3544
|
+
};
|
|
3545
|
+
const setupKnownValueSummarizer = (context) => {
|
|
3546
|
+
const knownValues = context.knownValues();
|
|
3547
|
+
const tags = context.tags();
|
|
3548
|
+
const summarizer = (cbor$1, _flat) => {
|
|
3549
|
+
try {
|
|
3550
|
+
const kv = _bcts_known_values.KnownValue.fromUntaggedCbor(cbor$1);
|
|
3551
|
+
return {
|
|
3552
|
+
ok: true,
|
|
3553
|
+
value: `'${knownValues.name(kv)}'`
|
|
3554
|
+
};
|
|
3555
|
+
} catch {
|
|
3556
|
+
return {
|
|
3557
|
+
ok: true,
|
|
3558
|
+
value: "'<unknown>'"
|
|
3559
|
+
};
|
|
3560
|
+
}
|
|
3561
|
+
};
|
|
3562
|
+
tags.setSummarizer(BigInt(_bcts_known_values.TAG_KNOWN_VALUE), summarizer);
|
|
3563
|
+
};
|
|
3564
|
+
const registerTagsIn = (context) => {
|
|
3565
|
+
(0, _bcts_dcbor.registerTags)();
|
|
3566
|
+
setupKnownValueSummarizer(context);
|
|
3567
|
+
};
|
|
3568
|
+
const registerTags = () => {
|
|
3569
|
+
withFormatContextMut((context) => {
|
|
3570
|
+
registerTagsIn(context);
|
|
3571
|
+
});
|
|
3572
|
+
};
|
|
3573
|
+
const globalFormatContext = getGlobalFormatContext;
|
|
3574
|
+
const GLOBAL_FORMAT_CONTEXT = { get: getGlobalFormatContext };
|
|
3575
|
+
|
|
3576
|
+
//#endregion
|
|
3577
|
+
//#region src/format/envelope-summary.ts
|
|
3578
|
+
const flankedBy = (s, prefix, suffix) => {
|
|
3579
|
+
return `${prefix}${s}${suffix}`;
|
|
3580
|
+
};
|
|
3581
|
+
const cborEnvelopeSummary = (cbor$1, maxLength, context) => {
|
|
3582
|
+
if ((0, _bcts_dcbor.isUnsigned)(cbor$1)) return String(cbor$1);
|
|
3583
|
+
if ((0, _bcts_dcbor.isNegative)(cbor$1)) {
|
|
3584
|
+
const n = cbor$1;
|
|
3585
|
+
return String(-1n - n);
|
|
3586
|
+
}
|
|
3587
|
+
if ((0, _bcts_dcbor.isBytes)(cbor$1)) return `Bytes(${cbor$1.length})`;
|
|
3588
|
+
if ((0, _bcts_dcbor.isText)(cbor$1)) {
|
|
3589
|
+
let text = (0, _bcts_dcbor.asText)(cbor$1) ?? "";
|
|
3590
|
+
if (text.length > maxLength) text = `${text.substring(0, maxLength)}…`;
|
|
3591
|
+
text = text.replace(/\n/g, "\\n");
|
|
3592
|
+
return flankedBy(text, "\"", "\"");
|
|
3593
|
+
}
|
|
3594
|
+
if ((0, _bcts_dcbor.isSimple)(cbor$1)) {
|
|
3595
|
+
const value = cbor$1;
|
|
3596
|
+
if (value === true) return "true";
|
|
3597
|
+
if (value === false) return "false";
|
|
3598
|
+
if (value === null) return "null";
|
|
3599
|
+
if (value === void 0) return "undefined";
|
|
3600
|
+
if (typeof value === "number") {
|
|
3601
|
+
if (Number.isNaN(value)) return "NaN";
|
|
3602
|
+
if (!Number.isFinite(value)) return value > 0 ? "Infinity" : "-Infinity";
|
|
3603
|
+
return String(value);
|
|
3604
|
+
}
|
|
3605
|
+
return (0, _bcts_dcbor.diagnosticOpt)(cbor$1, { summarize: true });
|
|
3606
|
+
}
|
|
3607
|
+
if ((0, _bcts_dcbor.isArray)(cbor$1) || (0, _bcts_dcbor.isMap)(cbor$1) || (0, _bcts_dcbor.isTagged)(cbor$1)) {
|
|
3608
|
+
const opts = { summarize: true };
|
|
3609
|
+
if (context.type === "custom") return (0, _bcts_dcbor.diagnosticOpt)(cbor$1, {
|
|
3610
|
+
...opts,
|
|
3611
|
+
tags: context.context.tags()
|
|
3612
|
+
});
|
|
3613
|
+
else if (context.type === "global") {
|
|
3614
|
+
const ctx = getGlobalFormatContext();
|
|
3615
|
+
return (0, _bcts_dcbor.diagnosticOpt)(cbor$1, {
|
|
3616
|
+
...opts,
|
|
3617
|
+
tags: ctx.tags()
|
|
3618
|
+
});
|
|
3619
|
+
} else return (0, _bcts_dcbor.diagnosticOpt)(cbor$1, opts);
|
|
3620
|
+
}
|
|
3621
|
+
return String(cbor$1);
|
|
3622
|
+
};
|
|
3623
|
+
Envelope.prototype.summaryWithContext = function(maxLength, context) {
|
|
3624
|
+
const c = this.case();
|
|
3625
|
+
switch (c.type) {
|
|
3626
|
+
case "node": return "NODE";
|
|
3627
|
+
case "leaf": return cborEnvelopeSummary(c.cbor, maxLength, {
|
|
3628
|
+
type: "custom",
|
|
3629
|
+
context
|
|
3630
|
+
});
|
|
3631
|
+
case "wrapped": return "WRAPPED";
|
|
3632
|
+
case "assertion": return "ASSERTION";
|
|
3633
|
+
case "elided": return "ELIDED";
|
|
3634
|
+
case "knownValue": return flankedBy(context.knownValues().name(c.value), "'", "'");
|
|
3635
|
+
case "encrypted": return "ENCRYPTED";
|
|
3636
|
+
case "compressed": return "COMPRESSED";
|
|
3637
|
+
default: return "UNKNOWN";
|
|
3638
|
+
}
|
|
3639
|
+
};
|
|
3640
|
+
|
|
3641
|
+
//#endregion
|
|
3642
|
+
//#region src/format/notation.ts
|
|
3643
|
+
const defaultFormatOpts = () => ({
|
|
3644
|
+
flat: false,
|
|
3645
|
+
context: formatContextGlobal()
|
|
3646
|
+
});
|
|
3647
|
+
const flatFormatOpts = () => ({
|
|
3648
|
+
flat: true,
|
|
3649
|
+
context: formatContextGlobal()
|
|
3650
|
+
});
|
|
3651
|
+
const formatBegin = (value) => ({
|
|
3652
|
+
type: "begin",
|
|
3653
|
+
value
|
|
3654
|
+
});
|
|
3655
|
+
const formatEnd = (value) => ({
|
|
3656
|
+
type: "end",
|
|
3657
|
+
value
|
|
3658
|
+
});
|
|
3659
|
+
const formatItem = (value) => ({
|
|
3660
|
+
type: "item",
|
|
3661
|
+
value
|
|
3662
|
+
});
|
|
3663
|
+
const formatSeparator = () => ({ type: "separator" });
|
|
3664
|
+
const formatList = (items) => ({
|
|
3665
|
+
type: "list",
|
|
3666
|
+
items
|
|
3667
|
+
});
|
|
3668
|
+
const flatten = (item) => {
|
|
3669
|
+
if (item.type === "list") return item.items.flatMap(flatten);
|
|
3670
|
+
return [item];
|
|
3671
|
+
};
|
|
3672
|
+
const nicen = (items) => {
|
|
3673
|
+
const input = [...items];
|
|
3674
|
+
const result = [];
|
|
3675
|
+
while (input.length > 0) {
|
|
3676
|
+
const current = input.shift();
|
|
3677
|
+
if (input.length === 0) {
|
|
3678
|
+
result.push(current);
|
|
3679
|
+
break;
|
|
3680
|
+
}
|
|
3681
|
+
if (current.type === "end" && input[0]?.type === "begin") {
|
|
3682
|
+
const endString = current.value;
|
|
3683
|
+
const beginString = input[0].value;
|
|
3684
|
+
result.push(formatEnd(`${endString} ${beginString}`));
|
|
3685
|
+
result.push(formatBegin(""));
|
|
3686
|
+
input.shift();
|
|
3687
|
+
} else result.push(current);
|
|
3688
|
+
}
|
|
3689
|
+
return result;
|
|
3690
|
+
};
|
|
3691
|
+
const indent = (level) => " ".repeat(level * 4);
|
|
3692
|
+
const addSpaceAtEndIfNeeded = (s) => {
|
|
3693
|
+
if (s.length === 0) return " ";
|
|
3694
|
+
if (s.endsWith(" ")) return s;
|
|
3695
|
+
return `${s} `;
|
|
3696
|
+
};
|
|
3697
|
+
const formatFlat = (item) => {
|
|
3698
|
+
let line = "";
|
|
3699
|
+
const items = flatten(item);
|
|
3700
|
+
for (const i of items) switch (i.type) {
|
|
3701
|
+
case "begin":
|
|
3702
|
+
if (!line.endsWith(" ")) line += " ";
|
|
3703
|
+
line += `${i.value} `;
|
|
3704
|
+
break;
|
|
3705
|
+
case "end":
|
|
3706
|
+
if (!line.endsWith(" ")) line += " ";
|
|
3707
|
+
line += `${i.value} `;
|
|
3708
|
+
break;
|
|
3709
|
+
case "item":
|
|
3710
|
+
line += i.value;
|
|
3711
|
+
break;
|
|
3712
|
+
case "separator":
|
|
3713
|
+
line = `${line.trimEnd()}, `;
|
|
3714
|
+
break;
|
|
3715
|
+
case "list":
|
|
3716
|
+
for (const subItem of i.items) line += formatFlat(subItem);
|
|
3717
|
+
break;
|
|
3718
|
+
}
|
|
3719
|
+
return line;
|
|
3720
|
+
};
|
|
3721
|
+
const formatHierarchical = (item) => {
|
|
3722
|
+
const lines = [];
|
|
3723
|
+
let level = 0;
|
|
3724
|
+
let currentLine = "";
|
|
3725
|
+
const items = nicen(flatten(item));
|
|
3726
|
+
for (const i of items) switch (i.type) {
|
|
3727
|
+
case "begin": {
|
|
3728
|
+
const delimiter = i.value;
|
|
3729
|
+
if (delimiter.length > 0) {
|
|
3730
|
+
const c = currentLine.length === 0 ? delimiter : `${addSpaceAtEndIfNeeded(currentLine)}${delimiter}`;
|
|
3731
|
+
lines.push(`${indent(level)}${c}\n`);
|
|
3732
|
+
}
|
|
3733
|
+
level += 1;
|
|
3734
|
+
currentLine = "";
|
|
3735
|
+
break;
|
|
3736
|
+
}
|
|
3737
|
+
case "end": {
|
|
3738
|
+
const delimiter = i.value;
|
|
3739
|
+
if (currentLine.length > 0) {
|
|
3740
|
+
lines.push(`${indent(level)}${currentLine}\n`);
|
|
3741
|
+
currentLine = "";
|
|
3742
|
+
}
|
|
3743
|
+
level -= 1;
|
|
3744
|
+
lines.push(`${indent(level)}${delimiter}\n`);
|
|
3745
|
+
break;
|
|
3746
|
+
}
|
|
3747
|
+
case "item":
|
|
3748
|
+
currentLine += i.value;
|
|
3749
|
+
break;
|
|
3750
|
+
case "separator":
|
|
3751
|
+
if (currentLine.length > 0) {
|
|
3752
|
+
lines.push(`${indent(level)}${currentLine}\n`);
|
|
3753
|
+
currentLine = "";
|
|
3754
|
+
}
|
|
3755
|
+
break;
|
|
3756
|
+
case "list":
|
|
3757
|
+
lines.push("<list>");
|
|
3758
|
+
break;
|
|
3759
|
+
}
|
|
3760
|
+
if (currentLine.length > 0) lines.push(currentLine);
|
|
3761
|
+
return lines.join("");
|
|
3762
|
+
};
|
|
3763
|
+
const formatFormatItem = (item, opts) => {
|
|
3764
|
+
if (opts.flat) return formatFlat(item);
|
|
3765
|
+
return formatHierarchical(item);
|
|
3766
|
+
};
|
|
3767
|
+
const formatCbor = (cbor$1, opts) => {
|
|
3768
|
+
if ((0, _bcts_dcbor.isTagged)(cbor$1)) {
|
|
3769
|
+
const tag = (0, _bcts_dcbor.tagValue)(cbor$1);
|
|
3770
|
+
if (tag === 200n || tag === 200) try {
|
|
3771
|
+
return formatEnvelope(Envelope.fromTaggedCbor(cbor$1), opts);
|
|
3772
|
+
} catch {
|
|
3773
|
+
return formatItem("<error>");
|
|
3774
|
+
}
|
|
3775
|
+
}
|
|
3776
|
+
return formatItem(cborEnvelopeSummary(cbor$1, Number.MAX_SAFE_INTEGER, opts.context));
|
|
3777
|
+
};
|
|
3778
|
+
const formatAssertion = (assertion, opts) => {
|
|
3779
|
+
return formatList([
|
|
3780
|
+
formatEnvelope(assertion.predicate(), opts),
|
|
3781
|
+
formatItem(": "),
|
|
3782
|
+
formatEnvelope(assertion.object(), opts)
|
|
3783
|
+
]);
|
|
3784
|
+
};
|
|
3785
|
+
const formatEnvelope = (envelope, opts) => {
|
|
3786
|
+
const c = envelope.case();
|
|
3787
|
+
switch (c.type) {
|
|
3788
|
+
case "leaf": return formatCbor(c.cbor, opts);
|
|
3789
|
+
case "wrapped": return formatList([
|
|
3790
|
+
formatBegin("{"),
|
|
3791
|
+
formatEnvelope(c.envelope, opts),
|
|
3792
|
+
formatEnd("}")
|
|
3793
|
+
]);
|
|
3794
|
+
case "assertion": return formatAssertion(c.assertion, opts);
|
|
3795
|
+
case "knownValue": {
|
|
3796
|
+
let name;
|
|
3797
|
+
if (opts.context.type === "custom") name = opts.context.context.knownValues().assignedName(c.value) ?? c.value.name();
|
|
3798
|
+
else if (opts.context.type === "global") name = getGlobalFormatContext().knownValues().assignedName(c.value) ?? c.value.name();
|
|
3799
|
+
else name = c.value.name();
|
|
3800
|
+
return formatItem(`'${name}'`);
|
|
3801
|
+
}
|
|
3802
|
+
case "encrypted": return formatItem("ENCRYPTED");
|
|
3803
|
+
case "compressed": return formatItem("COMPRESSED");
|
|
3804
|
+
case "elided": return formatItem("ELIDED");
|
|
3805
|
+
case "node": {
|
|
3806
|
+
const items = [];
|
|
3807
|
+
const subjectItem = formatEnvelope(c.subject, opts);
|
|
3808
|
+
let elidedCount = 0;
|
|
3809
|
+
let encryptedCount = 0;
|
|
3810
|
+
let compressedCount = 0;
|
|
3811
|
+
const typeAssertionItems = [];
|
|
3812
|
+
const assertionItems = [];
|
|
3813
|
+
for (const assertion of c.assertions) switch (assertion.case().type) {
|
|
3814
|
+
case "elided":
|
|
3815
|
+
elidedCount += 1;
|
|
3816
|
+
break;
|
|
3817
|
+
case "encrypted":
|
|
3818
|
+
encryptedCount += 1;
|
|
3819
|
+
break;
|
|
3820
|
+
case "compressed":
|
|
3821
|
+
compressedCount += 1;
|
|
3822
|
+
break;
|
|
3823
|
+
case "node":
|
|
3824
|
+
case "leaf":
|
|
3825
|
+
case "wrapped":
|
|
3826
|
+
case "assertion":
|
|
3827
|
+
case "knownValue": {
|
|
3828
|
+
const item = [formatEnvelope(assertion, opts)];
|
|
3829
|
+
let isTypeAssertion = false;
|
|
3830
|
+
if (assertion.asPredicate()?.subject().asKnownValue()?.equals(_bcts_known_values.IS_A) === true) isTypeAssertion = true;
|
|
3831
|
+
if (isTypeAssertion) typeAssertionItems.push(item);
|
|
3832
|
+
else assertionItems.push(item);
|
|
3833
|
+
break;
|
|
3834
|
+
}
|
|
3835
|
+
}
|
|
3836
|
+
typeAssertionItems.sort((a, b) => compareFormatItems(a[0], b[0]));
|
|
3837
|
+
assertionItems.sort((a, b) => compareFormatItems(a[0], b[0]));
|
|
3838
|
+
const allAssertionItems = [...typeAssertionItems, ...assertionItems];
|
|
3839
|
+
if (compressedCount > 1) allAssertionItems.push([formatItem(`COMPRESSED (${compressedCount})`)]);
|
|
3840
|
+
else if (compressedCount > 0) allAssertionItems.push([formatItem("COMPRESSED")]);
|
|
3841
|
+
if (elidedCount > 1) allAssertionItems.push([formatItem(`ELIDED (${elidedCount})`)]);
|
|
3842
|
+
else if (elidedCount > 0) allAssertionItems.push([formatItem("ELIDED")]);
|
|
3843
|
+
if (encryptedCount > 1) allAssertionItems.push([formatItem(`ENCRYPTED (${encryptedCount})`)]);
|
|
3844
|
+
else if (encryptedCount > 0) allAssertionItems.push([formatItem("ENCRYPTED")]);
|
|
3845
|
+
const joinedAssertionItems = [];
|
|
3846
|
+
for (let i = 0; i < allAssertionItems.length; i++) {
|
|
3847
|
+
if (i > 0) joinedAssertionItems.push(formatSeparator());
|
|
3848
|
+
joinedAssertionItems.push(...allAssertionItems[i]);
|
|
3849
|
+
}
|
|
3850
|
+
const needsBraces = c.subject.isSubjectAssertion();
|
|
3851
|
+
if (needsBraces) items.push(formatBegin("{"));
|
|
3852
|
+
items.push(subjectItem);
|
|
3853
|
+
if (needsBraces) items.push(formatEnd("}"));
|
|
3854
|
+
items.push(formatBegin("["));
|
|
3855
|
+
items.push(...joinedAssertionItems);
|
|
3856
|
+
items.push(formatEnd("]"));
|
|
3857
|
+
return formatList(items);
|
|
3858
|
+
}
|
|
3859
|
+
}
|
|
3860
|
+
};
|
|
3861
|
+
const compareFormatItems = (a, b) => {
|
|
3862
|
+
const getIndex = (item) => {
|
|
3863
|
+
switch (item.type) {
|
|
3864
|
+
case "begin": return 1;
|
|
3865
|
+
case "end": return 2;
|
|
3866
|
+
case "item": return 3;
|
|
3867
|
+
case "separator": return 4;
|
|
3868
|
+
case "list": return 5;
|
|
3869
|
+
}
|
|
3870
|
+
};
|
|
3871
|
+
const aIndex = getIndex(a);
|
|
3872
|
+
const bIndex = getIndex(b);
|
|
3873
|
+
if (aIndex !== bIndex) return aIndex - bIndex;
|
|
3874
|
+
if (a.type === "item" && b.type === "item") return a.value.localeCompare(b.value);
|
|
3875
|
+
if (a.type === "begin" && b.type === "begin") return a.value.localeCompare(b.value);
|
|
3876
|
+
if (a.type === "end" && b.type === "end") return a.value.localeCompare(b.value);
|
|
3877
|
+
return 0;
|
|
3878
|
+
};
|
|
3879
|
+
Envelope.prototype.formatOpt = function(opts) {
|
|
3880
|
+
return formatFormatItem(formatEnvelope(this, opts), opts).trim();
|
|
3881
|
+
};
|
|
3882
|
+
Envelope.prototype.format = function() {
|
|
3883
|
+
return this.formatOpt(defaultFormatOpts());
|
|
3884
|
+
};
|
|
3885
|
+
Envelope.prototype.formatFlat = function() {
|
|
3886
|
+
return this.formatOpt(flatFormatOpts());
|
|
3887
|
+
};
|
|
3888
|
+
|
|
3889
|
+
//#endregion
|
|
3890
|
+
//#region src/format/mermaid.ts
|
|
3891
|
+
let MermaidOrientation = /* @__PURE__ */ function(MermaidOrientation$1) {
|
|
3892
|
+
MermaidOrientation$1["LeftToRight"] = "LR";
|
|
3893
|
+
MermaidOrientation$1["TopToBottom"] = "TB";
|
|
3894
|
+
MermaidOrientation$1["RightToLeft"] = "RL";
|
|
3895
|
+
MermaidOrientation$1["BottomToTop"] = "BT";
|
|
3896
|
+
return MermaidOrientation$1;
|
|
3897
|
+
}({});
|
|
3898
|
+
let MermaidTheme = /* @__PURE__ */ function(MermaidTheme$1) {
|
|
3899
|
+
MermaidTheme$1["Default"] = "default";
|
|
3900
|
+
MermaidTheme$1["Neutral"] = "neutral";
|
|
3901
|
+
MermaidTheme$1["Dark"] = "dark";
|
|
3902
|
+
MermaidTheme$1["Forest"] = "forest";
|
|
3903
|
+
MermaidTheme$1["Base"] = "base";
|
|
3904
|
+
return MermaidTheme$1;
|
|
3905
|
+
}({});
|
|
3906
|
+
const defaultMermaidOpts = () => ({
|
|
3907
|
+
hideNodes: false,
|
|
3908
|
+
monochrome: false,
|
|
3909
|
+
theme: MermaidTheme.Default,
|
|
3910
|
+
orientation: MermaidOrientation.LeftToRight,
|
|
3911
|
+
highlightingTarget: /* @__PURE__ */ new Set()
|
|
3912
|
+
});
|
|
3913
|
+
Envelope.prototype.mermaidFormat = function() {
|
|
3914
|
+
return this.mermaidFormatOpt(defaultMermaidOpts());
|
|
3915
|
+
};
|
|
3916
|
+
Envelope.prototype.mermaidFormatOpt = function(opts) {
|
|
3917
|
+
const hideNodes = opts.hideNodes ?? false;
|
|
3918
|
+
const monochrome = opts.monochrome ?? false;
|
|
3919
|
+
const theme = opts.theme ?? MermaidTheme.Default;
|
|
3920
|
+
const orientation = opts.orientation ?? MermaidOrientation.LeftToRight;
|
|
3921
|
+
const highlightingTarget = opts.highlightingTarget ?? /* @__PURE__ */ new Set();
|
|
3922
|
+
const elements = [];
|
|
3923
|
+
let nextId = 0;
|
|
3924
|
+
const parentStack = [];
|
|
3925
|
+
this.walk(hideNodes, void 0, (envelope, level, incomingEdge, _state) => {
|
|
3926
|
+
const id = nextId++;
|
|
3927
|
+
let parent;
|
|
3928
|
+
while (parentStack.length > 0 && parentStack[parentStack.length - 1].level >= level) parentStack.pop();
|
|
3929
|
+
if (parentStack.length > 0) parent = parentStack[parentStack.length - 1];
|
|
3930
|
+
const isHighlighted = containsDigest(highlightingTarget, envelope.digest());
|
|
3931
|
+
const elem = {
|
|
3932
|
+
id,
|
|
3933
|
+
level,
|
|
3934
|
+
envelope,
|
|
3935
|
+
incomingEdge,
|
|
3936
|
+
showId: !hideNodes,
|
|
3937
|
+
isHighlighted,
|
|
3938
|
+
parent
|
|
3939
|
+
};
|
|
3940
|
+
elements.push(elem);
|
|
3941
|
+
parentStack.push(elem);
|
|
3942
|
+
return [void 0, false];
|
|
3943
|
+
});
|
|
3944
|
+
const formattedIds = /* @__PURE__ */ new Set();
|
|
3945
|
+
const lines = [`%%{ init: { 'theme': '${theme}', 'flowchart': { 'curve': 'basis' } } }%%`, `graph ${orientation}`];
|
|
3946
|
+
const nodeStyles = [];
|
|
3947
|
+
const linkStyles = [];
|
|
3948
|
+
let linkIndex = 0;
|
|
3949
|
+
for (const element of elements) {
|
|
3950
|
+
const indent$1 = " ".repeat(element.level);
|
|
3951
|
+
let content;
|
|
3952
|
+
if (element.parent !== void 0) {
|
|
3953
|
+
const thisLinkStyles = [];
|
|
3954
|
+
if (!monochrome) {
|
|
3955
|
+
const strokeColor = linkStrokeColor(element.incomingEdge);
|
|
3956
|
+
if (strokeColor !== void 0) thisLinkStyles.push(`stroke:${strokeColor}`);
|
|
3957
|
+
}
|
|
3958
|
+
if (element.isHighlighted && element.parent.isHighlighted) thisLinkStyles.push("stroke-width:4px");
|
|
3959
|
+
else thisLinkStyles.push("stroke-width:2px");
|
|
3960
|
+
if (thisLinkStyles.length > 0) linkStyles.push(`linkStyle ${linkIndex} ${thisLinkStyles.join(",")}`);
|
|
3961
|
+
linkIndex++;
|
|
3962
|
+
content = formatEdge(element, formattedIds);
|
|
3963
|
+
} else content = formatNode(element, formattedIds);
|
|
3964
|
+
const thisNodeStyles = [];
|
|
3965
|
+
if (!monochrome) {
|
|
3966
|
+
const strokeColor = nodeColor(element.envelope);
|
|
3967
|
+
thisNodeStyles.push(`stroke:${strokeColor}`);
|
|
3968
|
+
}
|
|
3969
|
+
if (element.isHighlighted) thisNodeStyles.push("stroke-width:6px");
|
|
3970
|
+
else thisNodeStyles.push("stroke-width:4px");
|
|
3971
|
+
if (thisNodeStyles.length > 0) nodeStyles.push(`style ${element.id} ${thisNodeStyles.join(",")}`);
|
|
3972
|
+
lines.push(`${indent$1}${content}`);
|
|
3973
|
+
}
|
|
3974
|
+
for (const style of nodeStyles) lines.push(style);
|
|
3975
|
+
for (const style of linkStyles) lines.push(style);
|
|
3976
|
+
return lines.join("\n");
|
|
3977
|
+
};
|
|
3978
|
+
const containsDigest = (set, digest) => {
|
|
3979
|
+
for (const d of set) if (d.equals(digest)) return true;
|
|
3980
|
+
return false;
|
|
3981
|
+
};
|
|
3982
|
+
const formatNode = (element, formattedIds) => {
|
|
3983
|
+
if (!formattedIds.has(element.id)) {
|
|
3984
|
+
formattedIds.add(element.id);
|
|
3985
|
+
const lines = [];
|
|
3986
|
+
const summary = withFormatContext((ctx) => {
|
|
3987
|
+
return element.envelope.summaryWithContext(20, ctx).replace(/"/g, """);
|
|
3988
|
+
});
|
|
3989
|
+
lines.push(summary);
|
|
3990
|
+
if (element.showId) {
|
|
3991
|
+
const id = element.envelope.digest().short();
|
|
3992
|
+
lines.push(id);
|
|
3993
|
+
}
|
|
3994
|
+
const content = lines.join("<br>");
|
|
3995
|
+
const [frameL, frameR] = mermaidFrame(element.envelope);
|
|
3996
|
+
return `${element.id}${frameL}"${content}"${frameR}`;
|
|
3997
|
+
} else return `${element.id}`;
|
|
3998
|
+
};
|
|
3999
|
+
const formatEdge = (element, formattedIds) => {
|
|
4000
|
+
const parent = element.parent;
|
|
4001
|
+
const label = edgeLabel(element.incomingEdge);
|
|
4002
|
+
const arrow = label !== void 0 ? `-- ${label} -->` : "-->";
|
|
4003
|
+
return `${formatNode(parent, formattedIds)} ${arrow} ${formatNode(element, formattedIds)}`;
|
|
4004
|
+
};
|
|
4005
|
+
const mermaidFrame = (envelope) => {
|
|
4006
|
+
switch (envelope.case().type) {
|
|
4007
|
+
case "node": return ["((", "))"];
|
|
4008
|
+
case "leaf": return ["[", "]"];
|
|
4009
|
+
case "wrapped": return ["[/", "\\]"];
|
|
4010
|
+
case "assertion": return ["([", "])"];
|
|
4011
|
+
case "elided": return ["{{", "}}"];
|
|
4012
|
+
case "knownValue": return ["[/", "/]"];
|
|
4013
|
+
case "encrypted": return [">", "]"];
|
|
4014
|
+
case "compressed": return ["[[", "]]"];
|
|
4015
|
+
default: return ["[", "]"];
|
|
4016
|
+
}
|
|
4017
|
+
};
|
|
4018
|
+
const nodeColor = (envelope) => {
|
|
4019
|
+
switch (envelope.case().type) {
|
|
4020
|
+
case "node": return "red";
|
|
4021
|
+
case "leaf": return "teal";
|
|
4022
|
+
case "wrapped": return "blue";
|
|
4023
|
+
case "assertion": return "green";
|
|
4024
|
+
case "elided": return "gray";
|
|
4025
|
+
case "knownValue": return "goldenrod";
|
|
4026
|
+
case "encrypted": return "coral";
|
|
4027
|
+
case "compressed": return "purple";
|
|
4028
|
+
default: return "gray";
|
|
4029
|
+
}
|
|
4030
|
+
};
|
|
4031
|
+
const linkStrokeColor = (edgeType) => {
|
|
4032
|
+
switch (edgeType) {
|
|
4033
|
+
case EdgeType.Subject: return "red";
|
|
4034
|
+
case EdgeType.Content: return "blue";
|
|
4035
|
+
case EdgeType.Predicate: return "cyan";
|
|
4036
|
+
case EdgeType.Object: return "magenta";
|
|
4037
|
+
case EdgeType.None:
|
|
4038
|
+
case EdgeType.Assertion: return;
|
|
4039
|
+
}
|
|
4040
|
+
};
|
|
4041
|
+
const registerMermaidExtension = () => {};
|
|
4042
|
+
|
|
4043
|
+
//#endregion
|
|
4044
|
+
//#region src/format/hex.ts
|
|
4045
|
+
Envelope.prototype.hex = function() {
|
|
4046
|
+
const bytes = this.cborBytes();
|
|
4047
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
4048
|
+
};
|
|
4049
|
+
Envelope.prototype.cborBytes = function() {
|
|
4050
|
+
return (0, _bcts_dcbor.cborData)(this.taggedCbor());
|
|
4051
|
+
};
|
|
4052
|
+
|
|
4053
|
+
//#endregion
|
|
4054
|
+
//#region src/format/diagnostic.ts
|
|
4055
|
+
function cborToDiagnostic(cbor$1, indent$1 = 0) {
|
|
4056
|
+
if (typeof cbor$1 === "object" && cbor$1 !== null && "tag" in cbor$1 && "value" in cbor$1) {
|
|
4057
|
+
const tagged = cbor$1;
|
|
4058
|
+
return `${tagged.tag}(${cborToDiagnostic(tagged.value, indent$1)})`;
|
|
4059
|
+
}
|
|
4060
|
+
if (Array.isArray(cbor$1)) {
|
|
4061
|
+
if (cbor$1.length === 0) return "[]";
|
|
4062
|
+
return `[${cbor$1.map((item) => cborToDiagnostic(item, indent$1 + 2)).join(", ")}]`;
|
|
4063
|
+
}
|
|
4064
|
+
if (cbor$1 instanceof Map) {
|
|
4065
|
+
if (cbor$1.size === 0) return "{}";
|
|
4066
|
+
const entries = [];
|
|
4067
|
+
for (const [key, value] of cbor$1) {
|
|
4068
|
+
const keyStr = cborToDiagnostic(key, indent$1 + 2);
|
|
4069
|
+
const valueStr = cborToDiagnostic(value, indent$1 + 2);
|
|
4070
|
+
entries.push(`${keyStr}: ${valueStr}`);
|
|
4071
|
+
}
|
|
4072
|
+
return `{${entries.join(", ")}}`;
|
|
4073
|
+
}
|
|
4074
|
+
if (cbor$1 instanceof Uint8Array) return `h'${Array.from(cbor$1).map((b) => b.toString(16).padStart(2, "0")).join("")}'`;
|
|
4075
|
+
if (typeof cbor$1 === "string") return JSON.stringify(cbor$1);
|
|
4076
|
+
if (typeof cbor$1 === "object" && cbor$1 !== null && "type" in cbor$1) {
|
|
4077
|
+
const typed = cbor$1;
|
|
4078
|
+
switch (typed.type) {
|
|
4079
|
+
case 0: return String(typed.value);
|
|
4080
|
+
case 1: return String(-1 - Number(typed.value));
|
|
4081
|
+
case 7: {
|
|
4082
|
+
const simpleValue = typed.value;
|
|
4083
|
+
if (simpleValue !== null && typeof simpleValue === "object" && "type" in simpleValue) {
|
|
4084
|
+
const floatValue = simpleValue;
|
|
4085
|
+
if (floatValue.type === "Float") return String(floatValue.value);
|
|
4086
|
+
}
|
|
4087
|
+
if (simpleValue === 20) return "false";
|
|
4088
|
+
if (simpleValue === 21) return "true";
|
|
4089
|
+
if (simpleValue === 22) return "null";
|
|
4090
|
+
if (simpleValue === 23) return "undefined";
|
|
4091
|
+
return `simple(${String(simpleValue)})`;
|
|
4092
|
+
}
|
|
4093
|
+
}
|
|
4094
|
+
}
|
|
4095
|
+
if (typeof cbor$1 === "boolean") return String(cbor$1);
|
|
4096
|
+
if (typeof cbor$1 === "number") return String(cbor$1);
|
|
4097
|
+
if (typeof cbor$1 === "bigint") return String(cbor$1);
|
|
4098
|
+
if (cbor$1 === null) return "null";
|
|
4099
|
+
if (cbor$1 === void 0) return "undefined";
|
|
4100
|
+
try {
|
|
4101
|
+
return JSON.stringify(cbor$1);
|
|
4102
|
+
} catch {
|
|
4103
|
+
return String(cbor$1);
|
|
4104
|
+
}
|
|
4105
|
+
}
|
|
4106
|
+
Envelope.prototype.diagnostic = function() {
|
|
4107
|
+
return cborToDiagnostic(this.taggedCbor());
|
|
4108
|
+
};
|
|
4109
|
+
|
|
4110
|
+
//#endregion
|
|
4111
|
+
//#region src/seal.ts
|
|
4112
|
+
Envelope.prototype.encryptToRecipient = function(recipientPublicKey) {
|
|
4113
|
+
return this.wrap().encryptSubjectToRecipient(recipientPublicKey);
|
|
4114
|
+
};
|
|
4115
|
+
Envelope.prototype.seal = function(sender, recipientPublicKey) {
|
|
4116
|
+
return this.addSignature(sender).encryptToRecipient(recipientPublicKey);
|
|
4117
|
+
};
|
|
4118
|
+
Envelope.prototype.unseal = function(senderPublicKey, recipientPrivateKey) {
|
|
4119
|
+
return this.decryptToRecipient(recipientPrivateKey).verifySignatureFrom(senderPublicKey);
|
|
4120
|
+
};
|
|
4121
|
+
const registerSealExtension = () => {};
|
|
4122
|
+
|
|
2543
4123
|
//#endregion
|
|
2544
4124
|
//#region src/utils/string.ts
|
|
2545
4125
|
/**
|
|
@@ -2576,16 +4156,32 @@ registerCompressExtension();
|
|
|
2576
4156
|
const VERSION = "0.37.0";
|
|
2577
4157
|
|
|
2578
4158
|
//#endregion
|
|
4159
|
+
exports.ADD = ADD;
|
|
4160
|
+
exports.ADD_VALUE = ADD_VALUE;
|
|
4161
|
+
exports.AND = AND;
|
|
4162
|
+
exports.AND_VALUE = AND_VALUE;
|
|
2579
4163
|
exports.ATTACHMENT = ATTACHMENT;
|
|
2580
4164
|
exports.Assertion = Assertion;
|
|
2581
4165
|
exports.Attachments = Attachments;
|
|
4166
|
+
exports.BLANK = BLANK;
|
|
4167
|
+
exports.BLANK_VALUE = BLANK_VALUE;
|
|
2582
4168
|
exports.CBOR_TAG_FUNCTION = CBOR_TAG_FUNCTION;
|
|
2583
4169
|
exports.CBOR_TAG_PARAMETER = CBOR_TAG_PARAMETER;
|
|
2584
4170
|
exports.CBOR_TAG_PLACEHOLDER = CBOR_TAG_PLACEHOLDER;
|
|
2585
4171
|
exports.CBOR_TAG_REPLACEMENT = CBOR_TAG_REPLACEMENT;
|
|
2586
4172
|
exports.CONFORMS_TO = CONFORMS_TO;
|
|
2587
4173
|
exports.Compressed = Compressed;
|
|
2588
|
-
exports.
|
|
4174
|
+
exports.DIV = DIV;
|
|
4175
|
+
exports.DIV_VALUE = DIV_VALUE;
|
|
4176
|
+
Object.defineProperty(exports, 'Digest', {
|
|
4177
|
+
enumerable: true,
|
|
4178
|
+
get: function () {
|
|
4179
|
+
return _bcts_components.Digest;
|
|
4180
|
+
}
|
|
4181
|
+
});
|
|
4182
|
+
exports.DigestDisplayFormat = DigestDisplayFormat;
|
|
4183
|
+
exports.EQ = EQ;
|
|
4184
|
+
exports.EQ_VALUE = EQ_VALUE;
|
|
2589
4185
|
exports.EdgeType = EdgeType;
|
|
2590
4186
|
exports.EncryptedMessage = EncryptedMessage;
|
|
2591
4187
|
exports.Envelope = Envelope;
|
|
@@ -2595,35 +4191,97 @@ exports.EnvelopeCBORTaggedEncodable = EnvelopeCBORTaggedEncodable;
|
|
|
2595
4191
|
exports.EnvelopeDecoder = EnvelopeDecoder;
|
|
2596
4192
|
exports.EnvelopeError = EnvelopeError;
|
|
2597
4193
|
exports.ErrorCode = ErrorCode;
|
|
4194
|
+
exports.Event = Event;
|
|
2598
4195
|
exports.Expression = Expression;
|
|
2599
4196
|
exports.FUNCTION_IDS = FUNCTION_IDS;
|
|
4197
|
+
exports.FormatContext = FormatContext;
|
|
2600
4198
|
exports.Function = Function;
|
|
4199
|
+
exports.FunctionsStore = FunctionsStore;
|
|
4200
|
+
exports.GE = GE;
|
|
4201
|
+
exports.GE_VALUE = GE_VALUE;
|
|
4202
|
+
exports.GLOBAL_FORMAT_CONTEXT = GLOBAL_FORMAT_CONTEXT;
|
|
4203
|
+
exports.GLOBAL_FUNCTIONS = GLOBAL_FUNCTIONS;
|
|
4204
|
+
exports.GLOBAL_PARAMETERS = GLOBAL_PARAMETERS;
|
|
4205
|
+
exports.GT = GT;
|
|
4206
|
+
exports.GT_VALUE = GT_VALUE;
|
|
2601
4207
|
exports.HAS_RECIPIENT = HAS_RECIPIENT;
|
|
2602
4208
|
exports.IS_A = IS_A;
|
|
4209
|
+
exports.LE = LE;
|
|
4210
|
+
exports.LE_VALUE = LE_VALUE;
|
|
4211
|
+
exports.LHS = LHS;
|
|
4212
|
+
exports.LHS_VALUE = LHS_VALUE;
|
|
4213
|
+
exports.LT = LT;
|
|
4214
|
+
exports.LT_VALUE = LT_VALUE;
|
|
4215
|
+
exports.LazyStore = LazyStore;
|
|
4216
|
+
exports.MUL = MUL;
|
|
4217
|
+
exports.MUL_VALUE = MUL_VALUE;
|
|
4218
|
+
exports.MermaidOrientation = MermaidOrientation;
|
|
4219
|
+
exports.MermaidTheme = MermaidTheme;
|
|
4220
|
+
exports.NE = NE;
|
|
4221
|
+
exports.NEG = NEG;
|
|
4222
|
+
exports.NEG_VALUE = NEG_VALUE;
|
|
4223
|
+
exports.NE_VALUE = NE_VALUE;
|
|
4224
|
+
exports.NOT = NOT;
|
|
2603
4225
|
exports.NOTE = NOTE;
|
|
4226
|
+
exports.NOT_VALUE = NOT_VALUE;
|
|
4227
|
+
exports.OR = OR;
|
|
4228
|
+
exports.OR_VALUE = OR_VALUE;
|
|
2604
4229
|
exports.ObscureType = ObscureType;
|
|
2605
4230
|
exports.PARAMETER_IDS = PARAMETER_IDS;
|
|
2606
4231
|
exports.Parameter = Parameter;
|
|
4232
|
+
exports.ParametersStore = ParametersStore;
|
|
2607
4233
|
exports.PrivateKeyBase = PrivateKeyBase;
|
|
2608
4234
|
exports.PublicKeyBase = PublicKeyBase;
|
|
4235
|
+
exports.RHS = RHS;
|
|
4236
|
+
exports.RHS_VALUE = RHS_VALUE;
|
|
4237
|
+
exports.Request = Request;
|
|
4238
|
+
exports.Response = Response;
|
|
2609
4239
|
exports.SALT = SALT;
|
|
2610
4240
|
exports.SIGNED = SIGNED;
|
|
4241
|
+
exports.SUB = SUB;
|
|
4242
|
+
exports.SUB_VALUE = SUB_VALUE;
|
|
2611
4243
|
exports.SealedMessage = SealedMessage;
|
|
2612
|
-
exports
|
|
4244
|
+
Object.defineProperty(exports, 'Signature', {
|
|
4245
|
+
enumerable: true,
|
|
4246
|
+
get: function () {
|
|
4247
|
+
return _bcts_components.Signature;
|
|
4248
|
+
}
|
|
4249
|
+
});
|
|
2613
4250
|
exports.SignatureMetadata = SignatureMetadata;
|
|
2614
|
-
exports
|
|
2615
|
-
|
|
2616
|
-
|
|
4251
|
+
Object.defineProperty(exports, 'SigningPrivateKey', {
|
|
4252
|
+
enumerable: true,
|
|
4253
|
+
get: function () {
|
|
4254
|
+
return _bcts_components.SigningPrivateKey;
|
|
4255
|
+
}
|
|
4256
|
+
});
|
|
4257
|
+
Object.defineProperty(exports, 'SigningPublicKey', {
|
|
4258
|
+
enumerable: true,
|
|
4259
|
+
get: function () {
|
|
4260
|
+
return _bcts_components.SigningPublicKey;
|
|
4261
|
+
}
|
|
4262
|
+
});
|
|
4263
|
+
Object.defineProperty(exports, 'SymmetricKey', {
|
|
4264
|
+
enumerable: true,
|
|
4265
|
+
get: function () {
|
|
4266
|
+
return _bcts_components.SymmetricKey;
|
|
4267
|
+
}
|
|
4268
|
+
});
|
|
2617
4269
|
exports.VENDOR = VENDOR;
|
|
2618
4270
|
exports.VERIFIED_BY = VERIFIED_BY;
|
|
2619
4271
|
exports.VERSION = VERSION;
|
|
4272
|
+
exports.XOR = XOR;
|
|
4273
|
+
exports.XOR_VALUE = XOR_VALUE;
|
|
2620
4274
|
exports.add = add;
|
|
2621
4275
|
exports.and = and;
|
|
4276
|
+
exports.cborEnvelopeSummary = cborEnvelopeSummary;
|
|
4277
|
+
exports.defaultFormatOpts = defaultFormatOpts;
|
|
4278
|
+
exports.defaultMermaidOpts = defaultMermaidOpts;
|
|
2622
4279
|
exports.div = div;
|
|
2623
4280
|
exports.edgeLabel = edgeLabel;
|
|
2624
4281
|
exports.elideAction = elideAction;
|
|
2625
4282
|
exports.envelopeFromBytes = envelopeFromBytes;
|
|
2626
4283
|
exports.envelopeFromCbor = envelopeFromCbor;
|
|
4284
|
+
exports.envelopeSummary = cborEnvelopeSummary;
|
|
2627
4285
|
exports.envelopeToBytes = envelopeToBytes;
|
|
2628
4286
|
exports.envelopeToCbor = envelopeToCbor;
|
|
2629
4287
|
exports.eq = eq;
|
|
@@ -2631,16 +4289,48 @@ exports.extractBoolean = extractBoolean;
|
|
|
2631
4289
|
exports.extractBytes = extractBytes;
|
|
2632
4290
|
exports.extractNull = extractNull;
|
|
2633
4291
|
exports.extractNumber = extractNumber;
|
|
4292
|
+
exports.extractObjectForPredicateWithDefault = extractObjectForPredicateWithDefault;
|
|
4293
|
+
exports.extractObjectsForPredicate = extractObjectsForPredicate;
|
|
2634
4294
|
exports.extractString = extractString;
|
|
4295
|
+
exports.extractSubject = extractSubject;
|
|
2635
4296
|
exports.flanked = flanked;
|
|
4297
|
+
exports.flatFormatOpts = flatFormatOpts;
|
|
4298
|
+
exports.formatAssertion = formatAssertion;
|
|
4299
|
+
exports.formatBegin = formatBegin;
|
|
4300
|
+
exports.formatCbor = formatCbor;
|
|
4301
|
+
exports.formatContextCustom = formatContextCustom;
|
|
4302
|
+
exports.formatContextGlobal = formatContextGlobal;
|
|
4303
|
+
exports.formatContextNone = formatContextNone;
|
|
4304
|
+
exports.formatEnd = formatEnd;
|
|
4305
|
+
exports.formatEnvelope = formatEnvelope;
|
|
4306
|
+
exports.formatItem = formatItem;
|
|
4307
|
+
exports.formatList = formatList;
|
|
4308
|
+
exports.formatSeparator = formatSeparator;
|
|
4309
|
+
exports.ge = ge;
|
|
4310
|
+
exports.getGlobalFormatContext = getGlobalFormatContext;
|
|
4311
|
+
exports.globalFormatContext = globalFormatContext;
|
|
2636
4312
|
exports.gt = gt;
|
|
2637
4313
|
exports.isEnvelopeEncodable = isEnvelopeEncodable;
|
|
4314
|
+
exports.le = le;
|
|
2638
4315
|
exports.lt = lt;
|
|
2639
4316
|
exports.mul = mul;
|
|
4317
|
+
exports.ne = ne;
|
|
2640
4318
|
exports.neg = neg;
|
|
2641
4319
|
exports.not = not;
|
|
2642
4320
|
exports.or = or;
|
|
2643
4321
|
exports.registerCompressExtension = registerCompressExtension;
|
|
2644
4322
|
exports.registerEncryptExtension = registerEncryptExtension;
|
|
4323
|
+
exports.registerMermaidExtension = registerMermaidExtension;
|
|
4324
|
+
exports.registerSealExtension = registerSealExtension;
|
|
4325
|
+
exports.registerSecretExtension = registerSecretExtension;
|
|
4326
|
+
exports.registerSskrExtension = registerSskrExtension;
|
|
4327
|
+
exports.registerTags = registerTags;
|
|
4328
|
+
exports.registerTagsIn = registerTagsIn;
|
|
2645
4329
|
exports.sub = sub;
|
|
4330
|
+
exports.tryObjectForPredicate = tryObjectForPredicate;
|
|
4331
|
+
exports.tryObjectsForPredicate = tryObjectsForPredicate;
|
|
4332
|
+
exports.tryOptionalObjectForPredicate = tryOptionalObjectForPredicate;
|
|
4333
|
+
exports.withFormatContext = withFormatContext;
|
|
4334
|
+
exports.withFormatContextMut = withFormatContextMut;
|
|
4335
|
+
exports.xor = xor;
|
|
2646
4336
|
//# sourceMappingURL=index.cjs.map
|