@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.
@@ -1088,6 +1088,7 @@ interface TxfStorageDataBase {
1088
1088
  _outbox?: TxfOutboxEntry[];
1089
1089
  _sent?: TxfSentEntry[];
1090
1090
  _invalid?: TxfInvalidEntry[];
1091
+ _history?: HistoryRecord[];
1091
1092
  [key: `_${string}`]: unknown;
1092
1093
  }
1093
1094
  interface TxfMeta {
@@ -2490,6 +2491,13 @@ declare class PaymentsModule {
2490
2491
  * Also performs one-time migration from legacy KV storage.
2491
2492
  */
2492
2493
  loadHistory(): Promise<void>;
2494
+ /**
2495
+ * Import history entries from remote TXF data into local store.
2496
+ * Delegates to the local TokenStorageProvider's importHistoryEntries() for
2497
+ * persistent storage, with in-memory fallback.
2498
+ * Reused by both load() (initial IPFS fetch) and _doSync() (merge result).
2499
+ */
2500
+ private importRemoteHistoryEntries;
2493
2501
  /**
2494
2502
  * Get the first local token storage provider (for history operations).
2495
2503
  */
@@ -1088,6 +1088,7 @@ interface TxfStorageDataBase {
1088
1088
  _outbox?: TxfOutboxEntry[];
1089
1089
  _sent?: TxfSentEntry[];
1090
1090
  _invalid?: TxfInvalidEntry[];
1091
+ _history?: HistoryRecord[];
1091
1092
  [key: `_${string}`]: unknown;
1092
1093
  }
1093
1094
  interface TxfMeta {
@@ -2490,6 +2491,13 @@ declare class PaymentsModule {
2490
2491
  * Also performs one-time migration from legacy KV storage.
2491
2492
  */
2492
2493
  loadHistory(): Promise<void>;
2494
+ /**
2495
+ * Import history entries from remote TXF data into local store.
2496
+ * Delegates to the local TokenStorageProvider's importHistoryEntries() for
2497
+ * persistent storage, with in-memory fallback.
2498
+ * Reused by both load() (initial IPFS fetch) and _doSync() (merge result).
2499
+ */
2500
+ private importRemoteHistoryEntries;
2493
2501
  /**
2494
2502
  * Get the first local token storage provider (for history operations).
2495
2503
  */
@@ -2506,7 +2506,7 @@ init_constants();
2506
2506
  // types/txf.ts
2507
2507
  var ARCHIVED_PREFIX = "archived-";
2508
2508
  var FORKED_PREFIX = "_forked_";
2509
- var RESERVED_KEYS = ["_meta", "_nametag", "_nametags", "_tombstones", "_invalidatedNametags", "_outbox", "_mintOutbox", "_sent", "_invalid", "_integrity"];
2509
+ var RESERVED_KEYS = ["_meta", "_nametag", "_nametags", "_tombstones", "_invalidatedNametags", "_outbox", "_mintOutbox", "_sent", "_invalid", "_integrity", "_history"];
2510
2510
  function isTokenKey(key) {
2511
2511
  return key.startsWith("_") && !key.startsWith(ARCHIVED_PREFIX) && !key.startsWith(FORKED_PREFIX) && !RESERVED_KEYS.includes(key);
2512
2512
  }
@@ -3091,6 +3091,9 @@ async function buildTxfStorageData(tokens, meta, options) {
3091
3091
  if (options?.invalidatedNametags && options.invalidatedNametags.length > 0) {
3092
3092
  storageData._invalidatedNametags = options.invalidatedNametags;
3093
3093
  }
3094
+ if (options?.historyEntries && options.historyEntries.length > 0) {
3095
+ storageData._history = options.historyEntries;
3096
+ }
3094
3097
  for (const token of tokens) {
3095
3098
  const txf = tokenToTxf(token);
3096
3099
  if (txf) {
@@ -3124,6 +3127,7 @@ function parseTxfStorageData(data) {
3124
3127
  outboxEntries: [],
3125
3128
  mintOutboxEntries: [],
3126
3129
  invalidatedNametags: [],
3130
+ historyEntries: [],
3127
3131
  validationErrors: []
3128
3132
  };
3129
3133
  if (!data || typeof data !== "object") {
@@ -3177,6 +3181,13 @@ function parseTxfStorageData(data) {
3177
3181
  }
3178
3182
  }
3179
3183
  }
3184
+ if (Array.isArray(storageData._history)) {
3185
+ for (const entry of storageData._history) {
3186
+ if (typeof entry === "object" && entry !== null && typeof entry.dedupKey === "string" && typeof entry.type === "string") {
3187
+ result.historyEntries.push(entry);
3188
+ }
3189
+ }
3190
+ }
3180
3191
  for (const key of Object.keys(storageData)) {
3181
3192
  if (isTokenKey(key)) {
3182
3193
  const tokenId = tokenIdFromKey(key);
@@ -4015,6 +4026,7 @@ function computeHistoryDedupKey(type, tokenId, transferId) {
4015
4026
  if (tokenId) return `${type}_${tokenId}`;
4016
4027
  return `${type}_${crypto.randomUUID()}`;
4017
4028
  }
4029
+ var MAX_SYNCED_HISTORY_ENTRIES = 5e3;
4018
4030
  function enrichWithRegistry(info) {
4019
4031
  const registry = TokenRegistry.getInstance();
4020
4032
  const def = registry.getDefinition(info.coinId);
@@ -4439,6 +4451,10 @@ var PaymentsModule = class _PaymentsModule {
4439
4451
  const result = await provider.load();
4440
4452
  if (result.success && result.data) {
4441
4453
  this.loadFromStorageData(result.data);
4454
+ const txfData = result.data;
4455
+ if (txfData._history && txfData._history.length > 0) {
4456
+ await this.importRemoteHistoryEntries(txfData._history);
4457
+ }
4442
4458
  this.log(`Loaded metadata from provider ${id}`);
4443
4459
  break;
4444
4460
  }
@@ -6811,6 +6827,33 @@ var PaymentsModule = class _PaymentsModule {
6811
6827
  }
6812
6828
  }
6813
6829
  }
6830
+ /**
6831
+ * Import history entries from remote TXF data into local store.
6832
+ * Delegates to the local TokenStorageProvider's importHistoryEntries() for
6833
+ * persistent storage, with in-memory fallback.
6834
+ * Reused by both load() (initial IPFS fetch) and _doSync() (merge result).
6835
+ */
6836
+ async importRemoteHistoryEntries(entries) {
6837
+ if (entries.length === 0) return 0;
6838
+ const provider = this.getLocalTokenStorageProvider();
6839
+ if (provider?.importHistoryEntries) {
6840
+ const imported2 = await provider.importHistoryEntries(entries);
6841
+ if (imported2 > 0) {
6842
+ this._historyCache = await provider.getHistoryEntries();
6843
+ }
6844
+ return imported2;
6845
+ }
6846
+ const existingKeys = new Set(this._historyCache.map((e) => e.dedupKey));
6847
+ let imported = 0;
6848
+ for (const entry of entries) {
6849
+ if (!existingKeys.has(entry.dedupKey)) {
6850
+ this._historyCache.push(entry);
6851
+ existingKeys.add(entry.dedupKey);
6852
+ imported++;
6853
+ }
6854
+ }
6855
+ return imported;
6856
+ }
6814
6857
  /**
6815
6858
  * Get the first local token storage provider (for history operations).
6816
6859
  */
@@ -7058,6 +7101,13 @@ var PaymentsModule = class _PaymentsModule {
7058
7101
  if (this.nametags.length === 0 && savedNametags.length > 0) {
7059
7102
  this.nametags = savedNametags;
7060
7103
  }
7104
+ const txfData = result.merged;
7105
+ if (txfData._history && txfData._history.length > 0) {
7106
+ const imported = await this.importRemoteHistoryEntries(txfData._history);
7107
+ if (imported > 0) {
7108
+ this.log(`Imported ${imported} history entries from IPFS sync`);
7109
+ }
7110
+ }
7061
7111
  totalAdded += result.added;
7062
7112
  totalRemoved += result.removed;
7063
7113
  }
@@ -7765,6 +7815,7 @@ var PaymentsModule = class _PaymentsModule {
7765
7815
  return data ? JSON.parse(data) : [];
7766
7816
  }
7767
7817
  async createStorageData() {
7818
+ const sorted = [...this._historyCache].sort((a, b) => b.timestamp - a.timestamp);
7768
7819
  return await buildTxfStorageData(
7769
7820
  Array.from(this.tokens.values()),
7770
7821
  {
@@ -7776,7 +7827,8 @@ var PaymentsModule = class _PaymentsModule {
7776
7827
  nametags: this.nametags,
7777
7828
  tombstones: this.tombstones,
7778
7829
  archivedTokens: this.archivedTokens,
7779
- forkedTokens: this.forkedTokens
7830
+ forkedTokens: this.forkedTokens,
7831
+ historyEntries: sorted.slice(0, MAX_SYNCED_HISTORY_ENTRIES)
7780
7832
  }
7781
7833
  );
7782
7834
  }