Skip to content

Commit fef9aef

Browse files
authored
Merge pull request #6613 from BitGo/SC-2625
fix(sdk-coin-avaxc): fix missing data for signTx
2 parents 978f3cf + 78c5db8 commit fef9aef

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

modules/sdk-coin-avaxc/src/avaxc.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,11 @@ export class AvaxC extends AbstractEthLikeNewCoins {
10321032
}
10331033
const transaction = await txBuilder.build();
10341034

1035-
const recipients = transaction.outputs.map((output) => ({ address: output.address, amount: output.value }));
1035+
// we need to preserve the calldata of the recipients specified in the request for custodial transactions
1036+
let recipients = params.txPrebuild.recipients || (params.recipients as Recipient[] | undefined);
1037+
if (recipients === undefined) {
1038+
recipients = transaction.outputs.map((output) => ({ address: output.address, amount: output.value }));
1039+
}
10361040

10371041
const txParams = {
10381042
eip1559: params.txPrebuild.eip1559,

modules/sdk-coin-avaxc/src/iface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export interface TxPreBuild extends BaseTransactionPrebuild {
150150
expireTime?: number;
151151
hopTransaction?: string;
152152
eip1559?: EIP1559;
153+
recipients?: Recipient[];
153154
txPrebuild?: {
154155
halfSigned: {
155156
txHex: string;

modules/sdk-coin-avaxc/test/unit/avaxc.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ describe('Avalanche C-Chain', function () {
257257
owner_3: '4421ab25dd91e1a3180d03d57c323a7886dcc313d3b3a4b4256a5791572bf597',
258258
};
259259

260-
it('should sign an unsigned test tx', async function () {
260+
it('should use recipients from txPrebuild when available including data field', async function () {
261261
const builder = getBuilder('tavaxc') as TransactionBuilder;
262262
builder.fee({
263263
fee: '280000000000',
@@ -271,21 +271,28 @@ describe('Avalanche C-Chain', function () {
271271
const unsignedTx = await builder.build();
272272
const unsignedTxForBroadcasting = unsignedTx.toBroadcastFormat();
273273

274+
// Test with recipients in txPrebuild including data field
275+
const customRecipients = [
276+
{ address: '0x1234567890123456789012345678901234567890', amount: '500', data: '0xa9059cbb' },
277+
{ address: '0x0987654321098765432109876543210987654321', amount: '300', data: '0x23b872dd' },
278+
];
279+
274280
const halfSignedRawTx = await tavaxCoin.signTransaction({
275281
txPrebuild: {
276282
txHex: unsignedTxForBroadcasting,
283+
recipients: customRecipients,
277284
},
278285
prv: account_1.owner_2,
279286
});
280287

281-
builder.transfer().key(account_1.owner_2);
282-
const halfSignedTx = await builder.build();
283-
const halfSignedTxForBroadcasting = halfSignedTx.toBroadcastFormat();
284-
285-
halfSignedRawTx.halfSigned.txHex.should.equals(halfSignedTxForBroadcasting);
286-
halfSignedRawTx.halfSigned.recipients.length.should.equals(1);
287-
halfSignedRawTx.halfSigned.recipients[0].address.toLowerCase().should.equals(account_2.address.toLowerCase());
288-
halfSignedRawTx.halfSigned.recipients[0].amount.toLowerCase().should.equals('1');
288+
// Should use recipients from txPrebuild, not from transaction outputs
289+
halfSignedRawTx.halfSigned.recipients.length.should.equals(2);
290+
halfSignedRawTx.halfSigned.recipients[0].address.should.equals(customRecipients[0].address);
291+
halfSignedRawTx.halfSigned.recipients[0].amount.should.equals(customRecipients[0].amount);
292+
halfSignedRawTx.halfSigned.recipients[0].data.should.equals(customRecipients[0].data);
293+
halfSignedRawTx.halfSigned.recipients[1].address.should.equals(customRecipients[1].address);
294+
halfSignedRawTx.halfSigned.recipients[1].amount.should.equals(customRecipients[1].amount);
295+
halfSignedRawTx.halfSigned.recipients[1].data.should.equals(customRecipients[1].data);
289296
});
290297

291298
it('should sign an unsigned test tx with eip1559', async function () {
@@ -329,7 +336,7 @@ describe('Avalanche C-Chain', function () {
329336
halfSignedRawTx.halfSigned.eip1559.maxPriorityFeePerGas.should.equal('150');
330337
});
331338

332-
it('should sign an unsigned mainnet tx', async function () {
339+
it('should preserve data field from txPrebuild recipients for contract interactions', async function () {
333340
const builder = getBuilder('avaxc') as TransactionBuilder;
334341
builder.fee({
335342
fee: '280000000000',
@@ -343,21 +350,30 @@ describe('Avalanche C-Chain', function () {
343350
const unsignedTx = await builder.build();
344351
const unsignedTxForBroadcasting = unsignedTx.toBroadcastFormat();
345352

353+
// Test with complex contract interaction data
354+
const contractCallData =
355+
'0xa9059cbb000000000000000000000000abcdef1234567890abcdef1234567890abcdef120000000000000000000000000000000000000000000000000de0b6b3a7640000';
356+
const customRecipients = [
357+
{
358+
address: '0x1234567890123456789012345678901234567890',
359+
amount: '0', // Contract call with 0 value
360+
data: contractCallData,
361+
},
362+
];
363+
346364
const halfSignedRawTx = await avaxCoin.signTransaction({
347365
txPrebuild: {
348366
txHex: unsignedTxForBroadcasting,
367+
recipients: customRecipients,
349368
},
350369
prv: account_1.owner_2,
351370
});
352371

353-
builder.transfer().key(account_1.owner_2);
354-
const halfSignedTx = await builder.build();
355-
const halfSignedTxForBroadcasting = halfSignedTx.toBroadcastFormat();
356-
357-
halfSignedRawTx.halfSigned.txHex.should.equals(halfSignedTxForBroadcasting);
372+
// Should preserve the contract call data in half-signed transaction
358373
halfSignedRawTx.halfSigned.recipients.length.should.equals(1);
359-
halfSignedRawTx.halfSigned.recipients[0].address.toLowerCase().should.equals(account_2.address.toLowerCase());
360-
halfSignedRawTx.halfSigned.recipients[0].amount.toLowerCase().should.equals('1');
374+
halfSignedRawTx.halfSigned.recipients[0].address.should.equals(customRecipients[0].address);
375+
halfSignedRawTx.halfSigned.recipients[0].amount.should.equals(customRecipients[0].amount);
376+
halfSignedRawTx.halfSigned.recipients[0].data.should.equals(customRecipients[0].data);
361377
});
362378
});
363379

0 commit comments

Comments
 (0)