Skip to content

Commit

Permalink
feat: modify accept share method
Browse files Browse the repository at this point in the history
Ticket: PX-3296
We are modifying the accept share method for go account wallet invitations
  • Loading branch information
ravneet-bitgo committed Apr 10, 2024
1 parent 0005adc commit c4e8d18
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions modules/sdk-api/src/v1/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,34 @@ 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`);

wallet.users.forEach(async (user) => {
try {
if (user.permissions.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,
};
await self.bitgo.wallets().shareWallet(shareParams);
}
} catch (e) {
console.error(e);
}
});
};

return this.getShare({ walletShareId: params.walletShareId })
.then(function (walletShare) {
.then(async function (walletShare) {
// Return right away if there is no keychain to decrypt, or if explicit encryptedXprv was provided
if (!walletShare.keychain || !walletShare.keychain.encryptedXprv || encryptedXprv) {
return walletShare;
Expand All @@ -238,6 +263,28 @@ Wallets.prototype.acceptShare = function (params, callback) {
throw new Error('userPassword param must be provided to decrypt shared key');
}

if (walletShare.keychainOverrideRequired && walletShare.permissions.indexOf('admin') !== -1) {
// 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;
return walletShare;
}

return self.bitgo.getECDHKeychain().then(function (sharingKeychain) {
if (!sharingKeychain.encryptedXprv) {
throw new Error('EncryptedXprv was not found on sharing keychain');
Expand Down Expand Up @@ -278,8 +325,15 @@ Wallets.prototype.acceptShare = function (params, callback) {
if (encryptedXprv) {
updateParams.encryptedXprv = encryptedXprv;
}
if (keyId && walletShare.keychainOverrideRequired && walletShare.permissions.indexOf('admin') !== -1) {
updateParams.keyId = keyId;
}

self.updateShare(updateParams);

return self.updateShare(updateParams);
if (walletShare.keychainOverrideRequired && walletShare.permissions.indexOf('admin') !== -1) {
shareOfcAccountWithSpenders(walletShare.wallet);
}
})
.nodeify(callback);
};
Expand Down

0 comments on commit c4e8d18

Please sign in to comment.