@unicitylabs/sphere-sdk 0.2.5 → 0.3.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.
@@ -1223,9 +1223,13 @@ function mergeTxfData(local, remote) {
1223
1223
  remote._invalid ?? [],
1224
1224
  "tokenId"
1225
1225
  );
1226
+ const localNametags = local._nametags ?? [];
1227
+ const remoteNametags = remote._nametags ?? [];
1228
+ const mergedNametags = mergeNametagsByName(localNametags, remoteNametags);
1226
1229
  const merged = {
1227
1230
  _meta: mergedMeta,
1228
1231
  _tombstones: mergedTombstones.length > 0 ? mergedTombstones : void 0,
1232
+ _nametags: mergedNametags.length > 0 ? mergedNametags : void 0,
1229
1233
  _outbox: mergedOutbox.length > 0 ? mergedOutbox : void 0,
1230
1234
  _sent: mergedSent.length > 0 ? mergedSent : void 0,
1231
1235
  _invalid: mergedInvalid.length > 0 ? mergedInvalid : void 0,
@@ -1252,6 +1256,7 @@ function getTokenKeys(data) {
1252
1256
  "_sent",
1253
1257
  "_invalid",
1254
1258
  "_nametag",
1259
+ "_nametags",
1255
1260
  "_mintOutbox",
1256
1261
  "_invalidatedNametags",
1257
1262
  "_integrity"
@@ -1259,7 +1264,7 @@ function getTokenKeys(data) {
1259
1264
  const keys = /* @__PURE__ */ new Set();
1260
1265
  for (const key of Object.keys(data)) {
1261
1266
  if (reservedKeys.has(key)) continue;
1262
- if (key.startsWith("archived-") || key.startsWith("_forked_") || key.startsWith("nametag-")) continue;
1267
+ if (key.startsWith("archived-") || key.startsWith("_forked_")) continue;
1263
1268
  keys.add(key);
1264
1269
  }
1265
1270
  return keys;
@@ -1274,6 +1279,18 @@ function isTokenTombstoned(tokenId, localToken, remoteToken, tombstoneKeys) {
1274
1279
  void remoteToken;
1275
1280
  return false;
1276
1281
  }
1282
+ function mergeNametagsByName(local, remote) {
1283
+ const seen = /* @__PURE__ */ new Map();
1284
+ for (const item of local) {
1285
+ if (item.name) seen.set(item.name, item);
1286
+ }
1287
+ for (const item of remote) {
1288
+ if (item.name && !seen.has(item.name)) {
1289
+ seen.set(item.name, item);
1290
+ }
1291
+ }
1292
+ return Array.from(seen.values());
1293
+ }
1277
1294
  function mergeArrayById(local, remote, idField) {
1278
1295
  const seen = /* @__PURE__ */ new Map();
1279
1296
  for (const item of local) {
@@ -1636,14 +1653,11 @@ var AsyncSerialQueue = class {
1636
1653
  var WriteBuffer = class {
1637
1654
  /** Full TXF data from save() calls — latest wins */
1638
1655
  txfData = null;
1639
- /** Individual token mutations: key -> { op: 'save'|'delete', data? } */
1640
- tokenMutations = /* @__PURE__ */ new Map();
1641
1656
  get isEmpty() {
1642
- return this.txfData === null && this.tokenMutations.size === 0;
1657
+ return this.txfData === null;
1643
1658
  }
1644
1659
  clear() {
1645
1660
  this.txfData = null;
1646
- this.tokenMutations.clear();
1647
1661
  }
1648
1662
  /**
1649
1663
  * Merge another buffer's contents into this one (for rollback).
@@ -1653,11 +1667,6 @@ var WriteBuffer = class {
1653
1667
  if (other.txfData && !this.txfData) {
1654
1668
  this.txfData = other.txfData;
1655
1669
  }
1656
- for (const [id, mutation] of other.tokenMutations) {
1657
- if (!this.tokenMutations.has(id)) {
1658
- this.tokenMutations.set(id, mutation);
1659
- }
1660
- }
1661
1670
  }
1662
1671
  };
1663
1672
 
@@ -1686,7 +1695,17 @@ var STORAGE_KEYS_GLOBAL = {
1686
1695
  /** Active addresses registry (JSON: TrackedAddressesStorage) */
1687
1696
  TRACKED_ADDRESSES: "tracked_addresses",
1688
1697
  /** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
1689
- LAST_WALLET_EVENT_TS: "last_wallet_event_ts"
1698
+ LAST_WALLET_EVENT_TS: "last_wallet_event_ts",
1699
+ /** Group chat: joined groups */
1700
+ GROUP_CHAT_GROUPS: "group_chat_groups",
1701
+ /** Group chat: messages */
1702
+ GROUP_CHAT_MESSAGES: "group_chat_messages",
1703
+ /** Group chat: members */
1704
+ GROUP_CHAT_MEMBERS: "group_chat_members",
1705
+ /** Group chat: processed event IDs for deduplication */
1706
+ GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events",
1707
+ /** Group chat: last used relay URL (stale data detection) */
1708
+ GROUP_CHAT_RELAY_URL: "group_chat_relay_url"
1690
1709
  };
1691
1710
  var STORAGE_KEYS_ADDRESS = {
1692
1711
  /** Pending transfers for this address */
@@ -1758,9 +1777,6 @@ var IpfsStorageProvider = class {
1758
1777
  subscriptionClient = null;
1759
1778
  /** Unsubscribe function from subscription client */
1760
1779
  subscriptionUnsubscribe = null;
1761
- /** In-memory buffer for individual token save/delete calls */
1762
- tokenBuffer = /* @__PURE__ */ new Map();
1763
- deletedTokenIds = /* @__PURE__ */ new Set();
1764
1780
  /** Write-behind buffer: serializes flush / sync / shutdown */
1765
1781
  flushQueue = new AsyncSerialQueue();
1766
1782
  /** Pending mutations not yet flushed to IPFS */
@@ -1949,14 +1965,6 @@ var IpfsStorageProvider = class {
1949
1965
  metaUpdate.lastCid = this.remoteCid;
1950
1966
  }
1951
1967
  const updatedData = { ...data, _meta: metaUpdate };
1952
- for (const [tokenId, tokenData] of this.tokenBuffer) {
1953
- if (!this.deletedTokenIds.has(tokenId)) {
1954
- updatedData[tokenId] = tokenData;
1955
- }
1956
- }
1957
- for (const tokenId of this.deletedTokenIds) {
1958
- delete updatedData[tokenId];
1959
- }
1960
1968
  const { cid } = await this.httpClient.upload(updatedData);
1961
1969
  this.log(`Content uploaded: CID=${cid}`);
1962
1970
  const baseSeq = this.ipnsSequenceNumber > this.lastKnownRemoteSequence ? this.ipnsSequenceNumber : this.lastKnownRemoteSequence;
@@ -1995,7 +2003,6 @@ var IpfsStorageProvider = class {
1995
2003
  lastCid: cid,
1996
2004
  version: this.dataVersion
1997
2005
  });
1998
- this.deletedTokenIds.clear();
1999
2006
  this.emitEvent({
2000
2007
  type: "storage:saved",
2001
2008
  timestamp: Date.now(),
@@ -2107,7 +2114,6 @@ var IpfsStorageProvider = class {
2107
2114
  if (typeof remoteVersion === "number" && remoteVersion > this.dataVersion) {
2108
2115
  this.dataVersion = remoteVersion;
2109
2116
  }
2110
- this.populateTokenBuffer(data);
2111
2117
  this.emitEvent({
2112
2118
  type: "storage:loaded",
2113
2119
  timestamp: Date.now(),
@@ -2153,7 +2159,7 @@ var IpfsStorageProvider = class {
2153
2159
  this.emitEvent({ type: "sync:completed", timestamp: Date.now() });
2154
2160
  return {
2155
2161
  success: saveResult2.success,
2156
- merged: this.enrichWithTokenBuffer(localData),
2162
+ merged: localData,
2157
2163
  added: 0,
2158
2164
  removed: 0,
2159
2165
  conflicts: 0,
@@ -2168,7 +2174,7 @@ var IpfsStorageProvider = class {
2168
2174
  this.emitEvent({ type: "sync:completed", timestamp: Date.now() });
2169
2175
  return {
2170
2176
  success: true,
2171
- merged: this.enrichWithTokenBuffer(localData),
2177
+ merged: localData,
2172
2178
  added: 0,
2173
2179
  removed: 0,
2174
2180
  conflicts: 0
@@ -2191,7 +2197,7 @@ var IpfsStorageProvider = class {
2191
2197
  });
2192
2198
  return {
2193
2199
  success: saveResult.success,
2194
- merged: this.enrichWithTokenBuffer(merged),
2200
+ merged,
2195
2201
  added,
2196
2202
  removed,
2197
2203
  conflicts,
@@ -2217,21 +2223,6 @@ var IpfsStorageProvider = class {
2217
2223
  // ---------------------------------------------------------------------------
2218
2224
  // Private Helpers
2219
2225
  // ---------------------------------------------------------------------------
2220
- /**
2221
- * Enrich TXF data with individually-buffered tokens before returning to caller.
2222
- * PaymentsModule.createStorageData() passes empty tokens (they're stored via
2223
- * saveToken()), but loadFromStorageData() needs them in the merged result.
2224
- */
2225
- enrichWithTokenBuffer(data) {
2226
- if (this.tokenBuffer.size === 0) return data;
2227
- const enriched = { ...data };
2228
- for (const [tokenId, tokenData] of this.tokenBuffer) {
2229
- if (!this.deletedTokenIds.has(tokenId)) {
2230
- enriched[tokenId] = tokenData;
2231
- }
2232
- }
2233
- return enriched;
2234
- }
2235
2226
  // ---------------------------------------------------------------------------
2236
2227
  // Optional Methods
2237
2228
  // ---------------------------------------------------------------------------
@@ -2261,8 +2252,6 @@ var IpfsStorageProvider = class {
2261
2252
  const result = await this._doSave(emptyData);
2262
2253
  if (result.success) {
2263
2254
  this.cache.clear();
2264
- this.tokenBuffer.clear();
2265
- this.deletedTokenIds.clear();
2266
2255
  await this.statePersistence.clear(this.ipnsName);
2267
2256
  }
2268
2257
  return result.success;
@@ -2273,27 +2262,6 @@ var IpfsStorageProvider = class {
2273
2262
  this.eventCallbacks.delete(callback);
2274
2263
  };
2275
2264
  }
2276
- async saveToken(tokenId, tokenData) {
2277
- this.pendingBuffer.tokenMutations.set(tokenId, { op: "save", data: tokenData });
2278
- this.tokenBuffer.set(tokenId, tokenData);
2279
- this.deletedTokenIds.delete(tokenId);
2280
- this.scheduleFlush();
2281
- }
2282
- async getToken(tokenId) {
2283
- if (this.deletedTokenIds.has(tokenId)) return null;
2284
- return this.tokenBuffer.get(tokenId) ?? null;
2285
- }
2286
- async listTokenIds() {
2287
- return Array.from(this.tokenBuffer.keys()).filter(
2288
- (id) => !this.deletedTokenIds.has(id)
2289
- );
2290
- }
2291
- async deleteToken(tokenId) {
2292
- this.pendingBuffer.tokenMutations.set(tokenId, { op: "delete" });
2293
- this.tokenBuffer.delete(tokenId);
2294
- this.deletedTokenIds.add(tokenId);
2295
- this.scheduleFlush();
2296
- }
2297
2265
  // ---------------------------------------------------------------------------
2298
2266
  // Public Accessors
2299
2267
  // ---------------------------------------------------------------------------
@@ -2385,26 +2353,6 @@ var IpfsStorageProvider = class {
2385
2353
  console.log(`[IPFS-Storage] ${message}`);
2386
2354
  }
2387
2355
  }
2388
- META_KEYS = /* @__PURE__ */ new Set([
2389
- "_meta",
2390
- "_tombstones",
2391
- "_outbox",
2392
- "_sent",
2393
- "_invalid",
2394
- "_nametag",
2395
- "_mintOutbox",
2396
- "_invalidatedNametags",
2397
- "_integrity"
2398
- ]);
2399
- populateTokenBuffer(data) {
2400
- this.tokenBuffer.clear();
2401
- this.deletedTokenIds.clear();
2402
- for (const key of Object.keys(data)) {
2403
- if (!this.META_KEYS.has(key)) {
2404
- this.tokenBuffer.set(key, data[key]);
2405
- }
2406
- }
2407
- }
2408
2356
  };
2409
2357
 
2410
2358
  // impl/browser/ipfs/browser-ipfs-state-persistence.ts