Skip to content

Commit 57d6ffd

Browse files
committed
fix(sdk-core): assert signableHex consistency in ECDSA MPCv1 signing
For TSS_VERIFY_USE_SERIALIZED_TX_HEX coins (BSC/XDC), the MPCv1 signing path verified serializedTxHex and signed signableHex without binding the two fields. Adds the same assertSignableConsistency check the MPCv2 path already performs (WCI-1398) so the signed payload must be consistent with the verified transaction. Ticket: WCI-2239
1 parent d4fda12 commit 57d6ffd

3 files changed

Lines changed: 206 additions & 1 deletion

File tree

  • modules

‎modules/sdk-coin-bsc/test/unit/bsc.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import 'should';
22

3+
import * as sinon from 'sinon';
34
import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test';
45
import { BitGoAPI } from '@bitgo/sdk-api';
5-
import { TransactionType, Wallet } from '@bitgo/sdk-core';
6+
import { EcdsaUtils, IWallet, TransactionType, TssSignTxRequestParamsWithPrv, Wallet } from '@bitgo/sdk-core';
67
import { FeeMarketEIP1559Transaction, Transaction as LegacyTransaction } from '@ethereumjs/tx';
78
import { RLP } from '@ethereumjs/rlp';
89
import { bufArrToArr } from 'ethereumjs-util';
@@ -283,6 +284,73 @@ describe('Native BNB', function () {
283284
/EIP-1559 not enabled on Common/
284285
);
285286
});
287+
288+
it('MPCv1 signRequestBase rejects a tampered signableHex end-to-end (WCI-2239)', async function () {
289+
const coin = bitgo.coin('tbsc') as Tbsc;
290+
sinon.stub(coin, 'verifyTransaction').resolves(undefined);
291+
const wallet = new Wallet(bitgo, coin, { coinSpecific: { baseAddress: recipientAddress } });
292+
const utils = new EcdsaUtils(bitgo, coin, wallet as unknown as IWallet);
293+
const benignSerializedTxHex = await buildSerializedTxHex(recipientAddress);
294+
const tamperedSignableHex = deriveSignableHex(await buildSerializedTxHex(wrongAddress), coin);
295+
const txRequest = {
296+
txRequestId: 'wci-2239-test',
297+
apiVersion: 'full',
298+
walletId: wallet.id(),
299+
transactions: [
300+
{
301+
unsignedTx: {
302+
derivationPath: 'm/0',
303+
serializedTxHex: benignSerializedTxHex,
304+
signableHex: tamperedSignableHex,
305+
},
306+
},
307+
],
308+
};
309+
const params = {
310+
txRequest,
311+
txParams: { recipients: [{ address: recipientAddress, amount: transferAmount }] },
312+
prv: JSON.stringify({ pShare: { i: 1 }, backupNShare: 'fake' }),
313+
reqId: { inc: sinon.stub(), toString: () => 'req' } as any,
314+
} as unknown as TssSignTxRequestParamsWithPrv;
315+
await utils
316+
.signTxRequest(params)
317+
.should.be.rejectedWith('signableHex is inconsistent with serializedTxHex: possible server tampering');
318+
});
319+
320+
it('MPCv1 signRequestBase passes a consistent signableHex end-to-end (WCI-2239)', async function () {
321+
const coin = bitgo.coin('tbsc') as Tbsc;
322+
sinon.stub(coin, 'verifyTransaction').resolves(undefined);
323+
const wallet = new Wallet(bitgo, coin, { coinSpecific: { baseAddress: recipientAddress } });
324+
const utils = new EcdsaUtils(bitgo, coin, wallet as unknown as IWallet);
325+
const benignSerializedTxHex = await buildSerializedTxHex(recipientAddress);
326+
const signableHex = deriveSignableHex(benignSerializedTxHex, coin);
327+
sinon.stub(utils, 'getOfflineSignerPaillierModulus').throws(new Error('SENTINEL_PAST_CONSISTENCY'));
328+
const txRequest = {
329+
txRequestId: 'wci-2239-test',
330+
apiVersion: 'full',
331+
walletId: wallet.id(),
332+
transactions: [
333+
{
334+
unsignedTx: {
335+
derivationPath: 'm/0',
336+
serializedTxHex: benignSerializedTxHex,
337+
signableHex,
338+
},
339+
},
340+
],
341+
};
342+
const params = {
343+
txRequest,
344+
txParams: { recipients: [{ address: recipientAddress, amount: transferAmount }] },
345+
prv: JSON.stringify({ pShare: { i: 1 }, backupNShare: 'fake' }),
346+
reqId: { inc: sinon.stub(), toString: () => 'req' } as any,
347+
} as unknown as TssSignTxRequestParamsWithPrv;
348+
await utils.signTxRequest(params).should.be.rejectedWith(/SENTINEL_PAST_CONSISTENCY/);
349+
});
350+
351+
after(function () {
352+
sinon.restore();
353+
});
286354
});
287355
});
288356
});

‎modules/sdk-core/src/bitgo/utils/tss/ecdsa/ecdsa.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import { BaseEcdsaUtils } from './base';
5959
import { EncryptionVersion, IRequestTracer } from '../../../../api';
6060
import { resolveEffectiveTxParams } from '../recipientUtils';
6161
import { shouldVerifyWithSerializedTxHex } from '../serializedTxHexVerify';
62+
import { isCoinWithSignableConsistency } from '../signableConsistency';
6263

6364
const encryptNShare = ECDSAMethods.encryptNShare;
6465

@@ -1075,6 +1076,9 @@ export class EcdsaUtils extends BaseEcdsaUtils {
10751076
wallet: this.wallet,
10761077
walletType: this.wallet.multisigType(),
10771078
});
1079+
if (shouldVerifyWithSerializedTxHex(this.baseCoin) && isCoinWithSignableConsistency(this.baseCoin)) {
1080+
this.baseCoin.assertSignableConsistency(unsignedTx.serializedTxHex, unsignedTx.signableHex);
1081+
}
10781082
} else {
10791083
await this.baseCoin.verifyTransaction({
10801084
txPrebuild: { txHex: unsignedTx.signableHex },
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import * as assert from 'assert';
2+
import * as sinon from 'sinon';
3+
import { CoinFeature } from '@bitgo/statics';
4+
import {
5+
BitGoBase,
6+
EcdsaUtils,
7+
IBaseCoin,
8+
IRequestTracer,
9+
IWallet,
10+
TssSignTxRequestParamsWithPrv,
11+
TxRequest,
12+
} from '../../../../../../src';
13+
import { InvalidTransactionError } from '../../../../../../src/bitgo/errors';
14+
15+
/**
16+
* Regression tests for WCI-2239: the ECDSA MPCv1 signing path verified
17+
* serializedTxHex and signed signableHex without binding the two fields for
18+
* TSS_VERIFY_USE_SERIALIZED_TX_HEX coins (BSC/XDC). signRequestBase must call
19+
* baseCoin.assertSignableConsistency(serializedTxHex, signableHex) after
20+
* verifyTransaction, matching the MPCv2 path.
21+
*/
22+
23+
const serializedTxHex = 'f86c808504a817c80082520894' + '00'.repeat(20) + '80808080';
24+
const signableHex = 'f86c808504a817c80082520894' + '11'.repeat(20) + '80808080';
25+
26+
const unsignedTx = {
27+
derivationPath: 'm/0',
28+
serializedTxHex,
29+
signableHex,
30+
};
31+
32+
const txRequest = {
33+
txRequestId: 'wci-2239-test',
34+
apiVersion: 'full',
35+
walletId: '62fe536a6b4cf70007acb48c0e7bb0b0',
36+
transactions: [
37+
{
38+
unsignedTx,
39+
},
40+
],
41+
} as unknown as TxRequest;
42+
43+
const params: TssSignTxRequestParamsWithPrv = {
44+
txRequest,
45+
txParams: { recipients: [{ address: '0x' + '00'.repeat(20), amount: '1000' }] },
46+
prv: JSON.stringify({ pShare: { i: 1 }, backupNShare: 'fake' }),
47+
reqId: { inc: sinon.stub(), toString: () => 'req' } as unknown as IRequestTracer,
48+
};
49+
50+
function makeUtils(mockCoin: unknown): EcdsaUtils {
51+
const mockBg = {
52+
getEnv: sinon.stub().returns('test'),
53+
setRequestTracer: sinon.stub(),
54+
} as unknown as BitGoBase;
55+
const mockWallet = {
56+
id: sinon.stub().returns('62fe536a6b4cf70007acb48c0e7bb0b0'),
57+
multisigType: sinon.stub().returns('tss'),
58+
coinSpecific: sinon.stub().returns({}),
59+
} as unknown as IWallet;
60+
return new EcdsaUtils(mockBg, mockCoin as IBaseCoin, mockWallet);
61+
}
62+
63+
describe('EcdsaUtils.signRequestBase - signableHex consistency (WCI-2239)', function () {
64+
afterEach(function () {
65+
sinon.restore();
66+
});
67+
68+
it('calls assertSignableConsistency with (serializedTxHex, signableHex) for TSS_VERIFY_USE_SERIALIZED_TX_HEX coins', async function () {
69+
const consistencySpy = sinon.spy();
70+
const mockCoin = {
71+
getConfig: sinon.stub().returns({ features: [CoinFeature.TSS_VERIFY_USE_SERIALIZED_TX_HEX] }),
72+
verifyTransaction: sinon.stub().resolves(),
73+
assertSignableConsistency: consistencySpy,
74+
getChain: sinon.stub().returns('tbsc'),
75+
};
76+
const utils = makeUtils(mockCoin);
77+
sinon.stub(utils, 'getOfflineSignerPaillierModulus').throws(new Error('SENTINEL_PAST_CONSISTENCY'));
78+
79+
await assert.rejects(utils.signTxRequest(params), /SENTINEL_PAST_CONSISTENCY/);
80+
81+
assert.strictEqual(consistencySpy.callCount, 1);
82+
assert.deepStrictEqual(consistencySpy.firstCall.args, [unsignedTx.serializedTxHex, unsignedTx.signableHex]);
83+
});
84+
85+
it('aborts before any share production when assertSignableConsistency throws', async function () {
86+
const mockCoin = {
87+
getConfig: sinon.stub().returns({ features: [CoinFeature.TSS_VERIFY_USE_SERIALIZED_TX_HEX] }),
88+
verifyTransaction: sinon.stub().resolves(),
89+
assertSignableConsistency: sinon
90+
.stub()
91+
.throws(
92+
new InvalidTransactionError('signableHex is inconsistent with serializedTxHex: possible server tampering')
93+
),
94+
getChain: sinon.stub().returns('tbsc'),
95+
};
96+
const utils = makeUtils(mockCoin);
97+
const paillierSpy = sinon.spy(utils, 'getOfflineSignerPaillierModulus');
98+
99+
await assert.rejects(utils.signTxRequest(params), /possible server tampering/);
100+
101+
assert.strictEqual(paillierSpy.callCount, 0);
102+
});
103+
104+
it('skips the check for coins without the TSS_VERIFY_USE_SERIALIZED_TX_HEX feature', async function () {
105+
const consistencySpy = sinon.spy();
106+
const verifyTransactionStub = sinon.stub().resolves();
107+
const mockCoin = {
108+
getConfig: sinon.stub().returns({ family: 'eth' }),
109+
verifyTransaction: verifyTransactionStub,
110+
assertSignableConsistency: consistencySpy,
111+
getChain: sinon.stub().returns('teth'),
112+
};
113+
const utils = makeUtils(mockCoin);
114+
sinon.stub(utils, 'getOfflineSignerPaillierModulus').throws(new Error('SENTINEL_PAST_CONSISTENCY'));
115+
116+
await assert.rejects(utils.signTxRequest(params), /SENTINEL_PAST_CONSISTENCY/);
117+
118+
assert.strictEqual(consistencySpy.callCount, 0);
119+
assert.strictEqual(verifyTransactionStub.firstCall.args[0].txPrebuild.txHex, signableHex);
120+
});
121+
122+
it('does not crash for flag coins without assertSignableConsistency (ICP shape)', async function () {
123+
const mockCoin = {
124+
getConfig: sinon.stub().returns({ features: [CoinFeature.TSS_VERIFY_USE_SERIALIZED_TX_HEX] }),
125+
verifyTransaction: sinon.stub().resolves(),
126+
getChain: sinon.stub().returns('ticp'),
127+
};
128+
const utils = makeUtils(mockCoin);
129+
sinon.stub(utils, 'getOfflineSignerPaillierModulus').throws(new Error('SENTINEL_PAST_CONSISTENCY'));
130+
131+
await assert.rejects(utils.signTxRequest(params), /SENTINEL_PAST_CONSISTENCY/);
132+
});
133+
});

0 commit comments

Comments
 (0)