@unicitylabs/sphere-sdk 0.3.3 → 0.3.5

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.
@@ -2844,6 +2844,8 @@ interface SphereCreateOptions {
2844
2844
  network?: NetworkType;
2845
2845
  /** Group chat configuration (NIP-29). Omit to disable groupchat. */
2846
2846
  groupChat?: GroupChatModuleConfig | boolean;
2847
+ /** Optional password to encrypt the wallet. If omitted, mnemonic is stored as plaintext. */
2848
+ password?: string;
2847
2849
  }
2848
2850
  /** Options for loading existing wallet */
2849
2851
  interface SphereLoadOptions {
@@ -2867,6 +2869,8 @@ interface SphereLoadOptions {
2867
2869
  network?: NetworkType;
2868
2870
  /** Group chat configuration (NIP-29). Omit to disable groupchat. */
2869
2871
  groupChat?: GroupChatModuleConfig | boolean;
2872
+ /** Optional password to decrypt the wallet. Must match the password used during creation. */
2873
+ password?: string;
2870
2874
  }
2871
2875
  /** Options for importing a wallet */
2872
2876
  interface SphereImportOptions {
@@ -2898,6 +2902,8 @@ interface SphereImportOptions {
2898
2902
  price?: PriceProvider;
2899
2903
  /** Group chat configuration (NIP-29). Omit to disable groupchat. */
2900
2904
  groupChat?: GroupChatModuleConfig | boolean;
2905
+ /** Optional password to encrypt the wallet. If omitted, mnemonic/key is stored as plaintext. */
2906
+ password?: string;
2901
2907
  }
2902
2908
  /** L1 (ALPHA blockchain) configuration */
2903
2909
  interface L1Config {
@@ -2943,6 +2949,8 @@ interface SphereInitOptions {
2943
2949
  * - Omit/undefined: No groupchat module
2944
2950
  */
2945
2951
  groupChat?: GroupChatModuleConfig | boolean;
2952
+ /** Optional password to encrypt/decrypt the wallet. If omitted, mnemonic is stored as plaintext. */
2953
+ password?: string;
2946
2954
  }
2947
2955
  /** Result of init operation */
2948
2956
  interface SphereInitResult {
@@ -2959,6 +2967,7 @@ declare class Sphere {
2959
2967
  private _identity;
2960
2968
  private _masterKey;
2961
2969
  private _mnemonic;
2970
+ private _password;
2962
2971
  private _source;
2963
2972
  private _derivationMode;
2964
2973
  private _basePath;
@@ -2844,6 +2844,8 @@ interface SphereCreateOptions {
2844
2844
  network?: NetworkType;
2845
2845
  /** Group chat configuration (NIP-29). Omit to disable groupchat. */
2846
2846
  groupChat?: GroupChatModuleConfig | boolean;
2847
+ /** Optional password to encrypt the wallet. If omitted, mnemonic is stored as plaintext. */
2848
+ password?: string;
2847
2849
  }
2848
2850
  /** Options for loading existing wallet */
2849
2851
  interface SphereLoadOptions {
@@ -2867,6 +2869,8 @@ interface SphereLoadOptions {
2867
2869
  network?: NetworkType;
2868
2870
  /** Group chat configuration (NIP-29). Omit to disable groupchat. */
2869
2871
  groupChat?: GroupChatModuleConfig | boolean;
2872
+ /** Optional password to decrypt the wallet. Must match the password used during creation. */
2873
+ password?: string;
2870
2874
  }
2871
2875
  /** Options for importing a wallet */
2872
2876
  interface SphereImportOptions {
@@ -2898,6 +2902,8 @@ interface SphereImportOptions {
2898
2902
  price?: PriceProvider;
2899
2903
  /** Group chat configuration (NIP-29). Omit to disable groupchat. */
2900
2904
  groupChat?: GroupChatModuleConfig | boolean;
2905
+ /** Optional password to encrypt the wallet. If omitted, mnemonic/key is stored as plaintext. */
2906
+ password?: string;
2901
2907
  }
2902
2908
  /** L1 (ALPHA blockchain) configuration */
2903
2909
  interface L1Config {
@@ -2943,6 +2949,8 @@ interface SphereInitOptions {
2943
2949
  * - Omit/undefined: No groupchat module
2944
2950
  */
2945
2951
  groupChat?: GroupChatModuleConfig | boolean;
2952
+ /** Optional password to encrypt/decrypt the wallet. If omitted, mnemonic is stored as plaintext. */
2953
+ password?: string;
2946
2954
  }
2947
2955
  /** Result of init operation */
2948
2956
  interface SphereInitResult {
@@ -2959,6 +2967,7 @@ declare class Sphere {
2959
2967
  private _identity;
2960
2968
  private _masterKey;
2961
2969
  private _mnemonic;
2970
+ private _password;
2962
2971
  private _source;
2963
2972
  private _derivationMode;
2964
2973
  private _basePath;
@@ -9120,7 +9120,9 @@ function decryptTextFormatKey(encryptedKey, password) {
9120
9120
  const key = deriveLegacyKey(password);
9121
9121
  const decrypted = CryptoJS7.AES.decrypt(encryptedKey, key);
9122
9122
  const result = decrypted.toString(CryptoJS7.enc.Utf8);
9123
- return result || null;
9123
+ if (!result) return null;
9124
+ if (!/^[0-9a-fA-F]+$/.test(result)) return null;
9125
+ return result;
9124
9126
  } catch {
9125
9127
  return null;
9126
9128
  }
@@ -9712,6 +9714,7 @@ var Sphere = class _Sphere {
9712
9714
  _identity = null;
9713
9715
  _masterKey = null;
9714
9716
  _mnemonic = null;
9717
+ _password = null;
9715
9718
  _source = "unknown";
9716
9719
  _derivationMode = "bip32";
9717
9720
  _basePath = DEFAULT_BASE_PATH;
@@ -9811,7 +9814,8 @@ var Sphere = class _Sphere {
9811
9814
  tokenStorage: options.tokenStorage,
9812
9815
  l1: options.l1,
9813
9816
  price: options.price,
9814
- groupChat
9817
+ groupChat,
9818
+ password: options.password
9815
9819
  });
9816
9820
  return { sphere: sphere2, created: false };
9817
9821
  }
@@ -9837,7 +9841,8 @@ var Sphere = class _Sphere {
9837
9841
  nametag: options.nametag,
9838
9842
  l1: options.l1,
9839
9843
  price: options.price,
9840
- groupChat
9844
+ groupChat,
9845
+ password: options.password
9841
9846
  });
9842
9847
  return { sphere, created: true, generatedMnemonic };
9843
9848
  }
@@ -9884,6 +9889,7 @@ var Sphere = class _Sphere {
9884
9889
  options.price,
9885
9890
  groupChatConfig
9886
9891
  );
9892
+ sphere._password = options.password ?? null;
9887
9893
  await sphere.storeMnemonic(options.mnemonic, options.derivationPath);
9888
9894
  await sphere.initializeIdentityFromMnemonic(options.mnemonic, options.derivationPath);
9889
9895
  await sphere.initializeProviders();
@@ -9917,6 +9923,7 @@ var Sphere = class _Sphere {
9917
9923
  options.price,
9918
9924
  groupChatConfig
9919
9925
  );
9926
+ sphere._password = options.password ?? null;
9920
9927
  await sphere.loadIdentityFromStorage();
9921
9928
  await sphere.initializeProviders();
9922
9929
  await sphere.initializeModules();
@@ -9969,6 +9976,7 @@ var Sphere = class _Sphere {
9969
9976
  options.price,
9970
9977
  groupChatConfig
9971
9978
  );
9979
+ sphere._password = options.password ?? null;
9972
9980
  if (options.mnemonic) {
9973
9981
  if (!_Sphere.validateMnemonic(options.mnemonic)) {
9974
9982
  throw new Error("Invalid mnemonic");
@@ -10936,7 +10944,7 @@ var Sphere = class _Sphere {
10936
10944
  await provider.initialize();
10937
10945
  }
10938
10946
  await this.reinitializeModulesForNewAddress();
10939
- if (this._identity.nametag) {
10947
+ if (!newNametag) {
10940
10948
  await this.syncIdentityWithTransport();
10941
10949
  }
10942
10950
  if (newNametag) {
@@ -12147,9 +12155,20 @@ var Sphere = class _Sphere {
12147
12155
  // Private: Encryption
12148
12156
  // ===========================================================================
12149
12157
  encrypt(data) {
12150
- return encryptSimple(data, DEFAULT_ENCRYPTION_KEY);
12158
+ if (!this._password) return data;
12159
+ return encryptSimple(data, this._password);
12151
12160
  }
12152
12161
  decrypt(encrypted) {
12162
+ if (this._password) {
12163
+ try {
12164
+ return decryptSimple(encrypted, this._password);
12165
+ } catch {
12166
+ return null;
12167
+ }
12168
+ }
12169
+ if (validateMnemonic2(encrypted) || /^[0-9a-f]{64}$/i.test(encrypted)) {
12170
+ return encrypted;
12171
+ }
12153
12172
  try {
12154
12173
  return decryptSimple(encrypted, DEFAULT_ENCRYPTION_KEY);
12155
12174
  } catch {