wallaby-cash 2.0.1-alpha.2 → 2.0.1-alpha.6

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
@@ -437,6 +437,7 @@ exports.LocalStorageItems = void 0;
437
437
  LocalStorageItems["AccessToken"] = "access-token";
438
438
  LocalStorageItems["EncryptedStandardSeedPhrase"] = "encrypted-standard-seedPhrase";
439
439
  LocalStorageItems["EncryptedKeyShares"] = "encrypted-key-shares";
440
+ LocalStorageItems["EncryptedShare"] = "encrypted-share";
440
441
  LocalStorageItems["EncryptedAlgorandSeedPhrase"] = "encrypted-algorand-seedPhrase";
441
442
  LocalStorageItems["EncryptedDevicePrivateKey"] = "encrypted-private-key";
442
443
  LocalStorageItems["ClientAuthPublicKey"] = "client-auth-pub-key";
@@ -5030,6 +5031,11 @@ var WalletRoutes = {
5030
5031
  method: exports.RequestMethod.Post,
5031
5032
  requiresAccessToken: true,
5032
5033
  },
5034
+ backupKeyShare: {
5035
+ fullPath: '/wallets/backup-share',
5036
+ method: exports.RequestMethod.Post,
5037
+ requiresAccessToken: true,
5038
+ },
5033
5039
  initKeyExchange: {
5034
5040
  fullPath: '/mpc/init-key-exchange',
5035
5041
  method: exports.RequestMethod.Post,
@@ -5760,7 +5766,8 @@ var AuthService = /** @class */ (function (_super) {
5760
5766
  _c.label = 7;
5761
5767
  case 7:
5762
5768
  if (!decryptedClientAuthPrivKey) {
5763
- throw new Error('Device private key could not be decrypted');
5769
+ logger.error('SDK::AuthService::handleSignInWithPassword::error', 'Device private key could not be decrypted');
5770
+ throw new Error('Please enter the correct password.');
5764
5771
  }
5765
5772
  clientAuthPubKey = publicKeyByPrivateKey(decryptedClientAuthPrivKey);
5766
5773
  return [4 /*yield*/, this.getSignature({ publicKey: clientAuthPubKey, privateKey: decryptedClientAuthPrivKey })];
@@ -6118,7 +6125,7 @@ var AuthService = /** @class */ (function (_super) {
6118
6125
  message: message,
6119
6126
  clientAuthPubKey: clientAuthPubKey,
6120
6127
  signature: signature,
6121
- newClientAuthPubKey: newIdentity,
6128
+ newClientAuthPubKey: newIdentity.publicKey,
6122
6129
  },
6123
6130
  })];
6124
6131
  case 7:
@@ -6214,7 +6221,7 @@ var AuthService = /** @class */ (function (_super) {
6214
6221
  },
6215
6222
  {
6216
6223
  key: exports.LocalStorageItems.ClientAuthPublicKey,
6217
- value: JSON.stringify(deviceAuthKeys.publicKey),
6224
+ value: deviceAuthKeys.publicKey,
6218
6225
  },
6219
6226
  {
6220
6227
  key: exports.LocalStorageItems.AccessToken,
@@ -6226,6 +6233,9 @@ var AuthService = /** @class */ (function (_super) {
6226
6233
  },
6227
6234
  ]);
6228
6235
  }
6236
+ else {
6237
+ throw new Error('Device private key could not be encrypted');
6238
+ }
6229
6239
  return [2 /*return*/, authResponse];
6230
6240
  }
6231
6241
  });
@@ -6375,7 +6385,7 @@ var AuthService = /** @class */ (function (_super) {
6375
6385
  allowCredentials: [
6376
6386
  {
6377
6387
  id: rawId,
6378
- transports: ['internal', 'hybrid'],
6388
+ transports: ['hybrid', 'internal'],
6379
6389
  // type: credential.type as PublicKeyCredentialType,
6380
6390
  type: 'public-key',
6381
6391
  },
@@ -6423,362 +6433,375 @@ var AuthService = /** @class */ (function (_super) {
6423
6433
  return AuthService;
6424
6434
  }(Base));
6425
6435
 
6426
- /**
6427
- * Sanitizes a Sui object ID by removing any part of the string after '::'.
6428
- *
6429
- * @param objectId - The Sui object ID to sanitize. Example input: "0xc91acfb75009c5ff2fd57c54f3caaee12ad1fbe997681334adc0b574fc277a07::icorgi::ICORGI"
6430
- * @returns The sanitized Sui object ID. Example output: "0xc91acfb75009c5ff2fd57c54f3caaee12ad1fbe997681334adc0b574fc277a07"
6431
- */
6432
- var sanitizeSuiObjectId = function (objectId) {
6433
- if (!objectId) {
6434
- throw new Error('Sui object ID is required');
6435
- }
6436
- if (!objectId.startsWith('0x')) {
6437
- throw new Error('Invalid Sui object ID');
6438
- }
6439
- if (!objectId.includes('::')) {
6440
- return objectId;
6441
- }
6442
- return objectId.split('::')[0];
6443
- };
6444
-
6445
- var WalletService = /** @class */ (function (_super) {
6446
- __extends(WalletService, _super);
6447
- function WalletService(config) {
6436
+ var WalletHandlersService = /** @class */ (function (_super) {
6437
+ __extends(WalletHandlersService, _super);
6438
+ function WalletHandlersService(config) {
6448
6439
  var _this = _super.call(this, config) || this;
6449
6440
  _this.authService = new AuthService(config);
6450
6441
  _this.cipher = new Cipher(_this.device);
6451
6442
  _this.userId = config.userId;
6452
6443
  return _this;
6453
6444
  }
6454
- /**
6455
- * @public
6456
- * Exports a encrypted shares and/or encrypted seedPhrase(s).
6457
- *
6458
- * @param {string} wallabyAuthPublicKey - Wallaby auth public key. Can be passed from [AuthResponse] (`signIn` | `signUp`)
6459
- * @param {EncryptionMechanism} encryptionMechanism - The mechanism used for encrypting the wallet data.
6460
- * @param {string} [password] - The password for additional encryption, if required.
6461
- * @param {Uint8Array} [prfValue] - The PRF value for encryption, used if applicable.
6462
- * @param {object} exportOptions - Specifies the options for exporting the wallet:
6463
- * - { boolean } standardSeedPhrase - Includes the standard seed phrase (bip32 standard 24 words mnemonic) in the export.
6464
- * - { boolean } algorandSeedPhrase - Includes the Algorand seed phrase (25 words mnemonic) in the export.
6465
- * - { boolean } keyShares - Includes key shares in the export.
6466
- */
6467
- WalletService.prototype.export = function (_a) {
6468
- return __awaiter(this, arguments, void 0, function (_b) {
6469
- var encryptedSeed, encryptedKeyShares, encryptedDeviceAuthPrivateKey, decryptedKeyShares, decryptedSeedPhrase, decryptedAlgorandSeedPhrase, inputKeyMaterial_1, _c, _d, _e, decryptedDevicePrivateKey_1, decryptedOnceSeedPhrase, decryptedTwiceSeedPhrase, algorandSeedPhrase, decryptedKeyShares_1, readyForTransportKeyShares, deviceAuthPublicKey, encryptedReconstructedSeedPhrase, algorandSeedPhrase, error_1;
6470
- var _this = this;
6471
- var walletId = _b.walletId, wallabyAuthPublicKey = _b.wallabyAuthPublicKey, prfValue = _b.prfValue, password = _b.password, encryptionMechanism = _b.encryptionMechanism, exportOptions = _b.exportOptions;
6445
+ WalletHandlersService.prototype.handleImportWallet = function (deviceAuthKeys, importWalletInput) {
6446
+ return __awaiter(this, void 0, void 0, function () {
6447
+ var _a, signature, message, _b, numberOfKeyShares, threshold, readyForTransportSeedPhrase, importWalletPayload, importWalletRequest, decryptedSeedPhrase, inputKeyMaterial, decryptedShares;
6448
+ var _c, _d, _e;
6472
6449
  return __generator(this, function (_f) {
6473
6450
  switch (_f.label) {
6474
- case 0:
6475
- _f.trys.push([0, 23, , 24]);
6476
- if (!exportOptions ||
6477
- (!exportOptions.algorandSeedPhrase && !exportOptions.standardSeedPhrase && !exportOptions.keyShares)) {
6478
- throw new Error('exportOptions must be provided with at least one of the following options: algorandSeedPhrase, standardSeedPhrase, keyShares');
6479
- }
6480
- return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedStandardSeedPhrase)];
6451
+ case 0: return [4 /*yield*/, this.authService.getSignature({
6452
+ publicKey: deviceAuthKeys.publicKey,
6453
+ privateKey: deviceAuthKeys.privateKey,
6454
+ })];
6481
6455
  case 1:
6482
- encryptedSeed = _f.sent();
6483
- return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedKeyShares)];
6456
+ _a = _f.sent(), signature = _a.signature, message = _a.message;
6457
+ if ((importWalletInput.returnWalletOption === exports.ReturnWalletOption.ReturnOnlyEncryptedShares ||
6458
+ importWalletInput.returnWalletOption === exports.ReturnWalletOption.ReturnBothEncryptedSharesAndSeed) &&
6459
+ !importWalletInput.keyShareOptions) {
6460
+ throw new Error('Key share options are required when requesting to split the seed phrase');
6461
+ }
6462
+ _b = (_c = importWalletInput.keyShareOptions) !== null && _c !== void 0 ? _c : {}, numberOfKeyShares = _b.numberOfKeyShares, threshold = _b.threshold;
6463
+ if (threshold && numberOfKeyShares && numberOfKeyShares < threshold) {
6464
+ throw new Error('Number of shares must be greater than or equal to the threshold');
6465
+ }
6466
+ readyForTransportSeedPhrase = encryptWithPublicKey(importWalletInput.authenticationOptions.wallabyAuthPublicKey, importWalletInput.seedPhrase);
6467
+ importWalletPayload = {
6468
+ reference: uuid.v4(),
6469
+ message: message,
6470
+ signature: signature,
6471
+ encryptedSeed: readyForTransportSeedPhrase,
6472
+ clientAuthPubKey: deviceAuthKeys.publicKey,
6473
+ backUpDevicePubKey: deviceAuthKeys.publicKey, // TODO: Replace this with backup device public key.
6474
+ returnWalletOption: importWalletInput.returnWalletOption,
6475
+ numShares: numberOfKeyShares !== null && numberOfKeyShares !== void 0 ? numberOfKeyShares : 0,
6476
+ threshold: threshold !== null && threshold !== void 0 ? threshold : 0,
6477
+ wallabyKeepsOne: (_e = (_d = importWalletInput.keyShareOptions) === null || _d === void 0 ? void 0 : _d.storeOneKeyShareWithWallaby) !== null && _e !== void 0 ? _e : false,
6478
+ };
6479
+ return [4 /*yield*/, this.request(WalletRoutes.importSeedPhraseV2, {
6480
+ method: WalletRoutes.importSeedPhraseV2.method,
6481
+ payload: importWalletPayload,
6482
+ })];
6484
6483
  case 2:
6485
- encryptedKeyShares = _f.sent();
6486
- return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedDevicePrivateKey)];
6487
- case 3:
6488
- encryptedDeviceAuthPrivateKey = _f.sent();
6489
- decryptedKeyShares = void 0;
6490
- decryptedSeedPhrase = void 0;
6491
- decryptedAlgorandSeedPhrase = void 0;
6492
- _c = encryptionMechanism;
6493
- switch (_c) {
6494
- case exports.EncryptionMechanism.Password: return [3 /*break*/, 4];
6495
- case exports.EncryptionMechanism.Prf: return [3 /*break*/, 6];
6484
+ importWalletRequest = _f.sent();
6485
+ if (!importWalletRequest || importWalletRequest.error) {
6486
+ throw new Error(importWalletRequest.error);
6496
6487
  }
6497
- return [3 /*break*/, 7];
6488
+ logger.info('SDK::WalletService::import::importWalletRequest', importWalletRequest);
6489
+ logger.info('SDK::WalletService::import::inputKeyMaterialPayload', {
6490
+ encryptionMechanism: importWalletInput.authenticationOptions.encryptionMechanism,
6491
+ salt: importWalletInput.authenticationOptions.salt,
6492
+ password: importWalletInput.authenticationOptions.password,
6493
+ prfValue: importWalletInput.authenticationOptions.prfValue,
6494
+ });
6495
+ inputKeyMaterial = importWalletInput.authenticationOptions.encryptionMechanism === exports.EncryptionMechanism.Password
6496
+ ? utf8ToBytes$2("".concat(importWalletInput.authenticationOptions.salt).concat(importWalletInput.authenticationOptions.password))
6497
+ : importWalletInput.authenticationOptions.prfValue;
6498
+ logger.info('SDK::WalletService::import::inputKeyMaterial', inputKeyMaterial);
6499
+ if (!importWalletRequest.encryptedSeed) return [3 /*break*/, 4];
6500
+ logger.info('SDK::WalletService::import::import does have an encrypted seed phrase');
6501
+ return [4 /*yield*/, this.handleSeedPhraseFromResponse({
6502
+ encryptedSeed: importWalletRequest.encryptedSeed,
6503
+ decryptedClientAuthPrivKey: deviceAuthKeys.privateKey,
6504
+ storeSeedPhrase: importWalletInput.storeSeedPhrase,
6505
+ inputKeyMaterial: inputKeyMaterial,
6506
+ })];
6507
+ case 3:
6508
+ decryptedSeedPhrase = _f.sent();
6509
+ logger.info('SDK::WalletService::import::decryptedSeedPhrase', decryptedSeedPhrase);
6510
+ _f.label = 4;
6498
6511
  case 4:
6499
- if (!password) {
6500
- throw new Error('password is required for this encryption mechanism type.');
6501
- }
6502
- _d = utf8ToBytes$2;
6503
- _e = "".concat;
6504
- return [4 /*yield*/, this.authService.getUserSalt()];
6512
+ if (!importWalletRequest.shares) return [3 /*break*/, 6];
6513
+ logger.info('SDK::WalletService::import::import does have key shares');
6514
+ return [4 /*yield*/, this.handleKeySharesFromResponse({
6515
+ keyShares: importWalletRequest.shares,
6516
+ decryptedClientAuthPrivKey: deviceAuthKeys.privateKey,
6517
+ inputKeyMaterial: inputKeyMaterial,
6518
+ })];
6505
6519
  case 5:
6506
- inputKeyMaterial_1 = _d.apply(void 0, [_e.apply("", [_f.sent()]).concat(password)]);
6507
- return [3 /*break*/, 7];
6508
- case 6:
6509
- if (!prfValue) {
6510
- throw new Error('prfValue is required for this encryption mechanism type.');
6520
+ decryptedShares = _f.sent();
6521
+ logger.info('SDK::WalletService::import::decryptedShares', decryptedShares);
6522
+ _f.label = 6;
6523
+ case 6: return [2 /*return*/, __assign(__assign({}, importWalletRequest), { standardSeedPhrase: decryptedSeedPhrase })];
6524
+ }
6525
+ });
6526
+ });
6527
+ };
6528
+ /**
6529
+ * Handles the creation of a wallet, including generating signatures, validating input,
6530
+ * and processing the wallet creation response.
6531
+ * @private
6532
+ * @param {Object} deviceAuthKeys - The device authentication key pair. { publicKey: string, privateKey: string}
6533
+ * @param {CreateWalletRequest} walletCreationInput - The input parameters for wallet creation.
6534
+ * @returns {Promise<Omit<CreateWalletResponse, 'standardSeedPhrase'> & { standardSeedPhrase?: string }>}
6535
+ * The response from the wallet creation process, including the decrypted seed phrase if applicable.
6536
+ * @throws {Error} If key share options are required but not provided, or if the number of shares is less than the threshold.
6537
+ */
6538
+ WalletHandlersService.prototype.handleWalletCreation = function (deviceAuthKeys, walletCreationInput) {
6539
+ return __awaiter(this, void 0, void 0, function () {
6540
+ var _a, signature, message, hashedMessage, _b, numberOfKeyShares, threshold, walletCreationPayload, walletCreationResponse, decryptedSeedPhrase, inputKeyMaterial;
6541
+ var _c, _d, _e, _f;
6542
+ return __generator(this, function (_g) {
6543
+ switch (_g.label) {
6544
+ case 0: return [4 /*yield*/, this.authService.getSignature({
6545
+ publicKey: deviceAuthKeys.publicKey,
6546
+ privateKey: deviceAuthKeys.privateKey,
6547
+ })];
6548
+ case 1:
6549
+ _a = _g.sent(), signature = _a.signature, message = _a.message, hashedMessage = _a.hashedMessage;
6550
+ if ((walletCreationInput.returnWalletOption === exports.ReturnWalletOption.ReturnOnlyEncryptedShares ||
6551
+ walletCreationInput.returnWalletOption === exports.ReturnWalletOption.ReturnBothEncryptedSharesAndSeed) &&
6552
+ !walletCreationInput.keyShareOptions) {
6553
+ throw new Error('Key share options are required when requesting to split the seed phrase');
6511
6554
  }
6512
- inputKeyMaterial_1 = prfValue;
6513
- return [3 /*break*/, 7];
6514
- case 7:
6515
- if (!encryptedDeviceAuthPrivateKey) {
6516
- throw new Error('Device private key is required to export the wallet');
6555
+ _b = (_c = walletCreationInput.keyShareOptions) !== null && _c !== void 0 ? _c : {}, numberOfKeyShares = _b.numberOfKeyShares, threshold = _b.threshold;
6556
+ if (threshold && numberOfKeyShares && numberOfKeyShares < threshold) {
6557
+ throw new Error('Number of shares must be greater than or equal to the threshold');
6517
6558
  }
6518
- return [4 /*yield*/, this.cipher.decrypt({
6519
- cipherText: encryptedDeviceAuthPrivateKey,
6520
- inputKeyMaterial: inputKeyMaterial_1,
6559
+ walletCreationPayload = {
6560
+ reference: uuid.v4(),
6561
+ message: message,
6562
+ signature: signature,
6563
+ hashedMessage: hashedMessage,
6564
+ clientAuthPubKey: deviceAuthKeys.publicKey,
6565
+ returnWalletOption: walletCreationInput.returnWalletOption,
6566
+ numShares: numberOfKeyShares !== null && numberOfKeyShares !== void 0 ? numberOfKeyShares : 0,
6567
+ threshold: threshold !== null && threshold !== void 0 ? threshold : 0,
6568
+ wallabyKeepsOne: (_e = (_d = walletCreationInput.keyShareOptions) === null || _d === void 0 ? void 0 : _d.storeOneKeyShareWithWallaby) !== null && _e !== void 0 ? _e : false,
6569
+ // backupDevicePublicKey: walletCreationInput.keyShareOptions?.backupDevicePublicKey,
6570
+ walletOwnershipType: (_f = walletCreationInput.walletOwnershipType) !== null && _f !== void 0 ? _f : exports.WalletOwnershipType.SingleOwner,
6571
+ };
6572
+ return [4 /*yield*/, this.request(WalletRoutes.createWithKeyShares, {
6573
+ method: WalletRoutes.createWithKeyShares.method,
6574
+ payload: walletCreationPayload,
6521
6575
  })];
6522
- case 8:
6523
- decryptedDevicePrivateKey_1 = _f.sent();
6524
- console.log('SDK::Wallet::export::decryptedDevicePrivateKey', decryptedDevicePrivateKey_1);
6525
- if (!decryptedDevicePrivateKey_1) {
6526
- throw new Error('Device private key could not be decrypted');
6527
- }
6528
- if (!(exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.keyShares)) return [3 /*break*/, 10];
6529
- if (!encryptedKeyShares) {
6530
- throw new Error('You do not have any key shares stored on this device.');
6576
+ case 2:
6577
+ walletCreationResponse = _g.sent();
6578
+ if (!walletCreationResponse || walletCreationResponse.error) {
6579
+ throw new Error(walletCreationResponse.error);
6531
6580
  }
6532
- return [4 /*yield*/, Promise.all(JSON.parse(encryptedKeyShares).map(function (encryptedKeyShare, i) { return __awaiter(_this, void 0, void 0, function () {
6533
- var decryptedOnceKeyShare, decryptedTwiceKeyShare;
6581
+ inputKeyMaterial = walletCreationInput.authenticationOptions.encryptionMechanism === exports.EncryptionMechanism.Password
6582
+ ? utf8ToBytes$2("".concat(walletCreationInput.authenticationOptions.salt).concat(walletCreationInput.authenticationOptions.password))
6583
+ : walletCreationInput.authenticationOptions.prfValue;
6584
+ if (!walletCreationResponse.encryptedSeed) return [3 /*break*/, 4];
6585
+ return [4 /*yield*/, this.handleSeedPhraseFromResponse({
6586
+ encryptedSeed: walletCreationResponse.encryptedSeed,
6587
+ decryptedClientAuthPrivKey: deviceAuthKeys.privateKey,
6588
+ storeSeedPhrase: walletCreationInput.storeSeedPhrase,
6589
+ inputKeyMaterial: inputKeyMaterial,
6590
+ })];
6591
+ case 3:
6592
+ decryptedSeedPhrase = _g.sent();
6593
+ _g.label = 4;
6594
+ case 4:
6595
+ if (!walletCreationResponse.shares) return [3 /*break*/, 6];
6596
+ return [4 /*yield*/, this.handleKeySharesFromResponse({
6597
+ keyShares: walletCreationResponse.shares,
6598
+ decryptedClientAuthPrivKey: deviceAuthKeys.privateKey,
6599
+ inputKeyMaterial: inputKeyMaterial,
6600
+ })];
6601
+ case 5:
6602
+ _g.sent();
6603
+ _g.label = 6;
6604
+ case 6: return [2 /*return*/, __assign(__assign({}, walletCreationResponse), { standardSeedPhrase: decryptedSeedPhrase })];
6605
+ }
6606
+ });
6607
+ });
6608
+ };
6609
+ WalletHandlersService.prototype.handleKeySharesFromResponse = function (_a) {
6610
+ return __awaiter(this, arguments, void 0, function (_b) {
6611
+ var deviceAuthPublicKey_1, encryptedKeyShares, error_1;
6612
+ var _this = this;
6613
+ var keyShares = _b.keyShares, decryptedClientAuthPrivKey = _b.decryptedClientAuthPrivKey, inputKeyMaterial = _b.inputKeyMaterial;
6614
+ return __generator(this, function (_c) {
6615
+ switch (_c.label) {
6616
+ case 0:
6617
+ _c.trys.push([0, 3, , 4]);
6618
+ deviceAuthPublicKey_1 = publicKeyByPrivateKey(decryptedClientAuthPrivKey);
6619
+ logger.info('SDK::handleKeySharesFromResponse::deviceAuthKeys', {
6620
+ deviceAuthPublicKey: deviceAuthPublicKey_1,
6621
+ decryptedClientAuthPrivKey: decryptedClientAuthPrivKey,
6622
+ });
6623
+ logger.info('SDK::handleKeySharesFromResponse::keyShares encrypted with wallaby key', keyShares);
6624
+ return [4 /*yield*/, Promise.all(keyShares.map(function (keyShare) { return __awaiter(_this, void 0, void 0, function () {
6625
+ var decryptedKeyShare, encryptedOnceKeyShare, encryptedTwiceKeyShare;
6534
6626
  return __generator(this, function (_a) {
6535
6627
  switch (_a.label) {
6536
- case 0:
6537
- logger.info("SDK::Wallet::export::encryptedKeyShare #".concat(i), encryptedKeyShare);
6538
- return [4 /*yield*/, this.cipher.decrypt({
6539
- cipherText: JSON.stringify(encryptedKeyShare),
6540
- inputKeyMaterial: inputKeyMaterial_1,
6541
- })];
6628
+ case 0: return [4 /*yield*/, decryptWithPrivateKey(decryptedClientAuthPrivKey, keyShare)];
6542
6629
  case 1:
6543
- decryptedOnceKeyShare = _a.sent();
6544
- if (!decryptedOnceKeyShare) {
6545
- throw new Error('Something went wrong decrypting the data.');
6546
- }
6547
- console.log('SDK::Wallet::export::decryptWithPrivateKeyPayload', {
6548
- decryptedOnceKeyShare: decryptedOnceKeyShare,
6549
- decryptedDevicePrivateKey: decryptedDevicePrivateKey_1,
6550
- });
6551
- return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, JSON.parse(decryptedOnceKeyShare))];
6630
+ decryptedKeyShare = _a.sent();
6631
+ logger.info('SDK::handleKeySharesFromResponse::decryptedKeyShare with client key', decryptedKeyShare);
6632
+ return [4 /*yield*/, encryptWithPublicKey(deviceAuthPublicKey_1, decryptedKeyShare)];
6552
6633
  case 2:
6553
- decryptedTwiceKeyShare = _a.sent();
6554
- return [2 /*return*/, decryptedTwiceKeyShare];
6555
- }
6556
- });
6557
- }); }))];
6558
- case 9:
6559
- decryptedKeyShares = _f.sent();
6560
- _f.label = 10;
6561
- case 10:
6562
- if (!((exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.standardSeedPhrase) || (exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.algorandSeedPhrase))) return [3 /*break*/, 22];
6563
- if (!encryptedSeed) return [3 /*break*/, 17];
6564
- console.log('SDK::Wallet::export::encryptedSeed', encryptedSeed);
6565
- if (!(exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.standardSeedPhrase)) return [3 /*break*/, 13];
6566
- return [4 /*yield*/, this.cipher.decrypt({
6567
- cipherText: encryptedSeed,
6568
- inputKeyMaterial: inputKeyMaterial_1,
6569
- })];
6570
- case 11:
6571
- decryptedOnceSeedPhrase = _f.sent();
6572
- if (!decryptedOnceSeedPhrase) {
6573
- throw new Error('Something went wrong decrypting the seed phrase.');
6574
- }
6575
- return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, JSON.parse(decryptedOnceSeedPhrase))];
6576
- case 12:
6577
- decryptedTwiceSeedPhrase = _f.sent();
6578
- decryptedSeedPhrase = decryptedTwiceSeedPhrase;
6579
- _f.label = 13;
6580
- case 13:
6581
- if (!(exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.algorandSeedPhrase)) return [3 /*break*/, 16];
6582
- return [4 /*yield*/, this.getAlgorandSeedPhrase({
6583
- encryptedStandardSeedPhrase: JSON.parse(encryptedSeed),
6584
- authenticationOptions: {
6585
- wallabyAuthPublicKey: wallabyAuthPublicKey,
6586
- encryptionMechanism: encryptionMechanism,
6587
- password: password,
6588
- prfValue: prfValue,
6589
- },
6590
- })];
6591
- case 14:
6592
- algorandSeedPhrase = _f.sent();
6593
- return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, algorandSeedPhrase.convertedEncryptedSeed)];
6594
- case 15:
6595
- decryptedAlgorandSeedPhrase = _f.sent();
6596
- if (!decryptedAlgorandSeedPhrase) {
6597
- throw new Error('Something went wrong decrypting algorand seed phrase.');
6598
- }
6599
- // decryptedAlgorandSeedPhrase = await this.cipher.prepDataForDisplay({
6600
- // wallabyAuthPublicKey: wallabyAuthPublicKey,
6601
- // storedEncryptedData: JSON.stringify(algorandSeedPhrase.convertedEncryptedSeed),
6602
- // encryptionMechanism,
6603
- // encryptedDevicePrivateKey: encryptedDeviceAuthPrivateKey,
6604
- // prfValue,
6605
- // password,
6606
- // getUserSalt: this.authService.getUserSalt,
6607
- // });
6608
- console.log('SDK::Wallet::export::decryptedAlgorandSeedPhrase', decryptedAlgorandSeedPhrase);
6609
- _f.label = 16;
6610
- case 16: return [3 /*break*/, 22];
6611
- case 17:
6612
- if (!encryptedKeyShares) {
6613
- throw new Error('You do not have a wallet stored on this device.');
6614
- }
6615
- return [4 /*yield*/, Promise.all(JSON.parse(encryptedKeyShares).map(function (encryptedKeyShare, i) { return __awaiter(_this, void 0, void 0, function () {
6616
- var decryptedOnceKeyShare, decryptedTwiceKeyShare;
6617
- return __generator(this, function (_a) {
6618
- switch (_a.label) {
6619
- case 0:
6620
- logger.info("SDK::Wallet::export::encryptedKeyShare #".concat(i), encryptedKeyShare);
6621
- return [4 /*yield*/, this.cipher.decrypt({
6622
- cipherText: JSON.stringify(encryptedKeyShare),
6623
- inputKeyMaterial: inputKeyMaterial_1,
6634
+ encryptedOnceKeyShare = _a.sent();
6635
+ if (!encryptedOnceKeyShare) {
6636
+ throw new Error('Failed to encrypt key share with device public key');
6637
+ }
6638
+ return [4 /*yield*/, this.cipher.encrypt({
6639
+ data: JSON.stringify(encryptedOnceKeyShare),
6640
+ inputKeyMaterial: inputKeyMaterial,
6624
6641
  })];
6625
- case 1:
6626
- decryptedOnceKeyShare = _a.sent();
6627
- if (!decryptedOnceKeyShare) {
6628
- throw new Error('Something went wrong decrypting the data.');
6642
+ case 3:
6643
+ encryptedTwiceKeyShare = _a.sent();
6644
+ if (!encryptedTwiceKeyShare) {
6645
+ throw new Error('Failed to encrypt key share');
6629
6646
  }
6630
- console.log('SDK::Wallet::export::decryptWithPrivateKeyPayload', {
6631
- decryptedOnceKeyShare: decryptedOnceKeyShare,
6632
- decryptedDevicePrivateKey: decryptedDevicePrivateKey_1,
6633
- });
6634
- return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, JSON.parse(decryptedOnceKeyShare))];
6635
- case 2:
6636
- decryptedTwiceKeyShare = _a.sent();
6637
- console.log('SDK::Wallet::export::decryptWithPrivateKeyPayload', {
6638
- decryptedOnceKeyShare: decryptedOnceKeyShare,
6639
- decryptedDevicePrivateKey: decryptedDevicePrivateKey_1,
6640
- });
6641
- return [2 /*return*/, decryptedTwiceKeyShare];
6647
+ return [2 /*return*/, encryptedTwiceKeyShare];
6642
6648
  }
6643
6649
  });
6644
6650
  }); }))];
6645
- case 18:
6646
- decryptedKeyShares_1 = _f.sent();
6647
- logger.info('SDK::WalletService::export::decryptedKeyShares', { decryptedKeyShares: decryptedKeyShares_1, wallabyAuthPublicKey: wallabyAuthPublicKey });
6648
- readyForTransportKeyShares = decryptedKeyShares_1.map(function (decryptedShare) {
6649
- return encryptWithPublicKey(wallabyAuthPublicKey, JSON.stringify(decryptedShare));
6650
- });
6651
- logger.info('SDK::WalletService::export::readyForTransportKeyShares', readyForTransportKeyShares);
6652
- deviceAuthPublicKey = publicKeyByPrivateKey(decryptedDevicePrivateKey_1);
6653
- return [4 /*yield*/, this.handleConstructingSeedPhraseFromShares({
6654
- encryptedKeyShares: readyForTransportKeyShares,
6655
- walletId: walletId,
6656
- deviceAuthPublicKey: deviceAuthPublicKey,
6657
- })];
6658
- case 19:
6659
- encryptedReconstructedSeedPhrase = _f.sent();
6660
- encryptedSeed = JSON.stringify(encryptedReconstructedSeedPhrase);
6661
- decryptedSeedPhrase = decryptWithPrivateKey(decryptedDevicePrivateKey_1, encryptedReconstructedSeedPhrase);
6662
- if (!(exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.algorandSeedPhrase)) return [3 /*break*/, 22];
6663
- return [4 /*yield*/, this.getAlgorandSeedPhrase({
6664
- encryptedStandardSeedPhrase: encryptedReconstructedSeedPhrase,
6665
- authenticationOptions: {
6666
- wallabyAuthPublicKey: wallabyAuthPublicKey,
6667
- encryptionMechanism: encryptionMechanism,
6668
- password: password,
6669
- prfValue: prfValue,
6670
- },
6671
- })];
6672
- case 20:
6673
- algorandSeedPhrase = _f.sent();
6674
- return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, algorandSeedPhrase.convertedEncryptedSeed)];
6675
- case 21:
6676
- decryptedAlgorandSeedPhrase = _f.sent();
6677
- _f.label = 22;
6678
- case 22: return [2 /*return*/, {
6679
- keyShares: decryptedKeyShares,
6680
- standardSeedPhrase: decryptedSeedPhrase,
6681
- algorandSeedPhrase: decryptedAlgorandSeedPhrase,
6682
- }];
6683
- case 23:
6684
- error_1 = _f.sent();
6685
- logger.error('SDK::WalletService::export::error', error_1);
6686
- return [2 /*return*/, Promise.reject(error_1)];
6687
- case 24: return [2 /*return*/];
6651
+ case 1:
6652
+ encryptedKeyShares = _c.sent();
6653
+ logger.info('SDK::handleKeySharesFromResponse::encryptedKeyShares', encryptedKeyShares);
6654
+ return [4 /*yield*/, this.storageService.save(exports.LocalStorageItems.EncryptedKeyShares, JSON.stringify(encryptedKeyShares))];
6655
+ case 2:
6656
+ _c.sent();
6657
+ return [3 /*break*/, 4];
6658
+ case 3:
6659
+ error_1 = _c.sent();
6660
+ logger.error('SDK::handleKeySharesFromResponse::error', error_1);
6661
+ throw error_1;
6662
+ case 4: return [2 /*return*/];
6688
6663
  }
6689
6664
  });
6690
6665
  });
6691
6666
  };
6692
- /**
6693
- *
6694
- * @public
6695
- * Creates a new wallet, returns assets list, encrypted shares and/or encrypted seedPhrase.
6696
- *
6697
- * @param { boolean } storeSeedPhrase An option to either store the encrypted seed phrase from the response or to only return it in the response.
6698
- * @param { ReturnWalletOption } returnWalletOption An enum `@link ReturnWalletOption` for what options to return
6699
- * - `RETURN_ONLY_ENCRYPTED_SHARES` An option to split the seed phrase into multiple shares. It only return the shares based on a threshold.
6700
- * - `RETURN_ONLY_ENCRYPTED_SEED_PHRASE` An option to only return a seed phrase. (Similar to how v1 works)
6701
- * - `RETURN_BOTH_ENCRYPTED_SHARES_AND_SEED` An option to split the seed phrase and to add it to the response.
6702
- * @param authenticationOptions parameters carried from the authentication request (`signIn` | `signUp`)
6703
- * - { EncryptionMechanism } encryptionMechanism - The mechanism used for encryption.
6704
- * - { object } [user] - User details, required if `publicKey` is not available and 'prf' is the encryption mechanism:
6705
- * - { string } id - The user's unique identifier.
6706
- * - { string } firstName - The user's first name.
6707
- * - { string } email - The user's email address.
6708
- * - {PublicKeyCredential } [publicKey] - Used if the user is already registered.
6709
- * - { Uint8Array } [prfValue] - PRF value, used if 'prf' encryption is applied.
6710
- * - { string } [password] - The user's password, if required for encryption.
6711
- * - { string } [salt] - The salt value for password-based encryption.
6712
- * @param [keyShareOptions] - Required if the return option involves seed splitting (`RETURN_ONLY_ENCRYPTED_SHARES` or `RETURN_BOTH_ENCRYPTED_SHARES_AND_SEED`):
6713
- * - { number } `threshold`
6714
- * - { number } `numberOfKeyShares`
6715
- * - { number } `numberOfSharesReturned`
6716
- * - { boolean } `storeOneKeyShareWithWallaby`
6717
- * @returns {ImportWalletResponse} The wallet has been created successfully.
6718
- */
6719
- WalletService.prototype.import = function (input) {
6720
- return __awaiter(this, void 0, void 0, function () {
6721
- var encryptedDevicePrivateKey, decryptedDevicePrivateKey, _a, clientAuthPubKey, importRequest, error_2;
6722
- return __generator(this, function (_b) {
6723
- switch (_b.label) {
6724
- case 0:
6725
- _b.trys.push([0, 8, , 9]);
6726
- return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedDevicePrivateKey)];
6667
+ // async handleGenerateQRCode(input: {
6668
+ // walletId: string;
6669
+ // backUpUrl: string;
6670
+ // password?: string;
6671
+ // prfValue?: Uint8Array;
6672
+ // encryptionMechanism: EncryptionMechanism;
6673
+ // authResponse: AuthenticationResponse;
6674
+ // clientAuthToken?: string;
6675
+ // decryptedKeyShares: string[];
6676
+ // }): Promise<string | undefined> {
6677
+ // try {
6678
+ // const qrCode = await QRCode.toString(JSON.stringify(input));
6679
+ // console.log(qrCode);
6680
+ // return qrCode;
6681
+ // } catch (err) {
6682
+ // console.error(err);
6683
+ // return;
6684
+ // }
6685
+ // }
6686
+ WalletHandlersService.prototype.handleSeedPhraseFromResponse = function (_a) {
6687
+ return __awaiter(this, arguments, void 0, function (_b) {
6688
+ var decryptedSeedPhrase, deviceAuthPublicKey, encryptedOnceSeedPhrase, encryptedTwiceSeedPhrase;
6689
+ var encryptedSeed = _b.encryptedSeed, decryptedClientAuthPrivKey = _b.decryptedClientAuthPrivKey, inputKeyMaterial = _b.inputKeyMaterial, storeSeedPhrase = _b.storeSeedPhrase;
6690
+ return __generator(this, function (_c) {
6691
+ switch (_c.label) {
6692
+ case 0: return [4 /*yield*/, decryptWithPrivateKey(decryptedClientAuthPrivKey, encryptedSeed)];
6727
6693
  case 1:
6728
- encryptedDevicePrivateKey = _b.sent();
6729
- if (!encryptedDevicePrivateKey) {
6730
- throw new Error('Device private key is required');
6731
- }
6732
- decryptedDevicePrivateKey = void 0;
6733
- _a = input.authenticationOptions.encryptionMechanism;
6734
- switch (_a) {
6735
- case exports.EncryptionMechanism.Password: return [3 /*break*/, 2];
6736
- case exports.EncryptionMechanism.Prf: return [3 /*break*/, 4];
6737
- }
6738
- return [3 /*break*/, 6];
6694
+ decryptedSeedPhrase = _c.sent();
6695
+ if (!storeSeedPhrase) return [3 /*break*/, 5];
6696
+ deviceAuthPublicKey = publicKeyByPrivateKey(decryptedClientAuthPrivKey);
6697
+ return [4 /*yield*/, encryptWithPublicKey(deviceAuthPublicKey, decryptedSeedPhrase)];
6739
6698
  case 2:
6740
- if (!input.authenticationOptions.password || !input.authenticationOptions.salt) {
6741
- throw new Error('Password and salt are required.');
6742
- }
6743
- return [4 /*yield*/, this.cipher.decrypt({
6744
- cipherText: encryptedDevicePrivateKey,
6745
- inputKeyMaterial: utf8ToBytes$2("".concat(input.authenticationOptions.salt).concat(input.authenticationOptions.password)),
6699
+ encryptedOnceSeedPhrase = _c.sent();
6700
+ return [4 /*yield*/, this.cipher.encrypt({
6701
+ data: JSON.stringify(encryptedOnceSeedPhrase),
6702
+ inputKeyMaterial: inputKeyMaterial,
6746
6703
  })];
6747
6704
  case 3:
6748
- decryptedDevicePrivateKey = _b.sent();
6749
- return [3 /*break*/, 6];
6705
+ encryptedTwiceSeedPhrase = _c.sent();
6706
+ return [4 /*yield*/, this.storageService.save(exports.LocalStorageItems.EncryptedStandardSeedPhrase, JSON.stringify(encryptedTwiceSeedPhrase))];
6750
6707
  case 4:
6751
- if (!input.authenticationOptions.prfValue) {
6752
- throw new Error('PRF value is required');
6708
+ _c.sent();
6709
+ _c.label = 5;
6710
+ case 5: return [2 /*return*/, decryptedSeedPhrase];
6711
+ }
6712
+ });
6713
+ });
6714
+ };
6715
+ WalletHandlersService.prototype.handleRecoverFromSeedPhrase = function (_a) {
6716
+ return __awaiter(this, arguments, void 0, function (_b) {
6717
+ var initRecoveryFlowResponse, _c, signature, message, recoverResponse;
6718
+ var encryptedSeedPhrase = _b.encryptedSeedPhrase, deviceAuthKeys = _b.deviceAuthKeys;
6719
+ return __generator(this, function (_d) {
6720
+ switch (_d.label) {
6721
+ case 0: return [4 /*yield*/, this.request(WalletRoutes.initRecoveryFlow, {
6722
+ method: WalletRoutes.initRecoveryFlow.method,
6723
+ payload: {
6724
+ externalUserId: this.userId,
6725
+ clientAuthPubKey: deviceAuthKeys.publicKey,
6726
+ },
6727
+ })];
6728
+ case 1:
6729
+ initRecoveryFlowResponse = _d.sent();
6730
+ if (!initRecoveryFlowResponse || initRecoveryFlowResponse.error) {
6731
+ throw new Error('Failed to recover wallet from seed phrase');
6753
6732
  }
6754
- return [4 /*yield*/, this.cipher.decrypt({
6755
- cipherText: encryptedDevicePrivateKey,
6756
- inputKeyMaterial: input.authenticationOptions.prfValue,
6733
+ return [4 /*yield*/, this.authService.getSignature(deviceAuthKeys)];
6734
+ case 2:
6735
+ _c = _d.sent(), signature = _c.signature, message = _c.message;
6736
+ return [4 /*yield*/, this.request(WalletRoutes.initRecoveryFlow, {
6737
+ method: WalletRoutes.recover.method,
6738
+ payload: {
6739
+ externalUserId: this.userId,
6740
+ signature: signature,
6741
+ message: message,
6742
+ clientAuthPubKey: deviceAuthKeys.publicKey,
6743
+ encryptedSeed: encryptedSeedPhrase,
6744
+ },
6757
6745
  })];
6758
- case 5:
6759
- decryptedDevicePrivateKey = _b.sent();
6760
- return [3 /*break*/, 6];
6761
- case 6:
6762
- if (!decryptedDevicePrivateKey) {
6763
- throw new Error('Failed to decrypt device private key');
6746
+ case 3:
6747
+ recoverResponse = _d.sent();
6748
+ if (!recoverResponse || recoverResponse.error) {
6749
+ throw new Error('Failed to recover wallet from seed phrase');
6764
6750
  }
6765
- clientAuthPubKey = publicKeyByPrivateKey(decryptedDevicePrivateKey);
6766
- return [4 /*yield*/, this.handleImportWallet({
6767
- publicKey: clientAuthPubKey,
6768
- privateKey: decryptedDevicePrivateKey,
6769
- }, input)];
6770
- case 7:
6771
- importRequest = _b.sent();
6772
- return [2 /*return*/, importRequest];
6773
- case 8:
6774
- error_2 = _b.sent();
6775
- logger.error('SDK::WalletService::import::error', error_2);
6751
+ return [2 /*return*/, recoverResponse];
6752
+ }
6753
+ });
6754
+ });
6755
+ };
6756
+ WalletHandlersService.prototype.handleConstructingSeedPhraseFromShares = function (_a) {
6757
+ return __awaiter(this, arguments, void 0, function (_b) {
6758
+ var retrieveSeedPhraseResponse, error_2;
6759
+ var encryptedKeyShares = _b.encryptedKeyShares, walletId = _b.walletId, deviceAuthPublicKey = _b.deviceAuthPublicKey;
6760
+ return __generator(this, function (_c) {
6761
+ switch (_c.label) {
6762
+ case 0:
6763
+ _c.trys.push([0, 2, , 3]);
6764
+ logger.info('SDK::handleConstructingSeedPhraseFromShares::payload', {
6765
+ encryptedKeyShares: encryptedKeyShares,
6766
+ walletId: walletId,
6767
+ deviceAuthPublicKey: deviceAuthPublicKey,
6768
+ });
6769
+ return [4 /*yield*/, this.request(WalletRoutes.retrieveSeedPhrase, {
6770
+ method: WalletRoutes.retrieveSeedPhrase.method,
6771
+ payload: {
6772
+ walletId: walletId,
6773
+ recoveryPubKey: deviceAuthPublicKey,
6774
+ encryptedShares: encryptedKeyShares,
6775
+ },
6776
+ })];
6777
+ case 1:
6778
+ retrieveSeedPhraseResponse = _c.sent();
6779
+ if (!retrieveSeedPhraseResponse || retrieveSeedPhraseResponse.error) {
6780
+ throw new Error('Failed to retrieve seed phrase');
6781
+ }
6782
+ return [2 /*return*/, retrieveSeedPhraseResponse.encryptedSeed];
6783
+ case 2:
6784
+ error_2 = _c.sent();
6785
+ logger.error('SDK::handleConstructingSeedPhraseFromShares::error', error_2);
6776
6786
  return [2 /*return*/, Promise.reject(error_2)];
6777
- case 9: return [2 /*return*/];
6787
+ case 3: return [2 /*return*/];
6778
6788
  }
6779
6789
  });
6780
6790
  });
6781
6791
  };
6792
+ return WalletHandlersService;
6793
+ }(Base));
6794
+
6795
+ var WalletService = /** @class */ (function (_super) {
6796
+ __extends(WalletService, _super);
6797
+ function WalletService(config) {
6798
+ var _this = _super.call(this, config) || this;
6799
+ _this.authService = new AuthService(config);
6800
+ _this.handlers = new WalletHandlersService(config);
6801
+ _this.cipher = new Cipher(_this.device);
6802
+ _this.userId = config.userId;
6803
+ return _this;
6804
+ }
6782
6805
  /**
6783
6806
  *
6784
6807
  * @public
@@ -6808,7 +6831,7 @@ var WalletService = /** @class */ (function (_super) {
6808
6831
  */
6809
6832
  WalletService.prototype.create = function (input) {
6810
6833
  return __awaiter(this, void 0, void 0, function () {
6811
- var encryptedDevicePrivateKey, decryptedDevicePrivateKey, _a, clientAuthPubKey, walletCreationResponse, error_3;
6834
+ var encryptedDevicePrivateKey, decryptedDevicePrivateKey, _a, clientAuthPubKey, walletCreationResponse, error_1;
6812
6835
  return __generator(this, function (_b) {
6813
6836
  switch (_b.label) {
6814
6837
  case 0:
@@ -6853,7 +6876,7 @@ var WalletService = /** @class */ (function (_super) {
6853
6876
  throw new Error('Failed to decrypt device private key');
6854
6877
  }
6855
6878
  clientAuthPubKey = publicKeyByPrivateKey(decryptedDevicePrivateKey);
6856
- return [4 /*yield*/, this.handleWalletCreation({
6879
+ return [4 /*yield*/, this.handlers.handleWalletCreation({
6857
6880
  publicKey: clientAuthPubKey,
6858
6881
  privateKey: decryptedDevicePrivateKey,
6859
6882
  }, input)];
@@ -6861,589 +6884,568 @@ var WalletService = /** @class */ (function (_super) {
6861
6884
  walletCreationResponse = _b.sent();
6862
6885
  return [2 /*return*/, walletCreationResponse];
6863
6886
  case 8:
6864
- error_3 = _b.sent();
6865
- logger.error('SDK::Wallet::createV2::error', error_3);
6866
- return [2 /*return*/, Promise.reject(error_3)];
6887
+ error_1 = _b.sent();
6888
+ logger.error('SDK::Wallet::createV2::error', error_1);
6889
+ return [2 /*return*/, Promise.reject(error_1)];
6867
6890
  case 9: return [2 /*return*/];
6868
6891
  }
6869
6892
  });
6870
6893
  });
6871
6894
  };
6872
- WalletService.prototype.recover = function (input) {
6873
- return __awaiter(this, void 0, void 0, function () {
6874
- var deviceAuthKeyPair, inputKeyMaterial, recoverWithSeedPhraseResponse, encryptedShares, encryptedSeed, error_4;
6875
- var _a;
6876
- return __generator(this, function (_b) {
6877
- switch (_b.label) {
6895
+ /**
6896
+ * @public
6897
+ * Exports a encrypted shares and/or encrypted seedPhrase(s).
6898
+ *
6899
+ * @param {string} wallabyAuthPublicKey - Wallaby auth public key. Can be passed from [AuthResponse] (`signIn` | `signUp`)
6900
+ * @param {EncryptionMechanism} encryptionMechanism - The mechanism used for encrypting the wallet data.
6901
+ * @param {string} [password] - The password for additional encryption, if required.
6902
+ * @param {Uint8Array} [prfValue] - The PRF value for encryption, used if applicable.
6903
+ * @param {object} exportOptions - Specifies the options for exporting the wallet:
6904
+ * - { boolean } standardSeedPhrase - Includes the standard seed phrase (bip32 standard 24 words mnemonic) in the export.
6905
+ * - { boolean } algorandSeedPhrase - Includes the Algorand seed phrase (25 words mnemonic) in the export.
6906
+ * - { boolean } keyShares - Includes key shares in the export.
6907
+ */
6908
+ WalletService.prototype.export = function (_a) {
6909
+ return __awaiter(this, arguments, void 0, function (_b) {
6910
+ var encryptedSeed, encryptedKeyShares, encryptedDeviceAuthPrivateKey, decryptedKeyShares, decryptedSeedPhrase, decryptedAlgorandSeedPhrase, inputKeyMaterial_1, _c, _d, _e, decryptedDevicePrivateKey_1, decryptedOnceSeedPhrase, decryptedTwiceSeedPhrase, algorandSeedPhrase, decryptedKeyShares_1, readyForTransportKeyShares, deviceAuthPublicKey, encryptedReconstructedSeedPhrase, algorandSeedPhrase, error_2;
6911
+ var _this = this;
6912
+ var walletId = _b.walletId, wallabyAuthPublicKey = _b.wallabyAuthPublicKey, prfValue = _b.prfValue, password = _b.password, encryptionMechanism = _b.encryptionMechanism, exportOptions = _b.exportOptions;
6913
+ return __generator(this, function (_f) {
6914
+ switch (_f.label) {
6878
6915
  case 0:
6879
- _b.trys.push([0, 11, , 12]);
6880
- deviceAuthKeyPair = createIdentity();
6881
- inputKeyMaterial = void 0;
6882
- switch (input.authenticationOptions.encryptionMechanism) {
6883
- case exports.EncryptionMechanism.Password:
6884
- if (!input.authenticationOptions.password || !input.authenticationOptions.salt) {
6885
- throw new Error('Password and salt are required.');
6886
- }
6887
- inputKeyMaterial = utf8ToBytes$2("".concat(input.authenticationOptions.salt).concat(input.authenticationOptions.password));
6888
- break;
6889
- case exports.EncryptionMechanism.Prf:
6890
- if (!input.authenticationOptions.prfValue) {
6891
- throw new Error('PRF value is required');
6892
- }
6893
- inputKeyMaterial = input.authenticationOptions.prfValue;
6894
- break;
6916
+ _f.trys.push([0, 23, , 24]);
6917
+ if (!exportOptions ||
6918
+ (!exportOptions.algorandSeedPhrase && !exportOptions.standardSeedPhrase && !exportOptions.keyShares)) {
6919
+ throw new Error('exportOptions must be provided with at least one of the following options: algorandSeedPhrase, standardSeedPhrase, keyShares');
6895
6920
  }
6896
- if (!input.decryptedSeedPhrase) return [3 /*break*/, 6];
6897
- console.log('SDK::WalletService::recover:: seed phrase was found in input', {
6898
- seed: input.decryptedSeedPhrase,
6899
- wallabyPub: input.wallabyAuthPubKey,
6900
- });
6901
- return [4 /*yield*/, this.handleRecoverFromSeedPhrase({
6902
- decryptedSeedPhrase: input.decryptedSeedPhrase,
6903
- deviceAuthKeys: { publicKey: deviceAuthKeyPair.publicKey, privateKey: deviceAuthKeyPair.privateKey },
6904
- })];
6921
+ return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedStandardSeedPhrase)];
6905
6922
  case 1:
6906
- recoverWithSeedPhraseResponse = _b.sent();
6907
- if (!recoverWithSeedPhraseResponse.encryptedSeed) return [3 /*break*/, 3];
6908
- // will take care of storing the seed phrase if needed.
6909
- return [4 /*yield*/, this.handleSeedPhraseFromResponse({
6910
- encryptedSeed: recoverWithSeedPhraseResponse.encryptedSeed,
6911
- decryptedClientAuthPrivKey: deviceAuthKeyPair.privateKey,
6912
- inputKeyMaterial: inputKeyMaterial,
6913
- storeSeedPhrase: input.storeSeedPhrase,
6914
- })];
6923
+ encryptedSeed = _f.sent();
6924
+ return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedKeyShares)];
6915
6925
  case 2:
6916
- // will take care of storing the seed phrase if needed.
6917
- _b.sent();
6918
- _b.label = 3;
6926
+ encryptedKeyShares = _f.sent();
6927
+ return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedDevicePrivateKey)];
6919
6928
  case 3:
6920
- if (!recoverWithSeedPhraseResponse.shares) return [3 /*break*/, 5];
6921
- return [4 /*yield*/, this.handleKeySharesFromResponse({
6922
- keyShares: recoverWithSeedPhraseResponse.shares,
6923
- decryptedClientAuthPrivKey: deviceAuthKeyPair.privateKey,
6924
- inputKeyMaterial: inputKeyMaterial,
6925
- })];
6929
+ encryptedDeviceAuthPrivateKey = _f.sent();
6930
+ decryptedKeyShares = void 0;
6931
+ decryptedSeedPhrase = void 0;
6932
+ decryptedAlgorandSeedPhrase = void 0;
6933
+ _c = encryptionMechanism;
6934
+ switch (_c) {
6935
+ case exports.EncryptionMechanism.Password: return [3 /*break*/, 4];
6936
+ case exports.EncryptionMechanism.Prf: return [3 /*break*/, 6];
6937
+ }
6938
+ return [3 /*break*/, 7];
6926
6939
  case 4:
6927
- _b.sent();
6928
- _b.label = 5;
6940
+ if (!password) {
6941
+ throw new Error('password is required for this encryption mechanism type.');
6942
+ }
6943
+ _d = utf8ToBytes$2;
6944
+ _e = "".concat;
6945
+ return [4 /*yield*/, this.authService.getUserSalt()];
6929
6946
  case 5:
6930
- this.storageService.saveInBulk([
6931
- {
6932
- key: exports.LocalStorageItems.EncryptedDevicePrivateKey,
6933
- value: deviceAuthKeyPair.privateKey,
6934
- },
6935
- {
6936
- key: exports.LocalStorageItems.ClientAuthPublicKey,
6937
- value: deviceAuthKeyPair.publicKey,
6938
- },
6939
- ]);
6940
- return [2 /*return*/, recoverWithSeedPhraseResponse];
6947
+ inputKeyMaterial_1 = _d.apply(void 0, [_e.apply("", [_f.sent()]).concat(password)]);
6948
+ return [3 /*break*/, 7];
6941
6949
  case 6:
6942
- if (!input.decryptedShares) return [3 /*break*/, 9];
6943
- encryptedShares = (_a = input.decryptedShares) === null || _a === void 0 ? void 0 : _a.map(function (decryptedShare) {
6944
- return encryptWithPublicKey(input.wallabyAuthPubKey, decryptedShare);
6945
- });
6946
- if (!encryptedShares) {
6947
- throw new Error('Decrypted shares are required');
6950
+ if (!prfValue) {
6951
+ throw new Error('prfValue is required for this encryption mechanism type.');
6948
6952
  }
6949
- return [4 /*yield*/, this.handleConstructingSeedPhraseFromShares({
6950
- encryptedKeyShares: encryptedShares,
6951
- deviceAuthPublicKey: deviceAuthKeyPair.publicKey,
6952
- walletId: input.walletId,
6953
- })];
6953
+ inputKeyMaterial_1 = prfValue;
6954
+ return [3 /*break*/, 7];
6954
6955
  case 7:
6955
- encryptedSeed = _b.sent();
6956
- return [4 /*yield*/, this.handleSeedPhraseFromResponse({
6957
- encryptedSeed: encryptedSeed,
6958
- decryptedClientAuthPrivKey: deviceAuthKeyPair.privateKey,
6959
- inputKeyMaterial: inputKeyMaterial,
6960
- storeSeedPhrase: input.storeSeedPhrase,
6956
+ if (!encryptedDeviceAuthPrivateKey) {
6957
+ throw new Error('Device private key is required to export the wallet');
6958
+ }
6959
+ return [4 /*yield*/, this.cipher.decrypt({
6960
+ cipherText: encryptedDeviceAuthPrivateKey,
6961
+ inputKeyMaterial: inputKeyMaterial_1,
6961
6962
  })];
6962
6963
  case 8:
6963
- _b.sent();
6964
- return [3 /*break*/, 10];
6965
- case 9: throw new Error('Decrypted seed phrase or shares are required to recover the wallet.');
6966
- case 10: return [2 /*return*/, { standardSeedPhrase: input.decryptedSeedPhrase }];
6967
- case 11:
6968
- error_4 = _b.sent();
6969
- logger.error('SDK::Wallet::recover::error', error_4);
6970
- return [2 /*return*/, Promise.reject(error_4)];
6971
- case 12: return [2 /*return*/];
6972
- }
6973
- });
6974
- });
6975
- };
6976
- /**
6977
- * @public
6978
- * Retrieves the encrypted shares for a specified wallet
6979
- *
6980
- * @param {string} walletId - The Wallaby wallet ID.
6981
- * @param {string} [password] - The password for decrypting the shares, if required.
6982
- * @param {Uint8Array} [prfValue] - The PRF value used for encryption, if applicable.
6983
- * @param {EncryptionMechanism} encryptionMechanism - The mechanism used to encrypt the shares.
6984
- *
6985
- * @returns {Promise<EncryptedSharesResponse>} A promise that resolves with the encrypted shares.
6986
- */
6987
- WalletService.prototype.retrieveShares = function (_a) {
6988
- return __awaiter(this, arguments, void 0, function (_b) {
6989
- var encryptedDevicePrivateKey, decryptedClientAuthPrivKey, _c, _d, _e, _f, _g, deviceAuthPublicKey, signatureMessage, response, _h, _j, _k, _l, error_5;
6990
- var _m, _o;
6991
- var walletId = _b.walletId, password = _b.password, prfValue = _b.prfValue, encryptionMechanism = _b.encryptionMechanism;
6992
- return __generator(this, function (_p) {
6993
- switch (_p.label) {
6994
- case 0:
6995
- _p.trys.push([0, 12, , 13]);
6996
- return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedDevicePrivateKey)];
6997
- case 1:
6998
- encryptedDevicePrivateKey = _p.sent();
6999
- if (!encryptedDevicePrivateKey) {
7000
- throw new Error('Device private was not found');
6964
+ decryptedDevicePrivateKey_1 = _f.sent();
6965
+ console.log('SDK::Wallet::export::decryptedDevicePrivateKey', decryptedDevicePrivateKey_1);
6966
+ if (!decryptedDevicePrivateKey_1) {
6967
+ throw new Error('Device private key could not be decrypted');
7001
6968
  }
7002
- _d = (_c = this.cipher).decrypt;
7003
- _m = {
7004
- cipherText: encryptedDevicePrivateKey
7005
- };
7006
- if (!(encryptionMechanism === exports.EncryptionMechanism.Password)) return [3 /*break*/, 3];
7007
- _f = utf8ToBytes$2;
7008
- _g = "".concat;
7009
- return [4 /*yield*/, this.authService.getUserSalt()];
7010
- case 2:
7011
- _e = _f.apply(void 0, [_g.apply("", [_p.sent()]).concat(password)]);
7012
- return [3 /*break*/, 4];
7013
- case 3:
7014
- _e = prfValue;
7015
- _p.label = 4;
7016
- case 4: return [4 /*yield*/, _d.apply(_c, [(_m.inputKeyMaterial = _e,
7017
- _m)])];
7018
- case 5:
7019
- decryptedClientAuthPrivKey = _p.sent();
7020
- if (!decryptedClientAuthPrivKey) {
7021
- throw new Error('Failed to decrypt device private key');
6969
+ if (!(exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.keyShares)) return [3 /*break*/, 10];
6970
+ if (!encryptedKeyShares) {
6971
+ throw new Error('You do not have any key shares stored on this device.');
7022
6972
  }
7023
- deviceAuthPublicKey = publicKeyByPrivateKey(decryptedClientAuthPrivKey);
7024
- return [4 /*yield*/, this.authService.getSignature({
7025
- publicKey: deviceAuthPublicKey,
7026
- privateKey: decryptedClientAuthPrivKey,
7027
- })];
7028
- case 6:
7029
- signatureMessage = _p.sent();
7030
- return [4 /*yield*/, this.request(WalletRoutes.retrieveShares, {
7031
- method: WalletRoutes.retrieveShares.method,
7032
- payload: {
7033
- walletId: walletId,
7034
- signature: signatureMessage.signature,
7035
- message: signatureMessage.message,
7036
- clientAuthPubKey: deviceAuthPublicKey,
7037
- },
7038
- })];
7039
- case 7:
7040
- response = _p.sent();
7041
- if (!response || response.error) {
7042
- throw new Error('Failed to retrieve shares');
7043
- }
7044
- _h = this.handleKeySharesFromResponse;
7045
- _o = {
7046
- keyShares: response.shares,
7047
- decryptedClientAuthPrivKey: decryptedClientAuthPrivKey
7048
- };
7049
- if (!(encryptionMechanism === exports.EncryptionMechanism.Password)) return [3 /*break*/, 9];
7050
- _k = utf8ToBytes$2;
7051
- _l = "".concat;
7052
- return [4 /*yield*/, this.authService.getUserSalt()];
7053
- case 8:
7054
- _j = _k.apply(void 0, [_l.apply("", [_p.sent()]).concat(password)]);
7055
- return [3 /*break*/, 10];
6973
+ return [4 /*yield*/, Promise.all(JSON.parse(encryptedKeyShares).map(function (encryptedKeyShare, i) { return __awaiter(_this, void 0, void 0, function () {
6974
+ var decryptedOnceKeyShare, decryptedTwiceKeyShare;
6975
+ return __generator(this, function (_a) {
6976
+ switch (_a.label) {
6977
+ case 0:
6978
+ logger.info("SDK::Wallet::export::encryptedKeyShare #".concat(i), encryptedKeyShare);
6979
+ return [4 /*yield*/, this.cipher.decrypt({
6980
+ cipherText: JSON.stringify(encryptedKeyShare),
6981
+ inputKeyMaterial: inputKeyMaterial_1,
6982
+ })];
6983
+ case 1:
6984
+ decryptedOnceKeyShare = _a.sent();
6985
+ if (!decryptedOnceKeyShare) {
6986
+ throw new Error('Something went wrong decrypting the data.');
6987
+ }
6988
+ console.log('SDK::Wallet::export::decryptWithPrivateKeyPayload', {
6989
+ decryptedOnceKeyShare: decryptedOnceKeyShare,
6990
+ decryptedDevicePrivateKey: decryptedDevicePrivateKey_1,
6991
+ });
6992
+ return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, JSON.parse(decryptedOnceKeyShare))];
6993
+ case 2:
6994
+ decryptedTwiceKeyShare = _a.sent();
6995
+ return [2 /*return*/, decryptedTwiceKeyShare];
6996
+ }
6997
+ });
6998
+ }); }))];
7056
6999
  case 9:
7057
- _j = prfValue;
7058
- _p.label = 10;
7059
- case 10: return [4 /*yield*/, _h.apply(this, [(_o.inputKeyMaterial = _j,
7060
- _o)])];
7061
- case 11:
7062
- _p.sent();
7063
- return [2 /*return*/, response];
7064
- case 12:
7065
- error_5 = _p.sent();
7066
- logger.error('SDK::Wallet::retrieveShares::error', error_5);
7067
- return [2 /*return*/, Promise.reject(error_5)];
7068
- case 13: return [2 /*return*/];
7069
- }
7070
- });
7071
- });
7072
- };
7073
- /**
7074
- * @public
7075
- * Updates the wallet password.
7076
- *
7077
- * @param {string} oldPassword - The Wallaby wallet ID.
7078
- * @param {string} newPassword - The Wallaby asset ID.
7079
- *
7080
- * @returns {Promise<boolean>} True/False.
7081
- */
7082
- WalletService.prototype.changePassword = function (_a) {
7083
- return __awaiter(this, arguments, void 0, function (_b) {
7084
- var decryptedDevicePrivateKey, clientSalt, decryptedSeedPhrase, error_6;
7085
- var oldPassword = _b.oldPassword, newPassword = _b.newPassword;
7086
- return __generator(this, function (_c) {
7087
- switch (_c.label) {
7088
- case 0:
7089
- _c.trys.push([0, 6, , 7]);
7090
- return [4 /*yield*/, this.storageService.getDecryptedPrivateKey(oldPassword)];
7091
- case 1:
7092
- decryptedDevicePrivateKey = _c.sent();
7093
- if (!decryptedDevicePrivateKey) {
7094
- throw new Error('Password is not valid, device private key could not be decrypted.');
7095
- }
7096
- return [4 /*yield*/, this.authService.getUserSalt()];
7097
- case 2:
7098
- clientSalt = _c.sent();
7099
- return [4 /*yield*/, this.storageService.getDecryptedSeedPhrase({
7100
- password: oldPassword,
7101
- seedPhraseType: exports.SeedPhraseType.Standard,
7102
- getUserSalt: this.authService.getUserSalt,
7103
- salt: clientSalt,
7104
- })];
7105
- case 3:
7106
- decryptedSeedPhrase = _c.sent();
7107
- if (!decryptedSeedPhrase) {
7108
- throw new Error('Password is not valid, seed phrase could not be decrypted, please recover the wallet.');
7109
- }
7110
- return [4 /*yield*/, this.storageService.setEncryptedPrivateKey(decryptedDevicePrivateKey, newPassword)];
7111
- case 4:
7112
- _c.sent();
7113
- return [4 /*yield*/, this.storageService.setEncryptedSeedPhrase({
7114
- decryptedSeedPhrase: decryptedSeedPhrase,
7115
- seedPhraseType: exports.SeedPhraseType.Standard,
7116
- password: newPassword,
7117
- deviceAuthPublicKey: publicKeyByPrivateKey(decryptedDevicePrivateKey),
7118
- getUserSalt: this.authService.getUserSalt,
7119
- salt: clientSalt,
7120
- })];
7121
- case 5:
7122
- _c.sent();
7123
- return [2 /*return*/, true];
7124
- case 6:
7125
- error_6 = _c.sent();
7126
- logger.error('SDK::Wallet::changePassword::error', error_6);
7127
- return [2 /*return*/, Promise.reject(false)];
7128
- case 7: return [2 /*return*/];
7129
- }
7130
- });
7131
- });
7132
- };
7133
- /**
7134
- *
7135
- * @public
7136
- * Create a newly fresh wallet, returns asset list and encrypted seedPhrase
7137
- * @param { string } address Wallet address
7138
- * @param { string } symbol Token symbol
7139
- * @param { BlockchainName } chainName the chain name enum. i.e. 'ETHEREUM', 'BITCOIN'
7140
- * @returns { CreateWalletResponse } The wallet has been created successfully
7141
- */
7142
- WalletService.prototype.getTokenBalance = function (_a) {
7143
- return __awaiter(this, arguments, void 0, function (_b) {
7144
- var response, error_7;
7145
- var address = _b.address, symbol = _b.symbol, tokenStandard = _b.tokenStandard, chainName = _b.chainName;
7146
- return __generator(this, function (_c) {
7147
- switch (_c.label) {
7148
- case 0:
7149
- _c.trys.push([0, 2, , 3]);
7150
- return [4 /*yield*/, this.request(WalletRoutes.getTokenBalance, {
7151
- method: WalletRoutes.getTokenBalance.method,
7152
- payload: { address: address, symbol: symbol, tokenStandard: tokenStandard, chainName: chainName },
7000
+ decryptedKeyShares = _f.sent();
7001
+ _f.label = 10;
7002
+ case 10:
7003
+ if (!((exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.standardSeedPhrase) || (exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.algorandSeedPhrase))) return [3 /*break*/, 22];
7004
+ if (!encryptedSeed) return [3 /*break*/, 17];
7005
+ console.log('SDK::Wallet::export::encryptedSeed', encryptedSeed);
7006
+ if (!(exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.standardSeedPhrase)) return [3 /*break*/, 13];
7007
+ return [4 /*yield*/, this.cipher.decrypt({
7008
+ cipherText: encryptedSeed,
7009
+ inputKeyMaterial: inputKeyMaterial_1,
7153
7010
  })];
7154
- case 1:
7155
- response = _c.sent();
7156
- if (!response || response.error) {
7157
- throw new Error(response.error);
7011
+ case 11:
7012
+ decryptedOnceSeedPhrase = _f.sent();
7013
+ if (!decryptedOnceSeedPhrase) {
7014
+ throw new Error('Something went wrong decrypting the seed phrase.');
7158
7015
  }
7159
- return [2 /*return*/, response];
7160
- case 2:
7161
- error_7 = _c.sent();
7162
- logger.error('SDK::Wallet::getTokenBalance::error', error_7);
7163
- return [2 /*return*/, Promise.reject(error_7)];
7164
- case 3: return [2 /*return*/];
7165
- }
7166
- });
7167
- });
7168
- };
7169
- /**
7170
- *
7171
- * @public
7172
- * Fetches the balances for a group of tokens in bulk.
7173
- * Takes an array of addresses and tokens and return their balances.
7174
- * @param { string } address Wallet address
7175
- * @param { string } symbol Token symbol
7176
- * @param { BlockchainName } chainName the chain name enum. i.e. 'ETHEREUM', 'BITCOIN'
7177
- * @returns { TokenBalanceResponse[] } Returns an array of the requested tokens balances.
7178
- */
7179
- WalletService.prototype.getMultiTokenBalances = function (tokensAndAddressesList) {
7180
- return __awaiter(this, void 0, void 0, function () {
7181
- var response, error_8;
7182
- return __generator(this, function (_a) {
7183
- switch (_a.label) {
7184
- case 0:
7185
- _a.trys.push([0, 2, , 3]);
7186
- return [4 /*yield*/, this.request(WalletRoutes.getMultiTokenBalances, {
7187
- method: WalletRoutes.getMultiTokenBalances.method,
7188
- payload: { tokensAndAddressesList: tokensAndAddressesList },
7016
+ return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, JSON.parse(decryptedOnceSeedPhrase))];
7017
+ case 12:
7018
+ decryptedTwiceSeedPhrase = _f.sent();
7019
+ decryptedSeedPhrase = decryptedTwiceSeedPhrase;
7020
+ _f.label = 13;
7021
+ case 13:
7022
+ if (!(exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.algorandSeedPhrase)) return [3 /*break*/, 16];
7023
+ return [4 /*yield*/, this.getAlgorandSeedPhrase({
7024
+ encryptedStandardSeedPhrase: JSON.parse(encryptedSeed),
7025
+ authenticationOptions: {
7026
+ wallabyAuthPublicKey: wallabyAuthPublicKey,
7027
+ encryptionMechanism: encryptionMechanism,
7028
+ password: password,
7029
+ prfValue: prfValue,
7030
+ },
7189
7031
  })];
7190
- case 1:
7191
- response = _a.sent();
7192
- if (!response || response.error) {
7193
- throw new Error(response.error);
7032
+ case 14:
7033
+ algorandSeedPhrase = _f.sent();
7034
+ return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, algorandSeedPhrase.convertedEncryptedSeed)];
7035
+ case 15:
7036
+ decryptedAlgorandSeedPhrase = _f.sent();
7037
+ if (!decryptedAlgorandSeedPhrase) {
7038
+ throw new Error('Something went wrong decrypting algorand seed phrase.');
7194
7039
  }
7195
- return [2 /*return*/, response];
7196
- case 2:
7197
- error_8 = _a.sent();
7198
- logger.error('SDK::Wallet::getTokenBalance::error', error_8);
7199
- return [2 /*return*/, Promise.reject(error_8)];
7200
- case 3: return [2 /*return*/];
7201
- }
7202
- });
7203
- });
7204
- };
7205
- /**
7206
- *
7207
- * @public
7208
- * Fetches the balances for all tokens in a specific wallet.
7209
- * @param { string } walletId Wallet address
7210
- * @returns { TokenBalanceResponse[] } Returns an array of the requested tokens balances.
7211
- */
7212
- WalletService.prototype.getWalletBalances = function (_a) {
7213
- return __awaiter(this, arguments, void 0, function (_b) {
7214
- var response, error_9;
7215
- var walletId = _b.walletId;
7216
- return __generator(this, function (_c) {
7217
- switch (_c.label) {
7218
- case 0:
7219
- _c.trys.push([0, 2, , 3]);
7220
- return [4 /*yield*/, this.request(WalletRoutes.getWalletBalances, {
7221
- method: WalletRoutes.getWalletBalances.method,
7222
- payload: { walletId: walletId },
7223
- })];
7224
- case 1:
7225
- response = _c.sent();
7226
- if (!response || response.error) {
7227
- throw new Error(response.error);
7040
+ // decryptedAlgorandSeedPhrase = await this.cipher.prepDataForDisplay({
7041
+ // wallabyAuthPublicKey: wallabyAuthPublicKey,
7042
+ // storedEncryptedData: JSON.stringify(algorandSeedPhrase.convertedEncryptedSeed),
7043
+ // encryptionMechanism,
7044
+ // encryptedDevicePrivateKey: encryptedDeviceAuthPrivateKey,
7045
+ // prfValue,
7046
+ // password,
7047
+ // getUserSalt: this.authService.getUserSalt,
7048
+ // });
7049
+ console.log('SDK::Wallet::export::decryptedAlgorandSeedPhrase', decryptedAlgorandSeedPhrase);
7050
+ _f.label = 16;
7051
+ case 16: return [3 /*break*/, 22];
7052
+ case 17:
7053
+ if (!encryptedKeyShares) {
7054
+ throw new Error('You do not have a wallet stored on this device.');
7228
7055
  }
7229
- return [2 /*return*/, response];
7230
- case 2:
7231
- error_9 = _c.sent();
7232
- logger.error('SDK::Wallet::getTokenBalance::error', error_9);
7233
- return [2 /*return*/, Promise.reject(error_9)];
7234
- case 3: return [2 /*return*/];
7235
- }
7236
- });
7237
- });
7238
- };
7239
- /**
7240
- *
7241
- * Get all user assets across all wallets by specifying user id
7242
- * @param userId Wallaby's user ID
7243
- * @param queryOptions Optional query options
7244
- * @returns {UserWalletsResponse} Returns an array of user assets by specifying user id
7245
- */
7246
- WalletService.prototype.getUserAssets = function (_a) {
7247
- return __awaiter(this, arguments, void 0, function (_b) {
7248
- var response, error_10;
7249
- var userId = _b.userId, queryOptions = _b.queryOptions;
7250
- return __generator(this, function (_c) {
7251
- switch (_c.label) {
7252
- case 0:
7253
- _c.trys.push([0, 2, , 3]);
7254
- return [4 /*yield*/, this.request(WalletRoutes.getUserAssets, {
7255
- method: WalletRoutes.getUserAssets.method,
7256
- payload: {
7257
- userId: userId,
7056
+ return [4 /*yield*/, Promise.all(JSON.parse(encryptedKeyShares).map(function (encryptedKeyShare, i) { return __awaiter(_this, void 0, void 0, function () {
7057
+ var decryptedOnceKeyShare, decryptedTwiceKeyShare;
7058
+ return __generator(this, function (_a) {
7059
+ switch (_a.label) {
7060
+ case 0:
7061
+ logger.info("SDK::Wallet::export::encryptedKeyShare #".concat(i), encryptedKeyShare);
7062
+ return [4 /*yield*/, this.cipher.decrypt({
7063
+ cipherText: JSON.stringify(encryptedKeyShare),
7064
+ inputKeyMaterial: inputKeyMaterial_1,
7065
+ })];
7066
+ case 1:
7067
+ decryptedOnceKeyShare = _a.sent();
7068
+ if (!decryptedOnceKeyShare) {
7069
+ throw new Error('Something went wrong decrypting the data.');
7070
+ }
7071
+ console.log('SDK::Wallet::export::decryptWithPrivateKeyPayload', {
7072
+ decryptedOnceKeyShare: decryptedOnceKeyShare,
7073
+ decryptedDevicePrivateKey: decryptedDevicePrivateKey_1,
7074
+ });
7075
+ return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, JSON.parse(decryptedOnceKeyShare))];
7076
+ case 2:
7077
+ decryptedTwiceKeyShare = _a.sent();
7078
+ console.log('SDK::Wallet::export::decryptWithPrivateKeyPayload', {
7079
+ decryptedOnceKeyShare: decryptedOnceKeyShare,
7080
+ decryptedDevicePrivateKey: decryptedDevicePrivateKey_1,
7081
+ });
7082
+ return [2 /*return*/, decryptedTwiceKeyShare];
7083
+ }
7084
+ });
7085
+ }); }))];
7086
+ case 18:
7087
+ decryptedKeyShares_1 = _f.sent();
7088
+ logger.info('SDK::WalletService::export::decryptedKeyShares', { decryptedKeyShares: decryptedKeyShares_1, wallabyAuthPublicKey: wallabyAuthPublicKey });
7089
+ readyForTransportKeyShares = decryptedKeyShares_1.map(function (decryptedShare) {
7090
+ return encryptWithPublicKey(wallabyAuthPublicKey, JSON.stringify(decryptedShare));
7091
+ });
7092
+ logger.info('SDK::WalletService::export::readyForTransportKeyShares', readyForTransportKeyShares);
7093
+ deviceAuthPublicKey = publicKeyByPrivateKey(decryptedDevicePrivateKey_1);
7094
+ return [4 /*yield*/, this.handlers.handleConstructingSeedPhraseFromShares({
7095
+ encryptedKeyShares: [readyForTransportKeyShares[0]],
7096
+ walletId: walletId,
7097
+ deviceAuthPublicKey: deviceAuthPublicKey,
7098
+ })];
7099
+ case 19:
7100
+ encryptedReconstructedSeedPhrase = _f.sent();
7101
+ encryptedSeed = JSON.stringify(encryptedReconstructedSeedPhrase);
7102
+ decryptedSeedPhrase = decryptWithPrivateKey(decryptedDevicePrivateKey_1, encryptedReconstructedSeedPhrase);
7103
+ if (!(exportOptions === null || exportOptions === void 0 ? void 0 : exportOptions.algorandSeedPhrase)) return [3 /*break*/, 22];
7104
+ return [4 /*yield*/, this.getAlgorandSeedPhrase({
7105
+ encryptedStandardSeedPhrase: encryptedReconstructedSeedPhrase,
7106
+ authenticationOptions: {
7107
+ wallabyAuthPublicKey: wallabyAuthPublicKey,
7108
+ encryptionMechanism: encryptionMechanism,
7109
+ password: password,
7110
+ prfValue: prfValue,
7258
7111
  },
7259
- queryParams: queryOptions,
7260
7112
  })];
7261
- case 1:
7262
- response = _c.sent();
7263
- if (!response || response.error) {
7264
- throw new Error(response.error);
7265
- }
7266
- return [2 /*return*/, response];
7267
- case 2:
7268
- error_10 = _c.sent();
7269
- logger.error('SDK::Wallet::getUserAssets::error', error_10);
7270
- return [2 /*return*/, Promise.reject(error_10)];
7271
- case 3: return [2 /*return*/];
7113
+ case 20:
7114
+ algorandSeedPhrase = _f.sent();
7115
+ return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey_1, algorandSeedPhrase.convertedEncryptedSeed)];
7116
+ case 21:
7117
+ decryptedAlgorandSeedPhrase = _f.sent();
7118
+ _f.label = 22;
7119
+ case 22: return [2 /*return*/, {
7120
+ keyShares: decryptedKeyShares,
7121
+ standardSeedPhrase: decryptedSeedPhrase,
7122
+ algorandSeedPhrase: decryptedAlgorandSeedPhrase,
7123
+ }];
7124
+ case 23:
7125
+ error_2 = _f.sent();
7126
+ logger.error('SDK::WalletService::export::error', error_2);
7127
+ return [2 /*return*/, Promise.reject(error_2)];
7128
+ case 24: return [2 /*return*/];
7272
7129
  }
7273
7130
  });
7274
7131
  });
7275
7132
  };
7276
7133
  /**
7277
7134
  *
7278
- * Get all wallets belonging to a user.
7279
- * @param userId Wallaby's user ID
7280
- * @returns {UserWalletsResponse} Returns an array of user wallets by specifying user ID
7135
+ * @public
7136
+ * Creates a new wallet, returns assets list, encrypted shares and/or encrypted seedPhrase.
7137
+ *
7138
+ * @param { boolean } storeSeedPhrase An option to either store the encrypted seed phrase from the response or to only return it in the response.
7139
+ * @param { ReturnWalletOption } returnWalletOption An enum `@link ReturnWalletOption` for what options to return
7140
+ * - `RETURN_ONLY_ENCRYPTED_SHARES` An option to split the seed phrase into multiple shares. It only return the shares based on a threshold.
7141
+ * - `RETURN_ONLY_ENCRYPTED_SEED_PHRASE` An option to only return a seed phrase. (Similar to how v1 works)
7142
+ * - `RETURN_BOTH_ENCRYPTED_SHARES_AND_SEED` An option to split the seed phrase and to add it to the response.
7143
+ * @param authenticationOptions parameters carried from the authentication request (`signIn` | `signUp`)
7144
+ * - { EncryptionMechanism } encryptionMechanism - The mechanism used for encryption.
7145
+ * - { object } [user] - User details, required if `publicKey` is not available and 'prf' is the encryption mechanism:
7146
+ * - { string } id - The user's unique identifier.
7147
+ * - { string } firstName - The user's first name.
7148
+ * - { string } email - The user's email address.
7149
+ * - {PublicKeyCredential } [publicKey] - Used if the user is already registered.
7150
+ * - { Uint8Array } [prfValue] - PRF value, used if 'prf' encryption is applied.
7151
+ * - { string } [password] - The user's password, if required for encryption.
7152
+ * - { string } [salt] - The salt value for password-based encryption.
7153
+ * @param [keyShareOptions] - Required if the return option involves seed splitting (`RETURN_ONLY_ENCRYPTED_SHARES` or `RETURN_BOTH_ENCRYPTED_SHARES_AND_SEED`):
7154
+ * - { number } `threshold`
7155
+ * - { number } `numberOfKeyShares`
7156
+ * - { number } `numberOfSharesReturned`
7157
+ * - { boolean } `storeOneKeyShareWithWallaby`
7158
+ * @returns {ImportWalletResponse} The wallet has been created successfully.
7281
7159
  */
7282
- WalletService.prototype.getUserWallets = function (_a) {
7283
- return __awaiter(this, arguments, void 0, function (_b) {
7284
- var response, error_11;
7285
- var userId = _b.userId, queryOptions = _b.queryOptions;
7286
- return __generator(this, function (_c) {
7287
- switch (_c.label) {
7160
+ WalletService.prototype.import = function (input) {
7161
+ return __awaiter(this, void 0, void 0, function () {
7162
+ var encryptedDevicePrivateKey, decryptedDevicePrivateKey, _a, clientAuthPubKey, importRequest, error_3;
7163
+ return __generator(this, function (_b) {
7164
+ switch (_b.label) {
7288
7165
  case 0:
7289
- _c.trys.push([0, 2, , 3]);
7290
- return [4 /*yield*/, this.request(WalletRoutes.getUserWallets, {
7291
- method: WalletRoutes.getUserWallets.method,
7292
- payload: {
7293
- userId: userId,
7294
- },
7295
- queryParams: queryOptions,
7296
- })];
7166
+ _b.trys.push([0, 8, , 9]);
7167
+ return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedDevicePrivateKey)];
7297
7168
  case 1:
7298
- response = _c.sent();
7299
- if (!response || response.error) {
7300
- throw new Error(response.error);
7169
+ encryptedDevicePrivateKey = _b.sent();
7170
+ if (!encryptedDevicePrivateKey) {
7171
+ throw new Error('Device private key is required');
7301
7172
  }
7302
- return [2 /*return*/, response];
7173
+ decryptedDevicePrivateKey = void 0;
7174
+ _a = input.authenticationOptions.encryptionMechanism;
7175
+ switch (_a) {
7176
+ case exports.EncryptionMechanism.Password: return [3 /*break*/, 2];
7177
+ case exports.EncryptionMechanism.Prf: return [3 /*break*/, 4];
7178
+ }
7179
+ return [3 /*break*/, 6];
7303
7180
  case 2:
7304
- error_11 = _c.sent();
7305
- logger.error('SDK::Wallet::getUserWallets::error', error_11);
7306
- return [2 /*return*/, Promise.reject(error_11)];
7307
- case 3: return [2 /*return*/];
7181
+ if (!input.authenticationOptions.password || !input.authenticationOptions.salt) {
7182
+ throw new Error('Password and salt are required.');
7183
+ }
7184
+ return [4 /*yield*/, this.cipher.decrypt({
7185
+ cipherText: encryptedDevicePrivateKey,
7186
+ inputKeyMaterial: utf8ToBytes$2("".concat(input.authenticationOptions.salt).concat(input.authenticationOptions.password)),
7187
+ })];
7188
+ case 3:
7189
+ decryptedDevicePrivateKey = _b.sent();
7190
+ return [3 /*break*/, 6];
7191
+ case 4:
7192
+ if (!input.authenticationOptions.prfValue) {
7193
+ throw new Error('PRF value is required');
7194
+ }
7195
+ return [4 /*yield*/, this.cipher.decrypt({
7196
+ cipherText: encryptedDevicePrivateKey,
7197
+ inputKeyMaterial: input.authenticationOptions.prfValue,
7198
+ })];
7199
+ case 5:
7200
+ decryptedDevicePrivateKey = _b.sent();
7201
+ return [3 /*break*/, 6];
7202
+ case 6:
7203
+ if (!decryptedDevicePrivateKey) {
7204
+ throw new Error('Failed to decrypt device private key');
7205
+ }
7206
+ clientAuthPubKey = publicKeyByPrivateKey(decryptedDevicePrivateKey);
7207
+ return [4 /*yield*/, this.handlers.handleImportWallet({
7208
+ publicKey: clientAuthPubKey,
7209
+ privateKey: decryptedDevicePrivateKey,
7210
+ }, input)];
7211
+ case 7:
7212
+ importRequest = _b.sent();
7213
+ return [2 /*return*/, importRequest];
7214
+ case 8:
7215
+ error_3 = _b.sent();
7216
+ logger.error('SDK::WalletService::import::error', error_3);
7217
+ return [2 /*return*/, Promise.reject(error_3)];
7218
+ case 9: return [2 /*return*/];
7308
7219
  }
7309
7220
  });
7310
7221
  });
7311
7222
  };
7312
- /**
7313
- *
7314
- * Get all assets that exist in a certain wallet, by specifying a wallet ID
7315
- * @param walletId Wallaby's wallet ID
7316
- * @param { BasicQueryOptions } queryOptions Optional query options
7317
- * @returns { WalletAssetsResponse } Returns all assets belonging in a certain wallet ID
7318
- */
7319
- WalletService.prototype.getWalletAssets = function (_a) {
7320
- return __awaiter(this, arguments, void 0, function (_b) {
7321
- var response, error_12;
7322
- var walletId = _b.walletId, queryOptions = _b.queryOptions;
7323
- return __generator(this, function (_c) {
7324
- switch (_c.label) {
7223
+ WalletService.prototype.recover = function (input) {
7224
+ return __awaiter(this, void 0, void 0, function () {
7225
+ var deviceAuthKeyPair, inputKeyMaterial, encryptedSeedPhrase, recoverWithSeedPhraseResponse, encryptedDeviceAuthPrivateKey, encryptedShares, encryptedSeed, error_4;
7226
+ var _a;
7227
+ return __generator(this, function (_b) {
7228
+ switch (_b.label) {
7325
7229
  case 0:
7326
- _c.trys.push([0, 2, , 3]);
7327
- return [4 /*yield*/, this.request(WalletRoutes.getWalletAssets, {
7328
- method: WalletRoutes.getWalletAssets.method,
7329
- payload: {
7330
- walletId: walletId,
7331
- },
7332
- queryParams: queryOptions,
7333
- })];
7334
- case 1:
7335
- response = _c.sent();
7336
- if (!response || response.error) {
7337
- throw new Error(response.error);
7230
+ _b.trys.push([0, 13, , 14]);
7231
+ deviceAuthKeyPair = createIdentity();
7232
+ inputKeyMaterial = void 0;
7233
+ switch (input.authenticationOptions.encryptionMechanism) {
7234
+ case exports.EncryptionMechanism.Password:
7235
+ if (!input.authenticationOptions.password || !input.authenticationOptions.salt) {
7236
+ throw new Error('Password and salt are required.');
7237
+ }
7238
+ inputKeyMaterial = utf8ToBytes$2("".concat(input.authenticationOptions.salt).concat(input.authenticationOptions.password));
7239
+ break;
7240
+ case exports.EncryptionMechanism.Prf:
7241
+ if (!input.authenticationOptions.prfValue) {
7242
+ throw new Error('PRF value is required');
7243
+ }
7244
+ inputKeyMaterial = input.authenticationOptions.prfValue;
7245
+ break;
7338
7246
  }
7339
- return [2 /*return*/, response];
7247
+ if (!input.decryptedSeedPhrase) return [3 /*break*/, 8];
7248
+ console.log('SDK::WalletService::recover:: seed phrase was found in input', {
7249
+ seed: input.decryptedSeedPhrase,
7250
+ wallabyPub: input.wallabyAuthPubKey,
7251
+ });
7252
+ return [4 /*yield*/, encryptWithPublicKey(input.wallabyAuthPubKey, input.decryptedSeedPhrase)];
7253
+ case 1:
7254
+ encryptedSeedPhrase = _b.sent();
7255
+ return [4 /*yield*/, this.handlers.handleRecoverFromSeedPhrase({
7256
+ encryptedSeedPhrase: encryptedSeedPhrase,
7257
+ deviceAuthKeys: { publicKey: deviceAuthKeyPair.publicKey, privateKey: deviceAuthKeyPair.privateKey },
7258
+ })];
7340
7259
  case 2:
7341
- error_12 = _c.sent();
7342
- logger.error('SDK::Wallet::getWalletAssets::error', error_12);
7343
- return [2 /*return*/, Promise.reject(error_12)];
7344
- case 3: return [2 /*return*/];
7260
+ recoverWithSeedPhraseResponse = _b.sent();
7261
+ if (!recoverWithSeedPhraseResponse.encryptedSeed) return [3 /*break*/, 4];
7262
+ // will take care of storing the seed phrase if needed.
7263
+ return [4 /*yield*/, this.handlers.handleSeedPhraseFromResponse({
7264
+ encryptedSeed: recoverWithSeedPhraseResponse.encryptedSeed,
7265
+ decryptedClientAuthPrivKey: deviceAuthKeyPair.privateKey,
7266
+ inputKeyMaterial: inputKeyMaterial,
7267
+ storeSeedPhrase: input.storeSeedPhrase,
7268
+ })];
7269
+ case 3:
7270
+ // will take care of storing the seed phrase if needed.
7271
+ _b.sent();
7272
+ _b.label = 4;
7273
+ case 4:
7274
+ if (!recoverWithSeedPhraseResponse.shares) return [3 /*break*/, 6];
7275
+ return [4 /*yield*/, this.handlers.handleKeySharesFromResponse({
7276
+ keyShares: recoverWithSeedPhraseResponse.shares,
7277
+ decryptedClientAuthPrivKey: deviceAuthKeyPair.privateKey,
7278
+ inputKeyMaterial: inputKeyMaterial,
7279
+ })];
7280
+ case 5:
7281
+ _b.sent();
7282
+ _b.label = 6;
7283
+ case 6: return [4 /*yield*/, this.cipher.encrypt({
7284
+ data: deviceAuthKeyPair.privateKey,
7285
+ inputKeyMaterial: inputKeyMaterial,
7286
+ })];
7287
+ case 7:
7288
+ encryptedDeviceAuthPrivateKey = _b.sent();
7289
+ this.storageService.saveInBulk([
7290
+ {
7291
+ key: exports.LocalStorageItems.EncryptedDevicePrivateKey,
7292
+ value: JSON.stringify(encryptedDeviceAuthPrivateKey),
7293
+ },
7294
+ {
7295
+ key: exports.LocalStorageItems.ClientAuthPublicKey,
7296
+ value: deviceAuthKeyPair.publicKey,
7297
+ },
7298
+ ]);
7299
+ return [2 /*return*/, recoverWithSeedPhraseResponse];
7300
+ case 8:
7301
+ if (!input.decryptedShares) return [3 /*break*/, 11];
7302
+ encryptedShares = (_a = input.decryptedShares) === null || _a === void 0 ? void 0 : _a.map(function (decryptedShare) {
7303
+ return encryptWithPublicKey(input.wallabyAuthPubKey, decryptedShare);
7304
+ });
7305
+ if (!encryptedShares) {
7306
+ throw new Error('Decrypted shares are required');
7307
+ }
7308
+ return [4 /*yield*/, this.handlers.handleConstructingSeedPhraseFromShares({
7309
+ encryptedKeyShares: encryptedShares,
7310
+ deviceAuthPublicKey: deviceAuthKeyPair.publicKey,
7311
+ walletId: input.walletId,
7312
+ })];
7313
+ case 9:
7314
+ encryptedSeed = _b.sent();
7315
+ return [4 /*yield*/, this.handlers.handleSeedPhraseFromResponse({
7316
+ encryptedSeed: encryptedSeed,
7317
+ decryptedClientAuthPrivKey: deviceAuthKeyPair.privateKey,
7318
+ inputKeyMaterial: inputKeyMaterial,
7319
+ storeSeedPhrase: input.storeSeedPhrase,
7320
+ })];
7321
+ case 10:
7322
+ _b.sent();
7323
+ return [3 /*break*/, 12];
7324
+ case 11: throw new Error('Decrypted seed phrase or shares are required to recover the wallet.');
7325
+ case 12: return [2 /*return*/, { standardSeedPhrase: input.decryptedSeedPhrase }];
7326
+ case 13:
7327
+ error_4 = _b.sent();
7328
+ logger.error('SDK::Wallet::recover::error', error_4);
7329
+ return [2 /*return*/, Promise.reject(error_4)];
7330
+ case 14: return [2 /*return*/];
7345
7331
  }
7346
7332
  });
7347
7333
  });
7348
7334
  };
7349
7335
  /**
7350
- * Adds a token to the wallet.
7351
- *
7352
- * @param {string} params.assetConfigId - The asset configuration ID.
7353
- * @param {string} params.walletId - The wallet ID.
7354
- * @param {string} params.userId - The user ID.
7355
- * @param {Object} params.config - The configuration object.
7356
- * @param {string} [params.config.contractAddress] - The contract address.
7357
- * @param {string} [params.config.suiObjectId] - The Sui object ID.
7358
- * @param {string} params.config.chainName - The name of the blockchain.
7336
+ * @public
7337
+ * Retrieves the encrypted shares for a specified wallet
7359
7338
  *
7360
- * @returns {Promise<AddTokenToWalletResponse>} The response from adding the token to the wallet.
7339
+ * @param {string} walletId - The Wallaby wallet ID.
7340
+ * @param {string} [password] - The password for decrypting the shares, if required.
7341
+ * @param {Uint8Array} [prfValue] - The PRF value used for encryption, if applicable.
7342
+ * @param {EncryptionMechanism} encryptionMechanism - The mechanism used to encrypt the shares.
7361
7343
  *
7362
- * @throws {Error} If required parameters are missing or invalid.
7344
+ * @returns {Promise<EncryptedSharesResponse>} A promise that resolves with the encrypted shares.
7363
7345
  */
7364
- WalletService.prototype.addTokenToWallet = function (_a) {
7346
+ WalletService.prototype.retrieveShares = function (_a) {
7365
7347
  return __awaiter(this, arguments, void 0, function (_b) {
7366
- var response, error_13;
7367
- var assetConfigId = _b.assetConfigId, walletId = _b.walletId, userId = _b.userId, config = _b.config;
7348
+ var signatureMessage, response, error_5;
7349
+ var walletId = _b.walletId, password = _b.password, prfValue = _b.prfValue, authResponse = _b.authResponse, deviceAuthKeys = _b.deviceAuthKeys, encryptionMechanism = _b.encryptionMechanism;
7368
7350
  return __generator(this, function (_c) {
7369
7351
  switch (_c.label) {
7370
7352
  case 0:
7371
- _c.trys.push([0, 2, , 3]);
7372
- if (!walletId || !userId) {
7373
- throw new Error('Missing required parameters');
7374
- }
7375
- if (!assetConfigId && !config) {
7376
- throw new Error('Either assetConfigId or config must be provided');
7377
- }
7378
- if ((config === null || config === void 0 ? void 0 : config.chainName) === exports.BlockchainName.Sui) {
7379
- if (!config.suiObjectId || !!config.contractAddress) {
7380
- throw new Error('`suiObjectId` instead of `contractAddress` is required for Sui chain');
7381
- }
7382
- config.suiObjectId = sanitizeSuiObjectId(config.suiObjectId);
7383
- }
7384
- return [4 /*yield*/, this.request(WalletRoutes.addTokenToWallet, {
7385
- method: WalletRoutes.addTokenToWallet.method,
7353
+ _c.trys.push([0, 4, , 5]);
7354
+ return [4 /*yield*/, this.authService.getSignature({
7355
+ publicKey: deviceAuthKeys.publicKey,
7356
+ privateKey: deviceAuthKeys.privateKey,
7357
+ })];
7358
+ case 1:
7359
+ signatureMessage = _c.sent();
7360
+ return [4 /*yield*/, this.request(WalletRoutes.retrieveShares, {
7361
+ method: WalletRoutes.retrieveShares.method,
7386
7362
  payload: {
7387
- assetConfigId: assetConfigId,
7388
7363
  walletId: walletId,
7389
- userId: userId,
7390
- config: config,
7364
+ signature: signatureMessage.signature,
7365
+ message: signatureMessage.message,
7366
+ clientAuthPubKey: deviceAuthKeys.publicKey,
7391
7367
  },
7392
7368
  })];
7393
- case 1:
7369
+ case 2:
7394
7370
  response = _c.sent();
7395
- if (!response || response.errors) {
7396
- throw new Error(response.errors);
7371
+ if (!response || response.error) {
7372
+ throw new Error('Failed to retrieve shares');
7397
7373
  }
7374
+ return [4 /*yield*/, this.handlers.handleKeySharesFromResponse({
7375
+ keyShares: response.shares,
7376
+ decryptedClientAuthPrivKey: deviceAuthKeys.privateKey,
7377
+ inputKeyMaterial: encryptionMechanism === exports.EncryptionMechanism.Password
7378
+ ? utf8ToBytes$2("".concat(authResponse.user.clientSalt).concat(password))
7379
+ : prfValue,
7380
+ })];
7381
+ case 3:
7382
+ _c.sent();
7398
7383
  return [2 /*return*/, response];
7399
- case 2:
7400
- error_13 = _c.sent();
7401
- logger.error('SDK::Wallet::addTokenToWallet::error', error_13);
7402
- return [2 /*return*/, Promise.reject(error_13)];
7403
- case 3: return [2 /*return*/];
7384
+ case 4:
7385
+ error_5 = _c.sent();
7386
+ logger.error('SDK::Wallet::retrieveShares::error', error_5);
7387
+ return [2 /*return*/, Promise.reject(error_5)];
7388
+ case 5: return [2 /*return*/];
7404
7389
  }
7405
7390
  });
7406
7391
  });
7407
7392
  };
7408
7393
  /**
7409
- * Updates a wallet asset.
7410
- *
7411
- * @param {string} params.assetConfigId - Config ID for the user asset.
7412
- * @param {string} params.walletId - Wallet ID to which the asset belongs.
7413
- * @param {string} params.assetName - Asset name.
7414
- * @param {boolean} params.isVisible - Asset visibility toggle.
7415
- * @param {string} params.assetSymbol - Asset symbol.
7394
+ * @public
7395
+ * Updates the wallet password.
7416
7396
  *
7417
- * @returns {Promise<UpdateAssetResponse>} The response from updating the wallet token.
7397
+ * @param {string} oldPassword - The Wallaby wallet ID.
7398
+ * @param {string} newPassword - The Wallaby asset ID.
7418
7399
  *
7419
- * @throws {Error} If required parameters are missing or invalid.
7400
+ * @returns {Promise<boolean>} True/False.
7420
7401
  */
7421
- WalletService.prototype.updateAsset = function (_a) {
7422
- return __awaiter(this, void 0, void 0, function () {
7423
- var response, error_14;
7424
- var assetConfigId = _a.assetConfigId, walletId = _a.walletId, assetConfig = __rest(_a, ["assetConfigId", "walletId"]);
7425
- return __generator(this, function (_b) {
7426
- switch (_b.label) {
7402
+ WalletService.prototype.changePassword = function (_a) {
7403
+ return __awaiter(this, arguments, void 0, function (_b) {
7404
+ var decryptedDevicePrivateKey, clientSalt, decryptedSeedPhrase, error_6;
7405
+ var oldPassword = _b.oldPassword, newPassword = _b.newPassword;
7406
+ return __generator(this, function (_c) {
7407
+ switch (_c.label) {
7427
7408
  case 0:
7428
- _b.trys.push([0, 2, , 3]);
7429
- if (!assetConfigId || !walletId) {
7430
- throw new Error('Missing required parameters');
7431
- }
7432
- return [4 /*yield*/, this.request(WalletRoutes.updateAsset, {
7433
- method: WalletRoutes.updateAsset.method,
7434
- payload: __assign({ configId: assetConfigId, walletId: walletId }, assetConfig),
7435
- })];
7409
+ _c.trys.push([0, 6, , 7]);
7410
+ return [4 /*yield*/, this.storageService.getDecryptedPrivateKey(oldPassword)];
7436
7411
  case 1:
7437
- response = _b.sent();
7438
- if (!response || response.errors) {
7439
- throw new Error(response.errors);
7412
+ decryptedDevicePrivateKey = _c.sent();
7413
+ if (!decryptedDevicePrivateKey) {
7414
+ throw new Error('Password is not valid, device private key could not be decrypted.');
7440
7415
  }
7441
- return [2 /*return*/, response];
7416
+ return [4 /*yield*/, this.authService.getUserSalt()];
7442
7417
  case 2:
7443
- error_14 = _b.sent();
7444
- logger.error('SDK::Wallet::updateAsset::error', error_14);
7445
- return [2 /*return*/, Promise.reject(error_14)];
7446
- case 3: return [2 /*return*/];
7418
+ clientSalt = _c.sent();
7419
+ return [4 /*yield*/, this.storageService.getDecryptedSeedPhrase({
7420
+ password: oldPassword,
7421
+ seedPhraseType: exports.SeedPhraseType.Standard,
7422
+ getUserSalt: this.authService.getUserSalt,
7423
+ salt: clientSalt,
7424
+ })];
7425
+ case 3:
7426
+ decryptedSeedPhrase = _c.sent();
7427
+ if (!decryptedSeedPhrase) {
7428
+ throw new Error('Password is not valid, seed phrase could not be decrypted, please recover the wallet.');
7429
+ }
7430
+ return [4 /*yield*/, this.storageService.setEncryptedPrivateKey(decryptedDevicePrivateKey, newPassword)];
7431
+ case 4:
7432
+ _c.sent();
7433
+ return [4 /*yield*/, this.storageService.setEncryptedSeedPhrase({
7434
+ decryptedSeedPhrase: decryptedSeedPhrase,
7435
+ seedPhraseType: exports.SeedPhraseType.Standard,
7436
+ password: newPassword,
7437
+ deviceAuthPublicKey: publicKeyByPrivateKey(decryptedDevicePrivateKey),
7438
+ getUserSalt: this.authService.getUserSalt,
7439
+ salt: clientSalt,
7440
+ })];
7441
+ case 5:
7442
+ _c.sent();
7443
+ return [2 /*return*/, true];
7444
+ case 6:
7445
+ error_6 = _c.sent();
7446
+ logger.error('SDK::Wallet::changePassword::error', error_6);
7447
+ return [2 /*return*/, Promise.reject(false)];
7448
+ case 7: return [2 /*return*/];
7447
7449
  }
7448
7450
  });
7449
7451
  });
@@ -7457,7 +7459,7 @@ var WalletService = /** @class */ (function (_super) {
7457
7459
  */
7458
7460
  WalletService.prototype.getAlgorandSeedPhrase = function (input) {
7459
7461
  return __awaiter(this, void 0, void 0, function () {
7460
- var encryptedDevicePrivateKey, encryptedStandardSeedPhrase, _a, decryptedDevicePrivateKey, _b, _c, _d, _e, _f, clientAuthPubKey, reference, signatureMessage, message, signature, seedPhraseEncryptedWithWallabyPubKey, response, error_15;
7462
+ var encryptedDevicePrivateKey, encryptedStandardSeedPhrase, _a, decryptedDevicePrivateKey, _b, _c, _d, _e, _f, clientAuthPubKey, reference, signatureMessage, message, signature, seedPhraseEncryptedWithWallabyPubKey, response, error_7;
7461
7463
  var _g;
7462
7464
  var _h;
7463
7465
  return __generator(this, function (_j) {
@@ -7546,355 +7548,573 @@ var WalletService = /** @class */ (function (_super) {
7546
7548
  encryptedSeed: seedPhraseEncryptedWithWallabyPubKey,
7547
7549
  },
7548
7550
  })];
7549
- case 13:
7550
- response = _j.sent();
7551
+ case 13:
7552
+ response = _j.sent();
7553
+ if (!response || response.error) {
7554
+ throw new Error(response.error);
7555
+ }
7556
+ return [2 /*return*/, response];
7557
+ case 14:
7558
+ error_7 = _j.sent();
7559
+ logger.error('SDK::Wallet::getAlgorandSeedPhrase::error', error_7);
7560
+ return [2 /*return*/, Promise.reject(error_7)];
7561
+ case 15: return [2 /*return*/];
7562
+ }
7563
+ });
7564
+ });
7565
+ };
7566
+ WalletService.prototype.initBackupKeyShare = function (input) {
7567
+ return __awaiter(this, void 0, void 0, function () {
7568
+ var walletId, backUpUrl, password, prfValue, encryptionMechanism, authResponse, persistCurrentSession, encryptedDevicePrivateKey, encryptedKeyShares, inputKeyMaterial, decryptedDevicePrivateKey, decryptedKeyShares, encryptedKeyShareForWallaby, _a;
7569
+ var _b;
7570
+ var _this = this;
7571
+ return __generator(this, function (_c) {
7572
+ switch (_c.label) {
7573
+ case 0:
7574
+ walletId = input.walletId, backUpUrl = input.backUpUrl, password = input.password, prfValue = input.prfValue, encryptionMechanism = input.encryptionMechanism, authResponse = input.authResponse, persistCurrentSession = input.persistCurrentSession;
7575
+ console.log('SDK::WaletService::backUpWallet::payload', { backUpUrl: backUpUrl, password: password, prfValue: prfValue, encryptionMechanism: encryptionMechanism, authResponse: authResponse });
7576
+ return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedDevicePrivateKey)];
7577
+ case 1:
7578
+ encryptedDevicePrivateKey = _c.sent();
7579
+ return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedKeyShares)];
7580
+ case 2:
7581
+ encryptedKeyShares = _c.sent();
7582
+ if (!encryptedKeyShares) {
7583
+ throw new Error('You do not have any key shares stored on this device.');
7584
+ }
7585
+ if (!encryptedDevicePrivateKey) {
7586
+ throw new Error('Device private key is required to export the wallet');
7587
+ }
7588
+ switch (encryptionMechanism) {
7589
+ case exports.EncryptionMechanism.Password:
7590
+ if (!password) {
7591
+ throw new Error('password is required for this encryption mechanism type.');
7592
+ }
7593
+ inputKeyMaterial = utf8ToBytes$2("".concat(authResponse.user.clientSalt).concat(password));
7594
+ break;
7595
+ case exports.EncryptionMechanism.Prf:
7596
+ if (!prfValue) {
7597
+ throw new Error('prfValue is required for this encryption mechanism type.');
7598
+ }
7599
+ inputKeyMaterial = prfValue;
7600
+ break;
7601
+ }
7602
+ return [4 /*yield*/, this.cipher.decrypt({
7603
+ cipherText: encryptedDevicePrivateKey,
7604
+ inputKeyMaterial: inputKeyMaterial,
7605
+ })];
7606
+ case 3:
7607
+ decryptedDevicePrivateKey = _c.sent();
7608
+ if (!decryptedDevicePrivateKey) {
7609
+ throw new Error('Device private key could not be decrypted');
7610
+ }
7611
+ return [4 /*yield*/, Promise.all(JSON.parse(encryptedKeyShares).map(function (encryptedKeyShare, i) { return __awaiter(_this, void 0, void 0, function () {
7612
+ var decryptedOnceKeyShare, decryptedTwiceKeyShare;
7613
+ return __generator(this, function (_a) {
7614
+ switch (_a.label) {
7615
+ case 0:
7616
+ logger.info("SDK::Wallet::export::encryptedKeyShare #".concat(i), encryptedKeyShare);
7617
+ return [4 /*yield*/, this.cipher.decrypt({
7618
+ cipherText: JSON.stringify(encryptedKeyShare),
7619
+ inputKeyMaterial: inputKeyMaterial,
7620
+ })];
7621
+ case 1:
7622
+ decryptedOnceKeyShare = _a.sent();
7623
+ if (!decryptedOnceKeyShare) {
7624
+ throw new Error('Something went wrong decrypting the data.');
7625
+ }
7626
+ console.log('SDK::Wallet::export::decryptWithPrivateKeyPayload', {
7627
+ decryptedOnceKeyShare: decryptedOnceKeyShare,
7628
+ decryptedDevicePrivateKey: decryptedDevicePrivateKey,
7629
+ });
7630
+ return [4 /*yield*/, decryptWithPrivateKey(decryptedDevicePrivateKey, JSON.parse(decryptedOnceKeyShare))];
7631
+ case 2:
7632
+ decryptedTwiceKeyShare = _a.sent();
7633
+ return [2 /*return*/, decryptedTwiceKeyShare];
7634
+ }
7635
+ });
7636
+ }); }))];
7637
+ case 4:
7638
+ decryptedKeyShares = _c.sent();
7639
+ console.log('SDK::Wallet::export::decryptedDevicePrivateKey', decryptedDevicePrivateKey);
7640
+ encryptedKeyShareForWallaby = encryptWithPublicKey(input.authResponse.user.wallabyAuthPubKey, decryptedKeyShares[0]);
7641
+ _b = {};
7642
+ if (!persistCurrentSession) return [3 /*break*/, 6];
7643
+ return [4 /*yield*/, this.device];
7644
+ case 5:
7645
+ _a = (_c.sent()).authTokens.clientAuthToken;
7646
+ return [3 /*break*/, 7];
7647
+ case 6:
7648
+ _a = undefined;
7649
+ _c.label = 7;
7650
+ case 7: return [2 /*return*/, (_b.clientAuthToken = _a,
7651
+ _b.walletId = walletId,
7652
+ _b.backUpUrl = backUpUrl,
7653
+ _b.password = password,
7654
+ _b.prfValue = prfValue,
7655
+ _b.encryptionMechanism = encryptionMechanism,
7656
+ _b.authResponse = authResponse,
7657
+ _b.encryptedShare = encryptedKeyShareForWallaby,
7658
+ _b)];
7659
+ }
7660
+ });
7661
+ });
7662
+ };
7663
+ WalletService.prototype.backupKeyShare = function (input) {
7664
+ return __awaiter(this, void 0, void 0, function () {
7665
+ var encryptedDevicePrivateKey, inputKeyMaterial, decryptedDevicePrivateKey, signatureMessage, backUpResponse, encryptedBackupShare, error_8;
7666
+ return __generator(this, function (_a) {
7667
+ switch (_a.label) {
7668
+ case 0:
7669
+ _a.trys.push([0, 7, , 8]);
7670
+ return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedDevicePrivateKey)];
7671
+ case 1:
7672
+ encryptedDevicePrivateKey = _a.sent();
7673
+ if (!encryptedDevicePrivateKey) {
7674
+ throw new Error('Device private key is required to export the wallet');
7675
+ }
7676
+ inputKeyMaterial = void 0;
7677
+ switch (input.encryptionMechanism) {
7678
+ case exports.EncryptionMechanism.Password:
7679
+ if (!input.password) {
7680
+ throw new Error('password is required for this encryption mechanism type.');
7681
+ }
7682
+ inputKeyMaterial = utf8ToBytes$2("".concat(input.authResponse.user.clientSalt).concat(input.password));
7683
+ break;
7684
+ case exports.EncryptionMechanism.Prf:
7685
+ if (!input.prfValue) {
7686
+ throw new Error('prfValue is required for this encryption mechanism type.');
7687
+ }
7688
+ inputKeyMaterial = input.prfValue;
7689
+ break;
7690
+ }
7691
+ return [4 /*yield*/, this.cipher.decrypt({
7692
+ cipherText: encryptedDevicePrivateKey,
7693
+ inputKeyMaterial: inputKeyMaterial,
7694
+ })];
7695
+ case 2:
7696
+ decryptedDevicePrivateKey = _a.sent();
7697
+ if (!decryptedDevicePrivateKey) {
7698
+ throw new Error('Device private key could not be decrypted');
7699
+ }
7700
+ return [4 /*yield*/, this.authService.getSignature({
7701
+ publicKey: publicKeyByPrivateKey(decryptedDevicePrivateKey),
7702
+ privateKey: decryptedDevicePrivateKey,
7703
+ })];
7704
+ case 3:
7705
+ signatureMessage = _a.sent();
7706
+ return [4 /*yield*/, this.request(WalletRoutes.backupKeyShare, {
7707
+ method: WalletRoutes.backupKeyShare.method,
7708
+ payload: {
7709
+ walletId: input.walletId,
7710
+ message: signatureMessage.message,
7711
+ signature: signatureMessage.signature,
7712
+ clientAuthPubKey: publicKeyByPrivateKey(decryptedDevicePrivateKey),
7713
+ backupShare: input.encryptedShare,
7714
+ },
7715
+ })];
7716
+ case 4:
7717
+ backUpResponse = _a.sent();
7718
+ return [4 /*yield*/, this.cipher.encrypt({
7719
+ data: JSON.stringify(backUpResponse.backupShare),
7720
+ inputKeyMaterial: inputKeyMaterial,
7721
+ })];
7722
+ case 5:
7723
+ encryptedBackupShare = _a.sent();
7724
+ return [4 /*yield*/, this.storageService.save(exports.LocalStorageItems.EncryptedShare, JSON.stringify(encryptedBackupShare))];
7725
+ case 6:
7726
+ _a.sent();
7727
+ console.log('backUpResponse', backUpResponse);
7728
+ return [2 /*return*/, { success: true, reference: backUpResponse.reference }];
7729
+ case 7:
7730
+ error_8 = _a.sent();
7731
+ logger.error('SDK::Wallet::backupKeyShare::error', error_8);
7732
+ return [2 /*return*/, Promise.reject(error_8)];
7733
+ case 8: return [2 /*return*/];
7734
+ }
7735
+ });
7736
+ });
7737
+ };
7738
+ WalletService.prototype.finalizeBackupKeyShare = function () {
7739
+ return __awaiter(this, void 0, void 0, function () {
7740
+ var encryptedShares, shareToKeep, error_9;
7741
+ return __generator(this, function (_a) {
7742
+ switch (_a.label) {
7743
+ case 0:
7744
+ _a.trys.push([0, 4, , 5]);
7745
+ return [4 /*yield*/, this.storageService.fetch(exports.LocalStorageItems.EncryptedKeyShares)];
7746
+ case 1:
7747
+ encryptedShares = _a.sent();
7748
+ if (!(encryptedShares === null || encryptedShares === void 0 ? void 0 : encryptedShares.length)) {
7749
+ throw new Error('Something went wrong, no encrypted shares were found.');
7750
+ }
7751
+ logger.log('SDK::Wallet::finalizeBackupKeyShare::encryptedShares', encryptedShares);
7752
+ shareToKeep = JSON.parse(encryptedShares)[0];
7753
+ logger.log('SDK::Wallet::finalizeBackupKeyShare::shareToKeep', shareToKeep);
7754
+ return [4 /*yield*/, this.storageService.save(exports.LocalStorageItems.EncryptedShare, JSON.stringify(shareToKeep))];
7755
+ case 2:
7756
+ _a.sent();
7757
+ return [4 /*yield*/, this.storageService.delete(exports.LocalStorageItems.EncryptedKeyShares)];
7758
+ case 3:
7759
+ _a.sent();
7760
+ return [2 /*return*/, { success: true }];
7761
+ case 4:
7762
+ error_9 = _a.sent();
7763
+ logger.error('SDK::Wallet::finalizeBackupKeyShare::error', error_9);
7764
+ return [2 /*return*/, Promise.reject(error_9)];
7765
+ case 5: return [2 /*return*/];
7766
+ }
7767
+ });
7768
+ });
7769
+ };
7770
+ return WalletService;
7771
+ }(Base));
7772
+
7773
+ /**
7774
+ * Sanitizes a Sui object ID by removing any part of the string after '::'.
7775
+ *
7776
+ * @param objectId - The Sui object ID to sanitize. Example input: "0xc91acfb75009c5ff2fd57c54f3caaee12ad1fbe997681334adc0b574fc277a07::icorgi::ICORGI"
7777
+ * @returns The sanitized Sui object ID. Example output: "0xc91acfb75009c5ff2fd57c54f3caaee12ad1fbe997681334adc0b574fc277a07"
7778
+ */
7779
+ var sanitizeSuiObjectId = function (objectId) {
7780
+ if (!objectId) {
7781
+ throw new Error('Sui object ID is required');
7782
+ }
7783
+ if (!objectId.startsWith('0x')) {
7784
+ throw new Error('Invalid Sui object ID');
7785
+ }
7786
+ if (!objectId.includes('::')) {
7787
+ return objectId;
7788
+ }
7789
+ return objectId.split('::')[0];
7790
+ };
7791
+
7792
+ var WalletDataService = /** @class */ (function (_super) {
7793
+ __extends(WalletDataService, _super);
7794
+ function WalletDataService(config) {
7795
+ var _this = _super.call(this, config) || this;
7796
+ _this.userId = config.userId;
7797
+ return _this;
7798
+ }
7799
+ /**
7800
+ *
7801
+ * @public
7802
+ * Create a newly fresh wallet, returns asset list and encrypted seedPhrase
7803
+ * @param { string } address Wallet address
7804
+ * @param { string } symbol Token symbol
7805
+ * @param { BlockchainName } chainName the chain name enum. i.e. 'ETHEREUM', 'BITCOIN'
7806
+ * @returns { CreateWalletResponse } The wallet has been created successfully
7807
+ */
7808
+ WalletDataService.prototype.getTokenBalance = function (_a) {
7809
+ return __awaiter(this, arguments, void 0, function (_b) {
7810
+ var response, error_1;
7811
+ var address = _b.address, symbol = _b.symbol, tokenStandard = _b.tokenStandard, chainName = _b.chainName;
7812
+ return __generator(this, function (_c) {
7813
+ switch (_c.label) {
7814
+ case 0:
7815
+ _c.trys.push([0, 2, , 3]);
7816
+ return [4 /*yield*/, this.request(WalletRoutes.getTokenBalance, {
7817
+ method: WalletRoutes.getTokenBalance.method,
7818
+ payload: { address: address, symbol: symbol, tokenStandard: tokenStandard, chainName: chainName },
7819
+ })];
7820
+ case 1:
7821
+ response = _c.sent();
7551
7822
  if (!response || response.error) {
7552
7823
  throw new Error(response.error);
7553
7824
  }
7554
7825
  return [2 /*return*/, response];
7555
- case 14:
7556
- error_15 = _j.sent();
7557
- logger.error('SDK::Wallet::getAlgorandSeedPhrase::error', error_15);
7558
- return [2 /*return*/, Promise.reject(error_15)];
7559
- case 15: return [2 /*return*/];
7826
+ case 2:
7827
+ error_1 = _c.sent();
7828
+ logger.error('SDK::Wallet::getTokenBalance::error', error_1);
7829
+ return [2 /*return*/, Promise.reject(error_1)];
7830
+ case 3: return [2 /*return*/];
7560
7831
  }
7561
7832
  });
7562
7833
  });
7563
7834
  };
7564
- WalletService.prototype.handleImportWallet = function (deviceAuthKeys, importWalletInput) {
7835
+ /**
7836
+ *
7837
+ * @public
7838
+ * Fetches the balances for a group of tokens in bulk.
7839
+ * Takes an array of addresses and tokens and return their balances.
7840
+ * @param { string } address Wallet address
7841
+ * @param { string } symbol Token symbol
7842
+ * @param { BlockchainName } chainName the chain name enum. i.e. 'ETHEREUM', 'BITCOIN'
7843
+ * @returns { TokenBalanceResponse[] } Returns an array of the requested tokens balances.
7844
+ */
7845
+ WalletDataService.prototype.getMultiTokenBalances = function (tokensAndAddressesList) {
7565
7846
  return __awaiter(this, void 0, void 0, function () {
7566
- var _a, signature, message, _b, numberOfKeyShares, threshold, readyForTransportSeedPhrase, importWalletPayload, importWalletResponse, decryptedSeedPhrase, inputKeyMaterial, decryptedShares;
7567
- var _c, _d, _e;
7568
- return __generator(this, function (_f) {
7569
- switch (_f.label) {
7570
- case 0: return [4 /*yield*/, this.authService.getSignature({
7571
- publicKey: deviceAuthKeys.publicKey,
7572
- privateKey: deviceAuthKeys.privateKey,
7573
- })];
7847
+ var response, error_2;
7848
+ return __generator(this, function (_a) {
7849
+ switch (_a.label) {
7850
+ case 0:
7851
+ _a.trys.push([0, 2, , 3]);
7852
+ return [4 /*yield*/, this.request(WalletRoutes.getMultiTokenBalances, {
7853
+ method: WalletRoutes.getMultiTokenBalances.method,
7854
+ payload: { tokensAndAddressesList: tokensAndAddressesList },
7855
+ })];
7574
7856
  case 1:
7575
- _a = _f.sent(), signature = _a.signature, message = _a.message;
7576
- if ((importWalletInput.returnWalletOption === exports.ReturnWalletOption.ReturnOnlyEncryptedShares ||
7577
- importWalletInput.returnWalletOption === exports.ReturnWalletOption.ReturnBothEncryptedSharesAndSeed) &&
7578
- !importWalletInput.keyShareOptions) {
7579
- throw new Error('Key share options are required when requesting to split the seed phrase');
7580
- }
7581
- _b = (_c = importWalletInput.keyShareOptions) !== null && _c !== void 0 ? _c : {}, numberOfKeyShares = _b.numberOfKeyShares, threshold = _b.threshold;
7582
- if (threshold && numberOfKeyShares && numberOfKeyShares < threshold) {
7583
- throw new Error('Number of shares must be greater than or equal to the threshold');
7857
+ response = _a.sent();
7858
+ if (!response || response.error) {
7859
+ throw new Error(response.error);
7584
7860
  }
7585
- readyForTransportSeedPhrase = encryptWithPublicKey(importWalletInput.authenticationOptions.wallabyAuthPublicKey, importWalletInput.seedPhrase);
7586
- importWalletPayload = {
7587
- reference: uuid.v4(),
7588
- message: message,
7589
- signature: signature,
7590
- encryptedSeed: readyForTransportSeedPhrase,
7591
- clientAuthPubKey: deviceAuthKeys.publicKey,
7592
- backUpDevicePubKey: deviceAuthKeys.publicKey, // TODO: Replace this with backup device public key.
7593
- returnWalletOption: importWalletInput.returnWalletOption,
7594
- numShares: numberOfKeyShares !== null && numberOfKeyShares !== void 0 ? numberOfKeyShares : 0,
7595
- threshold: threshold !== null && threshold !== void 0 ? threshold : 0,
7596
- wallabyKeepsOne: (_e = (_d = importWalletInput.keyShareOptions) === null || _d === void 0 ? void 0 : _d.storeOneKeyShareWithWallaby) !== null && _e !== void 0 ? _e : false,
7597
- };
7598
- return [4 /*yield*/, this.request(WalletRoutes.importSeedPhraseV2, {
7599
- method: WalletRoutes.importSeedPhraseV2.method,
7600
- payload: importWalletPayload,
7601
- })];
7861
+ return [2 /*return*/, response];
7602
7862
  case 2:
7603
- importWalletResponse = _f.sent();
7604
- if (!importWalletResponse || importWalletResponse.error) {
7605
- throw new Error(importWalletResponse.error);
7606
- }
7607
- logger.info('SDK::WalletService::import::importWalletResponse', importWalletResponse);
7608
- logger.info('SDK::WalletService::import::inputKeyMaterialPayload', {
7609
- encryptionMechanism: importWalletInput.authenticationOptions.encryptionMechanism,
7610
- salt: importWalletInput.authenticationOptions.salt,
7611
- password: importWalletInput.authenticationOptions.password,
7612
- prfValue: importWalletInput.authenticationOptions.prfValue,
7613
- });
7614
- inputKeyMaterial = importWalletInput.authenticationOptions.encryptionMechanism === exports.EncryptionMechanism.Password
7615
- ? utf8ToBytes$2("".concat(importWalletInput.authenticationOptions.salt).concat(importWalletInput.authenticationOptions.password))
7616
- : importWalletInput.authenticationOptions.prfValue;
7617
- logger.info('SDK::WalletService::import::inputKeyMaterial', inputKeyMaterial);
7618
- if (!importWalletResponse.encryptedSeed) return [3 /*break*/, 4];
7619
- logger.info('SDK::WalletService::import::import does have an encrypted seed phrase');
7620
- return [4 /*yield*/, this.handleSeedPhraseFromResponse({
7621
- encryptedSeed: importWalletResponse.encryptedSeed,
7622
- decryptedClientAuthPrivKey: deviceAuthKeys.privateKey,
7623
- storeSeedPhrase: importWalletInput.storeSeedPhrase,
7624
- inputKeyMaterial: inputKeyMaterial,
7625
- })];
7626
- case 3:
7627
- decryptedSeedPhrase = _f.sent();
7628
- logger.info('SDK::WalletService::import::decryptedSeedPhrase', decryptedSeedPhrase);
7629
- _f.label = 4;
7630
- case 4:
7631
- if (!importWalletResponse.shares) return [3 /*break*/, 6];
7632
- logger.info('SDK::WalletService::import::import does have key shares');
7633
- return [4 /*yield*/, this.handleKeySharesFromResponse({
7634
- keyShares: importWalletResponse.shares,
7635
- decryptedClientAuthPrivKey: deviceAuthKeys.privateKey,
7636
- inputKeyMaterial: inputKeyMaterial,
7637
- })];
7638
- case 5:
7639
- decryptedShares = _f.sent();
7640
- logger.info('SDK::WalletService::import::decryptedShares', decryptedShares);
7641
- _f.label = 6;
7642
- case 6: return [2 /*return*/, __assign(__assign({}, importWalletResponse), { standardSeedPhrase: decryptedSeedPhrase })];
7863
+ error_2 = _a.sent();
7864
+ logger.error('SDK::Wallet::getTokenBalance::error', error_2);
7865
+ return [2 /*return*/, Promise.reject(error_2)];
7866
+ case 3: return [2 /*return*/];
7643
7867
  }
7644
7868
  });
7645
7869
  });
7646
7870
  };
7647
7871
  /**
7648
- * Handles the creation of a wallet, including generating signatures, validating input,
7649
- * and processing the wallet creation response.
7650
- * @private
7651
- * @param {Object} deviceAuthKeys - The device authentication key pair. { publicKey: string, privateKey: string}
7652
- * @param {CreateWalletRequest} walletCreationInput - The input parameters for wallet creation.
7653
- * @returns {Promise<Omit<CreateWalletResponse, 'standardSeedPhrase'> & { standardSeedPhrase?: string }>}
7654
- * The response from the wallet creation process, including the decrypted seed phrase if applicable.
7655
- * @throws {Error} If key share options are required but not provided, or if the number of shares is less than the threshold.
7872
+ *
7873
+ * @public
7874
+ * Fetches the balances for all tokens in a specific wallet.
7875
+ * @param { string } walletId Wallet address
7876
+ * @returns { TokenBalanceResponse[] } Returns an array of the requested tokens balances.
7656
7877
  */
7657
- WalletService.prototype.handleWalletCreation = function (deviceAuthKeys, walletCreationInput) {
7658
- return __awaiter(this, void 0, void 0, function () {
7659
- var _a, signature, message, hashedMessage, _b, numberOfKeyShares, threshold, walletCreationPayload, walletCreationResponse, decryptedSeedPhrase, inputKeyMaterial;
7660
- var _c, _d, _e, _f;
7661
- return __generator(this, function (_g) {
7662
- switch (_g.label) {
7663
- case 0: return [4 /*yield*/, this.authService.getSignature({
7664
- publicKey: deviceAuthKeys.publicKey,
7665
- privateKey: deviceAuthKeys.privateKey,
7666
- })];
7878
+ WalletDataService.prototype.getWalletBalances = function (_a) {
7879
+ return __awaiter(this, arguments, void 0, function (_b) {
7880
+ var response, error_3;
7881
+ var walletId = _b.walletId;
7882
+ return __generator(this, function (_c) {
7883
+ switch (_c.label) {
7884
+ case 0:
7885
+ _c.trys.push([0, 2, , 3]);
7886
+ return [4 /*yield*/, this.request(WalletRoutes.getWalletBalances, {
7887
+ method: WalletRoutes.getWalletBalances.method,
7888
+ payload: { walletId: walletId },
7889
+ })];
7667
7890
  case 1:
7668
- _a = _g.sent(), signature = _a.signature, message = _a.message, hashedMessage = _a.hashedMessage;
7669
- if ((walletCreationInput.returnWalletOption === exports.ReturnWalletOption.ReturnOnlyEncryptedShares ||
7670
- walletCreationInput.returnWalletOption === exports.ReturnWalletOption.ReturnBothEncryptedSharesAndSeed) &&
7671
- !walletCreationInput.keyShareOptions) {
7672
- throw new Error('Key share options are required when requesting to split the seed phrase');
7673
- }
7674
- _b = (_c = walletCreationInput.keyShareOptions) !== null && _c !== void 0 ? _c : {}, numberOfKeyShares = _b.numberOfKeyShares, threshold = _b.threshold;
7675
- if (threshold && numberOfKeyShares && numberOfKeyShares < threshold) {
7676
- throw new Error('Number of shares must be greater than or equal to the threshold');
7891
+ response = _c.sent();
7892
+ if (!response || response.error) {
7893
+ throw new Error(response.error);
7677
7894
  }
7678
- walletCreationPayload = {
7679
- reference: uuid.v4(),
7680
- message: message,
7681
- signature: signature,
7682
- hashedMessage: hashedMessage,
7683
- clientAuthPubKey: deviceAuthKeys.publicKey,
7684
- returnWalletOption: walletCreationInput.returnWalletOption,
7685
- numShares: numberOfKeyShares !== null && numberOfKeyShares !== void 0 ? numberOfKeyShares : 0,
7686
- threshold: threshold !== null && threshold !== void 0 ? threshold : 0,
7687
- wallabyKeepsOne: (_e = (_d = walletCreationInput.keyShareOptions) === null || _d === void 0 ? void 0 : _d.storeOneKeyShareWithWallaby) !== null && _e !== void 0 ? _e : false,
7688
- // backupDevicePublicKey: walletCreationInput.keyShareOptions?.backupDevicePublicKey,
7689
- walletOwnershipType: (_f = walletCreationInput.walletOwnershipType) !== null && _f !== void 0 ? _f : exports.WalletOwnershipType.SingleOwner,
7690
- };
7691
- return [4 /*yield*/, this.request(WalletRoutes.createWithKeyShares, {
7692
- method: WalletRoutes.createWithKeyShares.method,
7693
- payload: walletCreationPayload,
7694
- })];
7895
+ return [2 /*return*/, response];
7695
7896
  case 2:
7696
- walletCreationResponse = _g.sent();
7697
- if (!walletCreationResponse || walletCreationResponse.error) {
7698
- throw new Error(walletCreationResponse.error);
7699
- }
7700
- inputKeyMaterial = walletCreationInput.authenticationOptions.encryptionMechanism === exports.EncryptionMechanism.Password
7701
- ? utf8ToBytes$2("".concat(walletCreationInput.authenticationOptions.salt).concat(walletCreationInput.authenticationOptions.password))
7702
- : walletCreationInput.authenticationOptions.prfValue;
7703
- if (!walletCreationResponse.encryptedSeed) return [3 /*break*/, 4];
7704
- return [4 /*yield*/, this.handleSeedPhraseFromResponse({
7705
- encryptedSeed: walletCreationResponse.encryptedSeed,
7706
- decryptedClientAuthPrivKey: deviceAuthKeys.privateKey,
7707
- storeSeedPhrase: walletCreationInput.storeSeedPhrase,
7708
- inputKeyMaterial: inputKeyMaterial,
7709
- })];
7710
- case 3:
7711
- decryptedSeedPhrase = _g.sent();
7712
- _g.label = 4;
7713
- case 4:
7714
- if (!walletCreationResponse.shares) return [3 /*break*/, 6];
7715
- return [4 /*yield*/, this.handleKeySharesFromResponse({
7716
- keyShares: walletCreationResponse.shares,
7717
- decryptedClientAuthPrivKey: deviceAuthKeys.privateKey,
7718
- inputKeyMaterial: inputKeyMaterial,
7719
- })];
7720
- case 5:
7721
- _g.sent();
7722
- _g.label = 6;
7723
- case 6: return [2 /*return*/, __assign(__assign({}, walletCreationResponse), { standardSeedPhrase: decryptedSeedPhrase })];
7897
+ error_3 = _c.sent();
7898
+ logger.error('SDK::Wallet::getTokenBalance::error', error_3);
7899
+ return [2 /*return*/, Promise.reject(error_3)];
7900
+ case 3: return [2 /*return*/];
7724
7901
  }
7725
7902
  });
7726
7903
  });
7727
7904
  };
7728
- WalletService.prototype.handleKeySharesFromResponse = function (_a) {
7905
+ /**
7906
+ *
7907
+ * Get all user assets across all wallets by specifying user id
7908
+ * @param userId Wallaby's user ID
7909
+ * @param queryOptions Optional query options
7910
+ * @returns {UserWalletsResponse} Returns an array of user assets by specifying user id
7911
+ */
7912
+ WalletDataService.prototype.getUserAssets = function (_a) {
7729
7913
  return __awaiter(this, arguments, void 0, function (_b) {
7730
- var deviceAuthPublicKey_1, encryptedKeyShares, error_16;
7731
- var _this = this;
7732
- var keyShares = _b.keyShares, decryptedClientAuthPrivKey = _b.decryptedClientAuthPrivKey, inputKeyMaterial = _b.inputKeyMaterial;
7914
+ var response, error_4;
7915
+ var userId = _b.userId, queryOptions = _b.queryOptions;
7733
7916
  return __generator(this, function (_c) {
7734
7917
  switch (_c.label) {
7735
7918
  case 0:
7736
- _c.trys.push([0, 3, , 4]);
7737
- deviceAuthPublicKey_1 = publicKeyByPrivateKey(decryptedClientAuthPrivKey);
7738
- logger.info('SDK::handleKeySharesFromResponse::deviceAuthKeys', {
7739
- deviceAuthPublicKey: deviceAuthPublicKey_1,
7740
- decryptedClientAuthPrivKey: decryptedClientAuthPrivKey,
7741
- });
7742
- logger.info('SDK::handleKeySharesFromResponse::keyShares encrypted with wallaby key', keyShares);
7743
- return [4 /*yield*/, Promise.all(keyShares.map(function (keyShare) { return __awaiter(_this, void 0, void 0, function () {
7744
- var decryptedKeyShare, encryptedOnceKeyShare, encryptedTwiceKeyShare;
7745
- return __generator(this, function (_a) {
7746
- switch (_a.label) {
7747
- case 0: return [4 /*yield*/, decryptWithPrivateKey(decryptedClientAuthPrivKey, keyShare)];
7748
- case 1:
7749
- decryptedKeyShare = _a.sent();
7750
- logger.info('SDK::handleKeySharesFromResponse::decryptedKeyShare with client private key', decryptedKeyShare);
7751
- return [4 /*yield*/, encryptWithPublicKey(deviceAuthPublicKey_1, decryptedKeyShare)];
7752
- case 2:
7753
- encryptedOnceKeyShare = _a.sent();
7754
- if (!encryptedOnceKeyShare) {
7755
- throw new Error('Failed to encrypt key share with device public key');
7756
- }
7757
- return [4 /*yield*/, this.cipher.encrypt({
7758
- data: JSON.stringify(encryptedOnceKeyShare),
7759
- inputKeyMaterial: inputKeyMaterial,
7760
- })];
7761
- case 3:
7762
- encryptedTwiceKeyShare = _a.sent();
7763
- if (!encryptedTwiceKeyShare) {
7764
- throw new Error('Failed to encrypt key share');
7765
- }
7766
- return [2 /*return*/, encryptedTwiceKeyShare];
7767
- }
7768
- });
7769
- }); }))];
7919
+ _c.trys.push([0, 2, , 3]);
7920
+ return [4 /*yield*/, this.request(WalletRoutes.getUserAssets, {
7921
+ method: WalletRoutes.getUserAssets.method,
7922
+ payload: {
7923
+ userId: userId,
7924
+ },
7925
+ queryParams: queryOptions,
7926
+ })];
7770
7927
  case 1:
7771
- encryptedKeyShares = _c.sent();
7772
- logger.info('SDK::handleKeySharesFromResponse::encryptedKeyShares', encryptedKeyShares);
7773
- return [4 /*yield*/, this.storageService.save(exports.LocalStorageItems.EncryptedKeyShares, JSON.stringify(encryptedKeyShares))];
7928
+ response = _c.sent();
7929
+ if (!response || response.error) {
7930
+ throw new Error(response.error);
7931
+ }
7932
+ return [2 /*return*/, response];
7774
7933
  case 2:
7775
- _c.sent();
7776
- return [3 /*break*/, 4];
7777
- case 3:
7778
- error_16 = _c.sent();
7779
- logger.error('SDK::handleKeySharesFromResponse::error', error_16);
7780
- throw error_16;
7781
- case 4: return [2 /*return*/];
7934
+ error_4 = _c.sent();
7935
+ logger.error('SDK::Wallet::getUserAssets::error', error_4);
7936
+ return [2 /*return*/, Promise.reject(error_4)];
7937
+ case 3: return [2 /*return*/];
7782
7938
  }
7783
7939
  });
7784
7940
  });
7785
7941
  };
7786
- WalletService.prototype.handleSeedPhraseFromResponse = function (_a) {
7942
+ /**
7943
+ *
7944
+ * Get all wallets belonging to a user.
7945
+ * @param userId Wallaby's user ID
7946
+ * @returns {UserWalletsResponse} Returns an array of user wallets by specifying user ID
7947
+ */
7948
+ WalletDataService.prototype.getUserWallets = function (_a) {
7787
7949
  return __awaiter(this, arguments, void 0, function (_b) {
7788
- var decryptedSeedPhrase, deviceAuthPublicKey, encryptedOnceSeedPhrase, encryptedTwiceSeedPhrase;
7789
- var encryptedSeed = _b.encryptedSeed, decryptedClientAuthPrivKey = _b.decryptedClientAuthPrivKey, inputKeyMaterial = _b.inputKeyMaterial, storeSeedPhrase = _b.storeSeedPhrase;
7950
+ var response, error_5;
7951
+ var userId = _b.userId, queryOptions = _b.queryOptions;
7790
7952
  return __generator(this, function (_c) {
7791
7953
  switch (_c.label) {
7792
- case 0: return [4 /*yield*/, decryptWithPrivateKey(decryptedClientAuthPrivKey, encryptedSeed)];
7954
+ case 0:
7955
+ _c.trys.push([0, 2, , 3]);
7956
+ return [4 /*yield*/, this.request(WalletRoutes.getUserWallets, {
7957
+ method: WalletRoutes.getUserWallets.method,
7958
+ payload: {
7959
+ userId: userId,
7960
+ },
7961
+ queryParams: queryOptions,
7962
+ })];
7793
7963
  case 1:
7794
- decryptedSeedPhrase = _c.sent();
7795
- if (!storeSeedPhrase) return [3 /*break*/, 5];
7796
- deviceAuthPublicKey = publicKeyByPrivateKey(decryptedClientAuthPrivKey);
7797
- return [4 /*yield*/, encryptWithPublicKey(deviceAuthPublicKey, decryptedSeedPhrase)];
7964
+ response = _c.sent();
7965
+ if (!response || response.error) {
7966
+ throw new Error(response.error);
7967
+ }
7968
+ return [2 /*return*/, response];
7798
7969
  case 2:
7799
- encryptedOnceSeedPhrase = _c.sent();
7800
- return [4 /*yield*/, this.cipher.encrypt({
7801
- data: JSON.stringify(encryptedOnceSeedPhrase),
7802
- inputKeyMaterial: inputKeyMaterial,
7803
- })];
7804
- case 3:
7805
- encryptedTwiceSeedPhrase = _c.sent();
7806
- return [4 /*yield*/, this.storageService.save(exports.LocalStorageItems.EncryptedStandardSeedPhrase, JSON.stringify(encryptedTwiceSeedPhrase))];
7807
- case 4:
7808
- _c.sent();
7809
- _c.label = 5;
7810
- case 5: return [2 /*return*/, decryptedSeedPhrase];
7970
+ error_5 = _c.sent();
7971
+ logger.error('SDK::Wallet::getUserWallets::error', error_5);
7972
+ return [2 /*return*/, Promise.reject(error_5)];
7973
+ case 3: return [2 /*return*/];
7811
7974
  }
7812
7975
  });
7813
7976
  });
7814
7977
  };
7815
- WalletService.prototype.handleRecoverFromSeedPhrase = function (_a) {
7978
+ /**
7979
+ *
7980
+ * Get all assets that exist in a certain wallet, by specifying a wallet ID
7981
+ * @param walletId Wallaby's wallet ID
7982
+ * @param { BasicQueryOptions } queryOptions Optional query options
7983
+ * @returns { WalletAssetsResponse } Returns all assets belonging in a certain wallet ID
7984
+ */
7985
+ WalletDataService.prototype.getWalletAssets = function (_a) {
7816
7986
  return __awaiter(this, arguments, void 0, function (_b) {
7817
- var initRecoveryFlowResponse, encryptedSeed, _c, signature, message, recoverResponse;
7818
- var decryptedSeedPhrase = _b.decryptedSeedPhrase, deviceAuthKeys = _b.deviceAuthKeys;
7819
- return __generator(this, function (_d) {
7820
- switch (_d.label) {
7821
- case 0: return [4 /*yield*/, this.request(WalletRoutes.initRecoveryFlow, {
7822
- method: WalletRoutes.initRecoveryFlow.method,
7823
- payload: {
7824
- externalUserId: this.userId,
7825
- clientAuthPubKey: deviceAuthKeys.publicKey,
7826
- },
7827
- })];
7828
- case 1:
7829
- initRecoveryFlowResponse = _d.sent();
7830
- if (!initRecoveryFlowResponse || initRecoveryFlowResponse.error) {
7831
- throw new Error('Failed to recover wallet from seed phrase');
7832
- }
7833
- encryptedSeed = encryptWithPublicKey(initRecoveryFlowResponse.wallabyAuthPubKey, decryptedSeedPhrase);
7834
- console.log('SDK::WalletService::recover:: encryptedSeed', encryptedSeed);
7835
- if (!encryptedSeed) {
7836
- throw new Error('Seed phrase could not be encrypted');
7837
- }
7838
- return [4 /*yield*/, this.authService.getSignature(deviceAuthKeys)];
7839
- case 2:
7840
- _c = _d.sent(), signature = _c.signature, message = _c.message;
7841
- return [4 /*yield*/, this.request(WalletRoutes.recover, {
7842
- method: WalletRoutes.recover.method,
7987
+ var response, error_6;
7988
+ var walletId = _b.walletId, queryOptions = _b.queryOptions;
7989
+ return __generator(this, function (_c) {
7990
+ switch (_c.label) {
7991
+ case 0:
7992
+ _c.trys.push([0, 2, , 3]);
7993
+ return [4 /*yield*/, this.request(WalletRoutes.getWalletAssets, {
7994
+ method: WalletRoutes.getWalletAssets.method,
7843
7995
  payload: {
7844
- externalUserId: this.userId,
7845
- signature: signature,
7846
- message: message,
7847
- clientAuthPubKey: deviceAuthKeys.publicKey,
7848
- encryptedSeed: encryptedSeed,
7996
+ walletId: walletId,
7849
7997
  },
7998
+ queryParams: queryOptions,
7850
7999
  })];
7851
- case 3:
7852
- recoverResponse = _d.sent();
7853
- if (!recoverResponse || recoverResponse.error) {
7854
- throw new Error('Failed to recover wallet from seed phrase');
8000
+ case 1:
8001
+ response = _c.sent();
8002
+ if (!response || response.error) {
8003
+ throw new Error(response.error);
7855
8004
  }
7856
- return [2 /*return*/, recoverResponse];
8005
+ return [2 /*return*/, response];
8006
+ case 2:
8007
+ error_6 = _c.sent();
8008
+ logger.error('SDK::Wallet::getWalletAssets::error', error_6);
8009
+ return [2 /*return*/, Promise.reject(error_6)];
8010
+ case 3: return [2 /*return*/];
7857
8011
  }
7858
8012
  });
7859
8013
  });
7860
8014
  };
7861
- WalletService.prototype.handleConstructingSeedPhraseFromShares = function (_a) {
8015
+ /**
8016
+ * Adds a token to the wallet.
8017
+ *
8018
+ * @param {string} params.assetConfigId - The asset configuration ID.
8019
+ * @param {string} params.walletId - The wallet ID.
8020
+ * @param {string} params.userId - The user ID.
8021
+ * @param {Object} params.config - The configuration object.
8022
+ * @param {string} [params.config.contractAddress] - The contract address.
8023
+ * @param {string} [params.config.suiObjectId] - The Sui object ID.
8024
+ * @param {string} params.config.chainName - The name of the blockchain.
8025
+ *
8026
+ * @returns {Promise<AddTokenToWalletResponse>} The response from adding the token to the wallet.
8027
+ *
8028
+ * @throws {Error} If required parameters are missing or invalid.
8029
+ */
8030
+ WalletDataService.prototype.addTokenToWallet = function (_a) {
7862
8031
  return __awaiter(this, arguments, void 0, function (_b) {
7863
- var retrieveSeedPhraseResponse, error_17;
7864
- var encryptedKeyShares = _b.encryptedKeyShares, walletId = _b.walletId, deviceAuthPublicKey = _b.deviceAuthPublicKey;
8032
+ var response, error_7;
8033
+ var assetConfigId = _b.assetConfigId, walletId = _b.walletId, userId = _b.userId, config = _b.config;
7865
8034
  return __generator(this, function (_c) {
7866
8035
  switch (_c.label) {
7867
8036
  case 0:
7868
8037
  _c.trys.push([0, 2, , 3]);
7869
- logger.info('SDK::handleConstructingSeedPhraseFromShares::payload', {
7870
- encryptedKeyShares: encryptedKeyShares,
7871
- walletId: walletId,
7872
- deviceAuthPublicKey: deviceAuthPublicKey,
7873
- });
7874
- return [4 /*yield*/, this.request(WalletRoutes.retrieveSeedPhrase, {
7875
- method: WalletRoutes.retrieveSeedPhrase.method,
8038
+ if (!walletId || !userId) {
8039
+ throw new Error('Missing required parameters');
8040
+ }
8041
+ if (!assetConfigId && !config) {
8042
+ throw new Error('Either assetConfigId or config must be provided');
8043
+ }
8044
+ if ((config === null || config === void 0 ? void 0 : config.chainName) === exports.BlockchainName.Sui) {
8045
+ if (!config.suiObjectId || !!config.contractAddress) {
8046
+ throw new Error('`suiObjectId` instead of `contractAddress` is required for Sui chain');
8047
+ }
8048
+ config.suiObjectId = sanitizeSuiObjectId(config.suiObjectId);
8049
+ }
8050
+ return [4 /*yield*/, this.request(WalletRoutes.addTokenToWallet, {
8051
+ method: WalletRoutes.addTokenToWallet.method,
7876
8052
  payload: {
8053
+ assetConfigId: assetConfigId,
7877
8054
  walletId: walletId,
7878
- recoveryPubKey: deviceAuthPublicKey,
7879
- encryptedShares: encryptedKeyShares,
8055
+ userId: userId,
8056
+ config: config,
7880
8057
  },
7881
8058
  })];
7882
8059
  case 1:
7883
- retrieveSeedPhraseResponse = _c.sent();
7884
- if (!retrieveSeedPhraseResponse || retrieveSeedPhraseResponse.error) {
7885
- throw new Error('Failed to retrieve seed phrase');
8060
+ response = _c.sent();
8061
+ if (!response || response.errors) {
8062
+ throw new Error(response.errors);
7886
8063
  }
7887
- return [2 /*return*/, retrieveSeedPhraseResponse.encryptedSeed];
8064
+ return [2 /*return*/, response];
7888
8065
  case 2:
7889
- error_17 = _c.sent();
7890
- logger.error('SDK::handleConstructingSeedPhraseFromShares::error', error_17);
7891
- return [2 /*return*/, Promise.reject(error_17)];
8066
+ error_7 = _c.sent();
8067
+ logger.error('SDK::Wallet::addTokenToWallet::error', error_7);
8068
+ return [2 /*return*/, Promise.reject(error_7)];
7892
8069
  case 3: return [2 /*return*/];
7893
8070
  }
7894
8071
  });
7895
8072
  });
7896
8073
  };
7897
- return WalletService;
8074
+ /**
8075
+ * Updates a wallet asset.
8076
+ *
8077
+ * @param {string} params.assetConfigId - Config ID for the user asset.
8078
+ * @param {string} params.walletId - Wallet ID to which the asset belongs.
8079
+ * @param {string} params.assetName - Asset name.
8080
+ * @param {boolean} params.isVisible - Asset visibility toggle.
8081
+ * @param {string} params.assetSymbol - Asset symbol.
8082
+ *
8083
+ * @returns {Promise<UpdateAssetResponse>} The response from updating the wallet token.
8084
+ *
8085
+ * @throws {Error} If required parameters are missing or invalid.
8086
+ */
8087
+ WalletDataService.prototype.updateAsset = function (_a) {
8088
+ return __awaiter(this, void 0, void 0, function () {
8089
+ var response, error_8;
8090
+ var assetConfigId = _a.assetConfigId, walletId = _a.walletId, assetConfig = __rest(_a, ["assetConfigId", "walletId"]);
8091
+ return __generator(this, function (_b) {
8092
+ switch (_b.label) {
8093
+ case 0:
8094
+ _b.trys.push([0, 2, , 3]);
8095
+ if (!assetConfigId || !walletId) {
8096
+ throw new Error('Missing required parameters');
8097
+ }
8098
+ return [4 /*yield*/, this.request(WalletRoutes.updateAsset, {
8099
+ method: WalletRoutes.updateAsset.method,
8100
+ payload: __assign({ configId: assetConfigId, walletId: walletId }, assetConfig),
8101
+ })];
8102
+ case 1:
8103
+ response = _b.sent();
8104
+ if (!response || response.errors) {
8105
+ throw new Error(response.errors);
8106
+ }
8107
+ return [2 /*return*/, response];
8108
+ case 2:
8109
+ error_8 = _b.sent();
8110
+ logger.error('SDK::Wallet::updateAsset::error', error_8);
8111
+ return [2 /*return*/, Promise.reject(error_8)];
8112
+ case 3: return [2 /*return*/];
8113
+ }
8114
+ });
8115
+ });
8116
+ };
8117
+ return WalletDataService;
7898
8118
  }(Base));
7899
8119
 
7900
8120
  // import jwt, { SignOptions } from 'jsonwebtoken';
@@ -9135,6 +9355,7 @@ var Wallaby = /** @class */ (function () {
9135
9355
  */
9136
9356
  function Wallaby(config) {
9137
9357
  this.wallet = new WalletService(config);
9358
+ this.walletData = new WalletDataService(config);
9138
9359
  this.utils = new Utils(config);
9139
9360
  this.auth = new AuthService(config);
9140
9361
  this.system = new System(config);