@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.
package/dist/index.d.cts CHANGED
@@ -1210,9 +1210,10 @@ interface WalletJSONExportOptions$1 {
1210
1210
  */
1211
1211
  interface TransportProvider extends BaseProvider {
1212
1212
  /**
1213
- * Set identity for signing/encryption
1213
+ * Set identity for signing/encryption.
1214
+ * If the transport is already connected, reconnects with the new identity.
1214
1215
  */
1215
- setIdentity(identity: FullIdentity): void;
1216
+ setIdentity(identity: FullIdentity): void | Promise<void>;
1216
1217
  /**
1217
1218
  * Send encrypted direct message
1218
1219
  * @param recipientTransportPubkey - Transport-specific pubkey for messaging
@@ -3393,7 +3394,9 @@ declare class Sphere {
3393
3394
  * await sphere.switchToAddress(0);
3394
3395
  * ```
3395
3396
  */
3396
- switchToAddress(index: number): Promise<void>;
3397
+ switchToAddress(index: number, options?: {
3398
+ nametag?: string;
3399
+ }): Promise<void>;
3397
3400
  /**
3398
3401
  * Re-initialize modules after address switch
3399
3402
  */
package/dist/index.d.ts CHANGED
@@ -1210,9 +1210,10 @@ interface WalletJSONExportOptions$1 {
1210
1210
  */
1211
1211
  interface TransportProvider extends BaseProvider {
1212
1212
  /**
1213
- * Set identity for signing/encryption
1213
+ * Set identity for signing/encryption.
1214
+ * If the transport is already connected, reconnects with the new identity.
1214
1215
  */
1215
- setIdentity(identity: FullIdentity): void;
1216
+ setIdentity(identity: FullIdentity): void | Promise<void>;
1216
1217
  /**
1217
1218
  * Send encrypted direct message
1218
1219
  * @param recipientTransportPubkey - Transport-specific pubkey for messaging
@@ -3393,7 +3394,9 @@ declare class Sphere {
3393
3394
  * await sphere.switchToAddress(0);
3394
3395
  * ```
3395
3396
  */
3396
- switchToAddress(index: number): Promise<void>;
3397
+ switchToAddress(index: number, options?: {
3398
+ nametag?: string;
3399
+ }): Promise<void>;
3397
3400
  /**
3398
3401
  * Re-initialize modules after address switch
3399
3402
  */
package/dist/index.js CHANGED
@@ -4227,6 +4227,19 @@ var PaymentsModule = class _PaymentsModule {
4227
4227
  * Initialize module with dependencies
4228
4228
  */
4229
4229
  initialize(deps) {
4230
+ this.unsubscribeTransfers?.();
4231
+ this.unsubscribeTransfers = null;
4232
+ this.unsubscribePaymentRequests?.();
4233
+ this.unsubscribePaymentRequests = null;
4234
+ this.unsubscribePaymentRequestResponses?.();
4235
+ this.unsubscribePaymentRequestResponses = null;
4236
+ this.tokens.clear();
4237
+ this.pendingTransfers.clear();
4238
+ this.tombstones = [];
4239
+ this.archivedTokens.clear();
4240
+ this.forkedTokens.clear();
4241
+ this.transactionHistory = [];
4242
+ this.nametag = null;
4230
4243
  this.deps = deps;
4231
4244
  this.priceProvider = deps.price ?? null;
4232
4245
  if (this.l1) {
@@ -7666,10 +7679,10 @@ var Sphere = class _Sphere {
7666
7679
  sphere._initialized = true;
7667
7680
  _Sphere.instance = sphere;
7668
7681
  await sphere.ensureAddressTracked(0);
7669
- await sphere.syncIdentityWithTransport();
7670
7682
  if (options.nametag) {
7671
7683
  await sphere.registerNametag(options.nametag);
7672
7684
  } else {
7685
+ await sphere.syncIdentityWithTransport();
7673
7686
  await sphere.recoverNametagFromTransport();
7674
7687
  }
7675
7688
  return sphere;
@@ -8523,7 +8536,7 @@ var Sphere = class _Sphere {
8523
8536
  * await sphere.switchToAddress(0);
8524
8537
  * ```
8525
8538
  */
8526
- async switchToAddress(index) {
8539
+ async switchToAddress(index, options) {
8527
8540
  this.ensureReady();
8528
8541
  if (!this._masterKey) {
8529
8542
  throw new Error("HD derivation requires master key with chain code. Cannot switch addresses.");
@@ -8531,11 +8544,27 @@ var Sphere = class _Sphere {
8531
8544
  if (index < 0) {
8532
8545
  throw new Error("Address index must be non-negative");
8533
8546
  }
8547
+ const newNametag = options?.nametag?.startsWith("@") ? options.nametag.slice(1) : options?.nametag;
8548
+ if (newNametag && !this.validateNametag(newNametag)) {
8549
+ throw new Error("Invalid nametag format. Use alphanumeric characters, 3-20 chars.");
8550
+ }
8534
8551
  const addressInfo = this.deriveAddress(index, false);
8535
8552
  const ipnsHash = sha256(addressInfo.publicKey, "hex").slice(0, 40);
8536
8553
  const predicateAddress = await deriveL3PredicateAddress(addressInfo.privateKey);
8537
8554
  await this.ensureAddressTracked(index);
8538
8555
  const addressId = getAddressId(predicateAddress);
8556
+ if (newNametag) {
8557
+ const existing = await this._transport.resolveNametag?.(newNametag);
8558
+ if (existing) {
8559
+ throw new Error(`Nametag @${newNametag} is already taken`);
8560
+ }
8561
+ let nametags = this._addressNametags.get(addressId);
8562
+ if (!nametags) {
8563
+ nametags = /* @__PURE__ */ new Map();
8564
+ this._addressNametags.set(addressId, nametags);
8565
+ }
8566
+ nametags.set(0, newNametag);
8567
+ }
8539
8568
  const nametag = this._addressNametags.get(addressId)?.get(0);
8540
8569
  this._identity = {
8541
8570
  privateKey: addressInfo.privateKey,
@@ -8549,12 +8578,47 @@ var Sphere = class _Sphere {
8549
8578
  await this._updateCachedProxyAddress();
8550
8579
  await this._storage.set(STORAGE_KEYS_GLOBAL.CURRENT_ADDRESS_INDEX, index.toString());
8551
8580
  this._storage.setIdentity(this._identity);
8552
- this._transport.setIdentity(this._identity);
8581
+ await this._transport.setIdentity(this._identity);
8553
8582
  for (const provider of this._tokenStorageProviders.values()) {
8554
8583
  provider.setIdentity(this._identity);
8584
+ await provider.initialize();
8555
8585
  }
8556
8586
  await this.reinitializeModulesForNewAddress();
8557
- await this.syncIdentityWithTransport();
8587
+ if (this._identity.nametag) {
8588
+ await this.syncIdentityWithTransport();
8589
+ }
8590
+ if (newNametag) {
8591
+ await this.persistAddressNametags();
8592
+ if (!this._payments.hasNametag()) {
8593
+ console.log(`[Sphere] Minting nametag token for @${newNametag}...`);
8594
+ try {
8595
+ const result = await this.mintNametag(newNametag);
8596
+ if (result.success) {
8597
+ console.log(`[Sphere] Nametag token minted successfully`);
8598
+ } else {
8599
+ console.warn(`[Sphere] Could not mint nametag token: ${result.error}`);
8600
+ }
8601
+ } catch (err) {
8602
+ console.warn(`[Sphere] Nametag token mint failed:`, err);
8603
+ }
8604
+ }
8605
+ this.emitEvent("nametag:registered", {
8606
+ nametag: newNametag,
8607
+ addressIndex: index
8608
+ });
8609
+ } else if (this._identity.nametag && !this._payments.hasNametag()) {
8610
+ console.log(`[Sphere] Nametag @${this._identity.nametag} has no token after switch, minting...`);
8611
+ try {
8612
+ const result = await this.mintNametag(this._identity.nametag);
8613
+ if (result.success) {
8614
+ console.log(`[Sphere] Nametag token minted successfully after switch`);
8615
+ } else {
8616
+ console.warn(`[Sphere] Could not mint nametag token after switch: ${result.error}`);
8617
+ }
8618
+ } catch (err) {
8619
+ console.warn(`[Sphere] Nametag token mint failed after switch:`, err);
8620
+ }
8621
+ }
8558
8622
  this.emitEvent("identity:changed", {
8559
8623
  l1Address: this._identity.l1Address,
8560
8624
  directAddress: this._identity.directAddress,
@@ -9305,7 +9369,7 @@ var Sphere = class _Sphere {
9305
9369
  // ===========================================================================
9306
9370
  async initializeProviders() {
9307
9371
  this._storage.setIdentity(this._identity);
9308
- this._transport.setIdentity(this._identity);
9372
+ await this._transport.setIdentity(this._identity);
9309
9373
  for (const provider of this._tokenStorageProviders.values()) {
9310
9374
  provider.setIdentity(this._identity);
9311
9375
  }