@bcts/provenance-mark 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 CHANGED
@@ -5,6 +5,7 @@ let _noble_ciphers_chacha_js = require("@noble/ciphers/chacha.js");
5
5
  let _bcts_rand = require("@bcts/rand");
6
6
  let _bcts_tags = require("@bcts/tags");
7
7
  let _bcts_uniform_resources = require("@bcts/uniform-resources");
8
+ let _bcts_envelope = require("@bcts/envelope");
8
9
 
9
10
  //#region src/error.ts
10
11
  /**
@@ -1014,7 +1015,7 @@ var ProvenanceMark = class ProvenanceMark {
1014
1015
  return ProvenanceMark.fromMessage(res, message);
1015
1016
  }
1016
1017
  /**
1017
- * Encode for URL (minimal bytewords of CBOR).
1018
+ * Encode for URL (minimal bytewords of tagged CBOR).
1018
1019
  */
1019
1020
  toUrlEncoding() {
1020
1021
  return (0, _bcts_uniform_resources.encodeBytewords)(this.toCborData(), _bcts_uniform_resources.BytewordsStyle.Minimal);
@@ -1027,6 +1028,20 @@ var ProvenanceMark = class ProvenanceMark {
1027
1028
  return ProvenanceMark.fromTaggedCbor(cborValue);
1028
1029
  }
1029
1030
  /**
1031
+ * Get the UR string representation (e.g., "ur:provenance/...").
1032
+ */
1033
+ urString() {
1034
+ return _bcts_uniform_resources.UR.new("provenance", this.untaggedCbor()).string();
1035
+ }
1036
+ /**
1037
+ * Create from a UR string.
1038
+ */
1039
+ static fromURString(urString) {
1040
+ const ur = _bcts_uniform_resources.UR.fromURString(urString);
1041
+ if (ur.urTypeStr() !== "provenance") throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Expected UR type 'provenance', got '${ur.urTypeStr()}'` });
1042
+ return ProvenanceMark.fromUntaggedCbor(ur.cbor());
1043
+ }
1044
+ /**
1030
1045
  * Build a URL with this mark as a query parameter.
1031
1046
  */
1032
1047
  toUrl(base) {
@@ -1103,17 +1118,23 @@ var ProvenanceMark = class ProvenanceMark {
1103
1118
  }
1104
1119
  /**
1105
1120
  * Detailed debug representation.
1121
+ * Matches Rust format exactly for parity.
1106
1122
  */
1107
1123
  toDebugString() {
1124
+ const dateStr = this._date.toISOString().replace(".000Z", "Z");
1108
1125
  const components = [
1109
1126
  `key: ${bytesToHex(this._key)}`,
1110
1127
  `hash: ${bytesToHex(this._hash)}`,
1111
1128
  `chainID: ${bytesToHex(this._chainId)}`,
1112
1129
  `seq: ${this._seq}`,
1113
- `date: ${this._date.toISOString()}`
1130
+ `date: ${dateStr}`
1114
1131
  ];
1115
1132
  const info = this.info();
1116
- if (info !== void 0) components.push(`info: ${JSON.stringify(info)}`);
1133
+ if (info !== void 0) {
1134
+ const textValue = info.asText();
1135
+ if (textValue !== void 0) components.push(`info: "${textValue}"`);
1136
+ else components.push(`info: ${info.toDiagnostic()}`);
1137
+ }
1117
1138
  return `ProvenanceMark(${components.join(", ")})`;
1118
1139
  }
1119
1140
  /**
@@ -1154,6 +1175,33 @@ var ProvenanceMark = class ProvenanceMark {
1154
1175
  if (typeof json["info_bytes"] === "string") infoBytes = fromBase64(json["info_bytes"]);
1155
1176
  return new ProvenanceMark(res, key, hash, chainId, seqBytes, dateBytes, infoBytes, seq, date);
1156
1177
  }
1178
+ /**
1179
+ * Convert this provenance mark to a Gordian Envelope.
1180
+ *
1181
+ * The envelope contains the tagged CBOR representation of the mark.
1182
+ *
1183
+ * Note: Use provenanceMarkToEnvelope() for a standalone function alternative.
1184
+ */
1185
+ intoEnvelope() {
1186
+ return _bcts_envelope.Envelope.new(this.toCborData());
1187
+ }
1188
+ /**
1189
+ * Extract a ProvenanceMark from a Gordian Envelope.
1190
+ *
1191
+ * @param envelope - The envelope to extract from
1192
+ * @returns The extracted provenance mark
1193
+ * @throws ProvenanceMarkError if extraction fails
1194
+ */
1195
+ static fromEnvelope(envelope) {
1196
+ const bytes = envelope.asByteString();
1197
+ if (bytes !== void 0) return ProvenanceMark.fromCborData(bytes);
1198
+ const envCase = envelope.case();
1199
+ if (envCase.type === "node") {
1200
+ const subjectBytes = envCase.subject.asByteString();
1201
+ if (subjectBytes !== void 0) return ProvenanceMark.fromCborData(subjectBytes);
1202
+ }
1203
+ throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Could not extract ProvenanceMark from envelope" });
1204
+ }
1157
1205
  };
1158
1206
  /**
1159
1207
  * Helper function to compare two Uint8Arrays.
@@ -1285,6 +1333,63 @@ var ProvenanceMarkGenerator = class ProvenanceMarkGenerator {
1285
1333
  const rngState = RngState.fromBytes(fromBase64(json["rngState"]));
1286
1334
  return ProvenanceMarkGenerator.new(res, seed, chainId, nextSeq, rngState);
1287
1335
  }
1336
+ /**
1337
+ * Convert this generator to a Gordian Envelope.
1338
+ *
1339
+ * The envelope contains structured assertions for all generator fields:
1340
+ * - isA: "provenance-generator"
1341
+ * - res: The resolution
1342
+ * - seed: The seed
1343
+ * - next-seq: The next sequence number
1344
+ * - rng-state: The RNG state
1345
+ *
1346
+ * Note: Use provenanceMarkGeneratorToEnvelope() for a standalone function alternative.
1347
+ */
1348
+ intoEnvelope() {
1349
+ let envelope = _bcts_envelope.Envelope.new(this._chainId);
1350
+ envelope = envelope.addAssertion("isA", "provenance-generator");
1351
+ envelope = envelope.addAssertion("res", resolutionToNumber(this._res));
1352
+ envelope = envelope.addAssertion("seed", this._seed.toBytes());
1353
+ envelope = envelope.addAssertion("next-seq", this._nextSeq);
1354
+ envelope = envelope.addAssertion("rng-state", this._rngState.toBytes());
1355
+ return envelope;
1356
+ }
1357
+ /**
1358
+ * Extract a ProvenanceMarkGenerator from a Gordian Envelope.
1359
+ *
1360
+ * @param envelope - The envelope to extract from
1361
+ * @returns The extracted generator
1362
+ * @throws ProvenanceMarkError if extraction fails
1363
+ */
1364
+ static fromEnvelope(envelope) {
1365
+ const env = envelope;
1366
+ if (!env.hasType("provenance-generator")) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Envelope is not a provenance-generator" });
1367
+ const chainId = env.subject().asByteString();
1368
+ if (chainId === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Could not extract chain ID" });
1369
+ const extractAssertion = (predicate) => {
1370
+ const assertions = env.assertionsWithPredicate(predicate);
1371
+ if (assertions.length === 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Missing ${predicate} assertion` });
1372
+ const assertionCase = assertions[0].case();
1373
+ if (assertionCase.type !== "assertion") throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Invalid ${predicate} assertion` });
1374
+ const obj = assertionCase.assertion.object();
1375
+ const objCase = obj.case();
1376
+ if (objCase.type === "leaf") return {
1377
+ cbor: objCase.cbor,
1378
+ bytes: obj.asByteString()
1379
+ };
1380
+ throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Invalid ${predicate} value` });
1381
+ };
1382
+ const res = resolutionFromCbor(extractAssertion("res").cbor);
1383
+ const seedValue = extractAssertion("seed");
1384
+ if (seedValue.bytes === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Invalid seed data" });
1385
+ const seed = ProvenanceSeed.fromBytes(seedValue.bytes);
1386
+ const seqValue = extractAssertion("next-seq");
1387
+ const nextSeq = Number(seqValue.cbor);
1388
+ const rngValue = extractAssertion("rng-state");
1389
+ if (rngValue.bytes === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Invalid rng-state data" });
1390
+ const rngState = RngState.fromBytes(rngValue.bytes);
1391
+ return ProvenanceMarkGenerator.new(res, seed, chainId, nextSeq, rngState);
1392
+ }
1288
1393
  };
1289
1394
 
1290
1395
  //#endregion
@@ -1647,6 +1752,124 @@ var ProvenanceMarkInfo = class ProvenanceMarkInfo {
1647
1752
  }
1648
1753
  };
1649
1754
 
1755
+ //#endregion
1756
+ //#region src/envelope.ts
1757
+ /**
1758
+ * Envelope support for Provenance Marks
1759
+ *
1760
+ * This module provides Gordian Envelope integration for ProvenanceMark and
1761
+ * ProvenanceMarkGenerator, enabling them to be used with the bc-envelope
1762
+ * ecosystem.
1763
+ *
1764
+ * Ported from provenance-mark-rust/src/mark.rs and generator.rs (envelope feature)
1765
+ */
1766
+ /**
1767
+ * Registers provenance mark tags in the global format context.
1768
+ *
1769
+ * This function sets up a summarizer for the PROVENANCE_MARK tag that displays
1770
+ * provenance marks in a human-readable format.
1771
+ */
1772
+ function registerTags() {
1773
+ registerTagsIn(globalTagsContext);
1774
+ }
1775
+ /**
1776
+ * Registers provenance mark tags in a specific format context.
1777
+ *
1778
+ * @param context - The format context to register tags in
1779
+ */
1780
+ function registerTagsIn(context) {
1781
+ context.setSummarizer(Number(_bcts_tags.PROVENANCE_MARK.value), (cborValue) => {
1782
+ return ProvenanceMark.fromUntaggedCbor(cborValue).toString();
1783
+ });
1784
+ }
1785
+ const globalTagsContext = { setSummarizer(_tag, _summarizer) {} };
1786
+ /**
1787
+ * Convert a ProvenanceMark to an Envelope.
1788
+ *
1789
+ * The envelope contains the tagged CBOR representation of the mark.
1790
+ *
1791
+ * @param mark - The provenance mark to convert
1792
+ * @returns An envelope containing the mark
1793
+ */
1794
+ function provenanceMarkToEnvelope(mark) {
1795
+ return _bcts_envelope.Envelope.new(mark.toCborData());
1796
+ }
1797
+ /**
1798
+ * Extract a ProvenanceMark from an Envelope.
1799
+ *
1800
+ * @param envelope - The envelope to extract from
1801
+ * @returns The extracted provenance mark
1802
+ * @throws ProvenanceMarkError if extraction fails
1803
+ */
1804
+ function provenanceMarkFromEnvelope(envelope) {
1805
+ const bytes = envelope.asByteString();
1806
+ if (bytes !== void 0) return ProvenanceMark.fromCborData(bytes);
1807
+ const envCase = envelope.case();
1808
+ if (envCase.type === "node") {
1809
+ const subjectBytes = envCase.subject.asByteString();
1810
+ if (subjectBytes !== void 0) return ProvenanceMark.fromCborData(subjectBytes);
1811
+ }
1812
+ throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Could not extract ProvenanceMark from envelope" });
1813
+ }
1814
+ /**
1815
+ * Convert a ProvenanceMarkGenerator to an Envelope.
1816
+ *
1817
+ * The envelope contains structured assertions for all generator fields:
1818
+ * - type: "provenance-generator"
1819
+ * - res: The resolution
1820
+ * - seed: The seed
1821
+ * - next-seq: The next sequence number
1822
+ * - rng-state: The RNG state
1823
+ *
1824
+ * @param generator - The generator to convert
1825
+ * @returns An envelope containing the generator
1826
+ */
1827
+ function provenanceMarkGeneratorToEnvelope(generator) {
1828
+ let envelope = _bcts_envelope.Envelope.new(generator.chainId());
1829
+ envelope = envelope.addAssertion("isA", "provenance-generator");
1830
+ envelope = envelope.addAssertion("res", resolutionToNumber(generator.res()));
1831
+ envelope = envelope.addAssertion("seed", generator.seed().toBytes());
1832
+ envelope = envelope.addAssertion("next-seq", generator.nextSeq());
1833
+ envelope = envelope.addAssertion("rng-state", generator.rngState().toBytes());
1834
+ return envelope;
1835
+ }
1836
+ /**
1837
+ * Extract a ProvenanceMarkGenerator from an Envelope.
1838
+ *
1839
+ * @param envelope - The envelope to extract from
1840
+ * @returns The extracted generator
1841
+ * @throws ProvenanceMarkError if extraction fails
1842
+ */
1843
+ function provenanceMarkGeneratorFromEnvelope(envelope) {
1844
+ const env = envelope;
1845
+ if (!env.hasType("provenance-generator")) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Envelope is not a provenance-generator" });
1846
+ const chainId = env.subject().asByteString();
1847
+ if (chainId === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Could not extract chain ID" });
1848
+ const extractAssertion = (predicate) => {
1849
+ const assertions = env.assertionsWithPredicate(predicate);
1850
+ if (assertions.length === 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Missing ${predicate} assertion` });
1851
+ const assertionCase = assertions[0].case();
1852
+ if (assertionCase.type !== "assertion") throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Invalid ${predicate} assertion` });
1853
+ const obj = assertionCase.assertion.object();
1854
+ const objCase = obj.case();
1855
+ if (objCase.type === "leaf") return {
1856
+ cbor: objCase.cbor,
1857
+ bytes: obj.asByteString()
1858
+ };
1859
+ throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: `Invalid ${predicate} value` });
1860
+ };
1861
+ const res = resolutionFromCbor(extractAssertion("res").cbor);
1862
+ const seedValue = extractAssertion("seed");
1863
+ if (seedValue.bytes === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Invalid seed data" });
1864
+ const seed = ProvenanceSeed.fromBytes(seedValue.bytes);
1865
+ const seqValue = extractAssertion("next-seq");
1866
+ const nextSeq = Number(seqValue.cbor);
1867
+ const rngValue = extractAssertion("rng-state");
1868
+ if (rngValue.bytes === void 0) throw new ProvenanceMarkError(ProvenanceMarkErrorType.CborError, void 0, { message: "Invalid rng-state data" });
1869
+ const rngState = RngState.fromBytes(rngValue.bytes);
1870
+ return ProvenanceMarkGenerator.new(res, seed, chainId, nextSeq, rngState);
1871
+ }
1872
+
1650
1873
  //#endregion
1651
1874
  exports.PROVENANCE_SEED_LENGTH = PROVENANCE_SEED_LENGTH;
1652
1875
  exports.ProvenanceMark = ProvenanceMark;
@@ -1684,7 +1907,13 @@ exports.infoRangeStart = infoRangeStart;
1684
1907
  exports.keyRange = keyRange;
1685
1908
  exports.linkLength = linkLength;
1686
1909
  exports.obfuscate = obfuscate;
1910
+ exports.provenanceMarkFromEnvelope = provenanceMarkFromEnvelope;
1911
+ exports.provenanceMarkGeneratorFromEnvelope = provenanceMarkGeneratorFromEnvelope;
1912
+ exports.provenanceMarkGeneratorToEnvelope = provenanceMarkGeneratorToEnvelope;
1913
+ exports.provenanceMarkToEnvelope = provenanceMarkToEnvelope;
1687
1914
  exports.rangeOfDaysInMonth = rangeOfDaysInMonth;
1915
+ exports.registerTags = registerTags;
1916
+ exports.registerTagsIn = registerTagsIn;
1688
1917
  exports.resolutionFromCbor = resolutionFromCbor;
1689
1918
  exports.resolutionFromNumber = resolutionFromNumber;
1690
1919
  exports.resolutionToCbor = resolutionToCbor;