@unicitylabs/sphere-sdk 0.5.2 → 0.5.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.
package/dist/index.cjs CHANGED
@@ -2875,7 +2875,7 @@ init_constants();
2875
2875
  // types/txf.ts
2876
2876
  var ARCHIVED_PREFIX = "archived-";
2877
2877
  var FORKED_PREFIX = "_forked_";
2878
- var RESERVED_KEYS = ["_meta", "_nametag", "_nametags", "_tombstones", "_invalidatedNametags", "_outbox", "_mintOutbox", "_sent", "_invalid", "_integrity"];
2878
+ var RESERVED_KEYS = ["_meta", "_nametag", "_nametags", "_tombstones", "_invalidatedNametags", "_outbox", "_mintOutbox", "_sent", "_invalid", "_integrity", "_history"];
2879
2879
  function isTokenKey(key) {
2880
2880
  return key.startsWith("_") && !key.startsWith(ARCHIVED_PREFIX) && !key.startsWith(FORKED_PREFIX) && !RESERVED_KEYS.includes(key);
2881
2881
  }
@@ -3497,6 +3497,9 @@ async function buildTxfStorageData(tokens, meta, options) {
3497
3497
  if (options?.invalidatedNametags && options.invalidatedNametags.length > 0) {
3498
3498
  storageData._invalidatedNametags = options.invalidatedNametags;
3499
3499
  }
3500
+ if (options?.historyEntries && options.historyEntries.length > 0) {
3501
+ storageData._history = options.historyEntries;
3502
+ }
3500
3503
  for (const token of tokens) {
3501
3504
  const txf = tokenToTxf(token);
3502
3505
  if (txf) {
@@ -3530,6 +3533,7 @@ function parseTxfStorageData(data) {
3530
3533
  outboxEntries: [],
3531
3534
  mintOutboxEntries: [],
3532
3535
  invalidatedNametags: [],
3536
+ historyEntries: [],
3533
3537
  validationErrors: []
3534
3538
  };
3535
3539
  if (!data || typeof data !== "object") {
@@ -3583,6 +3587,13 @@ function parseTxfStorageData(data) {
3583
3587
  }
3584
3588
  }
3585
3589
  }
3590
+ if (Array.isArray(storageData._history)) {
3591
+ for (const entry of storageData._history) {
3592
+ if (typeof entry === "object" && entry !== null && typeof entry.dedupKey === "string" && typeof entry.type === "string") {
3593
+ result.historyEntries.push(entry);
3594
+ }
3595
+ }
3596
+ }
3586
3597
  for (const key of Object.keys(storageData)) {
3587
3598
  if (isTokenKey(key)) {
3588
3599
  const tokenId = tokenIdFromKey(key);
@@ -4472,6 +4483,7 @@ function computeHistoryDedupKey(type, tokenId, transferId) {
4472
4483
  if (tokenId) return `${type}_${tokenId}`;
4473
4484
  return `${type}_${crypto.randomUUID()}`;
4474
4485
  }
4486
+ var MAX_SYNCED_HISTORY_ENTRIES = 5e3;
4475
4487
  function enrichWithRegistry(info) {
4476
4488
  const registry = TokenRegistry.getInstance();
4477
4489
  const def = registry.getDefinition(info.coinId);
@@ -4896,6 +4908,10 @@ var PaymentsModule = class _PaymentsModule {
4896
4908
  const result = await provider.load();
4897
4909
  if (result.success && result.data) {
4898
4910
  this.loadFromStorageData(result.data);
4911
+ const txfData = result.data;
4912
+ if (txfData._history && txfData._history.length > 0) {
4913
+ await this.importRemoteHistoryEntries(txfData._history);
4914
+ }
4899
4915
  this.log(`Loaded metadata from provider ${id}`);
4900
4916
  break;
4901
4917
  }
@@ -7268,6 +7284,33 @@ var PaymentsModule = class _PaymentsModule {
7268
7284
  }
7269
7285
  }
7270
7286
  }
7287
+ /**
7288
+ * Import history entries from remote TXF data into local store.
7289
+ * Delegates to the local TokenStorageProvider's importHistoryEntries() for
7290
+ * persistent storage, with in-memory fallback.
7291
+ * Reused by both load() (initial IPFS fetch) and _doSync() (merge result).
7292
+ */
7293
+ async importRemoteHistoryEntries(entries) {
7294
+ if (entries.length === 0) return 0;
7295
+ const provider = this.getLocalTokenStorageProvider();
7296
+ if (provider?.importHistoryEntries) {
7297
+ const imported2 = await provider.importHistoryEntries(entries);
7298
+ if (imported2 > 0) {
7299
+ this._historyCache = await provider.getHistoryEntries();
7300
+ }
7301
+ return imported2;
7302
+ }
7303
+ const existingKeys = new Set(this._historyCache.map((e) => e.dedupKey));
7304
+ let imported = 0;
7305
+ for (const entry of entries) {
7306
+ if (!existingKeys.has(entry.dedupKey)) {
7307
+ this._historyCache.push(entry);
7308
+ existingKeys.add(entry.dedupKey);
7309
+ imported++;
7310
+ }
7311
+ }
7312
+ return imported;
7313
+ }
7271
7314
  /**
7272
7315
  * Get the first local token storage provider (for history operations).
7273
7316
  */
@@ -7515,6 +7558,13 @@ var PaymentsModule = class _PaymentsModule {
7515
7558
  if (this.nametags.length === 0 && savedNametags.length > 0) {
7516
7559
  this.nametags = savedNametags;
7517
7560
  }
7561
+ const txfData = result.merged;
7562
+ if (txfData._history && txfData._history.length > 0) {
7563
+ const imported = await this.importRemoteHistoryEntries(txfData._history);
7564
+ if (imported > 0) {
7565
+ this.log(`Imported ${imported} history entries from IPFS sync`);
7566
+ }
7567
+ }
7518
7568
  totalAdded += result.added;
7519
7569
  totalRemoved += result.removed;
7520
7570
  }
@@ -8222,6 +8272,7 @@ var PaymentsModule = class _PaymentsModule {
8222
8272
  return data ? JSON.parse(data) : [];
8223
8273
  }
8224
8274
  async createStorageData() {
8275
+ const sorted = [...this._historyCache].sort((a, b) => b.timestamp - a.timestamp);
8225
8276
  return await buildTxfStorageData(
8226
8277
  Array.from(this.tokens.values()),
8227
8278
  {
@@ -8233,7 +8284,8 @@ var PaymentsModule = class _PaymentsModule {
8233
8284
  nametags: this.nametags,
8234
8285
  tombstones: this.tombstones,
8235
8286
  archivedTokens: this.archivedTokens,
8236
- forkedTokens: this.forkedTokens
8287
+ forkedTokens: this.forkedTokens,
8288
+ historyEntries: sorted.slice(0, MAX_SYNCED_HISTORY_ENTRIES)
8237
8289
  }
8238
8290
  );
8239
8291
  }