wallaby-cash 2.3.0 → 2.3.2

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.cjs.js CHANGED
@@ -805,7 +805,7 @@ const MASTER_ENCRYPTION_KEY = '9fa9d3f3c77c20d3a50a08e5299644be29f6e6206ef3479fe
805
805
  { type: 'public-key', alg: exports.PublicKeyCredentialAlgorithm.ES256 },
806
806
  ];
807
807
  const isWeb = typeof window !== 'undefined' && typeof document !== 'undefined';
808
- const currentPackageVersion = "2.3.0" ;
808
+ const currentPackageVersion = "2.3.2" ;
809
809
 
810
810
  /**
811
811
  * Storage keys adapter for Storage interface across different platforms.
@@ -10391,6 +10391,12 @@ class WalletService extends Base {
10391
10391
  const v2EncryptedSeed = await this.localStorageService.fetch(exports.LocalStorageItems.EncryptedStandardSeedPhrase, {
10392
10392
  walletId: input.walletId,
10393
10393
  });
10394
+ const v2EncryptedPrimaryShare = await this.localStorageService.fetch(exports.LocalStorageItems.PrimaryDeviceKeyShare, {
10395
+ walletId: input.walletId,
10396
+ });
10397
+ const v2EncryptedBackupShare = await this.localStorageService.fetch(exports.LocalStorageItems.BackupDeviceKeyShare, {
10398
+ walletId: input.walletId,
10399
+ });
10394
10400
  const clientSalt = await this.authService.getUserSalt();
10395
10401
  const passwordCredentialsKeyPair = await this.cipher.getCredentialsKeyPair({
10396
10402
  password: input.password,
@@ -10436,48 +10442,168 @@ class WalletService extends Base {
10436
10442
  },
10437
10443
  getSignature: this.authService.getSignature.bind(this),
10438
10444
  });
10445
+ const prfValue = authentication.prfValue;
10446
+ if (!prfValue || !(prfValue instanceof Uint8Array) || prfValue.length === 0) {
10447
+ logger.error('SDK::WalletService::convertToPasskey::prfValue is required and must be a non-empty Uint8Array');
10448
+ throw new WallabyError({
10449
+ message: 'PRF value from passkey is required to derive credentials and could not be obtained',
10450
+ statusCode: 500,
10451
+ errorCode: exports.ErrorResponseCodes.sdk_passkey_prf_not_available,
10452
+ });
10453
+ }
10439
10454
  logger.log('SDK::WalletService::convertToPasskey::Received authentication response', !!authentication);
10440
10455
  let decryptedSeedPhrase;
10441
- try {
10442
- decryptedSeedPhrase = await this.cipher.displayStoredData({
10443
- storedEncryptedData: safeJsonParse(v2EncryptedSeed),
10444
- encryptedDevicePrivateKey: safeJsonParse(v2EncryptedPriv),
10445
- options: {
10446
- authType: exports.AuthType.Password,
10447
- password: input.password,
10448
- salt: clientSalt,
10449
- },
10450
- });
10456
+ let decryptedPrimaryShare;
10457
+ let decryptedBackupShare;
10458
+ if (v2EncryptedSeed && v2EncryptedPriv) {
10459
+ try {
10460
+ decryptedSeedPhrase = await this.cipher.displayStoredData({
10461
+ storedEncryptedData: safeJsonParse(v2EncryptedSeed),
10462
+ encryptedDevicePrivateKey: safeJsonParse(v2EncryptedPriv),
10463
+ options: {
10464
+ authType: exports.AuthType.Password,
10465
+ password: input.password,
10466
+ salt: clientSalt,
10467
+ },
10468
+ });
10469
+ }
10470
+ catch (error) {
10471
+ logger.error('SDK::WalletService::convertToPasskey::seed_decryption_error', error);
10472
+ }
10451
10473
  }
10452
- catch (error) {
10453
- logger.error('SDK::WalletService::convertToPasskey::error', error);
10454
- decryptedSeedPhrase = await this.localStorageService.getV1DecryptedSeedPhrase(input.password, clientSalt);
10455
- if (!decryptedSeedPhrase) {
10456
- throw new WallabyError({
10457
- message: 'Seed phrase on this device could not be decrypted',
10458
- statusCode: 500,
10459
- errorCode: exports.ErrorResponseCodes.sdk_seed_phrase_decryption_failed,
10474
+ if (!decryptedSeedPhrase) {
10475
+ try {
10476
+ decryptedSeedPhrase = await this.localStorageService.getV1DecryptedSeedPhrase(input.password, clientSalt);
10477
+ }
10478
+ catch (error) {
10479
+ logger.error('SDK::WalletService::convertToPasskey::v1_seed_decryption_error', error);
10480
+ }
10481
+ }
10482
+ if (v2EncryptedPrimaryShare && v2EncryptedPriv) {
10483
+ try {
10484
+ const parsedPrimaryShare = safeJsonParse(v2EncryptedPrimaryShare);
10485
+ decryptedPrimaryShare = await this.cipher.displayStoredData({
10486
+ storedEncryptedData: parsedPrimaryShare,
10487
+ encryptedDevicePrivateKey: safeJsonParse(v2EncryptedPriv),
10488
+ options: {
10489
+ authType: exports.AuthType.Password,
10490
+ password: input.password,
10491
+ salt: clientSalt,
10492
+ },
10460
10493
  });
10461
10494
  }
10495
+ catch (error) {
10496
+ logger.error('SDK::WalletService::convertToPasskey::primary_share_decryption_error', error);
10497
+ }
10462
10498
  }
10463
- const importResult = await this.handlers.handleImportWallet({
10464
- publicKey: clientPublicKey,
10465
- privateKey: decryptedClientAuthPrivKey,
10466
- }, {
10467
- authenticationOptions: authentication,
10468
- seedPhrase: decryptedSeedPhrase,
10469
- storeSeedPhrase: false,
10470
- returnWalletOption: exports.ReturnWalletOption.ReturnOnlyEncryptedShares, // always only return the encrypted shares
10471
- keyShareOptions: {
10472
- threshold: 2,
10473
- numberOfKeyShares: 3,
10474
- numberOfSharesReturned: 2,
10475
- storeOneKeyShareWithWallaby: true,
10476
- },
10499
+ if (v2EncryptedBackupShare) {
10500
+ try {
10501
+ const parsedBackupShare = safeJsonParse(v2EncryptedBackupShare);
10502
+ // backup key share is wrapped with credentials public key at the outer layer
10503
+ // and wallaby public key at the inner layer, so only one local unwrap is needed.
10504
+ decryptedBackupShare = await this.cipher.decrypt({
10505
+ cipherText: parsedBackupShare,
10506
+ decryptionKey: passwordCredentialsKeyPair.privateKey,
10507
+ });
10508
+ }
10509
+ catch (error) {
10510
+ logger.error('SDK::WalletService::convertToPasskey::backup_share_decryption_error', error);
10511
+ }
10512
+ }
10513
+ const hasAnyDecryptedShare = !!decryptedPrimaryShare || !!decryptedBackupShare;
10514
+ if (!decryptedSeedPhrase && !hasAnyDecryptedShare) {
10515
+ throw new WallabyError({
10516
+ message: 'Seed phrase or key shares on this device could not be decrypted',
10517
+ statusCode: 500,
10518
+ errorCode: exports.ErrorResponseCodes.sdk_no_valid_seed_option,
10519
+ });
10520
+ }
10521
+ // We only care about the primary share for future local operations.
10522
+ if (decryptedSeedPhrase && !decryptedPrimaryShare) {
10523
+ logger.info('SDK::WalletService::convertToPasskey::No primary share found, importing wallet');
10524
+ await this.handlers.handleImportWallet({
10525
+ publicKey: clientPublicKey,
10526
+ privateKey: decryptedClientAuthPrivKey,
10527
+ }, {
10528
+ authenticationOptions: authentication,
10529
+ seedPhrase: decryptedSeedPhrase,
10530
+ storeSeedPhrase: false,
10531
+ returnWalletOption: exports.ReturnWalletOption.ReturnOnlyEncryptedShares, // always only return the encrypted shares
10532
+ keyShareOptions: {
10533
+ threshold: 2,
10534
+ numberOfKeyShares: 3,
10535
+ numberOfSharesReturned: 2,
10536
+ storeOneKeyShareWithWallaby: true,
10537
+ },
10538
+ });
10539
+ // Delete the old password-encrypted seed phrase since import stored the new passkey-encrypted version
10540
+ await this.localStorageService.delete(exports.LocalStorageItems.EncryptedStandardSeedPhrase, { walletId: input.walletId });
10541
+ logger.info('SDK::WalletService::convertToPasskey::Wallet imported and converted successfully, old seed phrase removed');
10542
+ return { authentication, seedPhrase: decryptedSeedPhrase };
10543
+ }
10544
+ const passkeyDerivedKeyPair = await this.cipher.getCredentialsKeyPair({
10545
+ authType: exports.AuthType.Passkey,
10546
+ prfValue,
10477
10547
  });
10478
- await this.localStorageService.delete(exports.LocalStorageItems.EncryptedStandardSeedPhrase, { walletId: input.walletId });
10479
- logger.info('SDK::WalletService::convertToPasskey::Seed phrase removed from device storage after conversion to passkey');
10480
- return Object.assign(Object.assign({}, importResult), { authentication, seedPhrase: decryptedSeedPhrase });
10548
+ const convertedShares = [];
10549
+ if (decryptedPrimaryShare) {
10550
+ const encryptedPrimaryShare = await this.cipher.doubleEncrypt({
10551
+ decryptedData: decryptedPrimaryShare,
10552
+ credentialsKeyPair: passkeyDerivedKeyPair,
10553
+ deviceKeyPair: {
10554
+ publicKey: clientPublicKey,
10555
+ privateKey: decryptedClientAuthPrivKey,
10556
+ },
10557
+ });
10558
+ convertedShares.push({
10559
+ key: exports.V2KeyNames.PrimaryDeviceKeyShare,
10560
+ value: encryptedPrimaryShare,
10561
+ walletId: input.walletId,
10562
+ });
10563
+ }
10564
+ if (decryptedBackupShare) {
10565
+ // Since this is only decrypted once, we only need passkey credential encryption.
10566
+ const encryptedBackupShare = await this.cipher.encrypt({
10567
+ data: decryptedBackupShare,
10568
+ encryptionKey: passkeyDerivedKeyPair.publicKey,
10569
+ });
10570
+ convertedShares.push({
10571
+ key: exports.V2KeyNames.BackupDeviceKeyShare,
10572
+ value: encryptedBackupShare,
10573
+ walletId: input.walletId,
10574
+ });
10575
+ }
10576
+ await this.localStorageService.saveInBulk(convertedShares);
10577
+ if (v2EncryptedSeed) {
10578
+ await this.localStorageService.delete(exports.LocalStorageItems.EncryptedStandardSeedPhrase, { walletId: input.walletId });
10579
+ logger.info('SDK::WalletService::convertToPasskey::Seed phrase removed from device storage after conversion to passkey');
10580
+ }
10581
+ if (!decryptedSeedPhrase && decryptedPrimaryShare) {
10582
+ const flippedEncryptedPrimaryShare = await this.cipher.encrypt({
10583
+ data: decryptedPrimaryShare,
10584
+ encryptionKey: authentication.user.wallabyAuthPubKey,
10585
+ });
10586
+ const reconstructedSeedPhrase = await this.handlers.handleConstructingSeedPhraseFromShares({
10587
+ walletId: input.walletId,
10588
+ encryptedKeyShares: [flippedEncryptedPrimaryShare],
10589
+ deviceAuthKeys: {
10590
+ publicKey: clientPublicKey,
10591
+ privateKey: decryptedClientAuthPrivKey,
10592
+ },
10593
+ });
10594
+ decryptedSeedPhrase = await this.cipher.decrypt({
10595
+ cipherText: reconstructedSeedPhrase,
10596
+ decryptionKey: decryptedClientAuthPrivKey,
10597
+ });
10598
+ }
10599
+ if (!decryptedSeedPhrase) {
10600
+ throw new WallabyError({
10601
+ message: 'Seed phrase could not be reconstructed from the provided shares',
10602
+ statusCode: 500,
10603
+ errorCode: exports.ErrorResponseCodes.sdk_no_valid_seed_option,
10604
+ });
10605
+ }
10606
+ return { authentication, seedPhrase: decryptedSeedPhrase };
10481
10607
  }
10482
10608
  catch (error) {
10483
10609
  logger.error('SDK::WalletService::convertToPasskey::error', error);
@@ -10797,7 +10923,7 @@ class WalletDataService extends Base {
10797
10923
  /**
10798
10924
  * Updates a wallet asset.
10799
10925
  *
10800
- * @param {string} params.assetConfigId - Config ID for the user asset.
10926
+ * @param {string} params.configId - Config ID for the user asset.
10801
10927
  * @param {string} params.walletId - Wallet ID to which the asset belongs.
10802
10928
  * @param {string} params.assetName - Asset name.
10803
10929
  * @param {boolean} params.isVisible - Asset visibility toggle.
@@ -10808,19 +10934,19 @@ class WalletDataService extends Base {
10808
10934
  * @throws {Error} If required parameters are missing or invalid.
10809
10935
  */
10810
10936
  async updateAsset(_a) {
10811
- var { assetConfigId, walletId } = _a, assetConfig = __rest(_a, ["assetConfigId", "walletId"]);
10937
+ var { configId, walletId } = _a, assetConfig = __rest(_a, ["configId", "walletId"]);
10812
10938
  try {
10813
- if (!assetConfig.configId || !walletId) {
10939
+ if (!configId || !walletId) {
10814
10940
  throw new WallabyError({
10815
10941
  message: 'Missing required parameters',
10816
10942
  statusCode: 400,
10817
10943
  errorCode: exports.ErrorResponseCodes.sdk_missing_parameter,
10818
10944
  });
10819
10945
  }
10820
- const response = await this.api.wallets.updateUserAsset({
10821
- configId: assetConfig.configId || assetConfigId,
10822
- walletId: walletId,
10823
- }, Object.assign({ walletId }, assetConfig));
10946
+ const response = await this.request(WalletRoutes.updateAsset, {
10947
+ payload: Object.assign({ configId,
10948
+ walletId }, assetConfig),
10949
+ });
10824
10950
  return response;
10825
10951
  }
10826
10952
  catch (error) {