@unicitylabs/sphere-sdk 0.8.0-dev.3 → 0.8.0-dev.5

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
@@ -7562,6 +7562,19 @@ var TokenRegistry = class _TokenRegistry {
7562
7562
  // ===========================================================================
7563
7563
  // Cache (StorageProvider)
7564
7564
  // ===========================================================================
7565
+ /**
7566
+ * Per-network cache keys. The registry cache MUST be namespaced by remoteUrl:
7567
+ * testnet and testnet2 ship DIFFERENT coinIds for the same symbol, so a single
7568
+ * global key let a stale testnet snapshot be served for a testnet2 request —
7569
+ * wrong/missing icons AND self-mint resolving the wrong-network coinId. Keying
7570
+ * by remoteUrl isolates the snapshots so a network can never read another's.
7571
+ */
7572
+ cacheKeys() {
7573
+ const base = STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE;
7574
+ const baseTs = STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE_TS;
7575
+ if (!this.remoteUrl) return { data: base, ts: baseTs };
7576
+ return { data: `${base}:${this.remoteUrl}`, ts: `${baseTs}:${this.remoteUrl}` };
7577
+ }
7565
7578
  /**
7566
7579
  * Load definitions from StorageProvider cache.
7567
7580
  * Only applies if cache exists and is fresh (within refreshIntervalMs).
@@ -7569,9 +7582,10 @@ var TokenRegistry = class _TokenRegistry {
7569
7582
  async loadFromCache() {
7570
7583
  if (!this.storage) return false;
7571
7584
  try {
7585
+ const { data: dataKey, ts: tsKey } = this.cacheKeys();
7572
7586
  const [cached, cachedTs] = await Promise.all([
7573
- this.storage.get(STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE),
7574
- this.storage.get(STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE_TS)
7587
+ this.storage.get(dataKey),
7588
+ this.storage.get(tsKey)
7575
7589
  ]);
7576
7590
  if (!cached || !cachedTs) return false;
7577
7591
  const ts = parseInt(cachedTs, 10);
@@ -7594,9 +7608,10 @@ var TokenRegistry = class _TokenRegistry {
7594
7608
  async saveToCache(definitions) {
7595
7609
  if (!this.storage) return;
7596
7610
  try {
7611
+ const { data: dataKey, ts: tsKey } = this.cacheKeys();
7597
7612
  await Promise.all([
7598
- this.storage.set(STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE, JSON.stringify(definitions)),
7599
- this.storage.set(STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE_TS, String(Date.now()))
7613
+ this.storage.set(dataKey, JSON.stringify(definitions)),
7614
+ this.storage.set(tsKey, String(Date.now()))
7600
7615
  ]);
7601
7616
  } catch {
7602
7617
  }
@@ -28420,6 +28435,8 @@ var Sphere = class _Sphere {
28420
28435
  const walletExists = await _Sphere.exists(options.storage);
28421
28436
  if (walletExists) {
28422
28437
  const sphere2 = await _Sphere.load({
28438
+ network: options.network,
28439
+ // forward network so load's configureTokenRegistry uses it (not the testnet default)
28423
28440
  storage: options.storage,
28424
28441
  transport: options.transport,
28425
28442
  oracle: options.oracle,
@@ -28454,6 +28471,8 @@ var Sphere = class _Sphere {
28454
28471
  }
28455
28472
  const sphere = await _Sphere.create({
28456
28473
  mnemonic,
28474
+ network: options.network,
28475
+ // forward network so create's configureTokenRegistry uses it (not the testnet default)
28457
28476
  storage: options.storage,
28458
28477
  transport: options.transport,
28459
28478
  oracle: options.oracle,
@@ -28488,13 +28507,14 @@ var Sphere = class _Sphere {
28488
28507
  */
28489
28508
  static resolveGroupChatConfig(config, network) {
28490
28509
  if (!config) return void 0;
28510
+ if (!network) {
28511
+ throw new SphereError("network is required to resolve group chat relays.", "INVALID_CONFIG");
28512
+ }
28491
28513
  if (config === true) {
28492
- const netConfig = network ? NETWORKS[network] : NETWORKS.mainnet;
28493
- return { relays: [...netConfig.groupRelays] };
28514
+ return { relays: [...NETWORKS[network].groupRelays] };
28494
28515
  }
28495
28516
  if (!config.relays || config.relays.length === 0) {
28496
- const netConfig = network ? NETWORKS[network] : NETWORKS.mainnet;
28497
- return { ...config, relays: [...netConfig.groupRelays] };
28517
+ return { ...config, relays: [...NETWORKS[network].groupRelays] };
28498
28518
  }
28499
28519
  return config;
28500
28520
  }
@@ -28541,8 +28561,13 @@ var Sphere = class _Sphere {
28541
28561
  * This method ensures the main bundle's TokenRegistry is properly configured.
28542
28562
  */
28543
28563
  static configureTokenRegistry(storage, network) {
28544
- const netConfig = network ? NETWORKS[network] : NETWORKS.testnet;
28545
- TokenRegistry.configure({ remoteUrl: netConfig.tokenRegistryUrl, storage });
28564
+ if (!network) {
28565
+ throw new SphereError(
28566
+ "network is required to configure the TokenRegistry. Every Sphere entry point must forward options.network.",
28567
+ "INVALID_CONFIG"
28568
+ );
28569
+ }
28570
+ TokenRegistry.configure({ remoteUrl: NETWORKS[network].tokenRegistryUrl, storage });
28546
28571
  }
28547
28572
  /**
28548
28573
  * Create new wallet with mnemonic
@@ -28692,7 +28717,8 @@ var Sphere = class _Sphere {
28692
28717
  await options.storage.connect();
28693
28718
  logger.debug("Sphere", "Storage reconnected");
28694
28719
  }
28695
- const groupChatConfig = _Sphere.resolveGroupChatConfig(options.groupChat);
28720
+ _Sphere.configureTokenRegistry(options.storage, options.network);
28721
+ const groupChatConfig = _Sphere.resolveGroupChatConfig(options.groupChat, options.network);
28696
28722
  const marketConfig = _Sphere.resolveMarketConfig(options.market);
28697
28723
  const accountingConfig = _Sphere.resolveAccountingConfig(options.accounting);
28698
28724
  const swapConfig = _Sphere.resolveSwapConfig(options.swap);