Skip to content

Commit bd5149a

Browse files
committed
feat(sdk-coin-dot): add verify transaction function
WIN-7596 TICKET: WIN-7596
1 parent d084733 commit bd5149a

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,45 @@ export class Dot extends BaseCoin {
651651
}
652652

653653
async verifyTransaction(params: VerifyTransactionOptions): Promise<boolean> {
654-
const { txParams } = params;
654+
const { txPrebuild, txParams } = params;
655+
if (!txParams) {
656+
throw new Error('missing txParams');
657+
}
658+
659+
if (!txPrebuild) {
660+
throw new Error('missing txPrebuild');
661+
}
662+
663+
if (!txPrebuild.txHex) {
664+
throw new Error('missing txHex in txPrebuild');
665+
}
666+
667+
if (!txParams.recipients || txParams.recipients.length === 0) {
668+
throw new Error('missing recipients in txParams');
669+
}
670+
671+
const factory = this.getBuilder();
672+
const txBuilder = factory.from(txPrebuild.txHex) as any;
673+
655674
if (Array.isArray(txParams.recipients) && txParams.recipients.length > 1) {
656675
throw new Error(
657676
`${this.getChain()} doesn't support sending to more than 1 destination address within a single transaction. Try again, using only a single recipient.`
658677
);
659678
}
679+
680+
// validate recipient is same as txBuilder._to
681+
if (txParams.recipients[0].address !== txBuilder._to) {
682+
throw new Error(
683+
`Recipient address ${txParams.recipients[0].address} does not match transaction destination address ${txBuilder._to}`
684+
);
685+
}
686+
687+
// and amount is same as txBuilder._amount
688+
if (txParams.recipients[0].amount !== txBuilder._amount) {
689+
throw new Error(
690+
`Recipient amount ${txParams.recipients[0].amount} does not match transaction amount ${txBuilder._amount}`
691+
);
692+
}
660693
return true;
661694
}
662695

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as testData from '../fixtures';
88
import { chainName, txVersion, genesisHash, specVersion } from '../resources';
99
import * as sinon from 'sinon';
1010
import { Wallet } from '@bitgo/sdk-core';
11+
import assert = require('assert');
1112

1213
describe('DOT:', function () {
1314
let bitgo: TestBitGoAPI;
@@ -664,12 +665,69 @@ describe('DOT:', function () {
664665
walletPassphrase: 'fakeWalletPassphrase',
665666
};
666667

668+
const txPrebuild = {
669+
txHex:
670+
'0xa80a0300161b969b6b53ef81225feea3882284c778cd4a406d23215fcf492e83f75d42960b00204aa9d101eb600400000065900f001000000067f9723393ef76214df0118c34bbbd3dbebc8ed46a10973a8c969d48fe7598c9a7b7420ee3e4fe2b88da0fc42b30897e18d56d8b56a1934211d9de730cf96de300',
671+
};
672+
667673
await basecoin
668-
.verifyTransaction({ txParams })
674+
.verifyTransaction({ txPrebuild, txParams })
669675
.should.be.rejectedWith(
670676
`tdot doesn't support sending to more than 1 destination address within a single transaction. Try again, using only a single recipient.`
671677
);
672678
});
679+
680+
it('should reject a txPrebuild with more than invalid amount', async function () {
681+
const wallet = new Wallet(bitgo, basecoin, {});
682+
const txParams = {
683+
recipients: [{ amount: '20000000000', address: '5CZh773vKGwKFCYUjGc31AwXCbf7TPkavdeuk2XoujJMjbBD' }],
684+
wallet: wallet,
685+
walletPassphrase: 'fakeWalletPassphrase',
686+
};
687+
const txPrebuild = {
688+
txHex:
689+
'0xa80a0300161b969b6b53ef81225feea3882284c778cd4a406d23215fcf492e83f75d42960b00204aa9d101eb600400000065900f001000000067f9723393ef76214df0118c34bbbd3dbebc8ed46a10973a8c969d48fe7598c9a7b7420ee3e4fe2b88da0fc42b30897e18d56d8b56a1934211d9de730cf96de300',
690+
};
691+
692+
await basecoin
693+
.verifyTransaction({ txPrebuild, txParams })
694+
.should.be.rejectedWith(`Recipient amount 20000000000 does not match transaction amount 2000000000000`);
695+
});
696+
697+
it('should reject a txPrebuild with more than invalid recipient', async function () {
698+
const wallet = new Wallet(bitgo, basecoin, {});
699+
const txParams = {
700+
recipients: [{ amount: '2000000000000', address: '5CZh773vKGwKFCUjGc31AwXCbf7TPkavduk2XoujJMjbBD' }],
701+
wallet: wallet,
702+
walletPassphrase: 'fakeWalletPassphrase',
703+
};
704+
const txPrebuild = {
705+
txHex:
706+
'0xa80a0300161b969b6b53ef81225feea3882284c778cd4a406d23215fcf492e83f75d42960b00204aa9d101eb600400000065900f001000000067f9723393ef76214df0118c34bbbd3dbebc8ed46a10973a8c969d48fe7598c9a7b7420ee3e4fe2b88da0fc42b30897e18d56d8b56a1934211d9de730cf96de300',
707+
};
708+
709+
await basecoin
710+
.verifyTransaction({ txPrebuild, txParams })
711+
.should.be.rejectedWith(
712+
`Recipient address 5CZh773vKGwKFCUjGc31AwXCbf7TPkavduk2XoujJMjbBD does not match transaction destination address 5CZh773vKGwKFCYUjGc31AwXCbf7TPkavdeuk2XoujJMjbBD`
713+
);
714+
});
715+
716+
it('should accept a txPrebuild with more than valid recipient and amount', async function () {
717+
const wallet = new Wallet(bitgo, basecoin, {});
718+
const txParams = {
719+
recipients: [{ amount: '2000000000000', address: '5CZh773vKGwKFCYUjGc31AwXCbf7TPkavdeuk2XoujJMjbBD' }],
720+
wallet: wallet,
721+
walletPassphrase: 'fakeWalletPassphrase',
722+
};
723+
const txPrebuild = {
724+
txHex:
725+
'0xa80a0300161b969b6b53ef81225feea3882284c778cd4a406d23215fcf492e83f75d42960b00204aa9d101eb600400000065900f001000000067f9723393ef76214df0118c34bbbd3dbebc8ed46a10973a8c969d48fe7598c9a7b7420ee3e4fe2b88da0fc42b30897e18d56d8b56a1934211d9de730cf96de300',
726+
};
727+
728+
const result = await basecoin.verifyTransaction({ txPrebuild, txParams });
729+
assert.strictEqual(result, true);
730+
});
673731
});
674732

675733
describe('isWalletAddress', () => {

0 commit comments

Comments
 (0)