@unicitylabs/sphere-sdk 0.1.8 → 0.1.9

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.
@@ -5942,7 +5942,6 @@ var import_SigningService2 = require("@unicitylabs/state-transition-sdk/lib/sign
5942
5942
  var import_TokenType2 = require("@unicitylabs/state-transition-sdk/lib/token/TokenType");
5943
5943
  var import_HashAlgorithm4 = require("@unicitylabs/state-transition-sdk/lib/hash/HashAlgorithm");
5944
5944
  var import_UnmaskedPredicateReference2 = require("@unicitylabs/state-transition-sdk/lib/predicate/embedded/UnmaskedPredicateReference");
5945
- var import_nostr_js_sdk = require("@unicitylabs/nostr-js-sdk");
5946
5945
  var UNICITY_TOKEN_TYPE_HEX2 = "f8aa13834268d29355ff12183066f0cb902003629bbc5eb9ef0efbe397867509";
5947
5946
  async function deriveL3PredicateAddress(privateKey) {
5948
5947
  const secret = Buffer.from(privateKey, "hex");
@@ -5971,6 +5970,8 @@ var Sphere = class _Sphere {
5971
5970
  _currentAddressIndex = 0;
5972
5971
  /** Map of addressId -> (nametagIndex -> nametag). Supports multiple nametags per address (e.g., from Nostr recovery) */
5973
5972
  _addressNametags = /* @__PURE__ */ new Map();
5973
+ /** Cached PROXY address (computed once when nametag is set) */
5974
+ _cachedProxyAddress = void 0;
5974
5975
  // Providers
5975
5976
  _storage;
5976
5977
  _tokenStorageProviders = /* @__PURE__ */ new Map();
@@ -6135,7 +6136,7 @@ var Sphere = class _Sphere {
6135
6136
  if (!options.mnemonic && !options.masterKey) {
6136
6137
  throw new Error("Either mnemonic or masterKey is required");
6137
6138
  }
6138
- await _Sphere.clear(options.storage);
6139
+ await _Sphere.clear({ storage: options.storage, tokenStorage: options.tokenStorage });
6139
6140
  const sphere = new _Sphere(
6140
6141
  options.storage,
6141
6142
  options.transport,
@@ -6177,10 +6178,27 @@ var Sphere = class _Sphere {
6177
6178
  return sphere;
6178
6179
  }
6179
6180
  /**
6180
- * Clear wallet data from storage
6181
- * Note: Token data is cleared via TokenStorageProvider, not here
6181
+ * Clear all SDK-owned wallet data from storage.
6182
+ *
6183
+ * Removes wallet keys, per-address data, and optionally token storage.
6184
+ * Does NOT affect application-level data stored outside the SDK.
6185
+ *
6186
+ * @param storageOrOptions - StorageProvider (backward compatible) or options object
6187
+ *
6188
+ * @example
6189
+ * // New usage (recommended) - clears wallet keys AND token data
6190
+ * await Sphere.clear({
6191
+ * storage: providers.storage,
6192
+ * tokenStorage: providers.tokenStorage,
6193
+ * });
6194
+ *
6195
+ * @example
6196
+ * // Legacy usage - clears only wallet keys
6197
+ * await Sphere.clear(storage);
6182
6198
  */
6183
- static async clear(storage) {
6199
+ static async clear(storageOrOptions) {
6200
+ const storage = "get" in storageOrOptions ? storageOrOptions : storageOrOptions.storage;
6201
+ const tokenStorage = "get" in storageOrOptions ? void 0 : storageOrOptions.tokenStorage;
6184
6202
  await storage.remove(STORAGE_KEYS_GLOBAL.MNEMONIC);
6185
6203
  await storage.remove(STORAGE_KEYS_GLOBAL.MASTER_KEY);
6186
6204
  await storage.remove(STORAGE_KEYS_GLOBAL.CHAIN_CODE);
@@ -6192,6 +6210,9 @@ var Sphere = class _Sphere {
6192
6210
  await storage.remove(STORAGE_KEYS_GLOBAL.ADDRESS_NAMETAGS);
6193
6211
  await storage.remove(STORAGE_KEYS_ADDRESS.PENDING_TRANSFERS);
6194
6212
  await storage.remove(STORAGE_KEYS_ADDRESS.OUTBOX);
6213
+ if (tokenStorage?.clear) {
6214
+ await tokenStorage.clear();
6215
+ }
6195
6216
  if (_Sphere.instance) {
6196
6217
  await _Sphere.instance.destroy();
6197
6218
  }
@@ -6875,6 +6896,7 @@ var Sphere = class _Sphere {
6875
6896
  nametag
6876
6897
  };
6877
6898
  this._currentAddressIndex = index;
6899
+ await this._updateCachedProxyAddress();
6878
6900
  await this._storage.set(STORAGE_KEYS_GLOBAL.CURRENT_ADDRESS_INDEX, index.toString());
6879
6901
  this._storage.setIdentity(this._identity);
6880
6902
  this._transport.setIdentity(this._identity);
@@ -7072,9 +7094,18 @@ var Sphere = class _Sphere {
7072
7094
  * @returns PROXY address string or undefined if no nametag
7073
7095
  */
7074
7096
  getProxyAddress() {
7097
+ return this._cachedProxyAddress;
7098
+ }
7099
+ /** Compute and cache the PROXY address from the current nametag */
7100
+ async _updateCachedProxyAddress() {
7075
7101
  const nametag = this._identity?.nametag;
7076
- if (!nametag) return void 0;
7077
- return `PROXY:${(0, import_nostr_js_sdk.hashNametag)(nametag)}`;
7102
+ if (!nametag) {
7103
+ this._cachedProxyAddress = void 0;
7104
+ return;
7105
+ }
7106
+ const { ProxyAddress } = await import("@unicitylabs/state-transition-sdk/lib/address/ProxyAddress");
7107
+ const proxyAddr = await ProxyAddress.fromNameTag(nametag);
7108
+ this._cachedProxyAddress = proxyAddr.toString();
7078
7109
  }
7079
7110
  /**
7080
7111
  * Register a nametag for the current active address
@@ -7114,6 +7145,7 @@ var Sphere = class _Sphere {
7114
7145
  }
7115
7146
  }
7116
7147
  this._identity.nametag = cleanNametag;
7148
+ await this._updateCachedProxyAddress();
7117
7149
  const addressId = this.getCurrentAddressId();
7118
7150
  if (addressId) {
7119
7151
  let nametagsMap = this._addressNametags.get(addressId);
@@ -7254,6 +7286,7 @@ var Sphere = class _Sphere {
7254
7286
  if (recoveredNametag) {
7255
7287
  if (this._identity) {
7256
7288
  this._identity.nametag = recoveredNametag;
7289
+ await this._updateCachedProxyAddress();
7257
7290
  }
7258
7291
  const addressId = this.getCurrentAddressId();
7259
7292
  if (addressId) {
@@ -7421,6 +7454,7 @@ var Sphere = class _Sphere {
7421
7454
  this._identity.nametag = nametag;
7422
7455
  }
7423
7456
  }
7457
+ await this._updateCachedProxyAddress();
7424
7458
  }
7425
7459
  async initializeIdentityFromMnemonic(mnemonic, derivationPath) {
7426
7460
  const basePath = derivationPath ?? DEFAULT_BASE_PATH;