|
| 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