@@ -36,7 +36,7 @@ import BigNumber from 'bignumber.js';
36
36
37
37
import { ExplainTransactionOptions , StxSignTransactionOptions , StxTransactionExplanation } from './types' ;
38
38
import { StxLib } from '.' ;
39
- import { TransactionBuilderFactory } from './lib' ;
39
+ import { Transaction , TransactionBuilderFactory } from './lib' ;
40
40
import { TransactionBuilder } from './lib/transactionBuilder' ;
41
41
import { findContractTokenNameUsingContract , findTokenNameByContract , getAddressDetails } from './lib/utils' ;
42
42
import {
@@ -52,6 +52,7 @@ import {
52
52
} from './lib/iface' ;
53
53
import { TransferBuilder } from './lib/transferBuilder' ;
54
54
import { FungibleTokenTransferBuilder } from './lib/fungibleTokenTransferBuilder' ;
55
+ import _ from 'lodash' ;
55
56
56
57
export class Stx extends BaseCoin {
57
58
protected readonly _staticsCoin : Readonly < StaticsBaseCoin > ;
@@ -101,12 +102,43 @@ export class Stx extends BaseCoin {
101
102
}
102
103
103
104
async verifyTransaction ( params : VerifyTransactionOptions ) : Promise < boolean > {
104
- const { txParams } = params ;
105
+ const coinConfig = coins . get ( this . getChain ( ) ) ;
106
+ const { txPrebuild : txPrebuild , txParams : txParams } = params ;
105
107
if ( Array . isArray ( txParams . recipients ) && txParams . recipients . length > 1 ) {
106
108
throw new Error (
107
109
`${ this . getChain ( ) } doesn't support sending to more than 1 destination address within a single transaction. Try again, using only a single recipient.`
108
110
) ;
109
111
}
112
+ const transaction = new Transaction ( coinConfig ) ;
113
+ const rawTx = txPrebuild . txHex ;
114
+ if ( ! rawTx ) {
115
+ throw new Error ( 'missing required tx prebuild property txHex' ) ;
116
+ }
117
+
118
+ transaction . fromRawTransaction ( Buffer . from ( rawTx , 'hex' ) . toString ( 'base64' ) ) ;
119
+ const explainedTx = transaction . explainTransaction ( ) ;
120
+ if ( txParams . recipients !== undefined ) {
121
+ const filteredRecipients = txParams . recipients . map ( ( recipient ) => ( {
122
+ address : recipient . address ,
123
+ amount : BigInt ( recipient . amount ) ,
124
+ } ) ) ;
125
+
126
+ const filteredOutputs = explainedTx . outputs . map ( ( output ) => ( {
127
+ address : output . address ,
128
+ amount : BigInt ( output . amount ) ,
129
+ } ) ) ;
130
+
131
+ if ( ! _ . isEqual ( filteredOutputs , filteredRecipients ) ) {
132
+ throw new Error ( 'Transaction outputs do not match the expected recipients in txParams.' ) ;
133
+ }
134
+
135
+ // Validate total amount
136
+ const totalAmount = txParams . recipients . reduce ( ( sum , recipient ) => sum . plus ( recipient . amount ) , new BigNumber ( 0 ) ) ;
137
+
138
+ if ( ! totalAmount . isEqualTo ( explainedTx . outputAmount ) ) {
139
+ throw new Error ( 'Transaction total amount does not match the expected total amount.' ) ;
140
+ }
141
+ }
110
142
return true ;
111
143
}
112
144
0 commit comments