Skip to content

Commit e5386ec

Browse files
authored
Merge pull request #7468 from BitGo/COIN-6436
fix: bug fix in canton verify txn flow
2 parents 5657c5d + d90534b commit e5386ec

File tree

5 files changed

+60
-7
lines changed

5 files changed

+60
-7
lines changed

modules/sdk-coin-canton/src/lib/transaction/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export class Transaction extends BaseTransaction {
244244
}
245245
case TransactionType.Send: {
246246
const txData = this.toJson();
247-
outputs.push({ address: txData.sender, amount: txData.amount });
247+
outputs.push({ address: txData.receiver, amount: txData.amount });
248248
outputAmount = txData.amount;
249249
break;
250250
}

modules/sdk-coin-canton/src/lib/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import BigNumber from 'bignumber.js';
12
import crypto from 'crypto';
23

34
import { BaseUtils, isValidEd25519PublicKey } from '@bitgo/sdk-core';
@@ -145,10 +146,11 @@ export class Utils implements BaseUtils {
145146
if (!amount) missingFields.push('amount');
146147
throw new Error(`invalid transaction data: missing ${missingFields.join(', ')}`);
147148
}
149+
const convertedAmount = this.convertAmountToLowestUnit(new BigNumber(amount));
148150
return {
149151
sender,
150152
receiver,
151-
amount,
153+
amount: convertedAmount,
152154
};
153155
}
154156

@@ -296,6 +298,16 @@ export class Utils implements BaseUtils {
296298
buf.writeInt32BE(value, 0);
297299
return buf;
298300
}
301+
302+
/**
303+
* Convert to canton raw units
304+
* @param {BigNumber} value
305+
* @returns {String} the converted raw canton units
306+
* @private
307+
*/
308+
private convertAmountToLowestUnit(value: BigNumber): string {
309+
return value.multipliedBy(new BigNumber(10).pow(10)).toFixed(0);
310+
}
299311
}
300312

301313
const utils = new Utils();

modules/sdk-coin-canton/test/integration/canton.integration.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,59 @@
11
import assert from 'assert';
22

33
import { BitGoAPI } from '@bitgo/sdk-api';
4-
import { TransactionType } from '@bitgo/sdk-core';
4+
import { ITransactionRecipient, TransactionType, Wallet } from '@bitgo/sdk-core';
55
import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test';
66

77
import { getCantonBuilderFactory } from '../helper';
88
import {
99
CANTON_RECEIVE_ADDRESS,
1010
GenerateTopologyResponse,
1111
TransferAcceptRawTransaction,
12+
TransferRawTxn,
1213
TransferRejectRawTransaction,
14+
TxParams,
1315
WalletInitRawTransaction,
1416
} from '../resources';
1517
import { Tcanton } from '../../src';
1618

1719
describe('Canton integration tests', function () {
1820
let bitgo: TestBitGoAPI;
1921
let basecoin: Tcanton;
22+
let newTxPrebuild: () => { txHex: string; txInfo: Record<string, unknown> };
23+
let newTxParams: () => { recipients: ITransactionRecipient[] };
24+
let wallet: Wallet;
25+
const txPrebuild = {
26+
txHex: TransferRawTxn,
27+
txInfo: {},
28+
};
29+
const txParams = {
30+
recipients: [
31+
{
32+
address: TxParams.RECIPIENT_ADDRESS,
33+
amount: TxParams.AMOUNT,
34+
},
35+
],
36+
};
2037
before(() => {
2138
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'mock' });
2239
bitgo.safeRegister('tcanton', Tcanton.createInstance);
2340
basecoin = bitgo.coin('tcanton') as Tcanton;
41+
newTxPrebuild = () => {
42+
return structuredClone(txPrebuild);
43+
};
44+
newTxParams = () => {
45+
return structuredClone(txParams);
46+
};
47+
wallet = new Wallet(bitgo, basecoin, {});
48+
});
49+
50+
describe('Verify Transaction', function () {
51+
it('should verify transfer transaction', async function () {
52+
const txPrebuild = newTxPrebuild();
53+
const txParams = newTxParams();
54+
const isTxnVerifies = await basecoin.verifyTransaction({ txPrebuild: txPrebuild, txParams: txParams, wallet });
55+
isTxnVerifies.should.equal(true);
56+
});
2457
});
2558

2659
describe('Explain raw transaction', function () {
@@ -41,7 +74,7 @@ describe('Canton integration tests', function () {
4174
assert(explainTxData);
4275
assert(explainTxData.id);
4376
assert.equal(explainTxData.type, TransactionType.TransferAccept);
44-
assert.equal(explainTxData.inputAmount, '5.0000000000');
77+
assert.equal(explainTxData.inputAmount, '50000000000');
4578
});
4679

4780
it('should explain raw transfer rejection transaction', function () {
@@ -50,7 +83,7 @@ describe('Canton integration tests', function () {
5083
const explainTxData = txn.explainTransaction();
5184
assert(explainTxData);
5285
assert.equal(explainTxData.type, TransactionType.TransferReject);
53-
assert.equal(explainTxData.inputAmount, '5.0000000000');
86+
assert.equal(explainTxData.inputAmount, '50000000000');
5487
});
5588
});
5689

modules/sdk-coin-canton/test/resources.ts

Lines changed: 8 additions & 0 deletions
Large diffs are not rendered by default.

modules/sdk-coin-canton/test/unit/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Canton Util', function () {
1818
should.exist(parsedData);
1919
assert.equal(parsedData.sender, 'abc-1::12200c1ee226fbdf9fba3461c2c0c73331b69d3c6fd8cfce28cdf864141141cc656d');
2020
assert.equal(parsedData.receiver, 'abc-2::12207e96ada18a845adf4dc01410265633d5266dca9bb280c98e35c3692db87d3e35');
21-
assert.equal(parsedData.amount, '20.0000000000');
21+
assert.equal(parsedData.amount, '200000000000');
2222
});
2323

2424
it('should parse the acceptance prepared transaction', () => {
@@ -29,7 +29,7 @@ describe('Canton Util', function () {
2929
parsedData.receiver,
3030
'ravi-demo-party-txn-01-tapper::1220ea7ab5a723f8a6b2078e617e6c58cb7e78e49947ddc239e1a941aa56e6ba08b4'
3131
);
32-
assert.equal(parsedData.amount, '5.0000000000');
32+
assert.equal(parsedData.amount, '50000000000');
3333
});
3434

3535
it('should parse the one-step preapproval prepared transaction', () => {

0 commit comments

Comments
 (0)