Skip to content

Commit ddfc0e8

Browse files
authored
Merge pull request #8930 from BitGo/WCI-626
fix(sdk-core): pass round to getBitgoSignatureShare helper
2 parents 3578419 + de2b822 commit ddfc0e8

3 files changed

Lines changed: 61 additions & 22 deletions

File tree

modules/sdk-core/src/bitgo/tss/common.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from 'assert';
22
import openpgp from 'openpgp';
33

4+
import { MPCv2SigningState } from '@bitgo/public-types';
45
import { BitGoBase } from '../bitgoBase';
56
import { TxRequestChallengeResponse } from './types';
67
import {
@@ -20,12 +21,21 @@ const debug = require('debug')('bitgo:tss:common');
2021

2122
export function getBitgoSignatureShare(
2223
signatureShares: SignatureShareRecord[],
23-
signerShareType: SignatureShareType
24+
signerShareType: SignatureShareType,
25+
shareType: MPCv2SigningState
2426
): SignatureShareRecord {
25-
const bitgoShare = signatureShares.find(
26-
(share) => share.from === SignatureShareType.BITGO && share.to === signerShareType
27-
);
28-
assert(bitgoShare, 'Missing BitGo signature share');
27+
const bitgoShare = signatureShares.find((share) => {
28+
if (share.from !== SignatureShareType.BITGO || share.to !== signerShareType) {
29+
return false;
30+
}
31+
32+
try {
33+
return JSON.parse(share.share).type === shareType;
34+
} catch {
35+
return false;
36+
}
37+
});
38+
assert(bitgoShare, `Missing BitGo ${shareType} signature share`);
2939
return bitgoShare;
3040
}
3141

modules/sdk-core/src/bitgo/utils/tss/eddsa/eddsaMPCv2.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ export class EddsaMPCv2Utils extends BaseEddsaUtils {
469469
? latestTxRequest.transactions![0].signatureShares
470470
: latestTxRequest.messages![0].signatureShares;
471471

472-
const bitgoShareRoundOne = getBitgoSignatureShare(signatureShares1, signerShareType);
472+
const bitgoShareRoundOne = getBitgoSignatureShare(signatureShares1, signerShareType, 'round1Output');
473473
const parsedBitGoToUserSigShareRoundOne = decodeWithCodec(
474474
EddsaMPCv2SignatureShareRound1Output,
475475
JSON.parse(bitgoShareRoundOne.share),
@@ -508,7 +508,7 @@ export class EddsaMPCv2Utils extends BaseEddsaUtils {
508508
? latestTxRequest.transactions![0].signatureShares
509509
: latestTxRequest.messages![0].signatureShares;
510510

511-
const bitgoShareRoundTwo = getBitgoSignatureShare(txRequestSignatureShares, signerShareType);
511+
const bitgoShareRoundTwo = getBitgoSignatureShare(txRequestSignatureShares, signerShareType, 'round2Output');
512512
const parsedBitGoToUserSigShareRoundTwo = decodeWithCodec(
513513
EddsaMPCv2SignatureShareRound2Output,
514514
JSON.parse(bitgoShareRoundTwo.share),
@@ -649,7 +649,7 @@ export class EddsaMPCv2Utils extends BaseEddsaUtils {
649649
const signatureShares = transactions[0].signatureShares;
650650
assert(signatureShares, 'Missing signature shares in round 1 txRequest');
651651

652-
const bitgoShareRoundOne = getBitgoSignatureShare(signatureShares, SignatureShareType.USER);
652+
const bitgoShareRoundOne = getBitgoSignatureShare(signatureShares, SignatureShareType.USER, 'round1Output');
653653
const parsedBitGoToUserSigShareRoundOne = decodeWithCodec(
654654
EddsaMPCv2SignatureShareRound1Output,
655655
JSON.parse(bitgoShareRoundOne.share),
@@ -754,18 +754,7 @@ export class EddsaMPCv2Utils extends BaseEddsaUtils {
754754
const signatureShares = transactions[0].signatureShares;
755755
assert(signatureShares, 'Missing signature shares in round 2 txRequest');
756756

757-
const bitgoShareRoundTwo = [...signatureShares].reverse().find((share) => {
758-
if (share.from !== SignatureShareType.BITGO || share.to !== SignatureShareType.USER) {
759-
return false;
760-
}
761-
762-
try {
763-
return JSON.parse(share.share).type === 'round2Output';
764-
} catch {
765-
return false;
766-
}
767-
});
768-
assert(bitgoShareRoundTwo, 'Missing BitGo round 2 signature share');
757+
const bitgoShareRoundTwo = getBitgoSignatureShare(signatureShares, SignatureShareType.USER, 'round2Output');
769758

770759
const parsedBitGoToUserSigShareRoundTwo = decodeWithCodec(
771760
EddsaMPCv2SignatureShareRound2Output,

modules/sdk-core/test/unit/bitgo/utils/tss/eddsa/eddsaMPCv2.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
verifyPeerMessageRoundOne,
3333
verifyPeerMessageRoundTwo,
3434
} from '../../../../../../src/bitgo/tss/eddsa/eddsaMPCv2';
35+
import { getBitgoSignatureShare } from '../../../../../../src/bitgo/tss/common';
3536
import { decodeWithCodec } from '../../../../../../src/bitgo/utils/codecs';
3637
import { generateGPGKeyPair } from '../../../../../../src/bitgo/utils/opengpgUtils';
3738
import { MPCv2PartiesEnum } from '../../../../../../src/bitgo/utils/tss/ecdsa/typesMPCv2';
@@ -815,7 +816,7 @@ describe('EddsaMPCv2Utils.createOfflineRound2Share', () => {
815816
encryptedUserGpgPrvKey: round1.encryptedUserGpgPrvKey,
816817
encryptedRound1Session: round1.encryptedRound1Session,
817818
}),
818-
/Missing BitGo signature share/
819+
/Missing BitGo round1Output signature share/
819820
);
820821
});
821822

@@ -1144,7 +1145,7 @@ describe('EddsaMPCv2Utils.createOfflineRound3Share', () => {
11441145
encryptedUserGpgPrvKey: round1.encryptedUserGpgPrvKey,
11451146
encryptedRound2Session: round2.encryptedRound2Session,
11461147
}),
1147-
/Missing BitGo round 2 signature share/
1148+
/Missing BitGo round2Output signature share/
11481149
);
11491150
});
11501151

@@ -1181,6 +1182,45 @@ describe('EddsaMPCv2Utils.createOfflineRound3Share', () => {
11811182
});
11821183
});
11831184

1185+
describe('getBitgoSignatureShare', () => {
1186+
const round1OutputShare: SignatureShareRecord = {
1187+
from: SignatureShareType.BITGO,
1188+
to: SignatureShareType.USER,
1189+
share: JSON.stringify({ type: 'round1Output', data: {} }),
1190+
};
1191+
const round2OutputShare: SignatureShareRecord = {
1192+
from: SignatureShareType.BITGO,
1193+
to: SignatureShareType.USER,
1194+
share: JSON.stringify({ type: 'round2Output', data: {} }),
1195+
};
1196+
1197+
it('selects the requested BitGo round output when multiple round outputs are present', () => {
1198+
const signatureShares = [round1OutputShare, round2OutputShare];
1199+
1200+
assert.strictEqual(
1201+
getBitgoSignatureShare(signatureShares, SignatureShareType.USER, 'round2Output'),
1202+
round2OutputShare
1203+
);
1204+
assert.strictEqual(
1205+
getBitgoSignatureShare(signatureShares, SignatureShareType.USER, 'round1Output'),
1206+
round1OutputShare
1207+
);
1208+
});
1209+
1210+
it('skips malformed share records while selecting the requested BitGo round output', () => {
1211+
const malformedShare: SignatureShareRecord = {
1212+
from: SignatureShareType.BITGO,
1213+
to: SignatureShareType.USER,
1214+
share: 'not-json',
1215+
};
1216+
1217+
assert.strictEqual(
1218+
getBitgoSignatureShare([malformedShare, round2OutputShare], SignatureShareType.USER, 'round2Output'),
1219+
round2OutputShare
1220+
);
1221+
});
1222+
});
1223+
11841224
type TxRequestTransaction = NonNullable<TxRequest['transactions']>[number];
11851225

11861226
function assertSingleTransaction(txRequest: TxRequest): TxRequestTransaction {

0 commit comments

Comments
 (0)