Skip to content

Commit 707b7fc

Browse files
committed
feat: modify accept share method
Ticket: PX-3296 We are modifying the accept share method for go account wallet invitations
1 parent 0005adc commit 707b7fc

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

modules/sdk-api/src/v1/wallets.ts

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,71 @@ Wallets.prototype.acceptShare = function (params, callback) {
225225

226226
const self = this;
227227
let encryptedXprv = params.overrideEncryptedXprv;
228+
let keyId = params.keyId;
229+
230+
const shareOfcAccountWithSpenders = async (walletId: string) => {
231+
const wallet = await self.bitgo.wallets().get({ id: walletId });
232+
const enterpriseUsersResponse = await self.bitgo.get(`/api/v2/enterprise/${wallet.enterprise}/user`);
233+
console.log('🚀 ~ shareOfcAccountWithSpenders ~ wallet:', wallet);
234+
console.log('🚀 ~ shareOfcAccountWithSpenders ~ enterpriseUsersResponse:', enterpriseUsersResponse);
235+
236+
wallet.users.forEach(async (user) => {
237+
console.log('🚀 ~ shareOfcAccountWithSpenders ~ user:', user);
238+
try {
239+
if (user.permissions.includes('spend')) {
240+
console.log('🚀 ~ user permission includes spend');
241+
const userObject = enterpriseUsersResponse.users.find((enterpriseUser) => enterpriseUser.id === user.user);
242+
const shareParams = {
243+
walletId: walletId,
244+
user: user.user,
245+
permissions: user.permissions.join(','),
246+
walletPassphrase: params.userPassword,
247+
email: userObject.email,
248+
coin: wallet.coin,
249+
};
250+
console.log('🚀 ~ wallet.users.forEach ~ shareParams:', shareParams);
251+
await self.bitgo.wallets().shareWallet(shareParams);
252+
}
253+
} catch (e) {
254+
console.error(e);
255+
}
256+
});
257+
};
228258

229259
return this.getShare({ walletShareId: params.walletShareId })
230-
.then(function (walletShare) {
260+
.then(async function (walletShare) {
261+
if (walletShare.keychainOverrideRequired && walletShare.permissions.indexOf('admin') !== -1) {
262+
if (!params.userPassword) {
263+
throw new Error('userPassword param must be provided to decrypt shared key');
264+
}
265+
console.log('Creating new keychain for wallet share');
266+
// generate new keychain
267+
const sdkCoin = await self.coin('ofc');
268+
const keychains = sdkCoin.keychains();
269+
const newKeychain = keychains.create();
270+
const originalPasscodeEncryptionCode = self.bitgo.generateRandomPassword();
271+
272+
const encryptedPrv = self.bitgo.encrypt({
273+
password: params.userPassword,
274+
input: newKeychain.prv,
275+
});
276+
277+
const walletKeychain = await keychains.add({
278+
encryptedPrv,
279+
originalPasscodeEncryptionCode,
280+
pub: newKeychain.pub,
281+
source: 'user',
282+
});
283+
keyId = walletKeychain.id;
284+
console.log('Keychain created successfully and returning wallet share 1');
285+
return walletShare;
286+
}
287+
231288
// Return right away if there is no keychain to decrypt, or if explicit encryptedXprv was provided
232289
if (!walletShare.keychain || !walletShare.keychain.encryptedXprv || encryptedXprv) {
290+
console.log('No keychain to decrypt, returning wallet share');
233291
return walletShare;
234292
}
235-
236293
// More than viewing was requested, so we need to process the wallet keys using the shared ecdh scheme
237294
if (!params.userPassword) {
238295
throw new Error('userPassword param must be provided to decrypt shared key');
@@ -243,6 +300,7 @@ Wallets.prototype.acceptShare = function (params, callback) {
243300
throw new Error('EncryptedXprv was not found on sharing keychain');
244301
}
245302

303+
console.log('Decrypting shared keychain');
246304
// Now we have the sharing keychain, we can work out the secret used for sharing the wallet with us
247305
sharingKeychain.xprv = self.bitgo.decrypt({
248306
password: params.userPassword,
@@ -266,6 +324,7 @@ Wallets.prototype.acceptShare = function (params, callback) {
266324
encryptedXprv = self.bitgo.encrypt({ password: newWalletPassphrase, input: decryptedSharedWalletXprv });
267325

268326
// Carry on to the next block where we will post the acceptance of the share with the encrypted xprv
327+
console.log('Decrypted shared keychain successfully and returning wallet share 2');
269328
return walletShare;
270329
});
271330
})
@@ -278,8 +337,16 @@ Wallets.prototype.acceptShare = function (params, callback) {
278337
if (encryptedXprv) {
279338
updateParams.encryptedXprv = encryptedXprv;
280339
}
340+
if (keyId && walletShare.keychainOverrideRequired && walletShare.permissions.indexOf('admin') !== -1) {
341+
updateParams.keyId = keyId;
342+
}
343+
console.log('🚀 ~ updateParams:', updateParams);
344+
self.updateShare(updateParams);
281345

282-
return self.updateShare(updateParams);
346+
if (walletShare.keychainOverrideRequired && walletShare.permissions.indexOf('admin') !== -1) {
347+
console.log('Sharing wallet with spenders');
348+
shareOfcAccountWithSpenders(walletShare.wallet);
349+
}
283350
})
284351
.nodeify(callback);
285352
};

0 commit comments

Comments
 (0)