From 707b7fcff710b861b29b7b7e6b7b65dbd92f01b9 Mon Sep 17 00:00:00 2001 From: Ravneet Sandhu Date: Wed, 10 Apr 2024 20:09:11 +0530 Subject: [PATCH] feat: modify accept share method Ticket: PX-3296 We are modifying the accept share method for go account wallet invitations --- modules/sdk-api/src/v1/wallets.ts | 73 +++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/modules/sdk-api/src/v1/wallets.ts b/modules/sdk-api/src/v1/wallets.ts index ca8f113a85..69f084a6ba 100644 --- a/modules/sdk-api/src/v1/wallets.ts +++ b/modules/sdk-api/src/v1/wallets.ts @@ -225,14 +225,71 @@ Wallets.prototype.acceptShare = function (params, callback) { const self = this; let encryptedXprv = params.overrideEncryptedXprv; + let keyId = params.keyId; + + const shareOfcAccountWithSpenders = async (walletId: string) => { + const wallet = await self.bitgo.wallets().get({ id: walletId }); + const enterpriseUsersResponse = await self.bitgo.get(`/api/v2/enterprise/${wallet.enterprise}/user`); + console.log('🚀 ~ shareOfcAccountWithSpenders ~ wallet:', wallet); + console.log('🚀 ~ shareOfcAccountWithSpenders ~ enterpriseUsersResponse:', enterpriseUsersResponse); + + wallet.users.forEach(async (user) => { + console.log('🚀 ~ shareOfcAccountWithSpenders ~ user:', user); + try { + if (user.permissions.includes('spend')) { + console.log('🚀 ~ user permission includes spend'); + const userObject = enterpriseUsersResponse.users.find((enterpriseUser) => enterpriseUser.id === user.user); + const shareParams = { + walletId: walletId, + user: user.user, + permissions: user.permissions.join(','), + walletPassphrase: params.userPassword, + email: userObject.email, + coin: wallet.coin, + }; + console.log('🚀 ~ wallet.users.forEach ~ shareParams:', shareParams); + await self.bitgo.wallets().shareWallet(shareParams); + } + } catch (e) { + console.error(e); + } + }); + }; return this.getShare({ walletShareId: params.walletShareId }) - .then(function (walletShare) { + .then(async function (walletShare) { + if (walletShare.keychainOverrideRequired && walletShare.permissions.indexOf('admin') !== -1) { + if (!params.userPassword) { + throw new Error('userPassword param must be provided to decrypt shared key'); + } + console.log('Creating new keychain for wallet share'); + // generate new keychain + const sdkCoin = await self.coin('ofc'); + const keychains = sdkCoin.keychains(); + const newKeychain = keychains.create(); + const originalPasscodeEncryptionCode = self.bitgo.generateRandomPassword(); + + const encryptedPrv = self.bitgo.encrypt({ + password: params.userPassword, + input: newKeychain.prv, + }); + + const walletKeychain = await keychains.add({ + encryptedPrv, + originalPasscodeEncryptionCode, + pub: newKeychain.pub, + source: 'user', + }); + keyId = walletKeychain.id; + console.log('Keychain created successfully and returning wallet share 1'); + return walletShare; + } + // Return right away if there is no keychain to decrypt, or if explicit encryptedXprv was provided if (!walletShare.keychain || !walletShare.keychain.encryptedXprv || encryptedXprv) { + console.log('No keychain to decrypt, returning wallet share'); return walletShare; } - // More than viewing was requested, so we need to process the wallet keys using the shared ecdh scheme if (!params.userPassword) { throw new Error('userPassword param must be provided to decrypt shared key'); @@ -243,6 +300,7 @@ Wallets.prototype.acceptShare = function (params, callback) { throw new Error('EncryptedXprv was not found on sharing keychain'); } + console.log('Decrypting shared keychain'); // Now we have the sharing keychain, we can work out the secret used for sharing the wallet with us sharingKeychain.xprv = self.bitgo.decrypt({ password: params.userPassword, @@ -266,6 +324,7 @@ Wallets.prototype.acceptShare = function (params, callback) { encryptedXprv = self.bitgo.encrypt({ password: newWalletPassphrase, input: decryptedSharedWalletXprv }); // Carry on to the next block where we will post the acceptance of the share with the encrypted xprv + console.log('Decrypted shared keychain successfully and returning wallet share 2'); return walletShare; }); }) @@ -278,8 +337,16 @@ Wallets.prototype.acceptShare = function (params, callback) { if (encryptedXprv) { updateParams.encryptedXprv = encryptedXprv; } + if (keyId && walletShare.keychainOverrideRequired && walletShare.permissions.indexOf('admin') !== -1) { + updateParams.keyId = keyId; + } + console.log('🚀 ~ updateParams:', updateParams); + self.updateShare(updateParams); - return self.updateShare(updateParams); + if (walletShare.keychainOverrideRequired && walletShare.permissions.indexOf('admin') !== -1) { + console.log('Sharing wallet with spenders'); + shareOfcAccountWithSpenders(walletShare.wallet); + } }) .nodeify(callback); };