Skip to content

Commit 69e0606

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

2 files changed

Lines changed: 92 additions & 2 deletions

File tree

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: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,12 +664,69 @@ describe('DOT:', function () {
664664
walletPassphrase: 'fakeWalletPassphrase',
665665
};
666666

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

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

0 commit comments

Comments
 (0)