Skip to content

Commit 6e95c1a

Browse files
authored
Merge pull request #10160 from BitGo/prajwalu/cshld-2227-bitgojs-xtzsigntransaction-signs
fix: bind Xtz dataToSign to verified txHex before signing
2 parents 1f5203c + 321e911 commit 6e95c1a

6 files changed

Lines changed: 225 additions & 12 deletions

File tree

‎modules/sdk-coin-xtz/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@bitgo/statics": "^59.30.0",
4747
"@noble/curves": "1.8.1",
4848
"@taquito/local-forging": "^23.0.2",
49+
"@taquito/michel-codec": "^23.0.3",
4950
"@taquito/rpc": "^23.0.2",
5051
"@taquito/signer": "^23.0.2",
5152
"bignumber.js": "^9.0.0",

‎modules/sdk-coin-xtz/src/lib/utils.ts‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { isValidXpub, SigningError } from '@bitgo/sdk-core';
22
import { InMemorySigner } from '@taquito/signer';
3+
import { packData } from '@taquito/michel-codec';
34
import * as base58check from 'bs58check';
45
import { secp256k1 } from '@noble/curves/secp256k1';
56
import sodium from 'libsodium-wrappers';
6-
import { HashType, SignResponse } from './iface';
7+
import { HashType, SignResponse, TransactionOp } from './iface';
78
import { KeyPair } from './keyPair';
8-
import { genericMultisigDataToSign } from './multisigUtils';
9+
import { genericMultisigDataToSign, getMultisigTransferDataFromOperation } from './multisigUtils';
910

1011
// By default, use the transactions prefix
1112
export const DEFAULT_WATERMARK = new Uint8Array([3]);
@@ -141,6 +142,56 @@ export function generateDataToSign(
141142
return genericMultisigDataToSign(contractAddress, destinationAddress, amount, contractCounter);
142143
}
143144

145+
/**
146+
* Pack the multisig transfer payload locally, producing the same bytes the Tezos node returns from
147+
* /helpers/scripts/pack_data. This avoids trusting a node or the platform for the bytes that the
148+
* wallet contract verifies signatures against.
149+
*
150+
* @param {string} contractAddress The wallet contract address with the funds to withdraw
151+
* @param {string} destinationAddress The address to transfer the funds to
152+
* @param {string} amount Number of mutez to transfer
153+
* @param {string} contractCounter Wallet counter to use in the transaction
154+
* @returns {string} The packed data to sign in hexadecimal format
155+
*/
156+
export function packGenericMultisigDataToSign(
157+
contractAddress: string,
158+
destinationAddress: string,
159+
amount: string,
160+
contractCounter: string
161+
): string {
162+
const { data, type } = generateDataToSign(contractAddress, destinationAddress, amount, contractCounter);
163+
return Buffer.from(packData(data, type)).toString('hex');
164+
}
165+
166+
/**
167+
* Fail unless the data to sign is exactly the payload authorizing the multisig transfer encoded in
168+
* the given transaction operation. The signature over dataToSign is a standalone spend
169+
* authorization for the wallet contract, so it must never be produced for bytes that are not
170+
* derived from the transfer the user verified.
171+
*
172+
* @param {TransactionOp} transferOperation The multisig transfer operation decoded from the txHex
173+
* @param {string} dataToSign The packed data to sign supplied alongside the txHex, in hexadecimal
174+
* @throws {Error} if the operation is not a multisig transfer or the data to sign does not match it
175+
*/
176+
export function assertDataToSignMatchesTransfer(transferOperation: TransactionOp, dataToSign: string): void {
177+
if (!transferOperation.parameters) {
178+
throw new Error('Cannot validate dataToSign: the transaction is not a multisig transfer');
179+
}
180+
const transfer = getMultisigTransferDataFromOperation(transferOperation);
181+
if (transfer.counter === undefined) {
182+
throw new Error('Cannot validate dataToSign: the multisig transfer has no counter');
183+
}
184+
const expectedDataToSign = packGenericMultisigDataToSign(
185+
transfer.from,
186+
transfer.to,
187+
transfer.amount,
188+
transfer.counter
189+
);
190+
if (dataToSign.toLowerCase() !== expectedDataToSign) {
191+
throw new Error('dataToSign does not match the transfer encoded in the transaction');
192+
}
193+
}
194+
144195
/**
145196
* Returns whether or not the string is a valid Tezos hash of the given type
146197
*

‎modules/sdk-coin-xtz/src/xtz.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { Interface, KeyPair, TransactionBuilder, Utils } from './lib';
3030
import { extractContractStorage, type ContractStorage } from './lib/multisigUtils';
3131
import { RecoverOptions } from './lib/iface';
3232
import {
33+
assertDataToSignMatchesTransfer,
3334
generateDataToSign,
3435
isValidOriginatedAddress,
3536
TRANSACTION_FEE,
@@ -157,6 +158,15 @@ export class Xtz extends BaseCoin {
157158
throw new Error(`Tezos prebuild contains ${txOutputs.length} output(s) but expected exactly 1`);
158159
}
159160

161+
const prebuildDataToSign = (txPrebuild as Interface.TransactionPrebuild).dataToSign;
162+
if (prebuildDataToSign) {
163+
try {
164+
await this.assertDataToSignBoundToTxHex(rawTx, prebuildDataToSign);
165+
} catch (e) {
166+
throw new Error(`Tezos prebuild dataToSign verification failed: ${e.message}`);
167+
}
168+
}
169+
160170
const { address: prebuildAddress, value: prebuildAmount } = txOutputs[0];
161171

162172
// Validate recipients if provided (normal send). Consolidation builds omit recipients.
@@ -200,6 +210,22 @@ export class Xtz extends BaseCoin {
200210
return derivedKeyNode.toBase58();
201211
}
202212

213+
/**
214+
* Ensure a supplied dataToSign is derived from the transfer in txHex. The platform prebuild carries
215+
* both artifacts independently, but only txHex is shown to the user for verification.
216+
*
217+
* @param {string} txHex The prebuilt transaction
218+
* @param {string} dataToSign The packed multisig payload that would be signed
219+
*/
220+
private async assertDataToSignBoundToTxHex(txHex: string, dataToSign: string): Promise<void> {
221+
const txBuilder = new TransactionBuilder(coins.get(this.getChain()));
222+
txBuilder.from(txHex);
223+
const transaction = await txBuilder.build();
224+
// The builder signs overridden dataToSign for the first operation, so that is the one it must match
225+
const transferOperation = transaction.toJson().contents[0] as Interface.TransactionOp;
226+
assertDataToSignMatchesTransfer(transferOperation, dataToSign);
227+
}
228+
203229
/**
204230
* Assemble keychain and half-sign prebuilt transaction
205231
*
@@ -209,6 +235,9 @@ export class Xtz extends BaseCoin {
209235
* @returns Bluebird<SignedTransaction>
210236
*/
211237
async signTransaction(params: Interface.XtzSignTransactionOptions): Promise<SignedTransaction> {
238+
if (params.txPrebuild.dataToSign) {
239+
await this.assertDataToSignBoundToTxHex(params.txPrebuild.txHex, params.txPrebuild.dataToSign);
240+
}
212241
const txBuilder = new TransactionBuilder(coins.get(this.getChain()));
213242
txBuilder.from(params.txPrebuild.txHex);
214243
txBuilder.source(params.txPrebuild.source);

‎modules/sdk-coin-xtz/test/fixtures.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export const dataToSign =
2-
'0507070a0000001601fe3263b84b615ee65c320d72a3d48fcb756fed580007070006050502000000510320053d036d0743036e0a0000001601619b12ee7d8808cf2434c1d8e432b5cf820a3415000555036c0200000015072f02000000090200000004034f032702000000000743036a0080897a034f034d031b';
2+
'0507070a000000160100ac4a8356a8b7e3061c39e7797ebf5b5cbee0590007070000050502000000350320053d036d0743035d0a0000001501aaca87bdbcdc4e6117b667e29f9b504362c831bb031e0743036a0080b7a00b034f034d031b';
33
export const unsignedHex =
44
'ba7a04fab1a3f77eda96b551947dd343e165d1b91b6f9f806648b63e57c88cc86c01622df81cc7ae34dd9d0346ba06198ee4de0dcae298f40202f093098102000100ac4a8356a8b7e3061c39e7797ebf5b5cbee05900ffff046d61696e0000005c070707070000050502000000440320053d036d0743035d0100000024747a3250744a397a67456746565452717936475873737435347448336b73456e59767653031e0743036a0080b7a00b034f034d031b0200000006030603060306';
55
export const oneSignatureHex =
6-
'ba7a04fab1a3f77eda96b551947dd343e165d1b91b6f9f806648b63e57c88cc86c01622df81cc7ae34dd9d0346ba06198ee4de0dcae298f40202f093098102000100ac4a8356a8b7e3061c39e7797ebf5b5cbee05900ffff046d61696e000000c1070707070000050502000000440320053d036d0743035d0100000024747a3250744a397a67456746565452717936475873737435347448336b73456e59767653031e0743036a0080b7a00b034f034d031b020000006b050901000000607369677368313645716a6835334546396e62735937434d4b5458354675325439623736616a435563415a376b4d50394c42394854575173547a506e4e64635858534c6a393870614d54374337376a7939504e65675855484a796d77313167797703060306';
6+
'ba7a04fab1a3f77eda96b551947dd343e165d1b91b6f9f806648b63e57c88cc86c01622df81cc7ae34dd9d0346ba06198ee4de0dcae298f40202f093098102000100ac4a8356a8b7e3061c39e7797ebf5b5cbee05900ffff046d61696e000000c1070707070000050502000000440320053d036d0743035d0100000024747a3250744a397a67456746565452717936475873737435347448336b73456e59767653031e0743036a0080b7a00b034f034d031b020000006b05090100000060736967735970596f746375727768505863766975676e38626b566a677159326e533661664754593672576e663968314e4b55684a463165736a3547474765775353314b3257657a776e4a373731343835586570314132706a763554424b454b7003060306';
77
export const twoSignatureHex =
8-
'ba7a04fab1a3f77eda96b551947dd343e165d1b91b6f9f806648b63e57c88cc86c01622df81cc7ae34dd9d0346ba06198ee4de0dcae298f40202f093098102000100ac4a8356a8b7e3061c39e7797ebf5b5cbee05900ffff046d61696e00000126070707070000050502000000440320053d036d0743035d0100000024747a3250744a397a67456746565452717936475873737435347448336b73456e59767653031e0743036a0080b7a00b034f034d031b02000000d0050901000000607369677368313645716a6835334546396e62735937434d4b5458354675325439623736616a435563415a376b4d50394c42394854575173547a506e4e64635858534c6a393870614d54374337376a7939504e65675855484a796d77313167797705090100000060736967504d463231434e5553534b755659356658546e437665694d456337767974654a36686257786834446a336a41644461473139757a4d725542797053595863585a5475373753566233376951546b4c6d4d35526b54385158394136344c560306';
8+
'ba7a04fab1a3f77eda96b551947dd343e165d1b91b6f9f806648b63e57c88cc86c01622df81cc7ae34dd9d0346ba06198ee4de0dcae298f40202f093098102000100ac4a8356a8b7e3061c39e7797ebf5b5cbee05900ffff046d61696e00000126070707070000050502000000440320053d036d0743035d0100000024747a3250744a397a67456746565452717936475873737435347448336b73456e59767653031e0743036a0080b7a00b034f034d031b02000000d005090100000060736967735970596f746375727768505863766975676e38626b566a677159326e533661664754593672576e663968314e4b55684a463165736a3547474765775353314b3257657a776e4a373731343835586570314132706a763554424b454b7005090100000060736967735970596f746375727768505863766975676e38626b566a677159326e533661664754593672576e663968314e4b55684a463165736a3547474765775353314b3257657a776e4a373731343835586570314132706a763554424b454b700306';
99
export const fullySignedHex =
10-
'ba7a04fab1a3f77eda96b551947dd343e165d1b91b6f9f806648b63e57c88cc86c01622df81cc7ae34dd9d0346ba06198ee4de0dcae298f40202f093098102000100ac4a8356a8b7e3061c39e7797ebf5b5cbee05900ffff046d61696e00000126070707070000050502000000440320053d036d0743035d0100000024747a3250744a397a67456746565452717936475873737435347448336b73456e59767653031e0743036a0080b7a00b034f034d031b02000000d0050901000000607369677368313645716a6835334546396e62735937434d4b5458354675325439623736616a435563415a376b4d50394c42394854575173547a506e4e64635858534c6a393870614d54374337376a7939504e65675855484a796d77313167797705090100000060736967504d463231434e5553534b755659356658546e437665694d456337767974654a36686257786834446a336a41644461473139757a4d725542797053595863585a5475373753566233376951546b4c6d4d35526b54385158394136344c5603068dbc64a90f936e0796d4ec5f7010238a13a2ff45a69a689664de0e04833c3d4e5d7e1c0533943ea21a2b2f3c1df90a5438d05139dd531d95a6d3fbad852b8032';
10+
'ba7a04fab1a3f77eda96b551947dd343e165d1b91b6f9f806648b63e57c88cc86c01622df81cc7ae34dd9d0346ba06198ee4de0dcae298f40202f093098102000100ac4a8356a8b7e3061c39e7797ebf5b5cbee05900ffff046d61696e00000126070707070000050502000000440320053d036d0743035d0100000024747a3250744a397a67456746565452717936475873737435347448336b73456e59767653031e0743036a0080b7a00b034f034d031b02000000d005090100000060736967735970596f746375727768505863766975676e38626b566a677159326e533661664754593672576e663968314e4b55684a463165736a3547474765775353314b3257657a776e4a373731343835586570314132706a763554424b454b7005090100000060736967735970596f746375727768505863766975676e38626b566a677159326e533661664754593672576e663968314e4b55684a463165736a3547474765775353314b3257657a776e4a373731343835586570314132706a763554424b454b7003063f48c23ee031ff264a9b987998dc68ae75287eb57af4603dd06ad45e2feec8d4375f1b6c58ee19e25bab804df78b3887cc29339fdd548206917ae2e33518785a';
1111

1212
export const unsignedTransactionWithTwoTransfersHex =
1313
'ba7a04fab1a3f77eda96b551947dd343e165d1b91b6f9f806648b63e57c88cc86c01aaca87bdbcdc4e6117b667e29f9b504362c831bb9c2500e8528102000196369c90625575ba44594b23794832a9337f7a2d00ffff046d61696e000000760707070700010505020000005e0320053d036d0743036e01000000244b543148557274366b66765979444559434a3247536a7654505a364b6d5266784c4255380555036c0200000015072f02000000090200000004034f032702000000000743036a00a401034f034d031b02000000060306030603066c01aaca87bdbcdc4e6117b667e29f9b504362c831bb9c2501e8528102000196369c90625575ba44594b23794832a9337f7a2d00ffff046d61696e0000005a070707070002050502000000420320053d036d0743035d0100000024747a3156526a5270564b6e76313641567072464831746b446e3454446656714138393341031e0743036a00a401034f034d031b0200000006030603060306';

‎modules/sdk-coin-xtz/test/unit/util.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
} from '../resources';
1111
import { XtzLib } from '../../src';
1212
import { HashType } from '../../src/lib/iface';
13+
import { localForger } from '@taquito/local-forging';
14+
import { dataToSign, unsignedHex } from '../fixtures';
1315

1416
describe('XTZ util library', function () {
1517
describe('address', function () {
@@ -238,6 +240,77 @@ describe('XTZ util library', function () {
238240
});
239241
});
240242

243+
describe('packGenericMultisigDataToSign', function () {
244+
it('should pack locally to the same bytes the node packs', function () {
245+
// The fixture dataToSign is the /pack_data RPC output for these exact parameters, so a local
246+
// match proves packGenericMultisigDataToSign is byte-identical to the node.
247+
const packed = XtzLib.Utils.packGenericMultisigDataToSign(
248+
'KT18eKxkx71uMp28oZHjCiEj7QvFUaGzXtrZ',
249+
'tz2PtJ9zgEgFVTRqy6GXsst54tH3ksEnYvvS',
250+
'11800000',
251+
'0'
252+
);
253+
packed.should.equal(dataToSign);
254+
});
255+
});
256+
257+
describe('assertDataToSignMatchesTransfer', function () {
258+
// The operation under test is decoded from the unsignedHex fixture with the same local forger
259+
// the transaction uses, so the multisig parameter shape is the production one.
260+
const getTransferOperation = async () => {
261+
const parsed = await localForger.parse(unsignedHex);
262+
const operation = parsed.contents.find((content) => content.kind === 'transaction' && 'parameters' in content);
263+
if (!operation) {
264+
throw new Error('fixture has no multisig transaction operation');
265+
}
266+
return operation as Parameters<typeof XtzLib.Utils.assertDataToSignMatchesTransfer>[0];
267+
};
268+
269+
it('should accept the locally packed payload for the transfer', async function () {
270+
XtzLib.Utils.assertDataToSignMatchesTransfer(await getTransferOperation(), dataToSign);
271+
});
272+
273+
it('should reject a payload that authorizes a different transfer', async function () {
274+
// Flipping bytes yields a payload that is not derived from this transfer; accepting it would
275+
// let a tampered prebuild be signed with the user key.
276+
const tampered = dataToSign.slice(0, -4) + 'beef';
277+
const operation = await getTransferOperation();
278+
assert.throws(
279+
() => XtzLib.Utils.assertDataToSignMatchesTransfer(operation, tampered),
280+
new RegExp('dataToSign does not match the transfer encoded in the transaction')
281+
);
282+
});
283+
284+
it('should reject a non-multisig operation', async function () {
285+
const parsed = await localForger.parse(unsignedHex);
286+
const singlesigOperation = parsed.contents.find(
287+
(content) => content.kind === 'transaction' && !('parameters' in content)
288+
);
289+
if (singlesigOperation === undefined) {
290+
// unsignedHex has no singlesig op; pass an operation without parameters directly to
291+
// exercise the same guard.
292+
const emptyOperation = { kind: 'transaction', source: 'tz2HGMx8YxPSLguVP6usgf1D8UALPLchLoSX' };
293+
assert.throws(
294+
() =>
295+
XtzLib.Utils.assertDataToSignMatchesTransfer(
296+
emptyOperation as unknown as Parameters<typeof XtzLib.Utils.assertDataToSignMatchesTransfer>[0],
297+
dataToSign
298+
),
299+
new RegExp('not a multisig transfer')
300+
);
301+
return;
302+
}
303+
assert.throws(
304+
() =>
305+
XtzLib.Utils.assertDataToSignMatchesTransfer(
306+
singlesigOperation as unknown as Parameters<typeof XtzLib.Utils.assertDataToSignMatchesTransfer>[0],
307+
dataToSign
308+
),
309+
new RegExp('not a multisig transfer')
310+
);
311+
});
312+
});
313+
241314
describe('signature', function () {
242315
it('should validate signature', function () {
243316
const validSignatures = [

0 commit comments

Comments
 (0)