@unicitylabs/sphere-sdk 0.2.0 → 0.2.1

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.
@@ -3957,6 +3957,19 @@ var PaymentsModule = class _PaymentsModule {
3957
3957
  * Initialize module with dependencies
3958
3958
  */
3959
3959
  initialize(deps) {
3960
+ this.unsubscribeTransfers?.();
3961
+ this.unsubscribeTransfers = null;
3962
+ this.unsubscribePaymentRequests?.();
3963
+ this.unsubscribePaymentRequests = null;
3964
+ this.unsubscribePaymentRequestResponses?.();
3965
+ this.unsubscribePaymentRequestResponses = null;
3966
+ this.tokens.clear();
3967
+ this.pendingTransfers.clear();
3968
+ this.tombstones = [];
3969
+ this.archivedTokens.clear();
3970
+ this.forkedTokens.clear();
3971
+ this.transactionHistory = [];
3972
+ this.nametag = null;
3960
3973
  this.deps = deps;
3961
3974
  this.priceProvider = deps.price ?? null;
3962
3975
  if (this.l1) {
@@ -7481,10 +7494,10 @@ var Sphere = class _Sphere {
7481
7494
  sphere._initialized = true;
7482
7495
  _Sphere.instance = sphere;
7483
7496
  await sphere.ensureAddressTracked(0);
7484
- await sphere.syncIdentityWithTransport();
7485
7497
  if (options.nametag) {
7486
7498
  await sphere.registerNametag(options.nametag);
7487
7499
  } else {
7500
+ await sphere.syncIdentityWithTransport();
7488
7501
  await sphere.recoverNametagFromTransport();
7489
7502
  }
7490
7503
  return sphere;
@@ -8338,7 +8351,7 @@ var Sphere = class _Sphere {
8338
8351
  * await sphere.switchToAddress(0);
8339
8352
  * ```
8340
8353
  */
8341
- async switchToAddress(index) {
8354
+ async switchToAddress(index, options) {
8342
8355
  this.ensureReady();
8343
8356
  if (!this._masterKey) {
8344
8357
  throw new Error("HD derivation requires master key with chain code. Cannot switch addresses.");
@@ -8346,11 +8359,27 @@ var Sphere = class _Sphere {
8346
8359
  if (index < 0) {
8347
8360
  throw new Error("Address index must be non-negative");
8348
8361
  }
8362
+ const newNametag = options?.nametag?.startsWith("@") ? options.nametag.slice(1) : options?.nametag;
8363
+ if (newNametag && !this.validateNametag(newNametag)) {
8364
+ throw new Error("Invalid nametag format. Use alphanumeric characters, 3-20 chars.");
8365
+ }
8349
8366
  const addressInfo = this.deriveAddress(index, false);
8350
8367
  const ipnsHash = sha256(addressInfo.publicKey, "hex").slice(0, 40);
8351
8368
  const predicateAddress = await deriveL3PredicateAddress(addressInfo.privateKey);
8352
8369
  await this.ensureAddressTracked(index);
8353
8370
  const addressId = getAddressId(predicateAddress);
8371
+ if (newNametag) {
8372
+ const existing = await this._transport.resolveNametag?.(newNametag);
8373
+ if (existing) {
8374
+ throw new Error(`Nametag @${newNametag} is already taken`);
8375
+ }
8376
+ let nametags = this._addressNametags.get(addressId);
8377
+ if (!nametags) {
8378
+ nametags = /* @__PURE__ */ new Map();
8379
+ this._addressNametags.set(addressId, nametags);
8380
+ }
8381
+ nametags.set(0, newNametag);
8382
+ }
8354
8383
  const nametag = this._addressNametags.get(addressId)?.get(0);
8355
8384
  this._identity = {
8356
8385
  privateKey: addressInfo.privateKey,
@@ -8364,12 +8393,47 @@ var Sphere = class _Sphere {
8364
8393
  await this._updateCachedProxyAddress();
8365
8394
  await this._storage.set(STORAGE_KEYS_GLOBAL.CURRENT_ADDRESS_INDEX, index.toString());
8366
8395
  this._storage.setIdentity(this._identity);
8367
- this._transport.setIdentity(this._identity);
8396
+ await this._transport.setIdentity(this._identity);
8368
8397
  for (const provider of this._tokenStorageProviders.values()) {
8369
8398
  provider.setIdentity(this._identity);
8399
+ await provider.initialize();
8370
8400
  }
8371
8401
  await this.reinitializeModulesForNewAddress();
8372
- await this.syncIdentityWithTransport();
8402
+ if (this._identity.nametag) {
8403
+ await this.syncIdentityWithTransport();
8404
+ }
8405
+ if (newNametag) {
8406
+ await this.persistAddressNametags();
8407
+ if (!this._payments.hasNametag()) {
8408
+ console.log(`[Sphere] Minting nametag token for @${newNametag}...`);
8409
+ try {
8410
+ const result = await this.mintNametag(newNametag);
8411
+ if (result.success) {
8412
+ console.log(`[Sphere] Nametag token minted successfully`);
8413
+ } else {
8414
+ console.warn(`[Sphere] Could not mint nametag token: ${result.error}`);
8415
+ }
8416
+ } catch (err) {
8417
+ console.warn(`[Sphere] Nametag token mint failed:`, err);
8418
+ }
8419
+ }
8420
+ this.emitEvent("nametag:registered", {
8421
+ nametag: newNametag,
8422
+ addressIndex: index
8423
+ });
8424
+ } else if (this._identity.nametag && !this._payments.hasNametag()) {
8425
+ console.log(`[Sphere] Nametag @${this._identity.nametag} has no token after switch, minting...`);
8426
+ try {
8427
+ const result = await this.mintNametag(this._identity.nametag);
8428
+ if (result.success) {
8429
+ console.log(`[Sphere] Nametag token minted successfully after switch`);
8430
+ } else {
8431
+ console.warn(`[Sphere] Could not mint nametag token after switch: ${result.error}`);
8432
+ }
8433
+ } catch (err) {
8434
+ console.warn(`[Sphere] Nametag token mint failed after switch:`, err);
8435
+ }
8436
+ }
8373
8437
  this.emitEvent("identity:changed", {
8374
8438
  l1Address: this._identity.l1Address,
8375
8439
  directAddress: this._identity.directAddress,
@@ -9120,7 +9184,7 @@ var Sphere = class _Sphere {
9120
9184
  // ===========================================================================
9121
9185
  async initializeProviders() {
9122
9186
  this._storage.setIdentity(this._identity);
9123
- this._transport.setIdentity(this._identity);
9187
+ await this._transport.setIdentity(this._identity);
9124
9188
  for (const provider of this._tokenStorageProviders.values()) {
9125
9189
  provider.setIdentity(this._identity);
9126
9190
  }