@@ -16,7 +16,8 @@ import {
16
16
import { bip32 } from '@bitgo/secp256k1' ;
17
17
import { CoinFamily , coins , BaseCoin as StaticsBaseCoin } from '@bitgo/statics' ;
18
18
import BigNumber from 'bignumber.js' ;
19
- import { Interface , KeyPair , TransactionBuilder , Utils } from './lib' ;
19
+ import { Interface , KeyPair , Transaction , TransactionBuilder , Utils } from './lib' ;
20
+ import _ from 'lodash' ;
20
21
21
22
export class Xtz extends BaseCoin {
22
23
protected readonly _staticsCoin : Readonly < StaticsBaseCoin > ;
@@ -116,12 +117,48 @@ export class Xtz extends BaseCoin {
116
117
}
117
118
118
119
async verifyTransaction ( params : VerifyTransactionOptions ) : Promise < boolean > {
119
- const { txParams } = params ;
120
+ const coinConfig = coins . get ( this . getChain ( ) ) ;
121
+ const { txPrebuild, txParams } = params ;
120
122
if ( Array . isArray ( txParams . recipients ) && txParams . recipients . length > 1 ) {
121
123
throw new Error (
122
124
`${ this . getChain ( ) } doesn't support sending to more than 1 destination address within a single transaction. Try again, using only a single recipient.`
123
125
) ;
124
126
}
127
+ // Validate the presence of txHex
128
+ const rawTx = txPrebuild . txHex ;
129
+ if ( ! rawTx ) {
130
+ throw new Error ( 'Missing required tx prebuild property: txHex' ) ;
131
+ }
132
+
133
+ // Parse the transaction
134
+ const transaction = new Transaction ( coinConfig ) ;
135
+ transaction . fromRawTransaction ( Buffer . from ( rawTx , 'hex' ) . toString ( 'base64' ) ) ;
136
+ const explainedTx = transaction . explainTransaction ( ) ;
137
+
138
+ // Validate recipients
139
+ if ( txParams . recipients ) {
140
+ const filteredRecipients = txParams . recipients . map ( ( recipient ) => ( {
141
+ address : recipient . address ,
142
+ amount : BigInt ( recipient . amount ) ,
143
+ } ) ) ;
144
+
145
+ const filteredOutputs = explainedTx . outputs . map ( ( output ) => ( {
146
+ address : output . address ,
147
+ amount : BigInt ( output . amount ) ,
148
+ } ) ) ;
149
+
150
+ if ( ! _ . isEqual ( filteredOutputs , filteredRecipients ) ) {
151
+ throw new Error ( 'Transaction outputs do not match the expected recipients in txParams.' ) ;
152
+ }
153
+
154
+ // Validate total amount
155
+ const totalAmount = txParams . recipients . reduce ( ( sum , recipient ) => sum . plus ( recipient . amount ) , new BigNumber ( 0 ) ) ;
156
+
157
+ if ( ! totalAmount . isEqualTo ( explainedTx . outputAmount ) ) {
158
+ throw new Error ( 'Transaction total amount does not match the expected total amount.' ) ;
159
+ }
160
+ }
161
+
125
162
return true ;
126
163
}
127
164
0 commit comments