eth 0.5.12 → 0.5.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/codeql.yml +1 -1
  3. data/.github/workflows/docs.yml +2 -2
  4. data/.github/workflows/spec.yml +5 -7
  5. data/CHANGELOG.md +39 -1
  6. data/Gemfile +8 -8
  7. data/LICENSE.txt +1 -1
  8. data/README.md +1 -3
  9. data/SECURITY.md +1 -1
  10. data/eth.gemspec +4 -1
  11. data/lib/eth/abi/decoder.rb +3 -4
  12. data/lib/eth/abi/encoder.rb +4 -5
  13. data/lib/eth/abi/event.rb +5 -1
  14. data/lib/eth/abi/packed/encoder.rb +196 -0
  15. data/lib/eth/abi/type.rb +12 -12
  16. data/lib/eth/abi.rb +27 -2
  17. data/lib/eth/address.rb +11 -1
  18. data/lib/eth/api.rb +96 -1
  19. data/lib/eth/chain.rb +63 -12
  20. data/lib/eth/client/http.rb +7 -3
  21. data/lib/eth/client/ipc.rb +1 -1
  22. data/lib/eth/client.rb +3 -3
  23. data/lib/eth/constant.rb +1 -1
  24. data/lib/eth/contract/event.rb +69 -16
  25. data/lib/eth/contract/function.rb +1 -1
  26. data/lib/eth/contract/function_input.rb +1 -1
  27. data/lib/eth/contract/function_output.rb +1 -1
  28. data/lib/eth/contract/initializer.rb +1 -1
  29. data/lib/eth/contract.rb +1 -1
  30. data/lib/eth/eip712.rb +3 -2
  31. data/lib/eth/ens/coin_type.rb +1 -1
  32. data/lib/eth/ens/resolver.rb +1 -1
  33. data/lib/eth/ens.rb +1 -1
  34. data/lib/eth/key/decrypter.rb +1 -1
  35. data/lib/eth/key/encrypter.rb +1 -1
  36. data/lib/eth/key.rb +1 -1
  37. data/lib/eth/rlp/decoder.rb +1 -1
  38. data/lib/eth/rlp/encoder.rb +1 -1
  39. data/lib/eth/rlp/sedes/big_endian_int.rb +1 -1
  40. data/lib/eth/rlp/sedes/binary.rb +1 -1
  41. data/lib/eth/rlp/sedes/list.rb +1 -1
  42. data/lib/eth/rlp/sedes.rb +1 -1
  43. data/lib/eth/rlp.rb +1 -1
  44. data/lib/eth/signature.rb +1 -1
  45. data/lib/eth/solidity.rb +1 -1
  46. data/lib/eth/tx/eip1559.rb +2 -2
  47. data/lib/eth/tx/eip2930.rb +2 -2
  48. data/lib/eth/tx/eip7702.rb +495 -0
  49. data/lib/eth/tx/legacy.rb +1 -1
  50. data/lib/eth/tx.rb +53 -2
  51. data/lib/eth/unit.rb +1 -1
  52. data/lib/eth/util.rb +6 -3
  53. data/lib/eth/version.rb +2 -2
  54. data/lib/eth.rb +1 -1
  55. metadata +20 -4
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2016-2023 The Ruby-Eth Contributors
1
+ # Copyright (c) 2016-2025 The Ruby-Eth Contributors
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -180,7 +180,7 @@ module Eth
180
180
  # allows us to force-setting a signature if the transaction is signed already
181
181
  _set_signature(recovery_id, r, s)
182
182
  else
183
- raise_error DecoderError, "Cannot decode EIP-1559 payload!"
183
+ raise DecoderError, "Cannot decode EIP-1559 payload!"
184
184
  end
185
185
 
186
186
  # last but not least, set the type.
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2016-2023 The Ruby-Eth Contributors
1
+ # Copyright (c) 2016-2025 The Ruby-Eth Contributors
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -175,7 +175,7 @@ module Eth
175
175
  # allows us to force-setting a signature if the transaction is signed already
176
176
  _set_signature(recovery_id, r, s)
177
177
  else
178
- raise_error DecoderError, "Cannot decode EIP-2930 payload!"
178
+ raise DecoderError, "Cannot decode EIP-2930 payload!"
179
179
  end
180
180
 
181
181
  # last but not least, set the type.
@@ -0,0 +1,495 @@
1
+ # Copyright (c) 2016-2025 The Ruby-Eth Contributors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # Provides the {Eth} module.
16
+ module Eth
17
+
18
+ # Provides the `Tx` module supporting various transaction types.
19
+ module Tx
20
+
21
+ # Provides support for EIP-7702 transactions utilizing EIP-2718
22
+ # types and envelopes.
23
+ # Ref: https://eips.ethereum.org/EIPS/eip-7702
24
+ class Eip7702
25
+
26
+ # Provides an EIP-7702 authorization that store the address to
27
+ # code which the signer desires to execute in the context of their EOA.
28
+ class Authorization
29
+
30
+ # The EIP-155 Chain ID.
31
+ # Ref: https://eips.ethereum.org/EIPS/eip-155
32
+ attr_reader :chain_id
33
+
34
+ # The authority addess.
35
+ attr_reader :address
36
+
37
+ # The transaction nonce.
38
+ attr_reader :nonce
39
+
40
+ # The signature's y-parity byte (not v).
41
+ attr_reader :signature_y_parity
42
+
43
+ # The signature `r` value.
44
+ attr_reader :signature_r
45
+
46
+ # The signature `s` value.
47
+ attr_reader :signature_s
48
+
49
+ # Create a type-4 (EIP-7702) authorization object that
50
+ # can be prepared for type-4 transactions.
51
+ # Ref: https://eips.ethereum.org/EIPS/eip-7702
52
+ #
53
+ # @param fields [Hash] all necessary transaction fields.
54
+ # @option fields [Integer] :chain_id the chain ID.
55
+ # @option fields [Eth::Address] :address the authority address.
56
+ # @option fields [Integer] :nonce the transaction nonce.
57
+ def initialize(fields)
58
+ @chain_id = fields[:chain_id].to_i
59
+ @address = fields[:address].to_s
60
+ @nonce = fields[:nonce].to_i
61
+ @signature_y_parity = fields[:recovery_id]
62
+ @signature_r = fields[:r]
63
+ @signature_s = fields[:s]
64
+ end
65
+
66
+ # Sign the authorization with a given key.
67
+ #
68
+ # @param key [Eth::Key] the key-pair to use for signing.
69
+ # @return [String] a transaction hash.
70
+ # @raise [Signature::SignatureError] if authorization is already signed.
71
+ # @raise [Signature::SignatureError] if sender address does not match signing key.
72
+ def sign(key)
73
+ if Tx.signed? self
74
+ raise Signature::SignatureError, "Authorization is already signed!"
75
+ end
76
+
77
+ # ensure the sender address matches the given key
78
+ unless @address.nil? or @address.empty?
79
+ signer_address = Tx.sanitize_address key.address.to_s
80
+ from_address = Tx.sanitize_address @address
81
+ raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
82
+ end
83
+
84
+ # sign a keccak hash of the unsigned, encoded transaction
85
+ signature = key.sign(unsigned_hash, @chain_id)
86
+ r, s, v = Signature.dissect signature
87
+ recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
88
+ @signature_y_parity = recovery_id
89
+ @signature_r = r
90
+ @signature_s = s
91
+ return hash
92
+ end
93
+
94
+ # Encodes the unsigned authorization payload required for signing.
95
+ #
96
+ # @return [String] an RLP-encoded, unsigned, enveloped EIP-7702 transaction.
97
+ def unsigned_encoded
98
+ authorization_data = []
99
+ authorization_data.push Util.serialize_int_to_big_endian @chain_id
100
+ authorization_data.push Util.hex_to_bin @address
101
+ authorization_data.push Util.serialize_int_to_big_endian @nonce
102
+ Rlp.encode authorization_data
103
+ end
104
+
105
+ # Gets the sign-hash required to sign.
106
+ #
107
+ # @return [String] a Keccak-256 hash.
108
+ def unsigned_hash
109
+ Util.keccak256 unsigned_encoded
110
+ end
111
+
112
+ # Gets the raw serialized authorization data.
113
+ #
114
+ # @return [Bytes] raw serialized authorization data.
115
+ def raw
116
+ authorization_data = []
117
+ authorization_data.push Util.serialize_int_to_big_endian @chain_id
118
+ authorization_data.push Util.hex_to_bin @address
119
+ authorization_data.push Util.serialize_int_to_big_endian @nonce
120
+
121
+ authorization_data.push Util.serialize_int_to_big_endian @signature_y_parity
122
+ authorization_data.push Util.serialize_int_to_big_endian @signature_r
123
+ authorization_data.push Util.serialize_int_to_big_endian @signature_s
124
+ authorization_data
125
+ end
126
+
127
+ # Compares two authorization data objects.
128
+ #
129
+ # @return [Bool] true if objects are same and share same state.
130
+ def ==(o)
131
+ o.class == self.class && o.state == state
132
+ end
133
+
134
+ protected
135
+
136
+ def state
137
+ [@chain_id, @address, @nonce, @signature_y_parity, @signature_r, @signature_s]
138
+ end
139
+ end
140
+
141
+ # The EIP-155 Chain ID.
142
+ # Ref: https://eips.ethereum.org/EIPS/eip-155
143
+ attr_reader :chain_id
144
+
145
+ # The transaction nonce provided by the signer.
146
+ attr_reader :signer_nonce
147
+
148
+ # The transaction max priority fee per gas in Wei.
149
+ attr_reader :max_priority_fee_per_gas
150
+
151
+ # The transaction max fee per gas in Wei.
152
+ attr_reader :max_fee_per_gas
153
+
154
+ # The gas limit for the transaction.
155
+ attr_reader :gas_limit
156
+
157
+ # The recipient address.
158
+ attr_reader :destination
159
+
160
+ # The transaction amount in Wei.
161
+ attr_reader :amount
162
+
163
+ # The transaction data payload.
164
+ attr_reader :payload
165
+
166
+ # An optional EIP-2930 access list.
167
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
168
+ attr_reader :access_list
169
+
170
+ # The list of of {Authorization} instances
171
+ attr_reader :authorization_list
172
+
173
+ # The signature's y-parity byte (not v).
174
+ attr_reader :signature_y_parity
175
+
176
+ # The signature `r` value.
177
+ attr_reader :signature_r
178
+
179
+ # The signature `s` value.
180
+ attr_reader :signature_s
181
+
182
+ # The sender address.
183
+ attr_reader :sender
184
+
185
+ # The transaction type.
186
+ attr_reader :type
187
+
188
+ # Create a type-4 (EIP-7702) transaction payload object that
189
+ # can be prepared for envelope, signature and broadcast.
190
+ # Ref: https://eips.ethereum.org/EIPS/eip-7702
191
+ #
192
+ # @param params [Hash] all necessary transaction fields.
193
+ # @option params [Integer] :chain_id the chain ID.
194
+ # @option params [Integer] :nonce the signer nonce.
195
+ # @option params [Integer] :priority_fee the max priority fee per gas.
196
+ # @option params [Integer] :max_gas_fee the max transaction fee per gas.
197
+ # @option params [Integer] :gas_limit the gas limit.
198
+ # @option params [Eth::Address] :from the sender address.
199
+ # @option params [Eth::Address] :to the reciever address.
200
+ # @option params [Integer] :value the transaction value.
201
+ # @option params [String] :data the transaction data payload.
202
+ # @option params [Array] :access_list an optional access list.
203
+ # @option params [Array] :authorization_list the list of authorization instances (a list of Eth::Tx::Eip7702::Authorization instances).
204
+ # @raise [ParameterError] if gas limit is too low.
205
+ def initialize(params)
206
+ fields = { recovery_id: nil, r: 0, s: 0 }.merge params
207
+
208
+ # populate optional fields with serializable empty values
209
+ fields[:chain_id] = Tx.sanitize_chain fields[:chain_id]
210
+ fields[:from] = Tx.sanitize_address fields[:from]
211
+ fields[:to] = Tx.sanitize_address fields[:to]
212
+ fields[:value] = Tx.sanitize_amount fields[:value]
213
+ fields[:data] = Tx.sanitize_data fields[:data]
214
+
215
+ # ensure sane values for all mandatory fields
216
+ fields = Tx.validate_params fields
217
+ fields = Tx.validate_eip1559_params fields
218
+ fields = Tx.validate_eip7702_params fields
219
+ fields[:access_list] = Tx.sanitize_list fields[:access_list]
220
+
221
+ # ensure gas limit is not too low
222
+ minimum_cost = Tx.estimate_intrinsic_gas fields[:data], fields[:access_list]
223
+ raise ParameterError, "Transaction gas limit is too low, try #{minimum_cost}!" if fields[:gas_limit].to_i < minimum_cost
224
+
225
+ # populate class attributes
226
+ @signer_nonce = fields[:nonce].to_i
227
+ @max_priority_fee_per_gas = fields[:priority_fee].to_i
228
+ @max_fee_per_gas = fields[:max_gas_fee].to_i
229
+ @gas_limit = fields[:gas_limit].to_i
230
+ @sender = fields[:from].to_s
231
+ @destination = fields[:to].to_s
232
+ @amount = fields[:value].to_i
233
+ @payload = fields[:data]
234
+ @access_list = fields[:access_list]
235
+ @authorization_list = fields[:authorization_list]
236
+
237
+ # the signature v is set to the chain id for unsigned transactions
238
+ @signature_y_parity = fields[:recovery_id]
239
+ @chain_id = fields[:chain_id]
240
+
241
+ # the signature fields are empty for unsigned transactions.
242
+ @signature_r = fields[:r]
243
+ @signature_s = fields[:s]
244
+
245
+ # last but not least, set the type.
246
+ @type = TYPE_7702
247
+ end
248
+
249
+ # Overloads the constructor for decoding raw transactions and creating unsigned copies.
250
+ konstructor :decode, :unsigned_copy
251
+
252
+ # Decodes a raw transaction hex into an {Eth::Tx::Eip7702}
253
+ # transaction object.
254
+ #
255
+ # @param hex [String] the raw transaction hex-string.
256
+ # @return [Eth::Tx::Eip7702] transaction payload.
257
+ # @raise [TransactionTypeError] if transaction type is invalid.
258
+ # @raise [ParameterError] if transaction is missing fields.
259
+ # @raise [DecoderError] if transaction decoding fails.
260
+ def decode(hex)
261
+ hex = Util.remove_hex_prefix hex
262
+ type = hex[0, 2]
263
+ raise TransactionTypeError, "Invalid transaction type #{type.inspect}!" if type.to_i(16) != TYPE_7702
264
+
265
+ bin = Util.hex_to_bin hex[2..]
266
+ tx = Rlp.decode bin
267
+
268
+ # decoded transactions always have 9 + 3 fields, even if they are empty or zero
269
+ raise ParameterError, "Transaction missing fields!" if tx.size < 10
270
+
271
+ # populate the 10 payload fields
272
+ chain_id = Util.deserialize_big_endian_to_int tx[0]
273
+ nonce = Util.deserialize_big_endian_to_int tx[1]
274
+ priority_fee = Util.deserialize_big_endian_to_int tx[2]
275
+ max_gas_fee = Util.deserialize_big_endian_to_int tx[3]
276
+ gas_limit = Util.deserialize_big_endian_to_int tx[4]
277
+ to = Util.bin_to_hex tx[5]
278
+ value = Util.deserialize_big_endian_to_int tx[6]
279
+ data = tx[7]
280
+ access_list = tx[8]
281
+ authorization_list = tx[9]
282
+ authorizations = deserialize_authorizations(authorization_list)
283
+
284
+ # populate class attributes
285
+ @chain_id = chain_id.to_i
286
+ @signer_nonce = nonce.to_i
287
+ @max_priority_fee_per_gas = priority_fee.to_i
288
+ @max_fee_per_gas = max_gas_fee.to_i
289
+ @gas_limit = gas_limit.to_i
290
+ @destination = to.to_s
291
+ @amount = value.to_i
292
+ @payload = data
293
+ @access_list = access_list
294
+ @authorization_list = authorizations
295
+
296
+ # populate the 3 signature fields
297
+ if tx.size == 10
298
+ _set_signature(nil, 0, 0)
299
+ elsif tx.size == 13
300
+ recovery_id = Util.bin_to_hex(tx[10]).to_i(16)
301
+ r = Util.bin_to_hex tx[11]
302
+ s = Util.bin_to_hex tx[12]
303
+
304
+ # allows us to force-setting a signature if the transaction is signed already
305
+ _set_signature(recovery_id, r, s)
306
+ else
307
+ raise DecoderError, "Cannot decode EIP-7702 payload!"
308
+ end
309
+
310
+ # last but not least, set the type.
311
+ @type = TYPE_7702
312
+
313
+ unless recovery_id.nil?
314
+ # recover sender address
315
+ v = Chain.to_v recovery_id, chain_id
316
+ public_key = Signature.recover(unsigned_hash, "#{r.rjust(64, "0")}#{s.rjust(64, "0")}#{v.to_s(16)}", chain_id)
317
+ address = Util.public_key_to_address(public_key).to_s
318
+ @sender = Tx.sanitize_address address
319
+ else
320
+ # keep the 'from' field blank
321
+ @sender = Tx.sanitize_address nil
322
+ end
323
+ end
324
+
325
+ # Creates an unsigned copy of a transaction payload (keeping the signatures on the authorizations).
326
+ #
327
+ # @param tx [Eth::Tx::Eip7702] an EIP-7702 transaction payload.
328
+ # @return [Eth::Tx::Eip7702] an unsigned EIP-7702 transaction payload.
329
+ # @raise [TransactionTypeError] if transaction type does not match.
330
+ def unsigned_copy(tx)
331
+
332
+ # not checking transaction validity unless it's of a different class
333
+ raise TransactionTypeError, "Cannot copy transaction of different payload type!" unless tx.instance_of? Tx::Eip7702
334
+
335
+ # populate class attributes
336
+ @signer_nonce = tx.signer_nonce
337
+ @max_priority_fee_per_gas = tx.max_priority_fee_per_gas
338
+ @max_fee_per_gas = tx.max_fee_per_gas
339
+ @gas_limit = tx.gas_limit
340
+ @destination = tx.destination
341
+ @amount = tx.amount
342
+ @payload = tx.payload
343
+ @access_list = tx.access_list
344
+ @chain_id = tx.chain_id
345
+
346
+ @authorization_list = tx.authorization_list.map do |authorization|
347
+ Authorization.new(chain_id: authorization.chain_id,
348
+ address: authorization.address,
349
+ nonce: authorization.nonce,
350
+ recovery_id: authorization.signature_y_parity,
351
+ r: authorization.signature_r,
352
+ s: authorization.signature_s)
353
+ end
354
+ # force-set signature to unsigned
355
+ _set_signature(nil, 0, 0)
356
+
357
+ # keep the 'from' field blank
358
+ @sender = Tx.sanitize_address nil
359
+
360
+ # last but not least, set the type.
361
+ @type = TYPE_7702
362
+ end
363
+
364
+ # Sign the transaction with a given key.
365
+ #
366
+ # @param key [Eth::Key] the key-pair to use for signing.
367
+ # @return [String] a transaction hash.
368
+ # @raise [Signature::SignatureError] if transaction is already signed.
369
+ # @raise [Signature::SignatureError] if sender address does not match signing key.
370
+ def sign(key)
371
+ if Tx.signed? self
372
+ raise Signature::SignatureError, "Transaction is already signed!"
373
+ end
374
+
375
+ # ensure the sender address matches the given key
376
+ unless @sender.nil? or sender.empty?
377
+ signer_address = Tx.sanitize_address key.address.to_s
378
+ from_address = Tx.sanitize_address @sender
379
+ raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
380
+ end
381
+
382
+ # sign a keccak hash of the unsigned, encoded transaction
383
+ signature = key.sign(unsigned_hash, @chain_id)
384
+ r, s, v = Signature.dissect signature
385
+ recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
386
+ @signature_y_parity = recovery_id
387
+ @signature_r = r
388
+ @signature_s = s
389
+ return hash
390
+ end
391
+
392
+ # Encodes a raw transaction object, wraps it in an EIP-2718 envelope
393
+ # with an EIP-7702 type prefix.
394
+ #
395
+ # @return [String] a raw, RLP-encoded EIP-7702 type transaction object.
396
+ # @raise [Signature::SignatureError] if the transaction is not yet signed.
397
+ def encoded
398
+ unless Tx.signed? self
399
+ raise Signature::SignatureError, "Transaction is not signed!"
400
+ end
401
+ tx_data = []
402
+ tx_data.push Util.serialize_int_to_big_endian @chain_id
403
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
404
+ tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
405
+ tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
406
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
407
+ tx_data.push Util.hex_to_bin @destination
408
+ tx_data.push Util.serialize_int_to_big_endian @amount
409
+ tx_data.push Rlp::Sedes.binary.serialize @payload
410
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
411
+
412
+ authorization_list = @authorization_list.map { |authorization| authorization.raw }
413
+ tx_data.push Rlp::Sedes.infer(authorization_list).serialize authorization_list
414
+
415
+ tx_data.push Util.serialize_int_to_big_endian @signature_y_parity
416
+ tx_data.push Util.serialize_int_to_big_endian @signature_r
417
+ tx_data.push Util.serialize_int_to_big_endian @signature_s
418
+ tx_encoded = Rlp.encode tx_data
419
+
420
+ # create an EIP-2718 envelope with EIP-7702 type payload
421
+ tx_type = Util.serialize_int_to_big_endian @type
422
+ return "#{tx_type}#{tx_encoded}"
423
+ end
424
+
425
+ # Gets the encoded, enveloped, raw transaction hex.
426
+ #
427
+ # @return [String] the raw transaction hex.
428
+ def hex
429
+ Util.bin_to_hex encoded
430
+ end
431
+
432
+ # Gets the transaction hash.
433
+ #
434
+ # @return [String] the transaction hash.
435
+ def hash
436
+ Util.bin_to_hex Util.keccak256 encoded
437
+ end
438
+
439
+ # Encodes the unsigned transaction payload in an EIP-7702 envelope,
440
+ # required for signing.
441
+ #
442
+ # @return [String] an RLP-encoded, unsigned, enveloped EIP-7702 transaction.
443
+ def unsigned_encoded
444
+ tx_data = []
445
+ tx_data.push Util.serialize_int_to_big_endian @chain_id
446
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
447
+ tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
448
+ tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
449
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
450
+ tx_data.push Util.hex_to_bin @destination
451
+ tx_data.push Util.serialize_int_to_big_endian @amount
452
+ tx_data.push Rlp::Sedes.binary.serialize @payload
453
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
454
+
455
+ authorization_list = @authorization_list.map { |authorization| authorization.raw }
456
+ tx_data.push Rlp::Sedes.infer(authorization_list).serialize authorization_list
457
+
458
+ tx_encoded = Rlp.encode tx_data
459
+
460
+ # create an EIP-2718 envelope with EIP-7702 type payload (unsigned)
461
+ tx_type = Util.serialize_int_to_big_endian @type
462
+
463
+ return "#{tx_type}#{tx_encoded}"
464
+ end
465
+
466
+ # Gets the sign-hash required to sign a raw transaction.
467
+ #
468
+ # @return [String] a Keccak-256 hash of an unsigned transaction.
469
+ def unsigned_hash
470
+ Util.keccak256 unsigned_encoded
471
+ end
472
+
473
+ private
474
+
475
+ def deserialize_authorizations(authorization_list)
476
+ authorization_list.map do |authorization_tuple|
477
+ chain_id = Util.deserialize_big_endian_to_int authorization_tuple[0]
478
+ address = Util.bin_to_hex authorization_tuple[1]
479
+ nonce = Util.deserialize_big_endian_to_int authorization_tuple[2]
480
+ recovery_id = Util.bin_to_hex(authorization_tuple[3]).to_i(16)
481
+ r = Util.bin_to_hex authorization_tuple[4]
482
+ s = Util.bin_to_hex authorization_tuple[5]
483
+ Authorization.new(chain_id: chain_id, address: address, nonce: nonce, recovery_id: recovery_id, r: r, s: s)
484
+ end
485
+ end
486
+
487
+ # Force-sets an existing signature of a decoded transaction.
488
+ def _set_signature(recovery_id, r, s)
489
+ @signature_y_parity = recovery_id
490
+ @signature_r = r
491
+ @signature_s = s
492
+ end
493
+ end
494
+ end
495
+ end
data/lib/eth/tx/legacy.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2016-2023 The Ruby-Eth Contributors
1
+ # Copyright (c) 2016-2025 The Ruby-Eth Contributors
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
data/lib/eth/tx.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2016-2023 The Ruby-Eth Contributors
1
+ # Copyright (c) 2016-2025 The Ruby-Eth Contributors
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ require "konstructor"
17
17
  require "eth/chain"
18
18
  require "eth/tx/eip1559"
19
19
  require "eth/tx/eip2930"
20
+ require "eth/tx/eip7702"
20
21
  require "eth/tx/legacy"
21
22
  require "eth/unit"
22
23
 
@@ -72,6 +73,12 @@ module Eth
72
73
  # The EIP-1559 transaction type is 2.
73
74
  TYPE_1559 = 0x02.freeze
74
75
 
76
+ # The EIP-4844 transaction type is 3.
77
+ TYPE_4844 = 0x03.freeze
78
+
79
+ # The EIP-7702 transaction type is 4.
80
+ TYPE_7702 = 0x04.freeze
81
+
75
82
  # The zero byte is 0x00.
76
83
  ZERO_BYTE = "\x00".freeze
77
84
 
@@ -84,12 +91,25 @@ module Eth
84
91
  # value, data, access_list)
85
92
  # - EIP-2930: chain_id, nonce, gas_price, gas_limit, access_list(, from, to,
86
93
  # value, data)
94
+ # - EIP-7702: chain_id, nonce, priority_fee, max_gas_fee, gas_limit, authorizations(, from, to,
95
+ # value, data, access_list)
87
96
  # - Legacy: nonce, gas_price, gas_limit(, from, to, value, data)
88
97
  #
89
98
  # @param params [Hash] all necessary transaction fields.
90
99
  # @param chain_id [Integer] the EIP-155 Chain ID (legacy transactions only).
91
100
  def new(params, chain_id = Chain::ETHEREUM)
92
101
 
102
+ # if we deal with blobs, attempt EIP-4844 (not implemented)
103
+ unless params[:max_fee_per_blob_gas].nil?
104
+ raise NotimplementedError, "EIP-4844 blob transactions are not implemented"
105
+ end
106
+
107
+ # if we deal with authorizations, attempt EIP-7702
108
+ unless params[:authorization_list].nil?
109
+ params[:chain_id] = chain_id if params[:chain_id].nil?
110
+ return Tx::Eip7702.new params
111
+ end
112
+
93
113
  # if we deal with max gas fee parameter, attempt EIP-1559
94
114
  unless params[:max_gas_fee].nil?
95
115
  params[:chain_id] = chain_id if params[:chain_id].nil?
@@ -115,6 +135,7 @@ module Eth
115
135
  def decode(hex)
116
136
  hex = Util.remove_hex_prefix hex
117
137
  type = hex[0, 2].to_i(16)
138
+
118
139
  case type
119
140
  when TYPE_1559
120
141
 
@@ -124,6 +145,14 @@ module Eth
124
145
 
125
146
  # EIP-2930 transaction (type 1)
126
147
  return Tx::Eip2930.decode hex
148
+ when TYPE_4844
149
+
150
+ # EIP-4844 transaction (type 3)
151
+ raise NotimplementedError, "EIP-4844 blob transactions are not implemented"
152
+ when TYPE_7702
153
+
154
+ # EIP-7702 transaction (type 4)
155
+ return Tx::Eip7702.decode hex
127
156
  else
128
157
 
129
158
  # Legacy transaction if first byte is RLP (>= 192)
@@ -150,6 +179,14 @@ module Eth
150
179
 
151
180
  # EIP-2930 transaction (type 1)
152
181
  return Tx::Eip2930.unsigned_copy tx
182
+ when TYPE_4844
183
+
184
+ # EIP-4844 transaction (type 3)
185
+ raise NotimplementedError, "EIP-4844 blob transactions are not implemented"
186
+ when TYPE_7702
187
+
188
+ # EIP-7702 transaction (type 4)
189
+ return Tx::Eip7702.unsigned_copy tx
153
190
  when TYPE_LEGACY
154
191
 
155
192
  # Legacy transaction ("type 0")
@@ -210,7 +247,9 @@ module Eth
210
247
  if fields[:nonce].nil? or fields[:nonce] < 0
211
248
  raise ParameterError, "Invalid signer nonce #{fields[:nonce]}!"
212
249
  end
213
- if fields[:gas_limit].nil? or fields[:gas_limit] < DEFAULT_GAS_LIMIT or fields[:gas_limit] > BLOCK_GAS_LIMIT
250
+ if fields[:gas_limit].nil? or
251
+ fields[:gas_limit] < DEFAULT_GAS_LIMIT or
252
+ (fields[:gas_limit] > BLOCK_GAS_LIMIT and fields[:chain_id] == Chain::ETHEREUM)
214
253
  raise ParameterError, "Invalid gas limit #{fields[:gas_limit]}!"
215
254
  end
216
255
  unless fields[:value] >= 0
@@ -239,6 +278,18 @@ module Eth
239
278
  return fields
240
279
  end
241
280
 
281
+ # Validates that the type-4 transaction field authorization list is present
282
+ #
283
+ # @param fields [Hash] the transaction fields.
284
+ # @return [Hash] the validated transaction fields.
285
+ # @raise [ParameterError] if authorization list is missing.
286
+ def validate_eip7702_params(fields)
287
+ unless fields[:authorization_list].nil? or fields[:authorization_list].is_a? Array
288
+ raise ParameterError, "Invalid authorization list #{fields[:authorization_list]}!"
289
+ end
290
+ return fields
291
+ end
292
+
242
293
  # Validates the common legacy transaction fields such as gas price.
243
294
  #
244
295
  # @param fields [Hash] the transaction fields.
data/lib/eth/unit.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2016-2023 The Ruby-Eth Contributors
1
+ # Copyright (c) 2016-2025 The Ruby-Eth Contributors
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
data/lib/eth/util.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2016-2023 The Ruby-Eth Contributors
1
+ # Copyright (c) 2016-2025 The Ruby-Eth Contributors
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ # -*- encoding : ascii-8bit -*-
16
+
15
17
  require "digest/keccak"
16
18
 
17
19
  # Provides the {Eth} module.
@@ -48,7 +50,7 @@ module Eth
48
50
  # @raise [TypeError] if value is not a string.
49
51
  def bin_to_hex(bin)
50
52
  raise TypeError, "Value must be an instance of String" unless bin.instance_of? String
51
- bin.unpack("H*").first
53
+ hex = bin.unpack("H*").first
52
54
  end
53
55
 
54
56
  # Packs a hexa-decimal string into a binary string. Also works with
@@ -61,7 +63,8 @@ module Eth
61
63
  raise TypeError, "Value must be an instance of String" unless hex.instance_of? String
62
64
  hex = remove_hex_prefix hex
63
65
  raise TypeError, "Non-hexadecimal digit found" unless hex? hex
64
- [hex].pack("H*")
66
+ hex = "0#{hex}" if hex.size % 2 != 0
67
+ bin = [hex].pack("H*")
65
68
  end
66
69
 
67
70
  # Prefixes a hexa-decimal string with `0x`.