@unicitylabs/sphere-sdk 0.1.7 → 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.
@@ -3813,6 +3813,34 @@ var PaymentsModule = class {
3813
3813
  }
3814
3814
  return Array.from(balances.values());
3815
3815
  }
3816
+ /**
3817
+ * Get aggregated assets (tokens grouped by coinId)
3818
+ * Only includes confirmed tokens
3819
+ */
3820
+ getAssets(coinId) {
3821
+ const assets = /* @__PURE__ */ new Map();
3822
+ for (const token of this.tokens.values()) {
3823
+ if (token.status !== "confirmed") continue;
3824
+ if (coinId && token.coinId !== coinId) continue;
3825
+ const key = token.coinId;
3826
+ const existing = assets.get(key);
3827
+ if (existing) {
3828
+ existing.totalAmount = (BigInt(existing.totalAmount) + BigInt(token.amount)).toString();
3829
+ existing.tokenCount++;
3830
+ } else {
3831
+ assets.set(key, {
3832
+ coinId: token.coinId,
3833
+ symbol: token.symbol,
3834
+ name: token.name,
3835
+ decimals: token.decimals,
3836
+ iconUrl: token.iconUrl,
3837
+ totalAmount: token.amount,
3838
+ tokenCount: 1
3839
+ });
3840
+ }
3841
+ }
3842
+ return Array.from(assets.values());
3843
+ }
3816
3844
  /**
3817
3845
  * Get all tokens
3818
3846
  */
@@ -5914,7 +5942,6 @@ var import_SigningService2 = require("@unicitylabs/state-transition-sdk/lib/sign
5914
5942
  var import_TokenType2 = require("@unicitylabs/state-transition-sdk/lib/token/TokenType");
5915
5943
  var import_HashAlgorithm4 = require("@unicitylabs/state-transition-sdk/lib/hash/HashAlgorithm");
5916
5944
  var import_UnmaskedPredicateReference2 = require("@unicitylabs/state-transition-sdk/lib/predicate/embedded/UnmaskedPredicateReference");
5917
- var import_nostr_js_sdk = require("@unicitylabs/nostr-js-sdk");
5918
5945
  var UNICITY_TOKEN_TYPE_HEX2 = "f8aa13834268d29355ff12183066f0cb902003629bbc5eb9ef0efbe397867509";
5919
5946
  async function deriveL3PredicateAddress(privateKey) {
5920
5947
  const secret = Buffer.from(privateKey, "hex");
@@ -5943,6 +5970,8 @@ var Sphere = class _Sphere {
5943
5970
  _currentAddressIndex = 0;
5944
5971
  /** Map of addressId -> (nametagIndex -> nametag). Supports multiple nametags per address (e.g., from Nostr recovery) */
5945
5972
  _addressNametags = /* @__PURE__ */ new Map();
5973
+ /** Cached PROXY address (computed once when nametag is set) */
5974
+ _cachedProxyAddress = void 0;
5946
5975
  // Providers
5947
5976
  _storage;
5948
5977
  _tokenStorageProviders = /* @__PURE__ */ new Map();
@@ -6107,7 +6136,7 @@ var Sphere = class _Sphere {
6107
6136
  if (!options.mnemonic && !options.masterKey) {
6108
6137
  throw new Error("Either mnemonic or masterKey is required");
6109
6138
  }
6110
- await _Sphere.clear(options.storage);
6139
+ await _Sphere.clear({ storage: options.storage, tokenStorage: options.tokenStorage });
6111
6140
  const sphere = new _Sphere(
6112
6141
  options.storage,
6113
6142
  options.transport,
@@ -6149,10 +6178,27 @@ var Sphere = class _Sphere {
6149
6178
  return sphere;
6150
6179
  }
6151
6180
  /**
6152
- * Clear wallet data from storage
6153
- * 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);
6154
6198
  */
6155
- 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;
6156
6202
  await storage.remove(STORAGE_KEYS_GLOBAL.MNEMONIC);
6157
6203
  await storage.remove(STORAGE_KEYS_GLOBAL.MASTER_KEY);
6158
6204
  await storage.remove(STORAGE_KEYS_GLOBAL.CHAIN_CODE);
@@ -6164,6 +6210,9 @@ var Sphere = class _Sphere {
6164
6210
  await storage.remove(STORAGE_KEYS_GLOBAL.ADDRESS_NAMETAGS);
6165
6211
  await storage.remove(STORAGE_KEYS_ADDRESS.PENDING_TRANSFERS);
6166
6212
  await storage.remove(STORAGE_KEYS_ADDRESS.OUTBOX);
6213
+ if (tokenStorage?.clear) {
6214
+ await tokenStorage.clear();
6215
+ }
6167
6216
  if (_Sphere.instance) {
6168
6217
  await _Sphere.instance.destroy();
6169
6218
  }
@@ -6847,6 +6896,7 @@ var Sphere = class _Sphere {
6847
6896
  nametag
6848
6897
  };
6849
6898
  this._currentAddressIndex = index;
6899
+ await this._updateCachedProxyAddress();
6850
6900
  await this._storage.set(STORAGE_KEYS_GLOBAL.CURRENT_ADDRESS_INDEX, index.toString());
6851
6901
  this._storage.setIdentity(this._identity);
6852
6902
  this._transport.setIdentity(this._identity);
@@ -7044,9 +7094,18 @@ var Sphere = class _Sphere {
7044
7094
  * @returns PROXY address string or undefined if no nametag
7045
7095
  */
7046
7096
  getProxyAddress() {
7097
+ return this._cachedProxyAddress;
7098
+ }
7099
+ /** Compute and cache the PROXY address from the current nametag */
7100
+ async _updateCachedProxyAddress() {
7047
7101
  const nametag = this._identity?.nametag;
7048
- if (!nametag) return void 0;
7049
- 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();
7050
7109
  }
7051
7110
  /**
7052
7111
  * Register a nametag for the current active address
@@ -7086,6 +7145,7 @@ var Sphere = class _Sphere {
7086
7145
  }
7087
7146
  }
7088
7147
  this._identity.nametag = cleanNametag;
7148
+ await this._updateCachedProxyAddress();
7089
7149
  const addressId = this.getCurrentAddressId();
7090
7150
  if (addressId) {
7091
7151
  let nametagsMap = this._addressNametags.get(addressId);
@@ -7226,6 +7286,7 @@ var Sphere = class _Sphere {
7226
7286
  if (recoveredNametag) {
7227
7287
  if (this._identity) {
7228
7288
  this._identity.nametag = recoveredNametag;
7289
+ await this._updateCachedProxyAddress();
7229
7290
  }
7230
7291
  const addressId = this.getCurrentAddressId();
7231
7292
  if (addressId) {
@@ -7393,6 +7454,7 @@ var Sphere = class _Sphere {
7393
7454
  this._identity.nametag = nametag;
7394
7455
  }
7395
7456
  }
7457
+ await this._updateCachedProxyAddress();
7396
7458
  }
7397
7459
  async initializeIdentityFromMnemonic(mnemonic, derivationPath) {
7398
7460
  const basePath = derivationPath ?? DEFAULT_BASE_PATH;