@@ -1085,26 +1085,66 @@ export class Near extends BaseCoin {
10851085 const freshTxData = freshTx . toJson ( ) ;
10861086 const originalTxData = transaction . toJson ( ) ;
10871087
1088- // Verify key transaction fields match to prevent tampering
1089- if (
1090- freshTxData . signerId !== originalTxData . signerId ||
1091- freshTxData . receiverId !== originalTxData . receiverId ||
1092- freshTxData . publicKey !== originalTxData . publicKey ||
1093- freshTxData . actions . length !== originalTxData . actions . length
1094- ) {
1095- throw new Error ( 'Transaction hex does not match provided transaction' ) ;
1088+ // Validate each aspect of the transaction separately
1089+ this . validateSigner ( originalTxData , freshTxData ) ;
1090+ this . validateReceiver ( originalTxData , freshTxData ) ;
1091+ this . validatePublicKey ( originalTxData , freshTxData ) ;
1092+ this . validateActions ( originalTxData , freshTxData ) ;
1093+ this . validateAddresses ( txParams , explainedTx ) ;
1094+ }
1095+
1096+ //Validates that the signer ID matches between original and fresh transaction
1097+ private validateSigner ( originalTxData : any , freshTxData : any ) : void {
1098+ if ( originalTxData . signerId !== freshTxData . signerId ) {
1099+ throw new Error (
1100+ `Error on token enablements: signers are not the same, expected ${ originalTxData . signerId } but got ${ freshTxData . signerId } `
1101+ ) ;
10961102 }
1103+ }
10971104
1098- // Validate addresses match between parameters and explained transaction
1099- if ( txParams . recipients && explainedTx . outputs ) {
1100- const expectedAddresses = txParams . recipients . map ( ( r ) => r . address ) ;
1101- const explainedAddresses = explainedTx . outputs . map ( ( o ) => o . address ) ;
1105+ //Validates that the receiver ID matches between original and fresh transaction
1106+ private validateReceiver ( originalTxData : any , freshTxData : any ) : void {
1107+ if ( originalTxData . receiverId !== freshTxData . receiverId ) {
1108+ throw new Error (
1109+ `Error on token enablements: receivers are not the same, expected ${ originalTxData . receiverId } but got ${ freshTxData . receiverId } `
1110+ ) ;
1111+ }
1112+ }
11021113
1103- for ( const addr of expectedAddresses ) {
1104- if ( ! explainedAddresses . includes ( addr ) ) {
1105- throw new Error ( `Address mismatch: ${ addr } ` ) ;
1106- }
1107- }
1114+ //Validates that the public key matches between original and fresh transaction
1115+ private validatePublicKey ( originalTxData : any , freshTxData : any ) : void {
1116+ if ( originalTxData . publicKey !== freshTxData . publicKey ) {
1117+ throw new Error (
1118+ `Error on token enablements: public keys are not the same, expected ${ originalTxData . publicKey } but got ${ freshTxData . publicKey } `
1119+ ) ;
1120+ }
1121+ }
1122+
1123+ //Validates that the actions length matches between original and fresh transaction
1124+ private validateActions ( originalTxData : any , freshTxData : any ) : void {
1125+ if ( originalTxData . actions . length !== freshTxData . actions . length ) {
1126+ throw new Error (
1127+ `Error on token enablements: actions length mismatch, expected ${ originalTxData . actions . length } but got ${ freshTxData . actions . length } `
1128+ ) ;
1129+ }
1130+ }
1131+
1132+ //Validates that addresses match between parameters and explained transaction
1133+ private validateAddresses ( txParams : VerifyTransactionOptions [ 'txParams' ] , explainedTx : TransactionExplanation ) : void {
1134+ if ( ! txParams . recipients || ! explainedTx . outputs ) {
1135+ return ;
1136+ }
1137+
1138+ if ( txParams . recipients . length !== explainedTx . outputs . length ) {
1139+ throw new Error ( 'Error on token enablements: output count does not match recipients count' ) ;
1140+ }
1141+
1142+ const mismatchedAddresses = txParams . recipients
1143+ . filter ( ( recipient , index ) => recipient . address !== explainedTx . outputs [ index ] . address )
1144+ . map ( ( recipient ) => recipient . address ) ;
1145+
1146+ if ( mismatchedAddresses . length > 0 ) {
1147+ throw new Error ( `Address mismatch: ${ mismatchedAddresses . join ( ', ' ) } ` ) ;
11081148 }
11091149 }
11101150}
0 commit comments