@unicitylabs/sphere-sdk 0.2.2 → 0.2.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.
@@ -578,7 +578,7 @@ interface PaymentRequest {
578
578
  readonly coinId: string;
579
579
  /** Optional message/memo */
580
580
  readonly message?: string;
581
- /** Recipient nametag (who should pay) */
581
+ /** Where tokens should be sent */
582
582
  readonly recipientNametag?: string;
583
583
  /** Custom metadata */
584
584
  readonly metadata?: Record<string, unknown>;
@@ -605,7 +605,7 @@ interface IncomingPaymentRequest$1 {
605
605
  readonly symbol: string;
606
606
  /** Message from sender */
607
607
  readonly message?: string;
608
- /** Who this request is for (our nametag) */
608
+ /** Requester's nametag (where tokens should be sent) */
609
609
  readonly recipientNametag?: string;
610
610
  /** Original request ID from sender */
611
611
  readonly requestId: string;
@@ -1094,6 +1094,8 @@ interface IncomingPaymentRequest {
1094
1094
  id: string;
1095
1095
  /** Transport-specific pubkey of sender */
1096
1096
  senderTransportPubkey: string;
1097
+ /** Sender's nametag (if included in encrypted content) */
1098
+ senderNametag?: string;
1097
1099
  /** Parsed request data */
1098
1100
  request: {
1099
1101
  requestId: string;
@@ -1252,6 +1254,12 @@ declare class L1PaymentsModule {
1252
1254
  private _transport?;
1253
1255
  constructor(config?: L1PaymentsModuleConfig);
1254
1256
  initialize(deps: L1PaymentsModuleDependencies): Promise<void>;
1257
+ /**
1258
+ * Ensure the Fulcrum WebSocket is connected. Called lazily before any
1259
+ * operation that needs the network. If the singleton is already connected
1260
+ * (e.g. by the address scanner), this is a no-op.
1261
+ */
1262
+ private ensureConnected;
1255
1263
  destroy(): void;
1256
1264
  /**
1257
1265
  * Check if a string looks like an L1 address (alpha1... or alphat1...)
@@ -1261,7 +1269,7 @@ declare class L1PaymentsModule {
1261
1269
  * Resolve recipient to L1 address
1262
1270
  * Supports: L1 address (alpha1...), nametag (with or without @)
1263
1271
  */
1264
- private resolveL1Address;
1272
+ resolveL1Address(recipient: string): Promise<string>;
1265
1273
  /**
1266
1274
  * Resolve nametag to L1 address using transport provider
1267
1275
  */
@@ -1703,8 +1711,8 @@ interface PaymentsModuleConfig {
1703
1711
  maxRetries?: number;
1704
1712
  /** Enable debug logging */
1705
1713
  debug?: boolean;
1706
- /** L1 (ALPHA blockchain) configuration */
1707
- l1?: L1PaymentsModuleConfig;
1714
+ /** L1 (ALPHA blockchain) configuration. Set to null to explicitly disable L1. */
1715
+ l1?: L1PaymentsModuleConfig | null;
1708
1716
  }
1709
1717
  interface PaymentsModuleDependencies {
1710
1718
  identity: FullIdentity;
@@ -2710,6 +2718,7 @@ declare class Sphere {
2710
2718
  transport: TransportProvider;
2711
2719
  oracle: OracleProvider;
2712
2720
  tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
2721
+ l1?: L1Config;
2713
2722
  }): Promise<{
2714
2723
  success: boolean;
2715
2724
  mnemonic?: string;
@@ -2764,6 +2773,8 @@ declare class Sphere {
2764
2773
  tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
2765
2774
  /** Optional nametag to register */
2766
2775
  nametag?: string;
2776
+ /** L1 (ALPHA blockchain) configuration */
2777
+ l1?: L1Config;
2767
2778
  }): Promise<{
2768
2779
  success: boolean;
2769
2780
  sphere?: Sphere;
@@ -578,7 +578,7 @@ interface PaymentRequest {
578
578
  readonly coinId: string;
579
579
  /** Optional message/memo */
580
580
  readonly message?: string;
581
- /** Recipient nametag (who should pay) */
581
+ /** Where tokens should be sent */
582
582
  readonly recipientNametag?: string;
583
583
  /** Custom metadata */
584
584
  readonly metadata?: Record<string, unknown>;
@@ -605,7 +605,7 @@ interface IncomingPaymentRequest$1 {
605
605
  readonly symbol: string;
606
606
  /** Message from sender */
607
607
  readonly message?: string;
608
- /** Who this request is for (our nametag) */
608
+ /** Requester's nametag (where tokens should be sent) */
609
609
  readonly recipientNametag?: string;
610
610
  /** Original request ID from sender */
611
611
  readonly requestId: string;
@@ -1094,6 +1094,8 @@ interface IncomingPaymentRequest {
1094
1094
  id: string;
1095
1095
  /** Transport-specific pubkey of sender */
1096
1096
  senderTransportPubkey: string;
1097
+ /** Sender's nametag (if included in encrypted content) */
1098
+ senderNametag?: string;
1097
1099
  /** Parsed request data */
1098
1100
  request: {
1099
1101
  requestId: string;
@@ -1252,6 +1254,12 @@ declare class L1PaymentsModule {
1252
1254
  private _transport?;
1253
1255
  constructor(config?: L1PaymentsModuleConfig);
1254
1256
  initialize(deps: L1PaymentsModuleDependencies): Promise<void>;
1257
+ /**
1258
+ * Ensure the Fulcrum WebSocket is connected. Called lazily before any
1259
+ * operation that needs the network. If the singleton is already connected
1260
+ * (e.g. by the address scanner), this is a no-op.
1261
+ */
1262
+ private ensureConnected;
1255
1263
  destroy(): void;
1256
1264
  /**
1257
1265
  * Check if a string looks like an L1 address (alpha1... or alphat1...)
@@ -1261,7 +1269,7 @@ declare class L1PaymentsModule {
1261
1269
  * Resolve recipient to L1 address
1262
1270
  * Supports: L1 address (alpha1...), nametag (with or without @)
1263
1271
  */
1264
- private resolveL1Address;
1272
+ resolveL1Address(recipient: string): Promise<string>;
1265
1273
  /**
1266
1274
  * Resolve nametag to L1 address using transport provider
1267
1275
  */
@@ -1703,8 +1711,8 @@ interface PaymentsModuleConfig {
1703
1711
  maxRetries?: number;
1704
1712
  /** Enable debug logging */
1705
1713
  debug?: boolean;
1706
- /** L1 (ALPHA blockchain) configuration */
1707
- l1?: L1PaymentsModuleConfig;
1714
+ /** L1 (ALPHA blockchain) configuration. Set to null to explicitly disable L1. */
1715
+ l1?: L1PaymentsModuleConfig | null;
1708
1716
  }
1709
1717
  interface PaymentsModuleDependencies {
1710
1718
  identity: FullIdentity;
@@ -2710,6 +2718,7 @@ declare class Sphere {
2710
2718
  transport: TransportProvider;
2711
2719
  oracle: OracleProvider;
2712
2720
  tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
2721
+ l1?: L1Config;
2713
2722
  }): Promise<{
2714
2723
  success: boolean;
2715
2724
  mnemonic?: string;
@@ -2764,6 +2773,8 @@ declare class Sphere {
2764
2773
  tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
2765
2774
  /** Optional nametag to register */
2766
2775
  nametag?: string;
2776
+ /** L1 (ALPHA blockchain) configuration */
2777
+ l1?: L1Config;
2767
2778
  }): Promise<{
2768
2779
  success: boolean;
2769
2780
  sphere?: Sphere;
@@ -1461,7 +1461,7 @@ var L1PaymentsModule = class {
1461
1461
  _transport;
1462
1462
  constructor(config) {
1463
1463
  this._config = {
1464
- electrumUrl: config?.electrumUrl ?? "wss://fulcrum.alpha.unicity.network:50004",
1464
+ electrumUrl: config?.electrumUrl ?? "wss://fulcrum.unicity.network:50004",
1465
1465
  network: config?.network ?? "mainnet",
1466
1466
  defaultFeeRate: config?.defaultFeeRate ?? 10,
1467
1467
  enableVesting: config?.enableVesting ?? true
@@ -1493,10 +1493,17 @@ var L1PaymentsModule = class {
1493
1493
  });
1494
1494
  }
1495
1495
  }
1496
- if (this._config.electrumUrl) {
1496
+ this._initialized = true;
1497
+ }
1498
+ /**
1499
+ * Ensure the Fulcrum WebSocket is connected. Called lazily before any
1500
+ * operation that needs the network. If the singleton is already connected
1501
+ * (e.g. by the address scanner), this is a no-op.
1502
+ */
1503
+ async ensureConnected() {
1504
+ if (!isWebSocketConnected() && this._config.electrumUrl) {
1497
1505
  await connect(this._config.electrumUrl);
1498
1506
  }
1499
- this._initialized = true;
1500
1507
  }
1501
1508
  destroy() {
1502
1509
  if (isWebSocketConnected()) {
@@ -1554,6 +1561,7 @@ var L1PaymentsModule = class {
1554
1561
  }
1555
1562
  async send(request) {
1556
1563
  this.ensureInitialized();
1564
+ await this.ensureConnected();
1557
1565
  if (!this._wallet || !this._identity) {
1558
1566
  return { success: false, error: "No wallet available" };
1559
1567
  }
@@ -1588,6 +1596,7 @@ var L1PaymentsModule = class {
1588
1596
  }
1589
1597
  async getBalance() {
1590
1598
  this.ensureInitialized();
1599
+ await this.ensureConnected();
1591
1600
  const addresses = this._getWatchedAddresses();
1592
1601
  let totalAlpha = 0;
1593
1602
  let vestedSats = BigInt(0);
@@ -1619,6 +1628,7 @@ var L1PaymentsModule = class {
1619
1628
  }
1620
1629
  async getUtxos() {
1621
1630
  this.ensureInitialized();
1631
+ await this.ensureConnected();
1622
1632
  const result = [];
1623
1633
  const currentHeight = await getCurrentBlockHeight();
1624
1634
  const allUtxos = await this._getAllUtxos();
@@ -1654,42 +1664,73 @@ var L1PaymentsModule = class {
1654
1664
  return result;
1655
1665
  }
1656
1666
  async getHistory(limit) {
1667
+ await this.ensureConnected();
1657
1668
  this.ensureInitialized();
1658
1669
  const addresses = this._getWatchedAddresses();
1659
1670
  const transactions = [];
1660
1671
  const seenTxids = /* @__PURE__ */ new Set();
1661
1672
  const currentHeight = await getCurrentBlockHeight();
1673
+ const txCache = /* @__PURE__ */ new Map();
1674
+ const fetchTx = async (txid) => {
1675
+ if (txCache.has(txid)) return txCache.get(txid);
1676
+ const detail = await getTransaction(txid);
1677
+ txCache.set(txid, detail);
1678
+ return detail;
1679
+ };
1680
+ const addressSet = new Set(addresses.map((a) => a.toLowerCase()));
1662
1681
  for (const address of addresses) {
1663
1682
  const history = await getTransactionHistory(address);
1664
1683
  for (const item of history) {
1665
1684
  if (seenTxids.has(item.tx_hash)) continue;
1666
1685
  seenTxids.add(item.tx_hash);
1667
- const tx = await getTransaction(item.tx_hash);
1686
+ const tx = await fetchTx(item.tx_hash);
1668
1687
  if (!tx) continue;
1669
- const isSend = tx.vin?.some(
1670
- (vin) => addresses.includes(vin.txid ?? "")
1671
- );
1672
- let amount = "0";
1688
+ let isSend = false;
1689
+ for (const vin of tx.vin ?? []) {
1690
+ if (!vin.txid) continue;
1691
+ const prevTx = await fetchTx(vin.txid);
1692
+ if (prevTx?.vout?.[vin.vout]) {
1693
+ const prevOut = prevTx.vout[vin.vout];
1694
+ const prevAddrs = [
1695
+ ...prevOut.scriptPubKey?.addresses ?? [],
1696
+ ...prevOut.scriptPubKey?.address ? [prevOut.scriptPubKey.address] : []
1697
+ ];
1698
+ if (prevAddrs.some((a) => addressSet.has(a.toLowerCase()))) {
1699
+ isSend = true;
1700
+ break;
1701
+ }
1702
+ }
1703
+ }
1704
+ let amountToUs = 0;
1705
+ let amountToOthers = 0;
1673
1706
  let txAddress = address;
1707
+ let externalAddress = "";
1674
1708
  if (tx.vout) {
1675
1709
  for (const vout of tx.vout) {
1676
- const voutAddresses = vout.scriptPubKey?.addresses ?? [];
1677
- if (vout.scriptPubKey?.address) {
1678
- voutAddresses.push(vout.scriptPubKey.address);
1679
- }
1680
- const matchedAddr = voutAddresses.find((a) => addresses.includes(a));
1681
- if (matchedAddr) {
1682
- amount = Math.floor((vout.value ?? 0) * 1e8).toString();
1683
- txAddress = matchedAddr;
1684
- break;
1710
+ const voutAddresses = [
1711
+ ...vout.scriptPubKey?.addresses ?? [],
1712
+ ...vout.scriptPubKey?.address ? [vout.scriptPubKey.address] : []
1713
+ ];
1714
+ const isOurs = voutAddresses.some((a) => addressSet.has(a.toLowerCase()));
1715
+ const valueSats = Math.floor((vout.value ?? 0) * 1e8);
1716
+ if (isOurs) {
1717
+ amountToUs += valueSats;
1718
+ if (!txAddress) txAddress = voutAddresses[0];
1719
+ } else {
1720
+ amountToOthers += valueSats;
1721
+ if (!externalAddress && voutAddresses.length > 0) {
1722
+ externalAddress = voutAddresses[0];
1723
+ }
1685
1724
  }
1686
1725
  }
1687
1726
  }
1727
+ const amount = isSend ? amountToOthers.toString() : amountToUs.toString();
1728
+ const displayAddress = isSend ? externalAddress || txAddress : txAddress;
1688
1729
  transactions.push({
1689
1730
  txid: item.tx_hash,
1690
1731
  type: isSend ? "send" : "receive",
1691
1732
  amount,
1692
- address: txAddress,
1733
+ address: displayAddress,
1693
1734
  confirmations: item.height > 0 ? currentHeight - item.height : 0,
1694
1735
  timestamp: tx.time ? tx.time * 1e3 : Date.now(),
1695
1736
  blockHeight: item.height > 0 ? item.height : void 0
@@ -1701,6 +1742,7 @@ var L1PaymentsModule = class {
1701
1742
  }
1702
1743
  async getTransaction(txid) {
1703
1744
  this.ensureInitialized();
1745
+ await this.ensureConnected();
1704
1746
  const tx = await getTransaction(txid);
1705
1747
  if (!tx) return null;
1706
1748
  const addresses = this._getWatchedAddresses();
@@ -1736,6 +1778,7 @@ var L1PaymentsModule = class {
1736
1778
  }
1737
1779
  async estimateFee(to, amount) {
1738
1780
  this.ensureInitialized();
1781
+ await this.ensureConnected();
1739
1782
  if (!this._wallet) {
1740
1783
  return { fee: "0", feeRate: this._config.defaultFeeRate ?? 10 };
1741
1784
  }
@@ -2255,7 +2298,9 @@ var STORAGE_KEYS_GLOBAL = {
2255
2298
  /** Nametag cache per address (separate from tracked addresses registry) */
2256
2299
  ADDRESS_NAMETAGS: "address_nametags",
2257
2300
  /** Active addresses registry (JSON: TrackedAddressesStorage) */
2258
- TRACKED_ADDRESSES: "tracked_addresses"
2301
+ TRACKED_ADDRESSES: "tracked_addresses",
2302
+ /** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
2303
+ LAST_WALLET_EVENT_TS: "last_wallet_event_ts"
2259
2304
  };
2260
2305
  var STORAGE_KEYS_ADDRESS = {
2261
2306
  /** Pending transfers for this address */
@@ -3659,7 +3704,7 @@ async function parseTokenInfo(tokenData) {
3659
3704
  try {
3660
3705
  const sdkToken = await SdkToken2.fromJSON(data);
3661
3706
  if (sdkToken.id) {
3662
- defaultInfo.tokenId = sdkToken.id.toString();
3707
+ defaultInfo.tokenId = sdkToken.id.toJSON();
3663
3708
  }
3664
3709
  if (sdkToken.coins && sdkToken.coins.coins) {
3665
3710
  const rawCoins = sdkToken.coins.coins;
@@ -3950,8 +3995,7 @@ var PaymentsModule = class _PaymentsModule {
3950
3995
  maxRetries: config?.maxRetries ?? 3,
3951
3996
  debug: config?.debug ?? false
3952
3997
  };
3953
- const l1Enabled = config?.l1?.electrumUrl && config.l1.electrumUrl.length > 0;
3954
- this.l1 = l1Enabled ? new L1PaymentsModule(config?.l1) : null;
3998
+ this.l1 = config?.l1 === null ? null : new L1PaymentsModule(config?.l1);
3955
3999
  }
3956
4000
  /** Get module configuration */
3957
4001
  getConfig() {
@@ -4613,13 +4657,16 @@ var PaymentsModule = class _PaymentsModule {
4613
4657
  if (this.paymentRequests.find((r) => r.id === transportRequest.id)) {
4614
4658
  return;
4615
4659
  }
4660
+ const coinId = transportRequest.request.coinId;
4661
+ const registry = TokenRegistry.getInstance();
4662
+ const coinDef = registry.getDefinition(coinId);
4616
4663
  const request = {
4617
4664
  id: transportRequest.id,
4618
4665
  senderPubkey: transportRequest.senderTransportPubkey,
4666
+ senderNametag: transportRequest.senderNametag,
4619
4667
  amount: transportRequest.request.amount,
4620
- coinId: transportRequest.request.coinId,
4621
- symbol: transportRequest.request.coinId,
4622
- // Use coinId as symbol for now
4668
+ coinId,
4669
+ symbol: coinDef?.symbol || coinId.slice(0, 8),
4623
4670
  message: transportRequest.request.message,
4624
4671
  recipientNametag: transportRequest.request.recipientNametag,
4625
4672
  requestId: transportRequest.request.requestId,
@@ -7638,8 +7685,8 @@ var Sphere = class _Sphere {
7638
7685
  if (options.nametag) {
7639
7686
  await sphere.registerNametag(options.nametag);
7640
7687
  } else {
7641
- await sphere.syncIdentityWithTransport();
7642
7688
  await sphere.recoverNametagFromTransport();
7689
+ await sphere.syncIdentityWithTransport();
7643
7690
  }
7644
7691
  return sphere;
7645
7692
  }
@@ -7686,9 +7733,14 @@ var Sphere = class _Sphere {
7686
7733
  if (!options.mnemonic && !options.masterKey) {
7687
7734
  throw new Error("Either mnemonic or masterKey is required");
7688
7735
  }
7736
+ console.log("[Sphere.import] Starting import...");
7737
+ console.log("[Sphere.import] Clearing existing wallet data...");
7689
7738
  await _Sphere.clear({ storage: options.storage, tokenStorage: options.tokenStorage });
7739
+ console.log("[Sphere.import] Clear done");
7690
7740
  if (!options.storage.isConnected()) {
7741
+ console.log("[Sphere.import] Reconnecting storage...");
7691
7742
  await options.storage.connect();
7743
+ console.log("[Sphere.import] Storage reconnected");
7692
7744
  }
7693
7745
  const sphere = new _Sphere(
7694
7746
  options.storage,
@@ -7702,9 +7754,12 @@ var Sphere = class _Sphere {
7702
7754
  if (!_Sphere.validateMnemonic(options.mnemonic)) {
7703
7755
  throw new Error("Invalid mnemonic");
7704
7756
  }
7757
+ console.log("[Sphere.import] Storing mnemonic...");
7705
7758
  await sphere.storeMnemonic(options.mnemonic, options.derivationPath, options.basePath);
7759
+ console.log("[Sphere.import] Initializing identity from mnemonic...");
7706
7760
  await sphere.initializeIdentityFromMnemonic(options.mnemonic, options.derivationPath);
7707
7761
  } else if (options.masterKey) {
7762
+ console.log("[Sphere.import] Storing master key...");
7708
7763
  await sphere.storeMasterKey(
7709
7764
  options.masterKey,
7710
7765
  options.chainCode,
@@ -7712,24 +7767,35 @@ var Sphere = class _Sphere {
7712
7767
  options.basePath,
7713
7768
  options.derivationMode
7714
7769
  );
7770
+ console.log("[Sphere.import] Initializing identity from master key...");
7715
7771
  await sphere.initializeIdentityFromMasterKey(
7716
7772
  options.masterKey,
7717
7773
  options.chainCode,
7718
7774
  options.derivationPath
7719
7775
  );
7720
7776
  }
7777
+ console.log("[Sphere.import] Initializing providers...");
7721
7778
  await sphere.initializeProviders();
7779
+ console.log("[Sphere.import] Providers initialized. Initializing modules...");
7722
7780
  await sphere.initializeModules();
7781
+ console.log("[Sphere.import] Modules initialized");
7723
7782
  if (!options.nametag) {
7783
+ console.log("[Sphere.import] Recovering nametag from transport...");
7724
7784
  await sphere.recoverNametagFromTransport();
7785
+ console.log("[Sphere.import] Nametag recovery done");
7786
+ await sphere.syncIdentityWithTransport();
7725
7787
  }
7788
+ console.log("[Sphere.import] Finalizing wallet creation...");
7726
7789
  await sphere.finalizeWalletCreation();
7727
7790
  sphere._initialized = true;
7728
7791
  _Sphere.instance = sphere;
7792
+ console.log("[Sphere.import] Tracking address 0...");
7729
7793
  await sphere.ensureAddressTracked(0);
7730
7794
  if (options.nametag) {
7795
+ console.log("[Sphere.import] Registering nametag...");
7731
7796
  await sphere.registerNametag(options.nametag);
7732
7797
  }
7798
+ console.log("[Sphere.import] Import complete");
7733
7799
  return sphere;
7734
7800
  }
7735
7801
  /**
@@ -7754,6 +7820,10 @@ var Sphere = class _Sphere {
7754
7820
  static async clear(storageOrOptions) {
7755
7821
  const storage = "get" in storageOrOptions ? storageOrOptions : storageOrOptions.storage;
7756
7822
  const tokenStorage = "get" in storageOrOptions ? void 0 : storageOrOptions.tokenStorage;
7823
+ if (!storage.isConnected()) {
7824
+ await storage.connect();
7825
+ }
7826
+ console.log("[Sphere.clear] Removing storage keys...");
7757
7827
  await storage.remove(STORAGE_KEYS_GLOBAL.MNEMONIC);
7758
7828
  await storage.remove(STORAGE_KEYS_GLOBAL.MASTER_KEY);
7759
7829
  await storage.remove(STORAGE_KEYS_GLOBAL.CHAIN_CODE);
@@ -7766,12 +7836,30 @@ var Sphere = class _Sphere {
7766
7836
  await storage.remove(STORAGE_KEYS_GLOBAL.ADDRESS_NAMETAGS);
7767
7837
  await storage.remove(STORAGE_KEYS_ADDRESS.PENDING_TRANSFERS);
7768
7838
  await storage.remove(STORAGE_KEYS_ADDRESS.OUTBOX);
7839
+ console.log("[Sphere.clear] Storage keys removed");
7769
7840
  if (tokenStorage?.clear) {
7770
- await tokenStorage.clear();
7841
+ console.log("[Sphere.clear] Clearing token storage...");
7842
+ try {
7843
+ await Promise.race([
7844
+ tokenStorage.clear(),
7845
+ new Promise(
7846
+ (_, reject) => setTimeout(() => reject(new Error("tokenStorage.clear() timed out after 2s")), 2e3)
7847
+ )
7848
+ ]);
7849
+ console.log("[Sphere.clear] Token storage cleared");
7850
+ } catch (err) {
7851
+ console.warn("[Sphere.clear] Token storage clear failed/timed out:", err);
7852
+ }
7771
7853
  }
7854
+ console.log("[Sphere.clear] Destroying vesting classifier...");
7772
7855
  await vestingClassifier.destroy();
7856
+ console.log("[Sphere.clear] Vesting classifier destroyed");
7773
7857
  if (_Sphere.instance) {
7858
+ console.log("[Sphere.clear] Destroying Sphere instance...");
7774
7859
  await _Sphere.instance.destroy();
7860
+ console.log("[Sphere.clear] Sphere instance destroyed");
7861
+ } else {
7862
+ console.log("[Sphere.clear] No Sphere instance to destroy");
7775
7863
  }
7776
7864
  }
7777
7865
  /**
@@ -8152,7 +8240,8 @@ var Sphere = class _Sphere {
8152
8240
  storage: options.storage,
8153
8241
  transport: options.transport,
8154
8242
  oracle: options.oracle,
8155
- tokenStorage: options.tokenStorage
8243
+ tokenStorage: options.tokenStorage,
8244
+ l1: options.l1
8156
8245
  });
8157
8246
  return { success: true, mnemonic };
8158
8247
  }
@@ -8165,7 +8254,8 @@ var Sphere = class _Sphere {
8165
8254
  storage: options.storage,
8166
8255
  transport: options.transport,
8167
8256
  oracle: options.oracle,
8168
- tokenStorage: options.tokenStorage
8257
+ tokenStorage: options.tokenStorage,
8258
+ l1: options.l1
8169
8259
  });
8170
8260
  return { success: true };
8171
8261
  }
@@ -8224,7 +8314,8 @@ var Sphere = class _Sphere {
8224
8314
  transport: options.transport,
8225
8315
  oracle: options.oracle,
8226
8316
  tokenStorage: options.tokenStorage,
8227
- nametag: options.nametag
8317
+ nametag: options.nametag,
8318
+ l1: options.l1
8228
8319
  });
8229
8320
  return { success: true, sphere, mnemonic };
8230
8321
  }
@@ -8253,7 +8344,8 @@ var Sphere = class _Sphere {
8253
8344
  transport: options.transport,
8254
8345
  oracle: options.oracle,
8255
8346
  tokenStorage: options.tokenStorage,
8256
- nametag: options.nametag
8347
+ nametag: options.nametag,
8348
+ l1: options.l1
8257
8349
  });
8258
8350
  return { success: true, sphere };
8259
8351
  }
@@ -8284,7 +8376,8 @@ var Sphere = class _Sphere {
8284
8376
  transport: options.transport,
8285
8377
  oracle: options.oracle,
8286
8378
  tokenStorage: options.tokenStorage,
8287
- nametag: options.nametag
8379
+ nametag: options.nametag,
8380
+ l1: options.l1
8288
8381
  });
8289
8382
  return { success: true, sphere };
8290
8383
  }
@@ -8303,7 +8396,8 @@ var Sphere = class _Sphere {
8303
8396
  storage: options.storage,
8304
8397
  transport: options.transport,
8305
8398
  oracle: options.oracle,
8306
- tokenStorage: options.tokenStorage
8399
+ tokenStorage: options.tokenStorage,
8400
+ l1: options.l1
8307
8401
  });
8308
8402
  if (result.success) {
8309
8403
  const sphere2 = _Sphere.getInstance();
@@ -8352,7 +8446,8 @@ var Sphere = class _Sphere {
8352
8446
  transport: options.transport,
8353
8447
  oracle: options.oracle,
8354
8448
  tokenStorage: options.tokenStorage,
8355
- nametag: options.nametag
8449
+ nametag: options.nametag,
8450
+ l1: options.l1
8356
8451
  });
8357
8452
  return { success: true, sphere: sphere2, mnemonic };
8358
8453
  }
@@ -8365,7 +8460,8 @@ var Sphere = class _Sphere {
8365
8460
  transport: options.transport,
8366
8461
  oracle: options.oracle,
8367
8462
  tokenStorage: options.tokenStorage,
8368
- nametag: options.nametag
8463
+ nametag: options.nametag,
8464
+ l1: options.l1
8369
8465
  });
8370
8466
  return { success: true, sphere };
8371
8467
  }
@@ -9228,35 +9324,40 @@ var Sphere = class _Sphere {
9228
9324
  if (this._identity?.nametag) {
9229
9325
  return;
9230
9326
  }
9231
- if (!this._transport.recoverNametag) {
9327
+ let recoveredNametag = null;
9328
+ if (this._transport.recoverNametag) {
9329
+ try {
9330
+ recoveredNametag = await this._transport.recoverNametag();
9331
+ } catch {
9332
+ }
9333
+ }
9334
+ if (!recoveredNametag && this._transport.resolveAddressInfo && this._identity?.l1Address) {
9335
+ try {
9336
+ const info = await this._transport.resolveAddressInfo(this._identity.l1Address);
9337
+ if (info?.nametag) {
9338
+ recoveredNametag = info.nametag;
9339
+ }
9340
+ } catch {
9341
+ }
9342
+ }
9343
+ if (!recoveredNametag) {
9232
9344
  return;
9233
9345
  }
9234
9346
  try {
9235
- const recoveredNametag = await this._transport.recoverNametag();
9236
- if (recoveredNametag) {
9237
- if (this._identity) {
9238
- this._identity.nametag = recoveredNametag;
9239
- await this._updateCachedProxyAddress();
9240
- }
9241
- const entry = await this.ensureAddressTracked(this._currentAddressIndex);
9242
- let nametags = this._addressNametags.get(entry.addressId);
9243
- if (!nametags) {
9244
- nametags = /* @__PURE__ */ new Map();
9245
- this._addressNametags.set(entry.addressId, nametags);
9246
- }
9247
- const nextIndex = nametags.size;
9248
- nametags.set(nextIndex, recoveredNametag);
9249
- await this.persistAddressNametags();
9250
- if (this._transport.publishIdentityBinding) {
9251
- await this._transport.publishIdentityBinding(
9252
- this._identity.chainPubkey,
9253
- this._identity.l1Address,
9254
- this._identity.directAddress || "",
9255
- recoveredNametag
9256
- );
9257
- }
9258
- this.emitEvent("nametag:recovered", { nametag: recoveredNametag });
9347
+ if (this._identity) {
9348
+ this._identity.nametag = recoveredNametag;
9349
+ await this._updateCachedProxyAddress();
9259
9350
  }
9351
+ const entry = await this.ensureAddressTracked(this._currentAddressIndex);
9352
+ let nametags = this._addressNametags.get(entry.addressId);
9353
+ if (!nametags) {
9354
+ nametags = /* @__PURE__ */ new Map();
9355
+ this._addressNametags.set(entry.addressId, nametags);
9356
+ }
9357
+ const nextIndex = nametags.size;
9358
+ nametags.set(nextIndex, recoveredNametag);
9359
+ await this.persistAddressNametags();
9360
+ this.emitEvent("nametag:recovered", { nametag: recoveredNametag });
9260
9361
  } catch {
9261
9362
  }
9262
9363
  }
@@ -9465,8 +9566,12 @@ var Sphere = class _Sphere {
9465
9566
  for (const provider of this._tokenStorageProviders.values()) {
9466
9567
  provider.setIdentity(this._identity);
9467
9568
  }
9468
- await this._storage.connect();
9469
- await this._transport.connect();
9569
+ if (!this._storage.isConnected()) {
9570
+ await this._storage.connect();
9571
+ }
9572
+ if (!this._transport.isConnected()) {
9573
+ await this._transport.connect();
9574
+ }
9470
9575
  await this._oracle.initialize();
9471
9576
  for (const provider of this._tokenStorageProviders.values()) {
9472
9577
  await provider.initialize();