|
| 1 | +import type { TypedTransaction } from '@ethereumjs/tx'; |
1 | 2 | import { TransactionFactory } from '@ethereumjs/tx';
|
2 | 3 |
|
3 | 4 | import logger from '../../logger';
|
4 | 5 | import { createCommon, formatTransactionData } from '../../util';
|
5 | 6 | import { hexlify } from '../../util/hexlify';
|
6 | 7 | import { TRANSACTION_TYPES } from '../constants';
|
| 8 | +import type { EthSignTransactionRequest } from '../types/EthSignTransactionRequest'; |
7 | 9 | import type { ITransactionDetails } from '../types/ITransactionDetails';
|
8 | 10 | import type { IEIP1559TxParams, ILegacyTXParams } from '../types/ITXParams';
|
9 | 11 |
|
@@ -38,19 +40,10 @@ export class TransactionHelper {
|
38 | 40 | transaction: ITransactionDetails,
|
39 | 41 | chainId: string,
|
40 | 42 | ): Promise<{ v: string; r: string; s: string }> {
|
41 |
| - const common = createCommon(transaction, chainId); |
42 |
| - |
43 | 43 | if (transaction.signedRawTransaction) {
|
44 | 44 | logger.info('Transaction is signed', transaction.signedRawTransaction);
|
45 |
| - const signedRawTransaction = Buffer.from( |
46 |
| - transaction.signedRawTransaction.substring(2), |
47 |
| - 'hex', |
48 |
| - ); |
49 |
| - |
50 |
| - const tx = TransactionFactory.fromSerializedData(signedRawTransaction, { |
51 |
| - common, |
52 |
| - }); |
53 | 45 |
|
| 46 | + const tx = this.getTypedTransaction(transaction, chainId); |
54 | 47 | return {
|
55 | 48 | v: hexlify(tx.v?.toString() ?? '0'),
|
56 | 49 | r: hexlify(tx.r?.toString() ?? '0'),
|
@@ -78,4 +71,90 @@ export class TransactionHelper {
|
78 | 71 | s: tx.s,
|
79 | 72 | };
|
80 | 73 | }
|
| 74 | + |
| 75 | + static getTypedTransaction( |
| 76 | + transactionDetails: ITransactionDetails, |
| 77 | + chainId: string, |
| 78 | + ): TypedTransaction { |
| 79 | + if (!transactionDetails.signedRawTransaction) { |
| 80 | + throw new Error('Transaction is not signed'); |
| 81 | + } |
| 82 | + |
| 83 | + const common = createCommon(transactionDetails, chainId); |
| 84 | + |
| 85 | + const signedRawTransaction = Buffer.from( |
| 86 | + transactionDetails.signedRawTransaction.substring(2), |
| 87 | + 'hex', |
| 88 | + ); |
| 89 | + |
| 90 | + const tx = TransactionFactory.fromSerializedData(signedRawTransaction, { |
| 91 | + common, |
| 92 | + }); |
| 93 | + |
| 94 | + return tx; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * We need to be certain that the custodian did not alter any of the fields |
| 99 | + * which was previously allowed in MMI but cannot work in the institutional snap |
| 100 | + * because the snap keyring does not allow us to return anything other than the signature |
| 101 | + * |
| 102 | + * @param request - The original request we got from the keyring |
| 103 | + * @param transactionDetails - The transaction details we got from the custodian |
| 104 | + */ |
| 105 | + |
| 106 | + static validateTransaction( |
| 107 | + request: EthSignTransactionRequest, |
| 108 | + transactionDetails: ITransactionDetails, |
| 109 | + ): { |
| 110 | + isValid: boolean; |
| 111 | + error?: string; |
| 112 | + } { |
| 113 | + const tx = this.getTypedTransaction(transactionDetails, request.chainId); |
| 114 | + // Validated nonce, gas price, maxFeePerGas, maxPriorityFeePerGas and gas limit |
| 115 | + if (Number(request.nonce) !== Number(tx.nonce)) { |
| 116 | + return { |
| 117 | + isValid: false, |
| 118 | + error: `Custodian altered the nonce from the request: ${Number( |
| 119 | + request.nonce, |
| 120 | + )} to ${Number(tx.nonce)}`, |
| 121 | + }; |
| 122 | + } |
| 123 | + if ( |
| 124 | + 'gasPrice' in request && |
| 125 | + 'gasPrice' in tx && |
| 126 | + Number(request.gasPrice) !== Number(tx.gasPrice) |
| 127 | + ) { |
| 128 | + return { |
| 129 | + isValid: false, |
| 130 | + error: `Custodian altered the gas price from the request: ${request.gasPrice} to ${tx.gasPrice}`, |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + if ( |
| 135 | + 'maxFeePerGas' in request && |
| 136 | + 'maxFeePerGas' in tx && |
| 137 | + Number(request.maxFeePerGas) !== Number(tx.maxFeePerGas) |
| 138 | + ) { |
| 139 | + return { |
| 140 | + isValid: false, |
| 141 | + error: `Custodian altered the maxFeePerGas from the request: ${request.maxFeePerGas} to ${tx.maxFeePerGas}`, |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + if ( |
| 146 | + 'maxPriorityFeePerGas' in request && |
| 147 | + 'maxPriorityFeePerGas' in tx && |
| 148 | + Number(request.maxPriorityFeePerGas) !== Number(tx.maxPriorityFeePerGas) |
| 149 | + ) { |
| 150 | + return { |
| 151 | + isValid: false, |
| 152 | + error: `Custodian altered the maxPriorityFeePerGas from the request: ${request.maxPriorityFeePerGas} to ${tx.maxPriorityFeePerGas}`, |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + return { |
| 157 | + isValid: true, |
| 158 | + }; |
| 159 | + } |
81 | 160 | }
|
0 commit comments