@unicitylabs/sphere-sdk 0.2.5 → 0.3.0

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.
@@ -1184,9 +1184,13 @@ function mergeTxfData(local, remote) {
1184
1184
  remote._invalid ?? [],
1185
1185
  "tokenId"
1186
1186
  );
1187
+ const localNametags = local._nametags ?? [];
1188
+ const remoteNametags = remote._nametags ?? [];
1189
+ const mergedNametags = mergeNametagsByName(localNametags, remoteNametags);
1187
1190
  const merged = {
1188
1191
  _meta: mergedMeta,
1189
1192
  _tombstones: mergedTombstones.length > 0 ? mergedTombstones : void 0,
1193
+ _nametags: mergedNametags.length > 0 ? mergedNametags : void 0,
1190
1194
  _outbox: mergedOutbox.length > 0 ? mergedOutbox : void 0,
1191
1195
  _sent: mergedSent.length > 0 ? mergedSent : void 0,
1192
1196
  _invalid: mergedInvalid.length > 0 ? mergedInvalid : void 0,
@@ -1213,6 +1217,7 @@ function getTokenKeys(data) {
1213
1217
  "_sent",
1214
1218
  "_invalid",
1215
1219
  "_nametag",
1220
+ "_nametags",
1216
1221
  "_mintOutbox",
1217
1222
  "_invalidatedNametags",
1218
1223
  "_integrity"
@@ -1220,7 +1225,7 @@ function getTokenKeys(data) {
1220
1225
  const keys = /* @__PURE__ */ new Set();
1221
1226
  for (const key of Object.keys(data)) {
1222
1227
  if (reservedKeys.has(key)) continue;
1223
- if (key.startsWith("archived-") || key.startsWith("_forked_") || key.startsWith("nametag-")) continue;
1228
+ if (key.startsWith("archived-") || key.startsWith("_forked_")) continue;
1224
1229
  keys.add(key);
1225
1230
  }
1226
1231
  return keys;
@@ -1235,6 +1240,18 @@ function isTokenTombstoned(tokenId, localToken, remoteToken, tombstoneKeys) {
1235
1240
  void remoteToken;
1236
1241
  return false;
1237
1242
  }
1243
+ function mergeNametagsByName(local, remote) {
1244
+ const seen = /* @__PURE__ */ new Map();
1245
+ for (const item of local) {
1246
+ if (item.name) seen.set(item.name, item);
1247
+ }
1248
+ for (const item of remote) {
1249
+ if (item.name && !seen.has(item.name)) {
1250
+ seen.set(item.name, item);
1251
+ }
1252
+ }
1253
+ return Array.from(seen.values());
1254
+ }
1238
1255
  function mergeArrayById(local, remote, idField) {
1239
1256
  const seen = /* @__PURE__ */ new Map();
1240
1257
  for (const item of local) {
@@ -1597,14 +1614,11 @@ var AsyncSerialQueue = class {
1597
1614
  var WriteBuffer = class {
1598
1615
  /** Full TXF data from save() calls — latest wins */
1599
1616
  txfData = null;
1600
- /** Individual token mutations: key -> { op: 'save'|'delete', data? } */
1601
- tokenMutations = /* @__PURE__ */ new Map();
1602
1617
  get isEmpty() {
1603
- return this.txfData === null && this.tokenMutations.size === 0;
1618
+ return this.txfData === null;
1604
1619
  }
1605
1620
  clear() {
1606
1621
  this.txfData = null;
1607
- this.tokenMutations.clear();
1608
1622
  }
1609
1623
  /**
1610
1624
  * Merge another buffer's contents into this one (for rollback).
@@ -1614,11 +1628,6 @@ var WriteBuffer = class {
1614
1628
  if (other.txfData && !this.txfData) {
1615
1629
  this.txfData = other.txfData;
1616
1630
  }
1617
- for (const [id, mutation] of other.tokenMutations) {
1618
- if (!this.tokenMutations.has(id)) {
1619
- this.tokenMutations.set(id, mutation);
1620
- }
1621
- }
1622
1631
  }
1623
1632
  };
1624
1633
 
@@ -1647,7 +1656,17 @@ var STORAGE_KEYS_GLOBAL = {
1647
1656
  /** Active addresses registry (JSON: TrackedAddressesStorage) */
1648
1657
  TRACKED_ADDRESSES: "tracked_addresses",
1649
1658
  /** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
1650
- LAST_WALLET_EVENT_TS: "last_wallet_event_ts"
1659
+ LAST_WALLET_EVENT_TS: "last_wallet_event_ts",
1660
+ /** Group chat: joined groups */
1661
+ GROUP_CHAT_GROUPS: "group_chat_groups",
1662
+ /** Group chat: messages */
1663
+ GROUP_CHAT_MESSAGES: "group_chat_messages",
1664
+ /** Group chat: members */
1665
+ GROUP_CHAT_MEMBERS: "group_chat_members",
1666
+ /** Group chat: processed event IDs for deduplication */
1667
+ GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events",
1668
+ /** Group chat: last used relay URL (stale data detection) */
1669
+ GROUP_CHAT_RELAY_URL: "group_chat_relay_url"
1651
1670
  };
1652
1671
  var STORAGE_KEYS_ADDRESS = {
1653
1672
  /** Pending transfers for this address */
@@ -1719,9 +1738,6 @@ var IpfsStorageProvider = class {
1719
1738
  subscriptionClient = null;
1720
1739
  /** Unsubscribe function from subscription client */
1721
1740
  subscriptionUnsubscribe = null;
1722
- /** In-memory buffer for individual token save/delete calls */
1723
- tokenBuffer = /* @__PURE__ */ new Map();
1724
- deletedTokenIds = /* @__PURE__ */ new Set();
1725
1741
  /** Write-behind buffer: serializes flush / sync / shutdown */
1726
1742
  flushQueue = new AsyncSerialQueue();
1727
1743
  /** Pending mutations not yet flushed to IPFS */
@@ -1910,14 +1926,6 @@ var IpfsStorageProvider = class {
1910
1926
  metaUpdate.lastCid = this.remoteCid;
1911
1927
  }
1912
1928
  const updatedData = { ...data, _meta: metaUpdate };
1913
- for (const [tokenId, tokenData] of this.tokenBuffer) {
1914
- if (!this.deletedTokenIds.has(tokenId)) {
1915
- updatedData[tokenId] = tokenData;
1916
- }
1917
- }
1918
- for (const tokenId of this.deletedTokenIds) {
1919
- delete updatedData[tokenId];
1920
- }
1921
1929
  const { cid } = await this.httpClient.upload(updatedData);
1922
1930
  this.log(`Content uploaded: CID=${cid}`);
1923
1931
  const baseSeq = this.ipnsSequenceNumber > this.lastKnownRemoteSequence ? this.ipnsSequenceNumber : this.lastKnownRemoteSequence;
@@ -1956,7 +1964,6 @@ var IpfsStorageProvider = class {
1956
1964
  lastCid: cid,
1957
1965
  version: this.dataVersion
1958
1966
  });
1959
- this.deletedTokenIds.clear();
1960
1967
  this.emitEvent({
1961
1968
  type: "storage:saved",
1962
1969
  timestamp: Date.now(),
@@ -2068,7 +2075,6 @@ var IpfsStorageProvider = class {
2068
2075
  if (typeof remoteVersion === "number" && remoteVersion > this.dataVersion) {
2069
2076
  this.dataVersion = remoteVersion;
2070
2077
  }
2071
- this.populateTokenBuffer(data);
2072
2078
  this.emitEvent({
2073
2079
  type: "storage:loaded",
2074
2080
  timestamp: Date.now(),
@@ -2114,7 +2120,7 @@ var IpfsStorageProvider = class {
2114
2120
  this.emitEvent({ type: "sync:completed", timestamp: Date.now() });
2115
2121
  return {
2116
2122
  success: saveResult2.success,
2117
- merged: this.enrichWithTokenBuffer(localData),
2123
+ merged: localData,
2118
2124
  added: 0,
2119
2125
  removed: 0,
2120
2126
  conflicts: 0,
@@ -2129,7 +2135,7 @@ var IpfsStorageProvider = class {
2129
2135
  this.emitEvent({ type: "sync:completed", timestamp: Date.now() });
2130
2136
  return {
2131
2137
  success: true,
2132
- merged: this.enrichWithTokenBuffer(localData),
2138
+ merged: localData,
2133
2139
  added: 0,
2134
2140
  removed: 0,
2135
2141
  conflicts: 0
@@ -2152,7 +2158,7 @@ var IpfsStorageProvider = class {
2152
2158
  });
2153
2159
  return {
2154
2160
  success: saveResult.success,
2155
- merged: this.enrichWithTokenBuffer(merged),
2161
+ merged,
2156
2162
  added,
2157
2163
  removed,
2158
2164
  conflicts,
@@ -2178,21 +2184,6 @@ var IpfsStorageProvider = class {
2178
2184
  // ---------------------------------------------------------------------------
2179
2185
  // Private Helpers
2180
2186
  // ---------------------------------------------------------------------------
2181
- /**
2182
- * Enrich TXF data with individually-buffered tokens before returning to caller.
2183
- * PaymentsModule.createStorageData() passes empty tokens (they're stored via
2184
- * saveToken()), but loadFromStorageData() needs them in the merged result.
2185
- */
2186
- enrichWithTokenBuffer(data) {
2187
- if (this.tokenBuffer.size === 0) return data;
2188
- const enriched = { ...data };
2189
- for (const [tokenId, tokenData] of this.tokenBuffer) {
2190
- if (!this.deletedTokenIds.has(tokenId)) {
2191
- enriched[tokenId] = tokenData;
2192
- }
2193
- }
2194
- return enriched;
2195
- }
2196
2187
  // ---------------------------------------------------------------------------
2197
2188
  // Optional Methods
2198
2189
  // ---------------------------------------------------------------------------
@@ -2222,8 +2213,6 @@ var IpfsStorageProvider = class {
2222
2213
  const result = await this._doSave(emptyData);
2223
2214
  if (result.success) {
2224
2215
  this.cache.clear();
2225
- this.tokenBuffer.clear();
2226
- this.deletedTokenIds.clear();
2227
2216
  await this.statePersistence.clear(this.ipnsName);
2228
2217
  }
2229
2218
  return result.success;
@@ -2234,27 +2223,6 @@ var IpfsStorageProvider = class {
2234
2223
  this.eventCallbacks.delete(callback);
2235
2224
  };
2236
2225
  }
2237
- async saveToken(tokenId, tokenData) {
2238
- this.pendingBuffer.tokenMutations.set(tokenId, { op: "save", data: tokenData });
2239
- this.tokenBuffer.set(tokenId, tokenData);
2240
- this.deletedTokenIds.delete(tokenId);
2241
- this.scheduleFlush();
2242
- }
2243
- async getToken(tokenId) {
2244
- if (this.deletedTokenIds.has(tokenId)) return null;
2245
- return this.tokenBuffer.get(tokenId) ?? null;
2246
- }
2247
- async listTokenIds() {
2248
- return Array.from(this.tokenBuffer.keys()).filter(
2249
- (id) => !this.deletedTokenIds.has(id)
2250
- );
2251
- }
2252
- async deleteToken(tokenId) {
2253
- this.pendingBuffer.tokenMutations.set(tokenId, { op: "delete" });
2254
- this.tokenBuffer.delete(tokenId);
2255
- this.deletedTokenIds.add(tokenId);
2256
- this.scheduleFlush();
2257
- }
2258
2226
  // ---------------------------------------------------------------------------
2259
2227
  // Public Accessors
2260
2228
  // ---------------------------------------------------------------------------
@@ -2346,26 +2314,6 @@ var IpfsStorageProvider = class {
2346
2314
  console.log(`[IPFS-Storage] ${message}`);
2347
2315
  }
2348
2316
  }
2349
- META_KEYS = /* @__PURE__ */ new Set([
2350
- "_meta",
2351
- "_tombstones",
2352
- "_outbox",
2353
- "_sent",
2354
- "_invalid",
2355
- "_nametag",
2356
- "_mintOutbox",
2357
- "_invalidatedNametags",
2358
- "_integrity"
2359
- ]);
2360
- populateTokenBuffer(data) {
2361
- this.tokenBuffer.clear();
2362
- this.deletedTokenIds.clear();
2363
- for (const key of Object.keys(data)) {
2364
- if (!this.META_KEYS.has(key)) {
2365
- this.tokenBuffer.set(key, data[key]);
2366
- }
2367
- }
2368
- }
2369
2317
  };
2370
2318
 
2371
2319
  // impl/browser/ipfs/browser-ipfs-state-persistence.ts