@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.d.cts CHANGED
@@ -7514,13 +7514,14 @@ interface LegacyFileImportOptions {
7514
7514
  *
7515
7515
  * const storage = createLocalStorageProvider();
7516
7516
  * const transport = createNostrTransportProvider();
7517
- * const oracle = createUnicityAggregatorProvider({ url: '/rpc' });
7517
+ * const oracle = createUnicityAggregatorProvider({ url: '/rpc', network: 'testnet2' });
7518
7518
  *
7519
7519
  * // Option 1: Unified init (recommended)
7520
7520
  * const { sphere, created, generatedMnemonic } = await Sphere.init({
7521
7521
  * storage,
7522
7522
  * transport,
7523
7523
  * oracle,
7524
+ * network: 'testnet2', // required: selects registry/trustbase/aggregator
7524
7525
  * mnemonic: 'your twelve words...', // optional - will load if wallet exists
7525
7526
  * autoGenerate: true, // generate new mnemonic if needed
7526
7527
  * });
@@ -7531,9 +7532,9 @@ interface LegacyFileImportOptions {
7531
7532
  *
7532
7533
  * // Option 2: Manual create/load
7533
7534
  * if (await Sphere.exists(storage)) {
7534
- * const sphere = await Sphere.load({ storage, transport, oracle });
7535
+ * const sphere = await Sphere.load({ storage, transport, oracle, network: 'testnet2' });
7535
7536
  * } else {
7536
- * const sphere = await Sphere.create({ mnemonic, storage, transport, oracle });
7537
+ * const sphere = await Sphere.create({ mnemonic, storage, transport, oracle, network: 'testnet2' });
7537
7538
  * }
7538
7539
  *
7539
7540
  * // Use the wallet
@@ -7664,6 +7665,10 @@ interface SphereImportOptions {
7664
7665
  derivationMode?: DerivationMode;
7665
7666
  /** Optional nametag to register for this wallet (e.g., 'alice' for @alice). Token is auto-minted. */
7666
7667
  nametag?: string;
7668
+ /** Network this wallet runs on — drives TokenRegistry config. Without it, import
7669
+ * falls back to NETWORKS.testnet and a non-testnet wallet loads the wrong registry
7670
+ * (symbols resolve via baked values but icons/metadata don't) until a reload. */
7671
+ network?: NetworkType;
7667
7672
  /** Storage provider instance */
7668
7673
  storage: StorageProvider;
7669
7674
  /** Optional token storage provider */
@@ -10024,6 +10029,14 @@ declare class TokenRegistry {
10024
10029
  * After initial data is available, start periodic auto-refresh if configured.
10025
10030
  */
10026
10031
  private performInitialLoad;
10032
+ /**
10033
+ * Per-network cache keys. The registry cache MUST be namespaced by remoteUrl:
10034
+ * testnet and testnet2 ship DIFFERENT coinIds for the same symbol, so a single
10035
+ * global key let a stale testnet snapshot be served for a testnet2 request —
10036
+ * wrong/missing icons AND self-mint resolving the wrong-network coinId. Keying
10037
+ * by remoteUrl isolates the snapshots so a network can never read another's.
10038
+ */
10039
+ private cacheKeys;
10027
10040
  /**
10028
10041
  * Load definitions from StorageProvider cache.
10029
10042
  * Only applies if cache exists and is fresh (within refreshIntervalMs).
package/dist/index.d.ts CHANGED
@@ -7514,13 +7514,14 @@ interface LegacyFileImportOptions {
7514
7514
  *
7515
7515
  * const storage = createLocalStorageProvider();
7516
7516
  * const transport = createNostrTransportProvider();
7517
- * const oracle = createUnicityAggregatorProvider({ url: '/rpc' });
7517
+ * const oracle = createUnicityAggregatorProvider({ url: '/rpc', network: 'testnet2' });
7518
7518
  *
7519
7519
  * // Option 1: Unified init (recommended)
7520
7520
  * const { sphere, created, generatedMnemonic } = await Sphere.init({
7521
7521
  * storage,
7522
7522
  * transport,
7523
7523
  * oracle,
7524
+ * network: 'testnet2', // required: selects registry/trustbase/aggregator
7524
7525
  * mnemonic: 'your twelve words...', // optional - will load if wallet exists
7525
7526
  * autoGenerate: true, // generate new mnemonic if needed
7526
7527
  * });
@@ -7531,9 +7532,9 @@ interface LegacyFileImportOptions {
7531
7532
  *
7532
7533
  * // Option 2: Manual create/load
7533
7534
  * if (await Sphere.exists(storage)) {
7534
- * const sphere = await Sphere.load({ storage, transport, oracle });
7535
+ * const sphere = await Sphere.load({ storage, transport, oracle, network: 'testnet2' });
7535
7536
  * } else {
7536
- * const sphere = await Sphere.create({ mnemonic, storage, transport, oracle });
7537
+ * const sphere = await Sphere.create({ mnemonic, storage, transport, oracle, network: 'testnet2' });
7537
7538
  * }
7538
7539
  *
7539
7540
  * // Use the wallet
@@ -7664,6 +7665,10 @@ interface SphereImportOptions {
7664
7665
  derivationMode?: DerivationMode;
7665
7666
  /** Optional nametag to register for this wallet (e.g., 'alice' for @alice). Token is auto-minted. */
7666
7667
  nametag?: string;
7668
+ /** Network this wallet runs on — drives TokenRegistry config. Without it, import
7669
+ * falls back to NETWORKS.testnet and a non-testnet wallet loads the wrong registry
7670
+ * (symbols resolve via baked values but icons/metadata don't) until a reload. */
7671
+ network?: NetworkType;
7667
7672
  /** Storage provider instance */
7668
7673
  storage: StorageProvider;
7669
7674
  /** Optional token storage provider */
@@ -10024,6 +10029,14 @@ declare class TokenRegistry {
10024
10029
  * After initial data is available, start periodic auto-refresh if configured.
10025
10030
  */
10026
10031
  private performInitialLoad;
10032
+ /**
10033
+ * Per-network cache keys. The registry cache MUST be namespaced by remoteUrl:
10034
+ * testnet and testnet2 ship DIFFERENT coinIds for the same symbol, so a single
10035
+ * global key let a stale testnet snapshot be served for a testnet2 request —
10036
+ * wrong/missing icons AND self-mint resolving the wrong-network coinId. Keying
10037
+ * by remoteUrl isolates the snapshots so a network can never read another's.
10038
+ */
10039
+ private cacheKeys;
10027
10040
  /**
10028
10041
  * Load definitions from StorageProvider cache.
10029
10042
  * Only applies if cache exists and is fresh (within refreshIntervalMs).
package/dist/index.js CHANGED
@@ -7380,6 +7380,19 @@ var TokenRegistry = class _TokenRegistry {
7380
7380
  // ===========================================================================
7381
7381
  // Cache (StorageProvider)
7382
7382
  // ===========================================================================
7383
+ /**
7384
+ * Per-network cache keys. The registry cache MUST be namespaced by remoteUrl:
7385
+ * testnet and testnet2 ship DIFFERENT coinIds for the same symbol, so a single
7386
+ * global key let a stale testnet snapshot be served for a testnet2 request —
7387
+ * wrong/missing icons AND self-mint resolving the wrong-network coinId. Keying
7388
+ * by remoteUrl isolates the snapshots so a network can never read another's.
7389
+ */
7390
+ cacheKeys() {
7391
+ const base = STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE;
7392
+ const baseTs = STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE_TS;
7393
+ if (!this.remoteUrl) return { data: base, ts: baseTs };
7394
+ return { data: `${base}:${this.remoteUrl}`, ts: `${baseTs}:${this.remoteUrl}` };
7395
+ }
7383
7396
  /**
7384
7397
  * Load definitions from StorageProvider cache.
7385
7398
  * Only applies if cache exists and is fresh (within refreshIntervalMs).
@@ -7387,9 +7400,10 @@ var TokenRegistry = class _TokenRegistry {
7387
7400
  async loadFromCache() {
7388
7401
  if (!this.storage) return false;
7389
7402
  try {
7403
+ const { data: dataKey, ts: tsKey } = this.cacheKeys();
7390
7404
  const [cached, cachedTs] = await Promise.all([
7391
- this.storage.get(STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE),
7392
- this.storage.get(STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE_TS)
7405
+ this.storage.get(dataKey),
7406
+ this.storage.get(tsKey)
7393
7407
  ]);
7394
7408
  if (!cached || !cachedTs) return false;
7395
7409
  const ts = parseInt(cachedTs, 10);
@@ -7412,9 +7426,10 @@ var TokenRegistry = class _TokenRegistry {
7412
7426
  async saveToCache(definitions) {
7413
7427
  if (!this.storage) return;
7414
7428
  try {
7429
+ const { data: dataKey, ts: tsKey } = this.cacheKeys();
7415
7430
  await Promise.all([
7416
- this.storage.set(STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE, JSON.stringify(definitions)),
7417
- this.storage.set(STORAGE_KEYS_GLOBAL.TOKEN_REGISTRY_CACHE_TS, String(Date.now()))
7431
+ this.storage.set(dataKey, JSON.stringify(definitions)),
7432
+ this.storage.set(tsKey, String(Date.now()))
7418
7433
  ]);
7419
7434
  } catch {
7420
7435
  }
@@ -28242,6 +28257,8 @@ var Sphere = class _Sphere {
28242
28257
  const walletExists = await _Sphere.exists(options.storage);
28243
28258
  if (walletExists) {
28244
28259
  const sphere2 = await _Sphere.load({
28260
+ network: options.network,
28261
+ // forward network so load's configureTokenRegistry uses it (not the testnet default)
28245
28262
  storage: options.storage,
28246
28263
  transport: options.transport,
28247
28264
  oracle: options.oracle,
@@ -28276,6 +28293,8 @@ var Sphere = class _Sphere {
28276
28293
  }
28277
28294
  const sphere = await _Sphere.create({
28278
28295
  mnemonic,
28296
+ network: options.network,
28297
+ // forward network so create's configureTokenRegistry uses it (not the testnet default)
28279
28298
  storage: options.storage,
28280
28299
  transport: options.transport,
28281
28300
  oracle: options.oracle,
@@ -28310,13 +28329,14 @@ var Sphere = class _Sphere {
28310
28329
  */
28311
28330
  static resolveGroupChatConfig(config, network) {
28312
28331
  if (!config) return void 0;
28332
+ if (!network) {
28333
+ throw new SphereError("network is required to resolve group chat relays.", "INVALID_CONFIG");
28334
+ }
28313
28335
  if (config === true) {
28314
- const netConfig = network ? NETWORKS[network] : NETWORKS.mainnet;
28315
- return { relays: [...netConfig.groupRelays] };
28336
+ return { relays: [...NETWORKS[network].groupRelays] };
28316
28337
  }
28317
28338
  if (!config.relays || config.relays.length === 0) {
28318
- const netConfig = network ? NETWORKS[network] : NETWORKS.mainnet;
28319
- return { ...config, relays: [...netConfig.groupRelays] };
28339
+ return { ...config, relays: [...NETWORKS[network].groupRelays] };
28320
28340
  }
28321
28341
  return config;
28322
28342
  }
@@ -28363,8 +28383,13 @@ var Sphere = class _Sphere {
28363
28383
  * This method ensures the main bundle's TokenRegistry is properly configured.
28364
28384
  */
28365
28385
  static configureTokenRegistry(storage, network) {
28366
- const netConfig = network ? NETWORKS[network] : NETWORKS.testnet;
28367
- TokenRegistry.configure({ remoteUrl: netConfig.tokenRegistryUrl, storage });
28386
+ if (!network) {
28387
+ throw new SphereError(
28388
+ "network is required to configure the TokenRegistry. Every Sphere entry point must forward options.network.",
28389
+ "INVALID_CONFIG"
28390
+ );
28391
+ }
28392
+ TokenRegistry.configure({ remoteUrl: NETWORKS[network].tokenRegistryUrl, storage });
28368
28393
  }
28369
28394
  /**
28370
28395
  * Create new wallet with mnemonic
@@ -28514,7 +28539,8 @@ var Sphere = class _Sphere {
28514
28539
  await options.storage.connect();
28515
28540
  logger.debug("Sphere", "Storage reconnected");
28516
28541
  }
28517
- const groupChatConfig = _Sphere.resolveGroupChatConfig(options.groupChat);
28542
+ _Sphere.configureTokenRegistry(options.storage, options.network);
28543
+ const groupChatConfig = _Sphere.resolveGroupChatConfig(options.groupChat, options.network);
28518
28544
  const marketConfig = _Sphere.resolveMarketConfig(options.market);
28519
28545
  const accountingConfig = _Sphere.resolveAccountingConfig(options.accounting);
28520
28546
  const swapConfig = _Sphere.resolveSwapConfig(options.swap);