41
41
ErrTxTypeNotSupported = errors .New ("transaction type not supported" )
42
42
ErrGasFeeCapTooLow = errors .New ("fee cap less than base fee" )
43
43
errEmptyTypedTx = errors .New ("empty typed transaction bytes" )
44
+ errShortTypedTx = errors .New ("typed transaction too short" )
44
45
errInvalidYParity = errors .New ("'yParity' field must be 0 or 1" )
45
46
errVYParityMismatch = errors .New ("'v' and 'yParity' fields do not match" )
46
47
errVYParityMissing = errors .New ("missing 'yParity' or 'v' field in transaction" )
@@ -94,6 +95,9 @@ type TxData interface {
94
95
95
96
rawSignatureValues () (v , r , s * big.Int )
96
97
setSignatureValues (chainID , v , r , s * big.Int )
98
+
99
+ encode (* bytes.Buffer ) error
100
+ decode ([]byte ) error
97
101
}
98
102
99
103
// EncodeRLP implements rlp.Encoder
@@ -114,7 +118,7 @@ func (tx *Transaction) EncodeRLP(w io.Writer) error {
114
118
// encodeTyped writes the canonical encoding of a typed transaction to w.
115
119
func (tx * Transaction ) encodeTyped (w * bytes.Buffer ) error {
116
120
w .WriteByte (tx .Type ())
117
- return rlp . Encode ( w , tx .inner )
121
+ return tx .inner . encode ( w )
118
122
}
119
123
120
124
// MarshalBinary returns the canonical encoding of the transaction.
@@ -143,7 +147,9 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
143
147
tx .setDecoded (& inner , int (rlp .ListSize (size )))
144
148
}
145
149
return err
146
- case kind == rlp .String :
150
+ case kind == rlp .Byte :
151
+ return errShortTypedTx
152
+ default :
147
153
// It's an EIP-2718 typed TX envelope.
148
154
var b []byte
149
155
if b , err = s .Bytes (); err != nil {
@@ -154,8 +160,6 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
154
160
tx .setDecoded (inner , len (b ))
155
161
}
156
162
return err
157
- default :
158
- return rlp .ErrExpectedList
159
163
}
160
164
}
161
165
@@ -183,29 +187,24 @@ func (tx *Transaction) UnmarshalBinary(b []byte) error {
183
187
184
188
// decodeTyped decodes a typed transaction from the canonical format.
185
189
func (tx * Transaction ) decodeTyped (b []byte ) (TxData , error ) {
186
- if len (b ) == 0 {
187
- return nil , errEmptyTypedTx
190
+ if len (b ) <= 1 {
191
+ return nil , errShortTypedTx
188
192
}
193
+ var inner TxData
189
194
switch b [0 ] {
190
195
case AccessListTxType :
191
- var inner AccessListTx
192
- err := rlp .DecodeBytes (b [1 :], & inner )
193
- return & inner , err
196
+ inner = new (AccessListTx )
194
197
case DynamicFeeTxType :
195
- var inner DynamicFeeTx
196
- err := rlp .DecodeBytes (b [1 :], & inner )
197
- return & inner , err
198
+ inner = new (DynamicFeeTx )
198
199
case BlobTxType :
199
- var inner BlobTx
200
- err := rlp .DecodeBytes (b [1 :], & inner )
201
- return & inner , err
200
+ inner = new (BlobTx )
202
201
case L1MessageTxType :
203
- var inner L1MessageTx
204
- err := rlp .DecodeBytes (b [1 :], & inner )
205
- return & inner , err
202
+ inner = new (L1MessageTx )
206
203
default :
207
204
return nil , ErrTxTypeNotSupported
208
205
}
206
+ err := inner .decode (b [1 :])
207
+ return inner , err
209
208
}
210
209
211
210
// setDecoded sets the inner transaction and size after decoding.
0 commit comments