@unicitylabs/sphere-sdk 0.18.0-dev.2 → 0.18.0-dev.3

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.
Files changed (58) hide show
  1. package/dist/connect/chunks/{chunk-OAAY2JEA.js → chunk-7BZL7HE2.js} +3 -3
  2. package/dist/connect/chunks/{chunk-MQQ3TMHM.js → chunk-CJ4D6M6W.js} +2 -2
  3. package/dist/connect/chunks/{chunk-FGQ6CKKK.js → chunk-LDEOQNG2.js} +2 -2
  4. package/dist/connect/chunks/{chunk-FGQ6CKKK.js.map → chunk-LDEOQNG2.js.map} +1 -1
  5. package/dist/connect/chunks/{chunk-3CYK5SIU.js → chunk-WCKTUB4V.js} +1 -1
  6. package/dist/connect/chunks/chunk-WCKTUB4V.js.map +1 -0
  7. package/dist/connect/chunks/{chunk-JYBI36GK.js → chunk-WL6VYP7G.js} +2 -2
  8. package/dist/connect/index.cjs +1 -1
  9. package/dist/connect/index.cjs.map +1 -1
  10. package/dist/connect/index.js +5 -5
  11. package/dist/connect/internal/host.js +3 -3
  12. package/dist/connect/internal/nft-wire.js +2 -2
  13. package/dist/core/index.cjs +831 -343
  14. package/dist/core/index.cjs.map +1 -1
  15. package/dist/core/index.d.cts +30 -1
  16. package/dist/core/index.d.ts +30 -1
  17. package/dist/core/index.js +831 -343
  18. package/dist/core/index.js.map +1 -1
  19. package/dist/impl/browser/connect/index.cjs +1 -1
  20. package/dist/impl/browser/connect/index.cjs.map +1 -1
  21. package/dist/impl/browser/connect/index.js +2 -2
  22. package/dist/impl/browser/index.cjs.map +1 -1
  23. package/dist/impl/browser/index.js.map +1 -1
  24. package/dist/impl/nodejs/index.cjs.map +1 -1
  25. package/dist/impl/nodejs/index.d.cts +1 -0
  26. package/dist/impl/nodejs/index.d.ts +1 -0
  27. package/dist/impl/nodejs/index.js.map +1 -1
  28. package/dist/impl/shared/wallet-api/index.d.cts +1 -0
  29. package/dist/impl/shared/wallet-api/index.d.ts +1 -0
  30. package/dist/impl/wallet-api-v2/index.cjs +2 -1
  31. package/dist/impl/wallet-api-v2/index.cjs.map +1 -1
  32. package/dist/impl/wallet-api-v2/index.d.cts +3 -0
  33. package/dist/impl/wallet-api-v2/index.d.ts +3 -0
  34. package/dist/impl/wallet-api-v2/index.js +2 -1
  35. package/dist/impl/wallet-api-v2/index.js.map +1 -1
  36. package/dist/index.cjs +833 -343
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.d.cts +32 -2
  39. package/dist/index.d.ts +32 -2
  40. package/dist/index.js +832 -343
  41. package/dist/index.js.map +1 -1
  42. package/dist/modules/payments-v2/index.cjs +658 -259
  43. package/dist/modules/payments-v2/index.cjs.map +1 -1
  44. package/dist/modules/payments-v2/index.d.cts +65 -2
  45. package/dist/modules/payments-v2/index.d.ts +65 -2
  46. package/dist/modules/payments-v2/index.js +656 -259
  47. package/dist/modules/payments-v2/index.js.map +1 -1
  48. package/dist/token-engine/index.cjs +185 -65
  49. package/dist/token-engine/index.cjs.map +1 -1
  50. package/dist/token-engine/index.d.cts +20 -3
  51. package/dist/token-engine/index.d.ts +20 -3
  52. package/dist/token-engine/index.js +184 -65
  53. package/dist/token-engine/index.js.map +1 -1
  54. package/package.json +1 -1
  55. package/dist/connect/chunks/chunk-3CYK5SIU.js.map +0 -1
  56. /package/dist/connect/chunks/{chunk-OAAY2JEA.js.map → chunk-7BZL7HE2.js.map} +0 -0
  57. /package/dist/connect/chunks/{chunk-MQQ3TMHM.js.map → chunk-CJ4D6M6W.js.map} +0 -0
  58. /package/dist/connect/chunks/{chunk-JYBI36GK.js.map → chunk-WL6VYP7G.js.map} +0 -0
@@ -22,6 +22,7 @@ var token_engine_exports = {};
22
22
  __export(token_engine_exports, {
23
23
  CheckpointPersistFailedError: () => CheckpointPersistFailedError,
24
24
  CheckpointTrustbaseMismatchError: () => CheckpointTrustbaseMismatchError,
25
+ MintReasonUnverifiableError: () => MintReasonUnverifiableError,
25
26
  NFT_DOCUMENT_MEDIA_TYPE: () => NFT_DOCUMENT_MEDIA_TYPE,
26
27
  NFT_FORMAT_VERSION: () => NFT_FORMAT_VERSION,
27
28
  NFT_LINK_TAG: () => NFT_LINK_TAG,
@@ -252,6 +253,150 @@ var logger = {
252
253
  }
253
254
  };
254
255
 
256
+ // token-engine/claims.ts
257
+ var CoinClaims = class _CoinClaims {
258
+ issuers = /* @__PURE__ */ new Map();
259
+ policies = [];
260
+ static fromPlugins(plugins) {
261
+ const claims = new _CoinClaims();
262
+ for (const plugin of plugins ?? []) {
263
+ for (const policy of plugin.tokenIssuancePolicies ?? []) claims.add(policy);
264
+ }
265
+ return claims;
266
+ }
267
+ add(policy) {
268
+ const tokenType = import_HexConverter.HexConverter.encode(policy.tokenType.bytes);
269
+ const coinIds = policy.coinIds.map((coinId) => coinId.toLowerCase());
270
+ const taken = coinIds.find((coinId) => this.issuers.has(coinId));
271
+ if (taken !== void 0) throw new Error(`Coin ${taken} is already issued by token type ${this.issuers.get(taken)}.`);
272
+ for (const coinId of coinIds) this.issuers.set(coinId, tokenType);
273
+ this.policies.push(JSON.stringify([tokenType, [...coinIds].sort(), policy.revision ?? ""]));
274
+ }
275
+ fingerprint() {
276
+ return [...this.policies].sort().join("\n");
277
+ }
278
+ issuerOf(coinId) {
279
+ return this.issuers.get(coinId.toLowerCase()) ?? null;
280
+ }
281
+ };
282
+
283
+ // token-engine/errors.ts
284
+ var TransferConflictError = class extends SphereError {
285
+ /**
286
+ * #625: the source token id (the caller's id, e.g. `v2_<genesis>`) whose state was already spent.
287
+ * Set by PaymentsModule at the engine call site so the self-healing retry can demote it and re-plan.
288
+ */
289
+ conflictedSourceId;
290
+ constructor(message, cause) {
291
+ super(message, "TRANSFER_CONFLICT", cause);
292
+ this.name = "TransferConflictError";
293
+ }
294
+ };
295
+ var MintReasonUnverifiableError = class extends SphereError {
296
+ constructor(message, cause) {
297
+ super(message, "MINT_REASON_UNVERIFIABLE", cause);
298
+ this.name = "MintReasonUnverifiableError";
299
+ }
300
+ };
301
+ var ProofUnconfirmedError = class extends SphereError {
302
+ mayHaveCertified = true;
303
+ constructor(message, cause) {
304
+ super(message, "CERTIFICATION_UNCONFIRMED", cause);
305
+ this.name = "ProofUnconfirmedError";
306
+ }
307
+ };
308
+ var CheckpointPersistFailedError = class extends SphereError {
309
+ mayHaveCertified = true;
310
+ constructor(message, cause) {
311
+ super(message, "CHECKPOINT_PERSIST_FAILED", cause);
312
+ this.name = "CheckpointPersistFailedError";
313
+ }
314
+ };
315
+ var SplitCheckpointLostError = class extends SphereError {
316
+ constructor(message, cause) {
317
+ super(message, "SPLIT_CHECKPOINT_LOST", cause);
318
+ this.name = "SplitCheckpointLostError";
319
+ }
320
+ };
321
+ var CheckpointTrustbaseMismatchError = class extends SphereError {
322
+ constructor(message, cause) {
323
+ super(message, "CHECKPOINT_TRUSTBASE_MISMATCH", cause);
324
+ this.name = "CheckpointTrustbaseMismatchError";
325
+ }
326
+ };
327
+ function isKeepOpenSplitError(err) {
328
+ return err instanceof ProofUnconfirmedError || err instanceof CheckpointPersistFailedError || err instanceof SplitCheckpointLostError || err instanceof CheckpointTrustbaseMismatchError;
329
+ }
330
+
331
+ // token-engine/mint-reasons.ts
332
+ var RULE = "MintJustificationVerification";
333
+ var MintReasonRegistry = class _MintReasonRegistry extends import_MintJustificationVerifierService.MintJustificationVerifierService {
334
+ byTag = /* @__PURE__ */ new Map();
335
+ pluginTags = /* @__PURE__ */ new Set();
336
+ register(verifier) {
337
+ super.register(verifier);
338
+ this.byTag.set(verifier.tag, verifier);
339
+ return this;
340
+ }
341
+ registerPlugin(verifier) {
342
+ this.register(verifier);
343
+ this.pluginTags.add(verifier.tag);
344
+ return this;
345
+ }
346
+ async verify(transaction, nestedTokenCollector) {
347
+ const tag = transaction.justification ? reasonTag(transaction.justification) : null;
348
+ if (tag === null) return super.verify(transaction, nestedTokenCollector);
349
+ const verifier = this.byTag.get(tag);
350
+ if (verifier === void 0) throw new MintReasonUnverifiableError(`No verifier is registered for mint-reason tag ${tag}`);
351
+ if (!this.pluginTags.has(tag)) return super.verify(transaction, nestedTokenCollector);
352
+ return verifyPlugin(verifier, transaction, nestedTokenCollector);
353
+ }
354
+ knows(tag) {
355
+ return this.byTag.has(tag);
356
+ }
357
+ /** `verifiers` replace the registered ones of their tags; a tag this registry lacks is refused. */
358
+ overlaid(verifiers) {
359
+ for (const verifier of verifiers) assertKnown(this, verifier.tag);
360
+ const replaced = new Map(verifiers.map((verifier) => [verifier.tag, verifier]));
361
+ const overlay = new _MintReasonRegistry();
362
+ for (const [tag, verifier] of this.byTag) {
363
+ const perCall = replaced.get(tag);
364
+ if (perCall !== void 0 || this.pluginTags.has(tag)) overlay.registerPlugin(perCall ?? verifier);
365
+ else overlay.register(verifier);
366
+ }
367
+ return overlay;
368
+ }
369
+ assertVerifiable(justification) {
370
+ const tag = reasonTag(justification);
371
+ if (tag === null) throw new SphereError("The mint reason is not tagged CBOR", "VALIDATION_ERROR");
372
+ assertKnown(this, tag);
373
+ }
374
+ };
375
+ function reasonTag(justification) {
376
+ try {
377
+ return BigInt(import_CborDeserializer.CborDeserializer.decodeTag(justification).tag);
378
+ } catch {
379
+ return null;
380
+ }
381
+ }
382
+ async function verifyPlugin(verifier, transaction, nestedTokenCollector) {
383
+ let result;
384
+ try {
385
+ result = await verifier.verify(transaction, nestedTokenCollector);
386
+ } catch (err) {
387
+ const why = err instanceof Error ? err.message : String(err);
388
+ throw new MintReasonUnverifiableError(`Mint-reason tag ${verifier.tag} cannot be verified yet: ${why}`, err);
389
+ }
390
+ return result.status === import_VerificationStatus.VerificationStatus.OK ? new import_VerificationResult.VerificationResult(RULE, import_VerificationStatus.VerificationStatus.OK, "", [result]) : new import_VerificationResult.VerificationResult(RULE, import_VerificationStatus.VerificationStatus.FAIL, `Verification failed for tag ${verifier.tag}.`, [result]);
391
+ }
392
+ function assertKnown(registry, tag) {
393
+ if (registry.knows(tag)) return;
394
+ throw new SphereError(
395
+ `No verifier is registered on the engine for mint-reason tag ${tag}, so a replay of this mint could never verify`,
396
+ "VALIDATION_ERROR"
397
+ );
398
+ }
399
+
255
400
  // token-engine/SpherePaymentData.ts
256
401
  var COIN_ID_PATTERN = /^([0-9a-f]{2})+$/;
257
402
  function assertAsset(coinId, amount) {
@@ -772,45 +917,6 @@ async function awaitProofBounded(deps, transaction, signal) {
772
917
  return value;
773
918
  }
774
919
 
775
- // token-engine/errors.ts
776
- var TransferConflictError = class extends SphereError {
777
- /**
778
- * #625: the source token id (the caller's id, e.g. `v2_<genesis>`) whose state was already spent.
779
- * Set by PaymentsModule at the engine call site so the self-healing retry can demote it and re-plan.
780
- */
781
- conflictedSourceId;
782
- constructor(message, cause) {
783
- super(message, "TRANSFER_CONFLICT", cause);
784
- this.name = "TransferConflictError";
785
- }
786
- };
787
- var ProofUnconfirmedError = class extends SphereError {
788
- mayHaveCertified = true;
789
- constructor(message, cause) {
790
- super(message, "CERTIFICATION_UNCONFIRMED", cause);
791
- this.name = "ProofUnconfirmedError";
792
- }
793
- };
794
- var CheckpointPersistFailedError = class extends SphereError {
795
- mayHaveCertified = true;
796
- constructor(message, cause) {
797
- super(message, "CHECKPOINT_PERSIST_FAILED", cause);
798
- this.name = "CheckpointPersistFailedError";
799
- }
800
- };
801
- var SplitCheckpointLostError = class extends SphereError {
802
- constructor(message, cause) {
803
- super(message, "SPLIT_CHECKPOINT_LOST", cause);
804
- this.name = "SplitCheckpointLostError";
805
- }
806
- };
807
- var CheckpointTrustbaseMismatchError = class extends SphereError {
808
- constructor(message, cause) {
809
- super(message, "CHECKPOINT_TRUSTBASE_MISMATCH", cause);
810
- this.name = "CheckpointTrustbaseMismatchError";
811
- }
812
- };
813
-
814
920
  // token-engine/certification-outcome.ts
815
921
  function certificationFailure(err, failLabel, submitError, submitRejectedCleanly) {
816
922
  if (err instanceof Error && err.message === `Invalid inclusion proof status: ${import_InclusionProofVerificationRule.InclusionProofVerificationStatus.TRANSACTION_HASH_MISMATCH}`) {
@@ -1475,15 +1581,17 @@ async function readTokenNft(token) {
1475
1581
  }
1476
1582
 
1477
1583
  // token-engine/plugin-ops.ts
1478
- function contextWithMintVerifiers(base, verifiers) {
1479
- const registry = new import_MintJustificationVerifierService.MintJustificationVerifierService();
1480
- for (const verifier of verifiers) registry.register(verifier);
1584
+ function mintContext(deps, params) {
1585
+ assertMintableData(params.data);
1586
+ if (params.justification !== void 0) deps.mintJustificationVerifier.assertVerifiable(params.justification);
1587
+ const verifiers = params.mintJustificationVerifiers;
1588
+ if (verifiers === void 0) return deps.verificationContext;
1481
1589
  return new import_VerificationContext.VerificationContext(
1482
- base.trustBase,
1483
- base.predicateVerifier,
1484
- base.unicityCertificateVerifier,
1485
- registry,
1486
- new import_TokenIssuanceVerifierService.TokenIssuanceVerifierService(false)
1590
+ deps.trustBase,
1591
+ deps.predicateVerifier,
1592
+ deps.unicityCertificateVerifier,
1593
+ deps.mintJustificationVerifier.overlaid(verifiers),
1594
+ deps.verificationContext.tokenIssuanceVerifier
1487
1595
  );
1488
1596
  }
1489
1597
 
@@ -1571,9 +1679,6 @@ async function burntTokenFromCheckpoint(deps, storedBytes, reDerivedBurnTx, sour
1571
1679
  // token-engine/SphereTokenEngine.ts
1572
1680
  var DEFAULT_PROOF_POLL_INTERVAL_MS = 300;
1573
1681
  var MAX_MINT_CONCURRENCY = 8;
1574
- function isKeepOpenSplitError(err) {
1575
- return err instanceof ProofUnconfirmedError || err instanceof CheckpointPersistFailedError || err instanceof SplitCheckpointLostError || err instanceof CheckpointTrustbaseMismatchError;
1576
- }
1577
1682
  var TRANSFER_ID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
1578
1683
  var CLEAN_REJECT_STATUSES = /* @__PURE__ */ new Set([
1579
1684
  import_CertificationResponse.CertificationStatus.STATE_ID_MISMATCH,
@@ -1663,8 +1768,9 @@ var SphereTokenEngine = class {
1663
1768
  const token = await import_Token.Token.mint(certified, this.deps.verificationContext);
1664
1769
  return wrapToken(token);
1665
1770
  }
1771
+ assertMintable = (params) => void mintContext(this.deps, params);
1666
1772
  async mintDataToken(params, options) {
1667
- assertMintableData(params.data);
1773
+ const context = mintContext(this.deps, params);
1668
1774
  const recipient = import_SignaturePredicate.SignaturePredicate.create(params.recipientPubkey);
1669
1775
  const tokenType = params.tokenType ? new import_TokenType.TokenType(params.tokenType) : import_TokenType.TokenType.generate();
1670
1776
  const salt = params.salt ? import_TokenSalt.TokenSalt.fromBytes(params.salt) : import_TokenSalt.TokenSalt.generate();
@@ -1678,8 +1784,7 @@ var SphereTokenEngine = class {
1678
1784
  options
1679
1785
  );
1680
1786
  const certified = await mintTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, this.deps.unicityCertificateVerifier, proof);
1681
- const verifiers = params.mintJustificationVerifiers;
1682
- return wrapToken(await import_Token.Token.mint(certified, verifiers ? contextWithMintVerifiers(this.deps, verifiers) : this.deps.verificationContext));
1787
+ return wrapToken(await import_Token.Token.mint(certified, context));
1683
1788
  }
1684
1789
  buildNftMint(params) {
1685
1790
  return planNftMint(this.deps, params);
@@ -2016,20 +2121,30 @@ var SphereTokenEngine = class {
2016
2121
 
2017
2122
  // token-engine/factory.ts
2018
2123
  var DEFAULT_VERIFICATION_POOL_SIZE = 4;
2019
- function registerPluginVerifiers(registry, plugins) {
2124
+ function registerPlugins(registries, plugins) {
2020
2125
  for (const plugin of plugins ?? []) {
2021
2126
  for (const verifier of plugin.mintJustificationVerifiers ?? []) {
2022
- try {
2023
- registry.register(verifier);
2024
- } catch (err) {
2025
- throw new SphereError(
2026
- `Token plugin '${plugin.id}' registers mint-reason tag ${verifier.tag} twice or over another plugin's: ` + (err instanceof Error ? err.message : String(err)),
2027
- "INVALID_CONFIG"
2028
- );
2029
- }
2127
+ claim(plugin, `mint-reason tag ${verifier.tag}`, () => registries.reasons.registerPlugin(verifier));
2128
+ }
2129
+ for (const policy of plugin.tokenIssuancePolicies ?? []) {
2130
+ const tokenType = import_HexConverter.HexConverter.encode(policy.tokenType.bytes);
2131
+ claim(plugin, `an issuance policy for token type ${tokenType}`, () => {
2132
+ registries.claims.add(policy);
2133
+ registries.policies.register(policy);
2134
+ });
2030
2135
  }
2031
2136
  }
2032
2137
  }
2138
+ function claim(plugin, what, register) {
2139
+ try {
2140
+ register();
2141
+ } catch (err) {
2142
+ throw new SphereError(
2143
+ `Token plugin '${plugin.id}' registers ${what} twice or over another plugin's: ` + (err instanceof Error ? err.message : String(err)),
2144
+ "INVALID_CONFIG"
2145
+ );
2146
+ }
2147
+ }
2033
2148
  function disposedError() {
2034
2149
  return new SphereError(
2035
2150
  "Verification worker pool disposed \u2014 the in-flight verification was cancelled",
@@ -2107,11 +2222,15 @@ async function createSphereTokenEngine(config) {
2107
2222
  const unicityCertificateVerifier = new import_UnicityCertificateVerifier.UnicityCertificateVerifier(
2108
2223
  new import_UnicitySealQuorumSignaturesVerificationRule.UnicitySealQuorumSignaturesVerificationRule(new import_Secp256k1SignatureVerifier.Secp256k1SignatureVerifier(), new import_VerifiedSealCache.VerifiedSealCache(256))
2109
2224
  );
2110
- const mintJustificationVerifier = new import_MintJustificationVerifierService.MintJustificationVerifierService();
2225
+ const mintJustificationVerifier = new MintReasonRegistry();
2111
2226
  mintJustificationVerifier.register(
2112
2227
  new import_SplitMintJustificationVerifier.SplitMintJustificationVerifier(decodeSpherePaymentData)
2113
2228
  );
2114
- registerPluginVerifiers(mintJustificationVerifier, config.plugins);
2229
+ const tokenIssuanceVerifier = new import_TokenIssuanceVerifierService.TokenIssuanceVerifierService(false);
2230
+ registerPlugins(
2231
+ { reasons: mintJustificationVerifier, policies: tokenIssuanceVerifier, claims: new CoinClaims() },
2232
+ config.plugins
2233
+ );
2115
2234
  const deps = {
2116
2235
  client: new import_StateTransitionClient.StateTransitionClient(new import_AggregatorClient.AggregatorClient(config.aggregatorUrl, config.apiKey ?? null)),
2117
2236
  trustBase,
@@ -2123,7 +2242,7 @@ async function createSphereTokenEngine(config) {
2123
2242
  predicateVerifier,
2124
2243
  unicityCertificateVerifier,
2125
2244
  mintJustificationVerifier,
2126
- new import_TokenIssuanceVerifierService.TokenIssuanceVerifierService(false)
2245
+ tokenIssuanceVerifier
2127
2246
  ),
2128
2247
  signingService: new import_SigningService.SigningService(config.privateKey),
2129
2248
  // Also the HKDF ikm for deterministic realization (Part E.1) — the
@@ -2145,6 +2264,7 @@ async function createSphereTokenEngine(config) {
2145
2264
  0 && (module.exports = {
2146
2265
  CheckpointPersistFailedError,
2147
2266
  CheckpointTrustbaseMismatchError,
2267
+ MintReasonUnverifiableError,
2148
2268
  NFT_DOCUMENT_MEDIA_TYPE,
2149
2269
  NFT_FORMAT_VERSION,
2150
2270
  NFT_LINK_TAG,