Skip to content

Commit 2af5ce8

Browse files
authored
Merge pull request #7652 from BitGo/WIN-7596-dot-verify
feat(sdk-coin-dot): add verify transaction function
2 parents 78e280a + 4b3ec16 commit 2af5ce8

File tree

2 files changed

+98
-6
lines changed

2 files changed

+98
-6
lines changed

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,46 @@ export class Dot extends BaseCoin {
651651
}
652652

653653
async verifyTransaction(params: VerifyTransactionOptions): Promise<boolean> {
654-
const { txParams } = params;
655-
if (Array.isArray(txParams.recipients) && txParams.recipients.length > 1) {
656-
throw new Error(
657-
`${this.getChain()} doesn't support sending to more than 1 destination address within a single transaction. Try again, using only a single recipient.`
658-
);
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+
const factory = this.getBuilder();
668+
const txBuilder = factory.from(txPrebuild.txHex) as any;
669+
670+
if (txParams.recipients !== undefined) {
671+
if (txParams.recipients.length === 0) {
672+
throw new Error('missing recipients in txParams');
673+
}
674+
675+
if (Array.isArray(txParams.recipients) && txParams.recipients.length > 1) {
676+
throw new Error(
677+
`${this.getChain()} doesn't support sending to more than 1 destination address within a single transaction. Try again, using only a single recipient.`
678+
);
679+
}
680+
681+
// validate recipient is same as txBuilder._to
682+
if (txParams.recipients[0].address !== txBuilder._to) {
683+
throw new Error(
684+
`Recipient address ${txParams.recipients[0].address} does not match transaction destination address ${txBuilder._to}`
685+
);
686+
}
687+
688+
// and amount is same as txBuilder._amount
689+
if (txParams.recipients[0].amount !== txBuilder._amount) {
690+
throw new Error(
691+
`Recipient amount ${txParams.recipients[0].amount} does not match transaction amount ${txBuilder._amount}`
692+
);
693+
}
659694
}
660695
return true;
661696
}

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)