Skip to content

Commit

Permalink
feat(sdk-core): 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
shivagupta94 authored and ravneet-bitgo committed Apr 25, 2024
1 parent 1240fac commit 74cba44
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
53 changes: 32 additions & 21 deletions modules/bitgo/test/v2/unit/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,11 +810,12 @@ describe('V2 Wallets:', function () {
const shareId = 'test_case_2';
const keychainId = 'test_case_2';
const userPassword = 'test_case_2';

// create a user key
const keyChainNock = nock(bgUrl)
.post('/api/v2/tbtc/key', _.conforms({ pub: (p) => p.startsWith('xpub') }))
.reply(200, { id: keychainId });
.reply(200, (uri, requestBody) => {
return { id: keychainId, encryptedPrv: requestBody['encryptedPrv'], pub: requestBody['pub'] };
});

const walletShareInfoNock = nock(bgUrl)
.get(`/api/v2/tbtc/walletshare/${shareId}`)
Expand All @@ -824,10 +825,11 @@ describe('V2 Wallets:', function () {
});

const acceptShareNock = nock(bgUrl)
.post(`/api/v2/tbtc/walletshare/${shareId}`, {
walletShareId: shareId,
state: 'accepted',
keyId: keychainId,
.post(`/api/v2/tbtc/walletshare/${shareId}`, (body: any) => {
if (body.walletShareId !== shareId || body.state !== 'accepted' || body.keyId !== keychainId) {
return false;
}
return true;
})
.reply(200, { changed: false });

Expand All @@ -850,7 +852,9 @@ describe('V2 Wallets:', function () {
// create a user key
const keyChainNock = nock(bgUrl)
.post('/api/v2/tbtc/key', _.conforms({ pub: (p) => p.startsWith('xpub') }))
.reply(200, { id: keychainId });
.reply(200, (uri, requestBody) => {
return { id: keychainId, encryptedPrv: requestBody['encryptedPrv'], pub: requestBody['pub'] };
});

const walletShareInfoNock = nock(bgUrl)
.get(`/api/v2/tbtc/walletshare/${shareId}`)
Expand All @@ -860,10 +864,11 @@ describe('V2 Wallets:', function () {
});

const acceptShareNock = nock(bgUrl)
.post(`/api/v2/tbtc/walletshare/${shareId}`, {
walletShareId: shareId,
state: 'accepted',
keyId: keychainId,
.post(`/api/v2/tbtc/walletshare/${shareId}`, (body: any) => {
if (body.walletShareId !== shareId || body.state !== 'accepted' || body.keyId !== keychainId) {
return false;
}
return true;
})
.reply(200, { changed: true, state: 'not_accepted' });

Expand Down Expand Up @@ -933,13 +938,16 @@ describe('V2 Wallets:', function () {
// create a user key
const keyChainCreateNock = nock(bgUrl)
.post('/api/v2/tbtc/key', _.conforms({ pub: (p) => p.startsWith('xpub') }))
.reply(200, { id: keychainId });
.reply(200, (uri, requestBody) => {
return { id: keychainId, encryptedPrv: requestBody['encryptedPrv'], pub: requestBody['pub'] };
});

const acceptShareNock = nock(bgUrl)
.post(`/api/v2/tbtc/walletshare/${shareId}`, {
walletShareId: shareId,
state: 'accepted',
keyId: keychainId,
.post(`/api/v2/tbtc/walletshare/${shareId}`, (body: any) => {
if (body.walletShareId !== shareId || body.state !== 'accepted' || body.keyId !== keychainId) {
return false;
}
return true;
})
.reply(200, { changed: true, state: 'accepted' });

Expand Down Expand Up @@ -1056,13 +1064,16 @@ describe('V2 Wallets:', function () {
// create a user key
const keyChainCreateNock = nock(bgUrl)
.post('/api/v2/tbtc/key', _.conforms({ pub: (p) => p.startsWith('xpub') }))
.reply(200, { id: keychainId });
.reply(200, (uri, requestBody) => {
return { id: keychainId, encryptedPrv: requestBody['encryptedPrv'], pub: requestBody['pub'] };
});

const acceptShareNock = nock(bgUrl)
.post(`/api/v2/tbtc/walletshare/${shareId}`, {
walletShareId: shareId,
state: 'accepted',
keyId: keychainId,
.post(`/api/v2/tbtc/walletshare/${shareId}`, (body: any) => {
if (body.walletShareId !== shareId || body.state !== 'accepted' || body.keyId !== keychainId) {
return false;
}
return true;
})
.reply(200, { changed: true, state: 'accepted' });

Expand Down
2 changes: 2 additions & 0 deletions modules/sdk-core/src/bitgo/wallet/iWallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export interface UpdateShareOptions {
state?: string;
encryptedPrv?: string;
keyId?: string;
signature?: string;
payload?: string;
}

export interface AcceptShareOptions {
Expand Down
21 changes: 21 additions & 0 deletions modules/sdk-core/src/bitgo/wallet/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,17 +601,38 @@ export class Wallets implements IWallets {
if (_.isUndefined(params.userPassword)) {
throw new Error('userPassword param must be provided to decrypt shared key');
}

const walletKeychain = await this.baseCoin.keychains().createUserKeychain(params.userPassword);
if (_.isUndefined(walletKeychain.encryptedPrv)) {
throw new Error('encryptedPrv was not found on wallet keychain');
}

const payload = {
tradingAccountId: walletShare.wallet,
pubkey: walletKeychain.pub,
timestamp: new Date().toISOString(),
};
const payloadString = JSON.stringify(payload);

const privateKey = this.bitgo.decrypt({
password: params.userPassword,
input: walletKeychain.encryptedPrv,
});
const signature = await this.baseCoin.signMessage({ prv: privateKey }, payloadString);

const response = await this.updateShare({
walletShareId: params.walletShareId,
state: 'accepted',
keyId: walletKeychain.id,
signature: signature.toString('hex'),
payload: payloadString,
});
// If the wallet share was accepted successfully (changed=true), reshare the wallet with the spenders
if (response.changed && response.state === 'accepted') {
try {
await this.reshareWalletWithSpenders(walletShare.wallet, params.userPassword);
} catch (e) {
// TODO: PX-3826
// Do nothing
}
}
Expand Down

0 comments on commit 74cba44

Please sign in to comment.