@@ -225,14 +225,71 @@ Wallets.prototype.acceptShare = function (params, callback) {
225
225
226
226
const self = this ;
227
227
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
+ } ;
228
258
229
259
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
+
231
288
// Return right away if there is no keychain to decrypt, or if explicit encryptedXprv was provided
232
289
if ( ! walletShare . keychain || ! walletShare . keychain . encryptedXprv || encryptedXprv ) {
290
+ console . log ( 'No keychain to decrypt, returning wallet share' ) ;
233
291
return walletShare ;
234
292
}
235
-
236
293
// More than viewing was requested, so we need to process the wallet keys using the shared ecdh scheme
237
294
if ( ! params . userPassword ) {
238
295
throw new Error ( 'userPassword param must be provided to decrypt shared key' ) ;
@@ -243,6 +300,7 @@ Wallets.prototype.acceptShare = function (params, callback) {
243
300
throw new Error ( 'EncryptedXprv was not found on sharing keychain' ) ;
244
301
}
245
302
303
+ console . log ( 'Decrypting shared keychain' ) ;
246
304
// Now we have the sharing keychain, we can work out the secret used for sharing the wallet with us
247
305
sharingKeychain . xprv = self . bitgo . decrypt ( {
248
306
password : params . userPassword ,
@@ -266,6 +324,7 @@ Wallets.prototype.acceptShare = function (params, callback) {
266
324
encryptedXprv = self . bitgo . encrypt ( { password : newWalletPassphrase , input : decryptedSharedWalletXprv } ) ;
267
325
268
326
// 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' ) ;
269
328
return walletShare ;
270
329
} ) ;
271
330
} )
@@ -278,8 +337,16 @@ Wallets.prototype.acceptShare = function (params, callback) {
278
337
if ( encryptedXprv ) {
279
338
updateParams . encryptedXprv = encryptedXprv ;
280
339
}
340
+ if ( keyId && walletShare . keychainOverrideRequired && walletShare . permissions . indexOf ( 'admin' ) !== - 1 ) {
341
+ updateParams . keyId = keyId ;
342
+ }
343
+ console . log ( '🚀 ~ updateParams:' , updateParams ) ;
344
+ self . updateShare ( updateParams ) ;
281
345
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
+ }
283
350
} )
284
351
. nodeify ( callback ) ;
285
352
} ;
0 commit comments