@unicitylabs/sphere-sdk 0.14.10 → 0.14.11-dev.2

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.
@@ -228,6 +228,8 @@ interface PendingTransfer {
228
228
  }
229
229
  type ConnectionStatus = 'connected' | 'degraded' | 'offline';
230
230
  interface PaymentsV2 {
231
+ prewarmSend(request: SendRequest): Promise<void>;
232
+ discardPrewarm(): void;
231
233
  assets(coinId?: string): Promise<Asset[]>;
232
234
  tokens(filter?: {
233
235
  coinId?: string;
@@ -1042,6 +1044,7 @@ declare const MAX_RESELECT = 8;
1042
1044
  declare const ATTENTION_MINT_UNRESOLVED = "mint:unresolved";
1043
1045
  declare class PaymentsFacade implements PaymentsV2 {
1044
1046
  private readonly deps;
1047
+ private readonly prewarmed;
1045
1048
  private readonly view;
1046
1049
  private readonly ledger;
1047
1050
  private readonly queue;
@@ -1086,6 +1089,9 @@ declare class PaymentsFacade implements PaymentsV2 {
1086
1089
  connectionStatus(): ConnectionStatus;
1087
1090
  /** §4 pending-transfers UI surface — derived on read, never cached (convergence.ts). */
1088
1091
  pendingTransfers(): Promise<PendingTransfer[]>;
1092
+ prewarmSend(request: SendRequest): Promise<void>;
1093
+ discardPrewarm(): void;
1094
+ private warmDeps;
1089
1095
  send(request: SendRequest): Promise<TransferResult>;
1090
1096
  receive(): Promise<{
1091
1097
  transfers: IncomingTransfer[];
@@ -1142,12 +1148,10 @@ declare class PaymentsFacade implements PaymentsV2 {
1142
1148
  /** @returns how many journal entries were RESOLVED (cleared) — heartbeat progress. */
1143
1149
  private replayMints;
1144
1150
  private replayMint;
1145
- private mintParams;
1146
1151
  private tokenInServerInventory;
1147
1152
  private engine;
1148
1153
  private newId;
1149
1154
  private seedHeldStates;
1150
- private toUiToken;
1151
1155
  private track;
1152
1156
  private trackTail;
1153
1157
  }
@@ -228,6 +228,8 @@ interface PendingTransfer {
228
228
  }
229
229
  type ConnectionStatus = 'connected' | 'degraded' | 'offline';
230
230
  interface PaymentsV2 {
231
+ prewarmSend(request: SendRequest): Promise<void>;
232
+ discardPrewarm(): void;
231
233
  assets(coinId?: string): Promise<Asset[]>;
232
234
  tokens(filter?: {
233
235
  coinId?: string;
@@ -1042,6 +1044,7 @@ declare const MAX_RESELECT = 8;
1042
1044
  declare const ATTENTION_MINT_UNRESOLVED = "mint:unresolved";
1043
1045
  declare class PaymentsFacade implements PaymentsV2 {
1044
1046
  private readonly deps;
1047
+ private readonly prewarmed;
1045
1048
  private readonly view;
1046
1049
  private readonly ledger;
1047
1050
  private readonly queue;
@@ -1086,6 +1089,9 @@ declare class PaymentsFacade implements PaymentsV2 {
1086
1089
  connectionStatus(): ConnectionStatus;
1087
1090
  /** §4 pending-transfers UI surface — derived on read, never cached (convergence.ts). */
1088
1091
  pendingTransfers(): Promise<PendingTransfer[]>;
1092
+ prewarmSend(request: SendRequest): Promise<void>;
1093
+ discardPrewarm(): void;
1094
+ private warmDeps;
1089
1095
  send(request: SendRequest): Promise<TransferResult>;
1090
1096
  receive(): Promise<{
1091
1097
  transfers: IncomingTransfer[];
@@ -1142,12 +1148,10 @@ declare class PaymentsFacade implements PaymentsV2 {
1142
1148
  /** @returns how many journal entries were RESOLVED (cleared) — heartbeat progress. */
1143
1149
  private replayMints;
1144
1150
  private replayMint;
1145
- private mintParams;
1146
1151
  private tokenInServerInventory;
1147
1152
  private engine;
1148
1153
  private newId;
1149
1154
  private seedHeldStates;
1150
- private toUiToken;
1151
1155
  private track;
1152
1156
  private trackTail;
1153
1157
  }
@@ -1600,6 +1600,90 @@ async function splitOpIndex(deps, envelope) {
1600
1600
  return Array.isArray(payload.direct) ? payload.direct.length : 0;
1601
1601
  }
1602
1602
 
1603
+ // modules/payments-v2/mint-params.ts
1604
+ function mintParams(recipientPubkey, coinId, amount) {
1605
+ return { recipientPubkey, value: { assets: [{ coinId, amount }] } };
1606
+ }
1607
+
1608
+ // modules/payments-v2/prewarm-cache.ts
1609
+ var PrewarmCache = class _PrewarmCache {
1610
+ entries = /* @__PURE__ */ new Map();
1611
+ generation = 0;
1612
+ static key(tokenId, stateHash) {
1613
+ return `${tokenId}:${stateHash}`;
1614
+ }
1615
+ /** Claim the cache for a warm about to start; supersedes any warm already running. */
1616
+ begin() {
1617
+ this.entries.clear();
1618
+ this.generation += 1;
1619
+ return this.generation;
1620
+ }
1621
+ /**
1622
+ * Publish a warm's result, unless it was cancelled or superseded while its
1623
+ * fetch was in flight — a cancel that only cleared what had landed so far
1624
+ * would be undone by the continuation of the very warm it cancelled.
1625
+ */
1626
+ commit(generation, warmed) {
1627
+ if (generation !== this.generation) return;
1628
+ for (const [tokenId, stateHash, bytes] of warmed) {
1629
+ this.entries.set(_PrewarmCache.key(tokenId, stateHash), bytes);
1630
+ }
1631
+ }
1632
+ get(tokenId, stateHash) {
1633
+ if (stateHash === void 0) return void 0;
1634
+ return this.entries.get(_PrewarmCache.key(tokenId, stateHash));
1635
+ }
1636
+ clear() {
1637
+ this.entries.clear();
1638
+ this.generation += 1;
1639
+ }
1640
+ get size() {
1641
+ return this.entries.size;
1642
+ }
1643
+ };
1644
+ async function warmSendSources(deps, request) {
1645
+ const ids = deps.queue.previewSelection(request);
1646
+ if (ids.length === 0) return;
1647
+ const generation = deps.cache.begin();
1648
+ const at = new Map(ids.map((id) => [id, deps.view.stateHashOf(id)]));
1649
+ let blobs;
1650
+ try {
1651
+ blobs = await deps.storagePort.getBlobs([...ids]);
1652
+ } catch {
1653
+ return;
1654
+ }
1655
+ const warmed = [];
1656
+ for (const [tokenId, bytes] of blobs) {
1657
+ const stateHash = at.get(tokenId);
1658
+ if (stateHash !== void 0) warmed.push([tokenId, stateHash, bytes]);
1659
+ }
1660
+ deps.cache.commit(generation, warmed);
1661
+ }
1662
+ async function takeSourceBlobs(deps, sourceIds) {
1663
+ const blobs = /* @__PURE__ */ new Map();
1664
+ const missing = [];
1665
+ let trustCache = deps.cache.size > 0;
1666
+ if (trustCache) {
1667
+ try {
1668
+ await deps.view.delta();
1669
+ } catch {
1670
+ trustCache = false;
1671
+ }
1672
+ }
1673
+ for (const tokenId of sourceIds) {
1674
+ const warm = trustCache ? deps.cache.get(tokenId, deps.view.stateHashOf(tokenId)) : void 0;
1675
+ if (warm === void 0) missing.push(tokenId);
1676
+ else blobs.set(tokenId, warm);
1677
+ }
1678
+ deps.cache.clear();
1679
+ if (missing.length > 0) {
1680
+ for (const [tokenId, bytes] of await deps.storagePort.getBlobs(missing)) {
1681
+ blobs.set(tokenId, bytes);
1682
+ }
1683
+ }
1684
+ return blobs;
1685
+ }
1686
+
1603
1687
  // modules/payments-v2/inventory/presentation.ts
1604
1688
  function toToken(tokenId, snapshot, asset, registry, flags) {
1605
1689
  const iconUrl = registry.getIconUrl(asset.coinId);
@@ -1686,6 +1770,13 @@ function priceOne(asset, quote) {
1686
1770
  fiatValueEur: quote.priceEur != null ? human * quote.priceEur : null
1687
1771
  };
1688
1772
  }
1773
+ function transferringToken(tokenId, coinId, amount, registry, now) {
1774
+ const snapshot = { assets: [], createdAt: now, updatedAt: now };
1775
+ return toToken(tokenId, snapshot, { coinId, amount: amount.toString() }, registry, {
1776
+ transferring: true,
1777
+ suspectedSpent: false
1778
+ });
1779
+ }
1689
1780
 
1690
1781
  // modules/payments-v2/machine/payload.ts
1691
1782
  function buildPayload(recipientPubkey, request, spend, spentStates) {
@@ -2774,6 +2865,15 @@ var InventoryView = class {
2774
2865
  }
2775
2866
  return out;
2776
2867
  }
2868
+ /**
2869
+ * Current mirrored state of an ACTIVE token, or undefined when it is unknown
2870
+ * or tombstoned. Exists so a cached blob can be state-scoped (F6): a blob is
2871
+ * only reusable while the token still sits at the state it was fetched at.
2872
+ */
2873
+ stateHashOf(tokenId) {
2874
+ const entry = this.mirror.get(tokenId);
2875
+ return entry !== void 0 && entry.status === "active" ? entry.stateHash : void 0;
2876
+ }
2777
2877
  tokens(registry, filter) {
2778
2878
  const out = [];
2779
2879
  for (const [tokenId, entry] of this.mirror) {
@@ -3053,6 +3153,7 @@ var logger = {
3053
3153
 
3054
3154
  // modules/payments-v2/receive/Receive.ts
3055
3155
  var ACK_BATCH_SIZE = 200;
3156
+ var REFRESH_INTERVAL_MS = 750;
3056
3157
  var POLL_INTERVAL_MS = 3e4;
3057
3158
  var ATTENTION_CLAIM_CONFLICT = "claim:conflict";
3058
3159
  function receivedDedupKey(tokenId, stateHash) {
@@ -3069,6 +3170,7 @@ var Receive = class {
3069
3170
  drainFlight = new SingleFlight();
3070
3171
  pollTimer = null;
3071
3172
  unsubscribeWake = null;
3173
+ lastRefreshAt = 0;
3072
3174
  drainOnce() {
3073
3175
  return this.drainFlight.run(() => this.doDrain());
3074
3176
  }
@@ -3086,6 +3188,14 @@ var Receive = class {
3086
3188
  this.unsubscribeWake?.();
3087
3189
  this.unsubscribeWake = null;
3088
3190
  }
3191
+ /** Ask for a mirror refresh at most once per REFRESH_INTERVAL_MS. */
3192
+ maybeRefresh(deps) {
3193
+ if (deps.refreshView === void 0) return;
3194
+ const now = deps.now?.() ?? Date.now();
3195
+ if (now - this.lastRefreshAt < REFRESH_INTERVAL_MS) return;
3196
+ this.lastRefreshAt = now;
3197
+ deps.refreshView();
3198
+ }
3089
3199
  async doDrain() {
3090
3200
  const deps = this.deps;
3091
3201
  const engine = deps.engine();
@@ -3099,6 +3209,7 @@ var Receive = class {
3099
3209
  for await (const entry of deps.delivery.incoming(basis === null ? void 0 : String(basis.cursor))) {
3100
3210
  await this.processEntry(deps, engine, entry, pending, stored);
3101
3211
  if (pending.length >= ACK_BATCH_SIZE) await flushAcks(deps, pending, pageEpoch);
3212
+ if (stored.length > 0) this.maybeRefresh(deps);
3102
3213
  }
3103
3214
  await flushAcks(deps, pending, pageEpoch);
3104
3215
  const served = deps.delivery.incomingEpoch();
@@ -3785,6 +3896,31 @@ var SpendQueue = class {
3785
3896
  }
3786
3897
  return { kind: "queued", settled: this.enqueue(reservationId, request.coinId, amount) };
3787
3898
  }
3899
+ /**
3900
+ * The sources `plan()` WOULD pick right now, reserving nothing and queueing
3901
+ * nothing — a read-only probe used to warm blobs while the user is still
3902
+ * looking at the confirm screen.
3903
+ *
3904
+ * Deliberately side-effect free: reserving here would pin the user's balance
3905
+ * across think-time and strand it if they never press send. The consequence is
3906
+ * that the real `plan()` may pick differently (inventory moves, another send
3907
+ * reserves first); callers must treat the result as a HINT and tolerate a miss,
3908
+ * never as a commitment that these sources will be spent.
3909
+ */
3910
+ previewSelection(request) {
3911
+ if (this.destroyed) return [];
3912
+ let amount;
3913
+ try {
3914
+ amount = parseAmount(request.amount);
3915
+ } catch {
3916
+ return [];
3917
+ }
3918
+ const { freeView, freeTotal } = this.freeView(request.coinId);
3919
+ if (freeTotal < amount) return [];
3920
+ const plan = selectCoins(freeView, amount, { workBudget: this.deps.workBudget });
3921
+ if (plan === null) return [];
3922
+ return [...plan.direct, ...plan.split !== void 0 ? [plan.split.tokenId] : []];
3923
+ }
3788
3924
  // Coverage math: free + Σ declared expected change of in-flight sends —
3789
3925
  // never the full amount of in-flight sources (§5.3).
3790
3926
  declareExpectedChange(transferId, coinId, amount) {
@@ -3973,7 +4109,7 @@ function composeFacadeParts(deps, hooks) {
3973
4109
  machineDeps,
3974
4110
  machine: new TransferMachine(machineDeps),
3975
4111
  heldStates,
3976
- receiveLoop: buildReceive(deps, hooks, historyStore, heldStates),
4112
+ receiveLoop: buildReceive(deps, hooks, historyStore, heldStates, view),
3977
4113
  requests: buildRequests(deps, hooks),
3978
4114
  restoreDeps: buildRestoreDeps(deps, machineDeps, machineStores, view)
3979
4115
  };
@@ -4040,7 +4176,7 @@ function buildMachineDeps(deps, hooks, historyStore) {
4040
4176
  }
4041
4177
  };
4042
4178
  }
4043
- function buildReceive(deps, hooks, historyStore, heldStates) {
4179
+ function buildReceive(deps, hooks, historyStore, heldStates, view) {
4044
4180
  return new Receive({
4045
4181
  delivery: deps.deliveryPort,
4046
4182
  engine: () => hooks.engine(),
@@ -4054,6 +4190,11 @@ function buildReceive(deps, hooks, historyStore, heldStates) {
4054
4190
  registry: deps.registry,
4055
4191
  recordReceived: (record) => recordReceived(historyStore, record),
4056
4192
  emit: (event, transfer) => deps.emit(event, transfer),
4193
+ // The mirror is the balance the wallet renders; refresh it as the drain runs
4194
+ // so a multi-token receive lands progressively instead of all at the end.
4195
+ refreshView: () => {
4196
+ void view.delta().catch(() => void 0);
4197
+ },
4057
4198
  attention: (transferId, code, detail) => {
4058
4199
  deps.emit(
4059
4200
  "transfer:attention",
@@ -4134,6 +4275,7 @@ var PaymentsFacade = class {
4134
4275
  now: () => this.nowMs()
4135
4276
  });
4136
4277
  }
4278
+ prewarmed = new PrewarmCache();
4137
4279
  view;
4138
4280
  ledger;
4139
4281
  queue;
@@ -4227,6 +4369,15 @@ var PaymentsFacade = class {
4227
4369
  return derivePendingTransfers(this.machineStores, this.machineDeps.decryptPayload);
4228
4370
  }
4229
4371
  // ── money movement ─────────────────────────────────────────────────────────
4372
+ async prewarmSend(request) {
4373
+ await warmSendSources(this.warmDeps(), request);
4374
+ }
4375
+ discardPrewarm() {
4376
+ this.prewarmed.clear();
4377
+ }
4378
+ warmDeps() {
4379
+ return { queue: this.queue, view: this.view, storagePort: this.deps.storagePort, cache: this.prewarmed };
4380
+ }
4230
4381
  send(request) {
4231
4382
  return this.track(this.sendOutcome(request));
4232
4383
  }
@@ -4436,7 +4587,7 @@ var PaymentsFacade = class {
4436
4587
  }
4437
4588
  async materialize(transferId, recipientPubkey, request, spend, sourceIds) {
4438
4589
  const engine = this.engine();
4439
- const blobs = await this.deps.storagePort.getBlobs([...sourceIds]);
4590
+ const blobs = await takeSourceBlobs(this.warmDeps(), sourceIds);
4440
4591
  const sources = /* @__PURE__ */ new Map();
4441
4592
  const sourceTokens = [];
4442
4593
  const spentStates = {};
@@ -4449,7 +4600,7 @@ var PaymentsFacade = class {
4449
4600
  const keys = await engine.deliveryKeys(bytes);
4450
4601
  spentStates[tokenId] = { local: keys.stateHash, protocol: keys.stateHash };
4451
4602
  sources.set(tokenId, token);
4452
- sourceTokens.push(this.toUiToken(tokenId, request.coinId, engine.balanceOf(token, request.coinId)));
4603
+ sourceTokens.push(transferringToken(tokenId, request.coinId, engine.balanceOf(token, request.coinId), this.deps.registry, this.nowMs()));
4453
4604
  }
4454
4605
  const payload = buildPayload(recipientPubkey, request, spend, spentStates);
4455
4606
  const plan = {
@@ -4593,7 +4744,7 @@ var PaymentsFacade = class {
4593
4744
  await this.machineStores.mintJournal.upsert(entry);
4594
4745
  let token;
4595
4746
  try {
4596
- token = await engine.mint(this.mintParams(coinId, amount), { transferId: mintId });
4747
+ token = await engine.mint(mintParams(this.ownPubkeyBytes, coinId, amount), { transferId: mintId });
4597
4748
  } catch (err) {
4598
4749
  this.heartbeat.arm();
4599
4750
  return { success: false, error: messageOf(err) };
@@ -4652,7 +4803,7 @@ var PaymentsFacade = class {
4652
4803
  });
4653
4804
  return false;
4654
4805
  }
4655
- const token = await engine.mint(this.mintParams(entry.coinId, BigInt(entry.amount)), {
4806
+ const token = await engine.mint(mintParams(this.ownPubkeyBytes, entry.coinId, BigInt(entry.amount)), {
4656
4807
  transferId: entry.mintId
4657
4808
  });
4658
4809
  if (token.blob.tokenId !== entry.tokenId) {
@@ -4661,12 +4812,6 @@ var PaymentsFacade = class {
4661
4812
  await this.finalizeMint(engine, entry.mintId, token, entry.coinId, entry.amount);
4662
4813
  return true;
4663
4814
  }
4664
- mintParams(coinId, amount) {
4665
- return {
4666
- recipientPubkey: this.ownPubkeyBytes,
4667
- value: { assets: [{ coinId, amount }] }
4668
- };
4669
- }
4670
4815
  async tokenInServerInventory(tokenId) {
4671
4816
  let page = await this.deps.storagePort.listInventory();
4672
4817
  for (let i = 0; i < INVENTORY_SCAN_PAGE_LIMIT; i++) {
@@ -4695,12 +4840,6 @@ var PaymentsFacade = class {
4695
4840
  page = await this.deps.storagePort.listInventory(page.cursor);
4696
4841
  }
4697
4842
  }
4698
- toUiToken(tokenId, coinId, amount) {
4699
- const now = this.nowMs();
4700
- const snapshot = { assets: [], createdAt: now, updatedAt: now };
4701
- const flags = { transferring: true, suspectedSpent: false };
4702
- return toToken(tokenId, snapshot, { coinId, amount: amount.toString() }, this.deps.registry, flags);
4703
- }
4704
4843
  track(op) {
4705
4844
  const settled = op.then(() => void 0, () => void 0);
4706
4845
  this.pendingOps.add(settled);