@bcts/provenance-mark 1.0.0-alpha.12 → 1.0.0-alpha.14

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.
@@ -1,4 +1,4 @@
1
- var bctsProvenanceMark = (function(exports, _bcts_dcbor, _noble_hashes_sha2_js, _noble_hashes_hkdf_js, _noble_ciphers_chacha_js, _bcts_rand, _bcts_tags, _bcts_uniform_resources) {
1
+ var bctsProvenanceMark = (function(exports, _bcts_dcbor, _noble_hashes_sha2_js, _noble_hashes_hkdf_js, _noble_ciphers_chacha_js, _bcts_rand, _bcts_tags, _bcts_uniform_resources, _bcts_envelope) {
2
2
 
3
3
 
4
4
  //#region src/error.ts
@@ -1009,7 +1009,7 @@ var bctsProvenanceMark = (function(exports, _bcts_dcbor, _noble_hashes_sha2_js,
1009
1009
  return ProvenanceMark.fromMessage(res, message);
1010
1010
  }
1011
1011
  /**
1012
- * Encode for URL (minimal bytewords of CBOR).
1012
+ * Encode for URL (minimal bytewords of tagged CBOR).
1013
1013
  */
1014
1014
  toUrlEncoding() {
1015
1015
  return (0, _bcts_uniform_resources.encodeBytewords)(this.toCborData(), _bcts_uniform_resources.BytewordsStyle.Minimal);
@@ -1022,6 +1022,20 @@ var bctsProvenanceMark = (function(exports, _bcts_dcbor, _noble_hashes_sha2_js,
1022
1022
  return ProvenanceMark.fromTaggedCbor(cborValue);
1023
1023
  }
1024
1024
  /**
1025
+ * Get the UR string representation (e.g., "ur:provenance/...").
1026
+ */
1027
+ urString() {
1028
+ return _bcts_uniform_resources.UR.new("provenance", this.untaggedCbor()).string();
1029
+ }
1030
+ /**
1031
+ * Create from a UR string.
1032
+ */
1033
+ static fromURString(urString) {
1034
+ const ur = _bcts_uniform_resources.UR.fromURString(urString);
1035
+ if (ur.urTypeStr() !== "provenance") throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Expected UR type 'provenance', got '${ur.urTypeStr()}'` });
1036
+ return ProvenanceMark.fromUntaggedCbor(ur.cbor());
1037
+ }
1038
+ /**
1025
1039
  * Build a URL with this mark as a query parameter.
1026
1040
  */
1027
1041
  toUrl(base) {
@@ -1098,17 +1112,23 @@ var bctsProvenanceMark = (function(exports, _bcts_dcbor, _noble_hashes_sha2_js,
1098
1112
  }
1099
1113
  /**
1100
1114
  * Detailed debug representation.
1115
+ * Matches Rust format exactly for parity.
1101
1116
  */
1102
1117
  toDebugString() {
1118
+ const dateStr = this._date.toISOString().replace(".000Z", "Z");
1103
1119
  const components = [
1104
1120
  `key: ${bytesToHex(this._key)}`,
1105
1121
  `hash: ${bytesToHex(this._hash)}`,
1106
1122
  `chainID: ${bytesToHex(this._chainId)}`,
1107
1123
  `seq: ${this._seq}`,
1108
- `date: ${this._date.toISOString()}`
1124
+ `date: ${dateStr}`
1109
1125
  ];
1110
1126
  const info = this.info();
1111
- if (info !== void 0) components.push(`info: ${JSON.stringify(info)}`);
1127
+ if (info !== void 0) {
1128
+ const textValue = info.asText();
1129
+ if (textValue !== void 0) components.push(`info: "${textValue}"`);
1130
+ else components.push(`info: ${info.toDiagnostic()}`);
1131
+ }
1112
1132
  return `ProvenanceMark(${components.join(", ")})`;
1113
1133
  }
1114
1134
  /**
@@ -1149,6 +1169,33 @@ var bctsProvenanceMark = (function(exports, _bcts_dcbor, _noble_hashes_sha2_js,
1149
1169
  if (typeof json["info_bytes"] === "string") infoBytes = fromBase64(json["info_bytes"]);
1150
1170
  return new ProvenanceMark(res, key, hash, chainId, seqBytes, dateBytes, infoBytes, seq, date);
1151
1171
  }
1172
+ /**
1173
+ * Convert this provenance mark to a Gordian Envelope.
1174
+ *
1175
+ * The envelope contains the tagged CBOR representation of the mark.
1176
+ *
1177
+ * Note: Use provenanceMarkToEnvelope() for a standalone function alternative.
1178
+ */
1179
+ intoEnvelope() {
1180
+ return _bcts_envelope.Envelope.new(this.toCborData());
1181
+ }
1182
+ /**
1183
+ * Extract a ProvenanceMark from a Gordian Envelope.
1184
+ *
1185
+ * @param envelope - The envelope to extract from
1186
+ * @returns The extracted provenance mark
1187
+ * @throws ProvenanceMarkError if extraction fails
1188
+ */
1189
+ static fromEnvelope(envelope) {
1190
+ const bytes = envelope.asByteString();
1191
+ if (bytes !== void 0) return ProvenanceMark.fromCborData(bytes);
1192
+ const envCase = envelope.case();
1193
+ if (envCase.type === "node") {
1194
+ const subjectBytes = envCase.subject.asByteString();
1195
+ if (subjectBytes !== void 0) return ProvenanceMark.fromCborData(subjectBytes);
1196
+ }
1197
+ throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Could not extract ProvenanceMark from envelope" });
1198
+ }
1152
1199
  };
1153
1200
  /**
1154
1201
  * Helper function to compare two Uint8Arrays.
@@ -1280,6 +1327,63 @@ var bctsProvenanceMark = (function(exports, _bcts_dcbor, _noble_hashes_sha2_js,
1280
1327
  const rngState = RngState.fromBytes(fromBase64(json["rngState"]));
1281
1328
  return ProvenanceMarkGenerator.new(res, seed, chainId, nextSeq, rngState);
1282
1329
  }
1330
+ /**
1331
+ * Convert this generator to a Gordian Envelope.
1332
+ *
1333
+ * The envelope contains structured assertions for all generator fields:
1334
+ * - isA: "provenance-generator"
1335
+ * - res: The resolution
1336
+ * - seed: The seed
1337
+ * - next-seq: The next sequence number
1338
+ * - rng-state: The RNG state
1339
+ *
1340
+ * Note: Use provenanceMarkGeneratorToEnvelope() for a standalone function alternative.
1341
+ */
1342
+ intoEnvelope() {
1343
+ let envelope = _bcts_envelope.Envelope.new(this._chainId);
1344
+ envelope = envelope.addType("provenance-generator");
1345
+ envelope = envelope.addAssertion("res", resolutionToNumber(this._res));
1346
+ envelope = envelope.addAssertion("seed", this._seed.toBytes());
1347
+ envelope = envelope.addAssertion("next-seq", this._nextSeq);
1348
+ envelope = envelope.addAssertion("rng-state", this._rngState.toBytes());
1349
+ return envelope;
1350
+ }
1351
+ /**
1352
+ * Extract a ProvenanceMarkGenerator from a Gordian Envelope.
1353
+ *
1354
+ * @param envelope - The envelope to extract from
1355
+ * @returns The extracted generator
1356
+ * @throws ProvenanceMarkError if extraction fails
1357
+ */
1358
+ static fromEnvelope(envelope) {
1359
+ const env = envelope;
1360
+ if (!env.hasType("provenance-generator")) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Envelope is not a provenance-generator" });
1361
+ const chainId = env.subject().asByteString();
1362
+ if (chainId === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Could not extract chain ID" });
1363
+ const extractAssertion = (predicate) => {
1364
+ const assertions = env.assertionsWithPredicate(predicate);
1365
+ if (assertions.length === 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Missing ${predicate} assertion` });
1366
+ const assertionCase = assertions[0].case();
1367
+ if (assertionCase.type !== "assertion") throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Invalid ${predicate} assertion` });
1368
+ const obj = assertionCase.assertion.object();
1369
+ const objCase = obj.case();
1370
+ if (objCase.type === "leaf") return {
1371
+ cbor: objCase.cbor,
1372
+ bytes: obj.asByteString()
1373
+ };
1374
+ throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Invalid ${predicate} value` });
1375
+ };
1376
+ const res = resolutionFromCbor(extractAssertion("res").cbor);
1377
+ const seedValue = extractAssertion("seed");
1378
+ if (seedValue.bytes === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Invalid seed data" });
1379
+ const seed = ProvenanceSeed.fromBytes(seedValue.bytes);
1380
+ const seqValue = extractAssertion("next-seq");
1381
+ const nextSeq = Number(seqValue.cbor);
1382
+ const rngValue = extractAssertion("rng-state");
1383
+ if (rngValue.bytes === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Invalid rng-state data" });
1384
+ const rngState = RngState.fromBytes(rngValue.bytes);
1385
+ return ProvenanceMarkGenerator.new(res, seed, chainId, nextSeq, rngState);
1386
+ }
1283
1387
  };
1284
1388
 
1285
1389
  //#endregion
@@ -1642,6 +1746,124 @@ var bctsProvenanceMark = (function(exports, _bcts_dcbor, _noble_hashes_sha2_js,
1642
1746
  }
1643
1747
  };
1644
1748
 
1749
+ //#endregion
1750
+ //#region src/envelope.ts
1751
+ /**
1752
+ * Envelope support for Provenance Marks
1753
+ *
1754
+ * This module provides Gordian Envelope integration for ProvenanceMark and
1755
+ * ProvenanceMarkGenerator, enabling them to be used with the bc-envelope
1756
+ * ecosystem.
1757
+ *
1758
+ * Ported from provenance-mark-rust/src/mark.rs and generator.rs (envelope feature)
1759
+ */
1760
+ /**
1761
+ * Registers provenance mark tags in the global format context.
1762
+ *
1763
+ * This function sets up a summarizer for the PROVENANCE_MARK tag that displays
1764
+ * provenance marks in a human-readable format.
1765
+ */
1766
+ function registerTags() {
1767
+ registerTagsIn(globalTagsContext);
1768
+ }
1769
+ /**
1770
+ * Registers provenance mark tags in a specific format context.
1771
+ *
1772
+ * @param context - The format context to register tags in
1773
+ */
1774
+ function registerTagsIn(context) {
1775
+ context.setSummarizer(Number(_bcts_tags.PROVENANCE_MARK.value), (cborValue) => {
1776
+ return ProvenanceMark.fromUntaggedCbor(cborValue).toString();
1777
+ });
1778
+ }
1779
+ const globalTagsContext = { setSummarizer(_tag, _summarizer) {} };
1780
+ /**
1781
+ * Convert a ProvenanceMark to an Envelope.
1782
+ *
1783
+ * The envelope contains the tagged CBOR representation of the mark.
1784
+ *
1785
+ * @param mark - The provenance mark to convert
1786
+ * @returns An envelope containing the mark
1787
+ */
1788
+ function provenanceMarkToEnvelope(mark) {
1789
+ return _bcts_envelope.Envelope.new(mark.toCborData());
1790
+ }
1791
+ /**
1792
+ * Extract a ProvenanceMark from an Envelope.
1793
+ *
1794
+ * @param envelope - The envelope to extract from
1795
+ * @returns The extracted provenance mark
1796
+ * @throws ProvenanceMarkError if extraction fails
1797
+ */
1798
+ function provenanceMarkFromEnvelope(envelope) {
1799
+ const bytes = envelope.asByteString();
1800
+ if (bytes !== void 0) return ProvenanceMark.fromCborData(bytes);
1801
+ const envCase = envelope.case();
1802
+ if (envCase.type === "node") {
1803
+ const subjectBytes = envCase.subject.asByteString();
1804
+ if (subjectBytes !== void 0) return ProvenanceMark.fromCborData(subjectBytes);
1805
+ }
1806
+ throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Could not extract ProvenanceMark from envelope" });
1807
+ }
1808
+ /**
1809
+ * Convert a ProvenanceMarkGenerator to an Envelope.
1810
+ *
1811
+ * The envelope contains structured assertions for all generator fields:
1812
+ * - type: "provenance-generator"
1813
+ * - res: The resolution
1814
+ * - seed: The seed
1815
+ * - next-seq: The next sequence number
1816
+ * - rng-state: The RNG state
1817
+ *
1818
+ * @param generator - The generator to convert
1819
+ * @returns An envelope containing the generator
1820
+ */
1821
+ function provenanceMarkGeneratorToEnvelope(generator) {
1822
+ let envelope = _bcts_envelope.Envelope.new(generator.chainId());
1823
+ envelope = envelope.addType("provenance-generator");
1824
+ envelope = envelope.addAssertion("res", resolutionToNumber(generator.res()));
1825
+ envelope = envelope.addAssertion("seed", generator.seed().toBytes());
1826
+ envelope = envelope.addAssertion("next-seq", generator.nextSeq());
1827
+ envelope = envelope.addAssertion("rng-state", generator.rngState().toBytes());
1828
+ return envelope;
1829
+ }
1830
+ /**
1831
+ * Extract a ProvenanceMarkGenerator from an Envelope.
1832
+ *
1833
+ * @param envelope - The envelope to extract from
1834
+ * @returns The extracted generator
1835
+ * @throws ProvenanceMarkError if extraction fails
1836
+ */
1837
+ function provenanceMarkGeneratorFromEnvelope(envelope) {
1838
+ const env = envelope;
1839
+ if (!env.hasType("provenance-generator")) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Envelope is not a provenance-generator" });
1840
+ const chainId = env.subject().asByteString();
1841
+ if (chainId === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Could not extract chain ID" });
1842
+ const extractAssertion = (predicate) => {
1843
+ const assertions = env.assertionsWithPredicate(predicate);
1844
+ if (assertions.length === 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Missing ${predicate} assertion` });
1845
+ const assertionCase = assertions[0].case();
1846
+ if (assertionCase.type !== "assertion") throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Invalid ${predicate} assertion` });
1847
+ const obj = assertionCase.assertion.object();
1848
+ const objCase = obj.case();
1849
+ if (objCase.type === "leaf") return {
1850
+ cbor: objCase.cbor,
1851
+ bytes: obj.asByteString()
1852
+ };
1853
+ throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Invalid ${predicate} value` });
1854
+ };
1855
+ const res = resolutionFromCbor(extractAssertion("res").cbor);
1856
+ const seedValue = extractAssertion("seed");
1857
+ if (seedValue.bytes === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Invalid seed data" });
1858
+ const seed = ProvenanceSeed.fromBytes(seedValue.bytes);
1859
+ const seqValue = extractAssertion("next-seq");
1860
+ const nextSeq = Number(seqValue.cbor);
1861
+ const rngValue = extractAssertion("rng-state");
1862
+ if (rngValue.bytes === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Invalid rng-state data" });
1863
+ const rngState = RngState.fromBytes(rngValue.bytes);
1864
+ return ProvenanceMarkGenerator.new(res, seed, chainId, nextSeq, rngState);
1865
+ }
1866
+
1645
1867
  //#endregion
1646
1868
  exports.PROVENANCE_SEED_LENGTH = PROVENANCE_SEED_LENGTH;
1647
1869
  exports.ProvenanceMark = ProvenanceMark;
@@ -1679,7 +1901,13 @@ exports.infoRangeStart = infoRangeStart;
1679
1901
  exports.keyRange = keyRange;
1680
1902
  exports.linkLength = linkLength;
1681
1903
  exports.obfuscate = obfuscate;
1904
+ exports.provenanceMarkFromEnvelope = provenanceMarkFromEnvelope;
1905
+ exports.provenanceMarkGeneratorFromEnvelope = provenanceMarkGeneratorFromEnvelope;
1906
+ exports.provenanceMarkGeneratorToEnvelope = provenanceMarkGeneratorToEnvelope;
1907
+ exports.provenanceMarkToEnvelope = provenanceMarkToEnvelope;
1682
1908
  exports.rangeOfDaysInMonth = rangeOfDaysInMonth;
1909
+ exports.registerTags = registerTags;
1910
+ exports.registerTagsIn = registerTagsIn;
1683
1911
  exports.resolutionFromCbor = resolutionFromCbor;
1684
1912
  exports.resolutionFromNumber = resolutionFromNumber;
1685
1913
  exports.resolutionToCbor = resolutionToCbor;
@@ -1696,5 +1924,5 @@ exports.sha256 = sha256;
1696
1924
  exports.sha256Prefix = sha256Prefix;
1697
1925
  exports.validate = validate;
1698
1926
  return exports;
1699
- })({}, bctsDcbor, nobleHashesSha2, nobleHashesHkdf, nobleCiphersChacha, bctsRand, bctsTags, bctsUniformResources);
1927
+ })({}, bctsDcbor, nobleHashesSha2, nobleHashesHkdf, nobleCiphersChacha, bctsRand, bctsTags, bctsUniformResources, bctsEnvelope);
1700
1928
  //# sourceMappingURL=index.iife.js.map