@unicitylabs/sphere-sdk 0.14.9 → 0.14.11-dev.1

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 (35) hide show
  1. package/dist/connect/index.cjs +1 -1
  2. package/dist/connect/index.cjs.map +1 -1
  3. package/dist/connect/index.js +1 -1
  4. package/dist/connect/index.js.map +1 -1
  5. package/dist/core/index.cjs +156 -22
  6. package/dist/core/index.cjs.map +1 -1
  7. package/dist/core/index.d.cts +2 -0
  8. package/dist/core/index.d.ts +2 -0
  9. package/dist/core/index.js +156 -22
  10. package/dist/core/index.js.map +1 -1
  11. package/dist/impl/browser/connect/index.cjs +1 -1
  12. package/dist/impl/browser/connect/index.cjs.map +1 -1
  13. package/dist/impl/browser/connect/index.js +1 -1
  14. package/dist/impl/browser/connect/index.js.map +1 -1
  15. package/dist/impl/wallet-api-v2/index.cjs +4 -0
  16. package/dist/impl/wallet-api-v2/index.cjs.map +1 -1
  17. package/dist/impl/wallet-api-v2/index.js +4 -0
  18. package/dist/impl/wallet-api-v2/index.js.map +1 -1
  19. package/dist/index.cjs +156 -22
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +2 -0
  22. package/dist/index.d.ts +2 -0
  23. package/dist/index.js +156 -22
  24. package/dist/index.js.map +1 -1
  25. package/dist/modules/payments-v2/index.cjs +143 -16
  26. package/dist/modules/payments-v2/index.cjs.map +1 -1
  27. package/dist/modules/payments-v2/index.d.cts +6 -2
  28. package/dist/modules/payments-v2/index.d.ts +6 -2
  29. package/dist/modules/payments-v2/index.js +143 -16
  30. package/dist/modules/payments-v2/index.js.map +1 -1
  31. package/dist/token-engine/index.cjs +17 -6
  32. package/dist/token-engine/index.cjs.map +1 -1
  33. package/dist/token-engine/index.js +17 -6
  34. package/dist/token-engine/index.js.map +1 -1
  35. package/package.json +2 -2
package/dist/index.cjs CHANGED
@@ -9362,6 +9362,10 @@ var import_StateMask = require("@unicitylabs/state-transition-sdk/lib/transactio
9362
9362
  var import_MintJustificationVerifierService = require("@unicitylabs/state-transition-sdk/lib/transaction/verification/MintJustificationVerifierService.js");
9363
9363
  var import_TokenIssuanceVerifierService = require("@unicitylabs/state-transition-sdk/lib/transaction/verification/TokenIssuanceVerifierService.js");
9364
9364
  var import_VerificationContext = require("@unicitylabs/state-transition-sdk/lib/transaction/verification/VerificationContext.js");
9365
+ var import_UnicityCertificateVerifier = require("@unicitylabs/state-transition-sdk/lib/api/bft/verification/UnicityCertificateVerifier.js");
9366
+ var import_UnicitySealQuorumSignaturesVerificationRule = require("@unicitylabs/state-transition-sdk/lib/api/bft/verification/rule/UnicitySealQuorumSignaturesVerificationRule.js");
9367
+ var import_VerifiedSealCache = require("@unicitylabs/state-transition-sdk/lib/api/bft/verification/VerifiedSealCache.js");
9368
+ var import_Secp256k1SignatureVerifier = require("@unicitylabs/state-transition-sdk/lib/crypto/secp256k1/Secp256k1SignatureVerifier.js");
9365
9369
  var import_WorkerTokenVerifier = require("@unicitylabs/state-transition-sdk/lib/transaction/verification/worker/WorkerTokenVerifier.js");
9366
9370
  var import_EncodedPredicate = require("@unicitylabs/state-transition-sdk/lib/predicate/EncodedPredicate.js");
9367
9371
  var import_SignaturePredicate = require("@unicitylabs/state-transition-sdk/lib/predicate/builtin/SignaturePredicate.js");
@@ -9581,6 +9585,7 @@ async function awaitProofBounded(deps, transaction, signal) {
9581
9585
  deps.client,
9582
9586
  deps.trustBase,
9583
9587
  deps.predicateVerifier,
9588
+ deps.unicityCertificateVerifier,
9584
9589
  transaction,
9585
9590
  signal,
9586
9591
  deps.intervalMs
@@ -9787,7 +9792,7 @@ async function burntTokenFromCheckpoint(deps, storedBytes, reDerivedBurnTx, sour
9787
9792
  }
9788
9793
  let burnCertified;
9789
9794
  try {
9790
- burnCertified = await reDerivedBurnTx.toCertifiedTransaction(deps.trustBase, deps.predicateVerifier, burnProof);
9795
+ burnCertified = await reDerivedBurnTx.toCertifiedTransaction(deps.trustBase, deps.predicateVerifier, deps.unicityCertificateVerifier, burnProof);
9791
9796
  } catch (err) {
9792
9797
  if (isTrustbaseFailure(err)) {
9793
9798
  throw new CheckpointTrustbaseMismatchError(
@@ -9895,7 +9900,7 @@ var SphereTokenEngine = class {
9895
9900
  "AGGREGATOR_ERROR",
9896
9901
  options
9897
9902
  );
9898
- const certified = await mintTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, proof);
9903
+ const certified = await mintTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, this.deps.unicityCertificateVerifier, proof);
9899
9904
  const token = await import_Token.Token.mint(certified, this.deps.verificationContext);
9900
9905
  return this.wrapToken(token);
9901
9906
  }
@@ -9912,7 +9917,7 @@ var SphereTokenEngine = class {
9912
9917
  "AGGREGATOR_ERROR",
9913
9918
  options
9914
9919
  );
9915
- const certified = await mintTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, proof);
9920
+ const certified = await mintTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, this.deps.unicityCertificateVerifier, proof);
9916
9921
  const token = await import_Token.Token.mint(certified, this.deps.verificationContext);
9917
9922
  return this.wrapToken(token);
9918
9923
  }
@@ -9933,7 +9938,7 @@ var SphereTokenEngine = class {
9933
9938
  "TRANSFER_FAILED",
9934
9939
  options
9935
9940
  );
9936
- const certified = await transferTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, proof);
9941
+ const certified = await transferTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, this.deps.unicityCertificateVerifier, proof);
9937
9942
  const transferred = await params.token.sdkToken.transfer(certified, this.deps.verificationContext);
9938
9943
  return this.wrapToken(transferred);
9939
9944
  }
@@ -10020,7 +10025,7 @@ var SphereTokenEngine = class {
10020
10025
  const burnUnlock = await import_SignaturePredicateUnlockScript.SignaturePredicateUnlockScript.create(burnTx, this.deps.signingService);
10021
10026
  const burnCert = await import_CertificationData.CertificationData.fromTransaction(burnTx, burnUnlock);
10022
10027
  const burnProof = await this.submitAndAwaitProof(burnCert, burnTx, "Split burn failed", "TRANSFER_FAILED", ctx.options);
10023
- const burnCertified = await burnTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, burnProof);
10028
+ const burnCertified = await burnTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, this.deps.unicityCertificateVerifier, burnProof);
10024
10029
  const burntToken = await ctx.params.token.sdkToken.transfer(burnCertified, this.deps.verificationContext);
10025
10030
  return { burntToken, burnProof };
10026
10031
  }
@@ -10059,7 +10064,7 @@ var SphereTokenEngine = class {
10059
10064
  );
10060
10065
  const certData = await import_CertificationData.CertificationData.fromMintTransaction(mintTx);
10061
10066
  const proof = await this.submitSplitMintLeg(certData, mintTx, options);
10062
- const certified = await mintTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, proof);
10067
+ const certified = await mintTx.toCertifiedTransaction(this.deps.trustBase, this.deps.predicateVerifier, this.deps.unicityCertificateVerifier, proof);
10063
10068
  const token = await import_Token.Token.mint(certified, this.deps.verificationContext);
10064
10069
  return this.wrapToken(token);
10065
10070
  }
@@ -10202,6 +10207,7 @@ var SphereTokenEngine = class {
10202
10207
  client: this.deps.client,
10203
10208
  trustBase: this.deps.trustBase,
10204
10209
  predicateVerifier: this.deps.predicateVerifier,
10210
+ unicityCertificateVerifier: this.deps.unicityCertificateVerifier,
10205
10211
  intervalMs: this.deps.proofPollIntervalMs ?? DEFAULT_PROOF_POLL_INTERVAL_MS
10206
10212
  // #683
10207
10213
  },
@@ -10293,6 +10299,9 @@ async function createSphereTokenEngine(config) {
10293
10299
  }
10294
10300
  const trustBase = import_RootTrustBase.RootTrustBase.fromJSON(config.trustBaseJson);
10295
10301
  const predicateVerifier = import_PredicateVerifierService.PredicateVerifierService.create();
10302
+ const unicityCertificateVerifier = new import_UnicityCertificateVerifier.UnicityCertificateVerifier(
10303
+ new import_UnicitySealQuorumSignaturesVerificationRule.UnicitySealQuorumSignaturesVerificationRule(new import_Secp256k1SignatureVerifier.Secp256k1SignatureVerifier(), new import_VerifiedSealCache.VerifiedSealCache(256))
10304
+ );
10296
10305
  const mintJustificationVerifier = new import_MintJustificationVerifierService.MintJustificationVerifierService();
10297
10306
  mintJustificationVerifier.register(
10298
10307
  new import_SplitMintJustificationVerifier.SplitMintJustificationVerifier(decodeSpherePaymentData)
@@ -10301,10 +10310,12 @@ async function createSphereTokenEngine(config) {
10301
10310
  client: new import_StateTransitionClient.StateTransitionClient(new import_AggregatorClient.AggregatorClient(config.aggregatorUrl, config.apiKey ?? null)),
10302
10311
  trustBase,
10303
10312
  predicateVerifier,
10313
+ unicityCertificateVerifier,
10304
10314
  mintJustificationVerifier,
10305
10315
  verificationContext: new import_VerificationContext.VerificationContext(
10306
10316
  trustBase,
10307
10317
  predicateVerifier,
10318
+ unicityCertificateVerifier,
10308
10319
  mintJustificationVerifier,
10309
10320
  new import_TokenIssuanceVerifierService.TokenIssuanceVerifierService(false)
10310
10321
  ),
@@ -12406,6 +12417,90 @@ async function splitOpIndex(deps, envelope) {
12406
12417
  return Array.isArray(payload.direct) ? payload.direct.length : 0;
12407
12418
  }
12408
12419
 
12420
+ // modules/payments-v2/mint-params.ts
12421
+ function mintParams(recipientPubkey, coinId, amount) {
12422
+ return { recipientPubkey, value: { assets: [{ coinId, amount }] } };
12423
+ }
12424
+
12425
+ // modules/payments-v2/prewarm-cache.ts
12426
+ var PrewarmCache = class _PrewarmCache {
12427
+ entries = /* @__PURE__ */ new Map();
12428
+ generation = 0;
12429
+ static key(tokenId, stateHash) {
12430
+ return `${tokenId}:${stateHash}`;
12431
+ }
12432
+ /** Claim the cache for a warm about to start; supersedes any warm already running. */
12433
+ begin() {
12434
+ this.entries.clear();
12435
+ this.generation += 1;
12436
+ return this.generation;
12437
+ }
12438
+ /**
12439
+ * Publish a warm's result, unless it was cancelled or superseded while its
12440
+ * fetch was in flight — a cancel that only cleared what had landed so far
12441
+ * would be undone by the continuation of the very warm it cancelled.
12442
+ */
12443
+ commit(generation, warmed) {
12444
+ if (generation !== this.generation) return;
12445
+ for (const [tokenId, stateHash, bytes] of warmed) {
12446
+ this.entries.set(_PrewarmCache.key(tokenId, stateHash), bytes);
12447
+ }
12448
+ }
12449
+ get(tokenId, stateHash) {
12450
+ if (stateHash === void 0) return void 0;
12451
+ return this.entries.get(_PrewarmCache.key(tokenId, stateHash));
12452
+ }
12453
+ clear() {
12454
+ this.entries.clear();
12455
+ this.generation += 1;
12456
+ }
12457
+ get size() {
12458
+ return this.entries.size;
12459
+ }
12460
+ };
12461
+ async function warmSendSources(deps, request) {
12462
+ const ids = deps.queue.previewSelection(request);
12463
+ if (ids.length === 0) return;
12464
+ const generation = deps.cache.begin();
12465
+ const at = new Map(ids.map((id) => [id, deps.view.stateHashOf(id)]));
12466
+ let blobs;
12467
+ try {
12468
+ blobs = await deps.storagePort.getBlobs([...ids]);
12469
+ } catch {
12470
+ return;
12471
+ }
12472
+ const warmed = [];
12473
+ for (const [tokenId, bytes] of blobs) {
12474
+ const stateHash = at.get(tokenId);
12475
+ if (stateHash !== void 0) warmed.push([tokenId, stateHash, bytes]);
12476
+ }
12477
+ deps.cache.commit(generation, warmed);
12478
+ }
12479
+ async function takeSourceBlobs(deps, sourceIds) {
12480
+ const blobs = /* @__PURE__ */ new Map();
12481
+ const missing = [];
12482
+ let trustCache = deps.cache.size > 0;
12483
+ if (trustCache) {
12484
+ try {
12485
+ await deps.view.delta();
12486
+ } catch {
12487
+ trustCache = false;
12488
+ }
12489
+ }
12490
+ for (const tokenId of sourceIds) {
12491
+ const warm = trustCache ? deps.cache.get(tokenId, deps.view.stateHashOf(tokenId)) : void 0;
12492
+ if (warm === void 0) missing.push(tokenId);
12493
+ else blobs.set(tokenId, warm);
12494
+ }
12495
+ deps.cache.clear();
12496
+ if (missing.length > 0) {
12497
+ for (const [tokenId, bytes] of await deps.storagePort.getBlobs(missing)) {
12498
+ blobs.set(tokenId, bytes);
12499
+ }
12500
+ }
12501
+ return blobs;
12502
+ }
12503
+
12409
12504
  // modules/payments-v2/inventory/presentation.ts
12410
12505
  function toToken(tokenId, snapshot, asset, registry, flags) {
12411
12506
  const iconUrl = registry.getIconUrl(asset.coinId);
@@ -12492,6 +12587,13 @@ function priceOne(asset, quote) {
12492
12587
  fiatValueEur: quote.priceEur != null ? human * quote.priceEur : null
12493
12588
  };
12494
12589
  }
12590
+ function transferringToken(tokenId, coinId, amount, registry, now) {
12591
+ const snapshot = { assets: [], createdAt: now, updatedAt: now };
12592
+ return toToken(tokenId, snapshot, { coinId, amount: amount.toString() }, registry, {
12593
+ transferring: true,
12594
+ suspectedSpent: false
12595
+ });
12596
+ }
12495
12597
 
12496
12598
  // modules/payments-v2/machine/payload.ts
12497
12599
  function buildPayload(recipientPubkey, request, spend, spentStates) {
@@ -12730,6 +12832,15 @@ var InventoryView = class {
12730
12832
  }
12731
12833
  return out;
12732
12834
  }
12835
+ /**
12836
+ * Current mirrored state of an ACTIVE token, or undefined when it is unknown
12837
+ * or tombstoned. Exists so a cached blob can be state-scoped (F6): a blob is
12838
+ * only reusable while the token still sits at the state it was fetched at.
12839
+ */
12840
+ stateHashOf(tokenId) {
12841
+ const entry = this.mirror.get(tokenId);
12842
+ return entry !== void 0 && entry.status === "active" ? entry.stateHash : void 0;
12843
+ }
12733
12844
  tokens(registry, filter) {
12734
12845
  const out = [];
12735
12846
  for (const [tokenId, entry] of this.mirror) {
@@ -13649,6 +13760,31 @@ var SpendQueue = class {
13649
13760
  }
13650
13761
  return { kind: "queued", settled: this.enqueue(reservationId, request.coinId, amount) };
13651
13762
  }
13763
+ /**
13764
+ * The sources `plan()` WOULD pick right now, reserving nothing and queueing
13765
+ * nothing — a read-only probe used to warm blobs while the user is still
13766
+ * looking at the confirm screen.
13767
+ *
13768
+ * Deliberately side-effect free: reserving here would pin the user's balance
13769
+ * across think-time and strand it if they never press send. The consequence is
13770
+ * that the real `plan()` may pick differently (inventory moves, another send
13771
+ * reserves first); callers must treat the result as a HINT and tolerate a miss,
13772
+ * never as a commitment that these sources will be spent.
13773
+ */
13774
+ previewSelection(request) {
13775
+ if (this.destroyed) return [];
13776
+ let amount;
13777
+ try {
13778
+ amount = parseAmount(request.amount);
13779
+ } catch {
13780
+ return [];
13781
+ }
13782
+ const { freeView, freeTotal } = this.freeView(request.coinId);
13783
+ if (freeTotal < amount) return [];
13784
+ const plan = selectCoins(freeView, amount, { workBudget: this.deps.workBudget });
13785
+ if (plan === null) return [];
13786
+ return [...plan.direct, ...plan.split !== void 0 ? [plan.split.tokenId] : []];
13787
+ }
13652
13788
  // Coverage math: free + Σ declared expected change of in-flight sends —
13653
13789
  // never the full amount of in-flight sources (§5.3).
13654
13790
  declareExpectedChange(transferId, coinId, amount) {
@@ -13998,6 +14134,7 @@ var PaymentsFacade = class {
13998
14134
  now: () => this.nowMs()
13999
14135
  });
14000
14136
  }
14137
+ prewarmed = new PrewarmCache();
14001
14138
  view;
14002
14139
  ledger;
14003
14140
  queue;
@@ -14091,6 +14228,15 @@ var PaymentsFacade = class {
14091
14228
  return derivePendingTransfers(this.machineStores, this.machineDeps.decryptPayload);
14092
14229
  }
14093
14230
  // ── money movement ─────────────────────────────────────────────────────────
14231
+ async prewarmSend(request) {
14232
+ await warmSendSources(this.warmDeps(), request);
14233
+ }
14234
+ discardPrewarm() {
14235
+ this.prewarmed.clear();
14236
+ }
14237
+ warmDeps() {
14238
+ return { queue: this.queue, view: this.view, storagePort: this.deps.storagePort, cache: this.prewarmed };
14239
+ }
14094
14240
  send(request) {
14095
14241
  return this.track(this.sendOutcome(request));
14096
14242
  }
@@ -14300,7 +14446,7 @@ var PaymentsFacade = class {
14300
14446
  }
14301
14447
  async materialize(transferId, recipientPubkey, request, spend, sourceIds) {
14302
14448
  const engine = this.engine();
14303
- const blobs = await this.deps.storagePort.getBlobs([...sourceIds]);
14449
+ const blobs = await takeSourceBlobs(this.warmDeps(), sourceIds);
14304
14450
  const sources = /* @__PURE__ */ new Map();
14305
14451
  const sourceTokens = [];
14306
14452
  const spentStates = {};
@@ -14313,7 +14459,7 @@ var PaymentsFacade = class {
14313
14459
  const keys = await engine.deliveryKeys(bytes);
14314
14460
  spentStates[tokenId] = { local: keys.stateHash, protocol: keys.stateHash };
14315
14461
  sources.set(tokenId, token);
14316
- sourceTokens.push(this.toUiToken(tokenId, request.coinId, engine.balanceOf(token, request.coinId)));
14462
+ sourceTokens.push(transferringToken(tokenId, request.coinId, engine.balanceOf(token, request.coinId), this.deps.registry, this.nowMs()));
14317
14463
  }
14318
14464
  const payload = buildPayload(recipientPubkey, request, spend, spentStates);
14319
14465
  const plan = {
@@ -14457,7 +14603,7 @@ var PaymentsFacade = class {
14457
14603
  await this.machineStores.mintJournal.upsert(entry);
14458
14604
  let token;
14459
14605
  try {
14460
- token = await engine.mint(this.mintParams(coinId, amount), { transferId: mintId });
14606
+ token = await engine.mint(mintParams(this.ownPubkeyBytes, coinId, amount), { transferId: mintId });
14461
14607
  } catch (err) {
14462
14608
  this.heartbeat.arm();
14463
14609
  return { success: false, error: messageOf(err) };
@@ -14516,7 +14662,7 @@ var PaymentsFacade = class {
14516
14662
  });
14517
14663
  return false;
14518
14664
  }
14519
- const token = await engine.mint(this.mintParams(entry.coinId, BigInt(entry.amount)), {
14665
+ const token = await engine.mint(mintParams(this.ownPubkeyBytes, entry.coinId, BigInt(entry.amount)), {
14520
14666
  transferId: entry.mintId
14521
14667
  });
14522
14668
  if (token.blob.tokenId !== entry.tokenId) {
@@ -14525,12 +14671,6 @@ var PaymentsFacade = class {
14525
14671
  await this.finalizeMint(engine, entry.mintId, token, entry.coinId, entry.amount);
14526
14672
  return true;
14527
14673
  }
14528
- mintParams(coinId, amount) {
14529
- return {
14530
- recipientPubkey: this.ownPubkeyBytes,
14531
- value: { assets: [{ coinId, amount }] }
14532
- };
14533
- }
14534
14674
  async tokenInServerInventory(tokenId) {
14535
14675
  let page = await this.deps.storagePort.listInventory();
14536
14676
  for (let i = 0; i < INVENTORY_SCAN_PAGE_LIMIT; i++) {
@@ -14559,12 +14699,6 @@ var PaymentsFacade = class {
14559
14699
  page = await this.deps.storagePort.listInventory(page.cursor);
14560
14700
  }
14561
14701
  }
14562
- toUiToken(tokenId, coinId, amount) {
14563
- const now = this.nowMs();
14564
- const snapshot = { assets: [], createdAt: now, updatedAt: now };
14565
- const flags = { transferring: true, suspectedSpent: false };
14566
- return toToken(tokenId, snapshot, { coinId, amount: amount.toString() }, this.deps.registry, flags);
14567
- }
14568
14702
  track(op) {
14569
14703
  const settled = op.then(() => void 0, () => void 0);
14570
14704
  this.pendingOps.add(settled);