eth 0.5.14 → 0.5.17

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.
@@ -0,0 +1,401 @@
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-4844 transactions utilizing EIP-2718
22
+ # types and envelopes.
23
+ # Ref: https://eips.ethereum.org/EIPS/eip-4844
24
+ class Eip4844
25
+
26
+ # The blob gas consumed by a single blob.
27
+ GAS_PER_BLOB = (2 ** 17).freeze
28
+
29
+ # The target blob gas per block as per EIP-7691.
30
+ TARGET_BLOB_GAS_PER_BLOCK = 786_432.freeze
31
+
32
+ # The maximum blob gas allowed in a block as per EIP-7691.
33
+ MAX_BLOB_GAS_PER_BLOCK = 1_179_648.freeze
34
+
35
+ # The maximum number of blobs permitted in a single block.
36
+ MAX_BLOBS_PER_BLOCK = (MAX_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB).freeze
37
+
38
+ # The EIP-155 Chain ID.
39
+ # Ref: https://eips.ethereum.org/EIPS/eip-155
40
+ attr_reader :chain_id
41
+
42
+ # The transaction nonce provided by the signer.
43
+ attr_reader :signer_nonce
44
+
45
+ # The transaction max priority fee per gas in Wei.
46
+ attr_reader :max_priority_fee_per_gas
47
+
48
+ # The transaction max fee per gas in Wei.
49
+ attr_reader :max_fee_per_gas
50
+
51
+ # The gas limit for the transaction.
52
+ attr_reader :gas_limit
53
+
54
+ # The recipient address.
55
+ attr_reader :destination
56
+
57
+ # The transaction amount in Wei.
58
+ attr_reader :amount
59
+
60
+ # The transaction data payload.
61
+ attr_reader :payload
62
+
63
+ # An optional EIP-2930 access list.
64
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
65
+ attr_reader :access_list
66
+
67
+ # The transaction max fee per blob gas in Wei.
68
+ attr_reader :max_fee_per_blob_gas
69
+
70
+ # The list of KZG commitment versioned hashes.
71
+ attr_reader :blob_versioned_hashes
72
+
73
+ # The signature's y-parity byte (not v).
74
+ attr_reader :signature_y_parity
75
+
76
+ # The signature `r` value.
77
+ attr_reader :signature_r
78
+
79
+ # The signature `s` value.
80
+ attr_reader :signature_s
81
+
82
+ # The sender address.
83
+ attr_reader :sender
84
+
85
+ # The transaction type.
86
+ attr_reader :type
87
+
88
+ # Create a type-3 (EIP-4844) transaction payload object that
89
+ # can be prepared for envelope, signature and broadcast.
90
+ # Ref: https://eips.ethereum.org/EIPS/eip-4844
91
+ #
92
+ # @param params [Hash] all necessary transaction fields.
93
+ # @option params [Integer] :chain_id the chain ID.
94
+ # @option params [Integer] :nonce the signer nonce.
95
+ # @option params [Integer] :priority_fee the max priority fee per gas.
96
+ # @option params [Integer] :max_gas_fee the max transaction fee per gas.
97
+ # @option params [Integer] :gas_limit the gas limit.
98
+ # @option params [Eth::Address] :from the sender address.
99
+ # @option params [Eth::Address] :to the receiver address.
100
+ # @option params [Integer] :value the transaction value.
101
+ # @option params [String] :data the transaction data payload.
102
+ # @option params [Array] :access_list an optional access list.
103
+ # @option params [Integer] :max_fee_per_blob_gas the max blob fee per gas.
104
+ # @option params [Array] :blob_versioned_hashes the blob versioned hashes (max 9).
105
+ # @raise [ParameterError] if gas limit is too low.
106
+ def initialize(params)
107
+ fields = { recovery_id: nil, r: 0, s: 0 }.merge params
108
+
109
+ # populate optional fields with serializable empty values
110
+ fields[:chain_id] = Tx.sanitize_chain fields[:chain_id]
111
+ fields[:from] = Tx.sanitize_address fields[:from]
112
+ fields[:to] = Tx.sanitize_address fields[:to]
113
+ fields[:value] = Tx.sanitize_amount fields[:value]
114
+ fields[:data] = Tx.sanitize_data fields[:data]
115
+
116
+ # ensure sane values for all mandatory fields
117
+ fields = Tx.validate_params fields
118
+ fields = Tx.validate_eip1559_params fields
119
+ fields = Tx.validate_eip4844_params fields
120
+ fields[:access_list] = Tx.sanitize_list fields[:access_list]
121
+ fields[:blob_versioned_hashes] = Tx.sanitize_hashes fields[:blob_versioned_hashes]
122
+
123
+ # ensure gas limit is not too low
124
+ minimum_cost = Tx.estimate_intrinsic_gas fields[:data], fields[:access_list]
125
+ raise ParameterError, "Transaction gas limit is too low, try #{minimum_cost}!" if fields[:gas_limit].to_i < minimum_cost
126
+
127
+ # populate class attributes
128
+ @signer_nonce = fields[:nonce].to_i
129
+ @max_priority_fee_per_gas = fields[:priority_fee].to_i
130
+ @max_fee_per_gas = fields[:max_gas_fee].to_i
131
+ @gas_limit = fields[:gas_limit].to_i
132
+ @sender = fields[:from].to_s
133
+ @destination = fields[:to].to_s
134
+ @amount = fields[:value].to_i
135
+ @payload = fields[:data]
136
+ @access_list = fields[:access_list]
137
+ @max_fee_per_blob_gas = fields[:max_fee_per_blob_gas].to_i
138
+ @blob_versioned_hashes = fields[:blob_versioned_hashes]
139
+
140
+ # the signature v is set to the chain id for unsigned transactions
141
+ @signature_y_parity = fields[:recovery_id]
142
+ @chain_id = fields[:chain_id]
143
+
144
+ # the signature fields are empty for unsigned transactions.
145
+ @signature_r = fields[:r]
146
+ @signature_s = fields[:s]
147
+
148
+ # last but not least, set the type.
149
+ @type = TYPE_4844
150
+ end
151
+
152
+ # Overloads the constructor for decoding raw transactions and creating unsigned copies.
153
+ konstructor :decode, :unsigned_copy
154
+
155
+ # Decodes a raw transaction hex into an {Eth::Tx::Eip4844}
156
+ # transaction object.
157
+ #
158
+ # @param hex [String] the raw transaction hex-string.
159
+ # @return [Eth::Tx::Eip4844] transaction payload.
160
+ # @raise [TransactionTypeError] if transaction type is invalid.
161
+ # @raise [ParameterError] if transaction is missing fields.
162
+ # @raise [DecoderError] if transaction decoding fails.
163
+ def decode(hex)
164
+ hex = Util.remove_hex_prefix hex
165
+ type = hex[0, 2]
166
+ raise TransactionTypeError, "Invalid transaction type #{type}!" if type.to_i(16) != TYPE_4844
167
+
168
+ bin = Util.hex_to_bin hex[2..]
169
+ tx = Rlp.decode bin
170
+
171
+ # decoded transactions always have 11 + 3 fields, even if they are empty or zero
172
+ raise ParameterError, "Transaction missing fields!" if tx.size < 11
173
+
174
+ # populate the 11 payload fields
175
+ chain_id = Util.deserialize_rlp_int tx[0]
176
+ nonce = Util.deserialize_rlp_int tx[1]
177
+ priority_fee = Util.deserialize_rlp_int tx[2]
178
+ max_gas_fee = Util.deserialize_rlp_int tx[3]
179
+ gas_limit = Util.deserialize_rlp_int tx[4]
180
+ to = Util.bin_to_hex tx[5]
181
+ value = Util.deserialize_rlp_int tx[6]
182
+ data = tx[7]
183
+ access_list = tx[8]
184
+ max_fee_per_blob_gas = Util.deserialize_rlp_int tx[9]
185
+ blob_versioned_hashes = tx[10]
186
+
187
+ # populate class attributes
188
+ @chain_id = chain_id.to_i
189
+ @signer_nonce = nonce.to_i
190
+ @max_priority_fee_per_gas = priority_fee.to_i
191
+ @max_fee_per_gas = max_gas_fee.to_i
192
+ @gas_limit = gas_limit.to_i
193
+ @destination = to.to_s
194
+ @amount = value.to_i
195
+ @payload = data
196
+ @access_list = access_list
197
+ @max_fee_per_blob_gas = max_fee_per_blob_gas.to_i
198
+ @blob_versioned_hashes = blob_versioned_hashes
199
+
200
+ # populate the 3 signature fields
201
+ if tx.size == 11
202
+ _set_signature(nil, 0, 0)
203
+ elsif tx.size == 14
204
+ recovery_id = Util.bin_to_hex(tx[11]).to_i(16)
205
+ r = Util.bin_to_hex tx[12]
206
+ s = Util.bin_to_hex tx[13]
207
+
208
+ # allows us to force-setting a signature if the transaction is signed already
209
+ _set_signature(recovery_id, r, s)
210
+ else
211
+ raise DecoderError, "Cannot decode EIP-4844 payload!"
212
+ end
213
+
214
+ # last but not least, set the type.
215
+ @type = TYPE_4844
216
+
217
+ unless recovery_id.nil?
218
+ # recover sender address
219
+ v = Chain.to_v recovery_id, chain_id
220
+ public_key = Signature.recover(unsigned_hash, "#{r.rjust(64, "0")}#{s.rjust(64, "0")}#{v.to_s(16)}", chain_id)
221
+ address = Util.public_key_to_address(public_key).to_s
222
+ @sender = Tx.sanitize_address address
223
+ else
224
+ # keep the 'from' field blank
225
+ @sender = Tx.sanitize_address nil
226
+ end
227
+ end
228
+
229
+ # Creates an unsigned copy of a transaction payload.
230
+ #
231
+ # @param tx [Eth::Tx::Eip4844] an EIP-4844 transaction payload.
232
+ # @return [Eth::Tx::Eip4844] an unsigned EIP-4844 transaction payload.
233
+ # @raise [TransactionTypeError] if transaction type does not match.
234
+ def unsigned_copy(tx)
235
+
236
+ # not checking transaction validity unless it's of a different class
237
+ raise TransactionTypeError, "Cannot copy transaction of different payload type!" unless tx.instance_of? Tx::Eip4844
238
+
239
+ # populate class attributes
240
+ @signer_nonce = tx.signer_nonce
241
+ @max_priority_fee_per_gas = tx.max_priority_fee_per_gas
242
+ @max_fee_per_gas = tx.max_fee_per_gas
243
+ @gas_limit = tx.gas_limit
244
+ @destination = tx.destination
245
+ @amount = tx.amount
246
+ @payload = tx.payload
247
+ @access_list = tx.access_list
248
+ @max_fee_per_blob_gas = tx.max_fee_per_blob_gas
249
+ @blob_versioned_hashes = tx.blob_versioned_hashes
250
+ @chain_id = tx.chain_id
251
+
252
+ # force-set signature to unsigned
253
+ _set_signature(nil, 0, 0)
254
+
255
+ # keep the 'from' field blank
256
+ @sender = Tx.sanitize_address nil
257
+
258
+ # last but not least, set the type.
259
+ @type = TYPE_4844
260
+ end
261
+
262
+ # Sign the transaction with a given key.
263
+ #
264
+ # @param key [Eth::Key] the key-pair to use for signing.
265
+ # @return [String] a transaction hash.
266
+ # @raise [Signature::SignatureError] if transaction is already signed.
267
+ # @raise [Signature::SignatureError] if sender address does not match signing key.
268
+ def sign(key)
269
+ if Tx.signed? self
270
+ raise Signature::SignatureError, "Transaction is already signed!"
271
+ end
272
+
273
+ # ensure the sender address matches the given key
274
+ unless @sender.nil? or sender.empty?
275
+ signer_address = Tx.sanitize_address key.address.to_s
276
+ from_address = Tx.sanitize_address @sender
277
+ raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
278
+ end
279
+
280
+ # sign a keccak hash of the unsigned, encoded transaction
281
+ signature = key.sign(unsigned_hash, @chain_id)
282
+ r, s, v = Signature.dissect signature
283
+ recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
284
+ @signature_y_parity = recovery_id
285
+ @signature_r = r
286
+ @signature_s = s
287
+ return hash
288
+ end
289
+
290
+ # Signs the transaction with a provided signature blob.
291
+ #
292
+ # @param signature [String] the concatenated `r`, `s`, and `v` values.
293
+ # @return [String] a transaction hash.
294
+ # @raise [Signature::SignatureError] if transaction is already signed.
295
+ # @raise [Signature::SignatureError] if sender address does not match signer.
296
+ def sign_with(signature)
297
+ if Tx.signed? self
298
+ raise Signature::SignatureError, "Transaction is already signed!"
299
+ end
300
+
301
+ # ensure the sender address matches the signature
302
+ unless @sender.nil? or sender.empty?
303
+ public_key = Signature.recover(unsigned_hash, signature, @chain_id)
304
+ signer_address = Tx.sanitize_address Util.public_key_to_address(public_key).to_s
305
+ from_address = Tx.sanitize_address @sender
306
+ raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
307
+ end
308
+
309
+ r, s, v = Signature.dissect signature
310
+ recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
311
+ send :_set_signature, recovery_id, r, s
312
+ return hash
313
+ end
314
+
315
+ # Encodes a raw transaction object, wraps it in an EIP-2718 envelope
316
+ # with an EIP-4844 type prefix.
317
+ #
318
+ # @return [String] a raw, RLP-encoded EIP-4844 type transaction object.
319
+ # @raise [Signature::SignatureError] if the transaction is not yet signed.
320
+ def encoded
321
+ unless Tx.signed? self
322
+ raise Signature::SignatureError, "Transaction is not signed!"
323
+ end
324
+ tx_data = []
325
+ tx_data.push Util.serialize_int_to_big_endian @chain_id
326
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
327
+ tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
328
+ tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
329
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
330
+ tx_data.push Util.hex_to_bin @destination
331
+ tx_data.push Util.serialize_int_to_big_endian @amount
332
+ tx_data.push Rlp::Sedes.binary.serialize @payload
333
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
334
+ tx_data.push Util.serialize_int_to_big_endian @max_fee_per_blob_gas
335
+ tx_data.push Rlp::Sedes.infer(@blob_versioned_hashes).serialize @blob_versioned_hashes
336
+ tx_data.push Util.serialize_int_to_big_endian @signature_y_parity
337
+ tx_data.push Util.serialize_int_to_big_endian @signature_r
338
+ tx_data.push Util.serialize_int_to_big_endian @signature_s
339
+ tx_encoded = Rlp.encode tx_data
340
+
341
+ # create an EIP-2718 envelope with EIP-4844 type payload
342
+ tx_type = Util.serialize_int_to_big_endian @type
343
+ return "#{tx_type}#{tx_encoded}"
344
+ end
345
+
346
+ # Gets the encoded, enveloped, raw transaction hex.
347
+ #
348
+ # @return [String] the raw transaction hex.
349
+ def hex
350
+ Util.bin_to_hex encoded
351
+ end
352
+
353
+ # Gets the transaction hash.
354
+ #
355
+ # @return [String] the transaction hash.
356
+ def hash
357
+ Util.bin_to_hex Util.keccak256 encoded
358
+ end
359
+
360
+ # Encodes the unsigned transaction payload in an EIP-4844 envelope,
361
+ # required for signing.
362
+ #
363
+ # @return [String] an RLP-encoded, unsigned, enveloped EIP-4844 transaction.
364
+ def unsigned_encoded
365
+ tx_data = []
366
+ tx_data.push Util.serialize_int_to_big_endian @chain_id
367
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
368
+ tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
369
+ tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
370
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
371
+ tx_data.push Util.hex_to_bin @destination
372
+ tx_data.push Util.serialize_int_to_big_endian @amount
373
+ tx_data.push Rlp::Sedes.binary.serialize @payload
374
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
375
+ tx_data.push Util.serialize_int_to_big_endian @max_fee_per_blob_gas
376
+ tx_data.push Rlp::Sedes.infer(@blob_versioned_hashes).serialize @blob_versioned_hashes
377
+ tx_encoded = Rlp.encode tx_data
378
+
379
+ # create an EIP-2718 envelope with EIP-4844 type payload (unsigned)
380
+ tx_type = Util.serialize_int_to_big_endian @type
381
+ return "#{tx_type}#{tx_encoded}"
382
+ end
383
+
384
+ # Gets the sign-hash required to sign a raw transaction.
385
+ #
386
+ # @return [String] a Keccak-256 hash of an unsigned transaction.
387
+ def unsigned_hash
388
+ Util.keccak256 unsigned_encoded
389
+ end
390
+
391
+ private
392
+
393
+ # Force-sets an existing signature of a decoded transaction.
394
+ def _set_signature(recovery_id, r, s)
395
+ @signature_y_parity = recovery_id
396
+ @signature_r = r
397
+ @signature_s = s
398
+ end
399
+ end
400
+ end
401
+ end
@@ -196,7 +196,7 @@ module Eth
196
196
  # @option params [Integer] :max_gas_fee the max transaction fee per gas.
197
197
  # @option params [Integer] :gas_limit the gas limit.
198
198
  # @option params [Eth::Address] :from the sender address.
199
- # @option params [Eth::Address] :to the reciever address.
199
+ # @option params [Eth::Address] :to the receiver address.
200
200
  # @option params [Integer] :value the transaction value.
201
201
  # @option params [String] :data the transaction data payload.
202
202
  # @option params [Array] :access_list an optional access list.
@@ -269,13 +269,13 @@ module Eth
269
269
  raise ParameterError, "Transaction missing fields!" if tx.size < 10
270
270
 
271
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]
272
+ chain_id = Util.deserialize_rlp_int tx[0]
273
+ nonce = Util.deserialize_rlp_int tx[1]
274
+ priority_fee = Util.deserialize_rlp_int tx[2]
275
+ max_gas_fee = Util.deserialize_rlp_int tx[3]
276
+ gas_limit = Util.deserialize_rlp_int tx[4]
277
277
  to = Util.bin_to_hex tx[5]
278
- value = Util.deserialize_big_endian_to_int tx[6]
278
+ value = Util.deserialize_rlp_int tx[6]
279
279
  data = tx[7]
280
280
  access_list = tx[8]
281
281
  authorization_list = tx[9]
@@ -389,6 +389,31 @@ module Eth
389
389
  return hash
390
390
  end
391
391
 
392
+ # Signs the transaction with a provided signature blob.
393
+ #
394
+ # @param signature [String] the concatenated `r`, `s`, and `v` values.
395
+ # @return [String] a transaction hash.
396
+ # @raise [Signature::SignatureError] if transaction is already signed.
397
+ # @raise [Signature::SignatureError] if sender address does not match signer.
398
+ def sign_with(signature)
399
+ if Tx.signed? self
400
+ raise Signature::SignatureError, "Transaction is already signed!"
401
+ end
402
+
403
+ # ensure the sender address matches the signature
404
+ unless @sender.nil? or sender.empty?
405
+ public_key = Signature.recover(unsigned_hash, signature, @chain_id)
406
+ signer_address = Tx.sanitize_address Util.public_key_to_address(public_key).to_s
407
+ from_address = Tx.sanitize_address @sender
408
+ raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
409
+ end
410
+
411
+ r, s, v = Signature.dissect signature
412
+ recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
413
+ send :_set_signature, recovery_id, r, s
414
+ return hash
415
+ end
416
+
392
417
  # Encodes a raw transaction object, wraps it in an EIP-2718 envelope
393
418
  # with an EIP-7702 type prefix.
394
419
  #
@@ -474,9 +499,9 @@ module Eth
474
499
 
475
500
  def deserialize_authorizations(authorization_list)
476
501
  authorization_list.map do |authorization_tuple|
477
- chain_id = Util.deserialize_big_endian_to_int authorization_tuple[0]
502
+ chain_id = Util.deserialize_rlp_int authorization_tuple[0]
478
503
  address = Util.bin_to_hex authorization_tuple[1]
479
- nonce = Util.deserialize_big_endian_to_int authorization_tuple[2]
504
+ nonce = Util.deserialize_rlp_int authorization_tuple[2]
480
505
  recovery_id = Util.bin_to_hex(authorization_tuple[3]).to_i(16)
481
506
  r = Util.bin_to_hex authorization_tuple[4]
482
507
  s = Util.bin_to_hex authorization_tuple[5]
data/lib/eth/tx/legacy.rb CHANGED
@@ -68,7 +68,7 @@ module Eth
68
68
  # @option params [Integer] :gas_price the gas price.
69
69
  # @option params [Integer] :gas_limit the gas limit.
70
70
  # @option params [Eth::Address] :from the sender address.
71
- # @option params [Eth::Address] :to the reciever address.
71
+ # @option params [Eth::Address] :to the receiver address.
72
72
  # @option params [Integer] :value the transaction value.
73
73
  # @option params [String] :data the transaction data payload.
74
74
  # @param chain_id [Integer] the EIP-155 Chain ID.
@@ -128,18 +128,18 @@ module Eth
128
128
  raise ParameterError, "Transaction missing fields!" if tx.size < 9
129
129
 
130
130
  # populate the 9 fields
131
- nonce = Util.deserialize_big_endian_to_int tx[0]
132
- gas_price = Util.deserialize_big_endian_to_int tx[1]
133
- gas_limit = Util.deserialize_big_endian_to_int tx[2]
131
+ nonce = Util.deserialize_rlp_int tx[0]
132
+ gas_price = Util.deserialize_rlp_int tx[1]
133
+ gas_limit = Util.deserialize_rlp_int tx[2]
134
134
  to = Util.bin_to_hex tx[3]
135
- value = Util.deserialize_big_endian_to_int tx[4]
135
+ value = Util.deserialize_rlp_int tx[4]
136
136
  data = tx[5]
137
137
  v = Util.bin_to_hex tx[6]
138
138
  r = Util.bin_to_hex tx[7]
139
139
  s = Util.bin_to_hex tx[8]
140
140
 
141
141
  # try to recover the chain id from v
142
- chain_id = Chain.to_chain_id Util.deserialize_big_endian_to_int tx[6]
142
+ chain_id = Chain.to_chain_id Util.deserialize_rlp_int tx[6]
143
143
 
144
144
  # populate class attributes
145
145
  @signer_nonce = nonce.to_i
@@ -223,6 +223,30 @@ module Eth
223
223
  return hash
224
224
  end
225
225
 
226
+ # Signs the transaction with a provided signature blob.
227
+ #
228
+ # @param signature [String] the concatenated `r`, `s`, and `v` values.
229
+ # @return [String] a transaction hash.
230
+ # @raise [Signature::SignatureError] if transaction is already signed.
231
+ # @raise [Signature::SignatureError] if sender address does not match signer.
232
+ def sign_with(signature)
233
+ if Tx.signed? self
234
+ raise Signature::SignatureError, "Transaction is already signed!"
235
+ end
236
+
237
+ # ensure the sender address matches the signature
238
+ unless @sender.nil? or sender.empty?
239
+ public_key = Signature.recover(unsigned_hash, signature, @chain_id)
240
+ signer_address = Tx.sanitize_address Util.public_key_to_address(public_key).to_s
241
+ from_address = Tx.sanitize_address @sender
242
+ raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
243
+ end
244
+
245
+ r, s, v = Signature.dissect signature
246
+ send :_set_signature, v, r, s
247
+ return hash
248
+ end
249
+
226
250
  # Encodes a raw transaction object.
227
251
  #
228
252
  # @return [String] a raw, RLP-encoded legacy transaction.
data/lib/eth/tx.rb CHANGED
@@ -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/eip4844"
20
21
  require "eth/tx/eip7702"
21
22
  require "eth/tx/legacy"
22
23
  require "eth/unit"
@@ -87,6 +88,8 @@ module Eth
87
88
 
88
89
  # Creates a new transaction of any type for given parameters and chain ID.
89
90
  # Required parameters are (optional in brackets):
91
+ # - EIP-4844: chain_id, nonce, priority_fee, max_gas_fee, gas_limit, max_fee_per_blob_gas, blob_versioned_hashes(, from, to,
92
+ # value, data, access_list)
90
93
  # - EIP-1559: chain_id, nonce, priority_fee, max_gas_fee, gas_limit(, from, to,
91
94
  # value, data, access_list)
92
95
  # - EIP-2930: chain_id, nonce, gas_price, gas_limit, access_list(, from, to,
@@ -99,9 +102,10 @@ module Eth
99
102
  # @param chain_id [Integer] the EIP-155 Chain ID (legacy transactions only).
100
103
  def new(params, chain_id = Chain::ETHEREUM)
101
104
 
102
- # if we deal with blobs, attempt EIP-4844 (not implemented)
105
+ # if we deal with blobs, attempt EIP-4844
103
106
  unless params[:max_fee_per_blob_gas].nil?
104
- raise NotimplementedError, "EIP-4844 blob transactions are not implemented"
107
+ params[:chain_id] = chain_id if params[:chain_id].nil?
108
+ return Tx::Eip4844.new params
105
109
  end
106
110
 
107
111
  # if we deal with authorizations, attempt EIP-7702
@@ -148,7 +152,7 @@ module Eth
148
152
  when TYPE_4844
149
153
 
150
154
  # EIP-4844 transaction (type 3)
151
- raise NotimplementedError, "EIP-4844 blob transactions are not implemented"
155
+ return Tx::Eip4844.decode hex
152
156
  when TYPE_7702
153
157
 
154
158
  # EIP-7702 transaction (type 4)
@@ -182,7 +186,7 @@ module Eth
182
186
  when TYPE_4844
183
187
 
184
188
  # EIP-4844 transaction (type 3)
185
- raise NotimplementedError, "EIP-4844 blob transactions are not implemented"
189
+ return Tx::Eip4844.unsigned_copy tx
186
190
  when TYPE_7702
187
191
 
188
192
  # EIP-7702 transaction (type 4)
@@ -278,6 +282,27 @@ module Eth
278
282
  return fields
279
283
  end
280
284
 
285
+ # Validates that the type-3 transaction blob fields are present
286
+ #
287
+ # @param fields [Hash] the transaction fields.
288
+ # @return [Hash] the validated transaction fields.
289
+ # @raise [ParameterError] if max blob fee or blob hashes are invalid or exceed limits.
290
+ def validate_eip4844_params(fields)
291
+ if fields[:max_fee_per_blob_gas].nil? or fields[:max_fee_per_blob_gas] < 0
292
+ raise ParameterError, "Invalid max blob fee #{fields[:max_fee_per_blob_gas]}!"
293
+ end
294
+ if fields[:blob_versioned_hashes].nil? or !fields[:blob_versioned_hashes].is_a? Array or fields[:blob_versioned_hashes].empty?
295
+ raise ParameterError, "Invalid blob versioned hashes #{fields[:blob_versioned_hashes]}!"
296
+ end
297
+ if fields[:blob_versioned_hashes].length > Eip4844::MAX_BLOBS_PER_BLOCK
298
+ raise ParameterError, "Too many blob versioned hashes #{fields[:blob_versioned_hashes].length}!"
299
+ end
300
+ if fields[:to].nil? or fields[:to].empty?
301
+ raise ParameterError, "Invalid destination address #{fields[:to]}!"
302
+ end
303
+ return fields
304
+ end
305
+
281
306
  # Validates that the type-4 transaction field authorization list is present
282
307
  #
283
308
  # @param fields [Hash] the transaction fields.
@@ -372,6 +397,22 @@ module Eth
372
397
  return list
373
398
  end
374
399
 
400
+ # Populates the blob versioned hashes field with a serializable empty
401
+ # array in case it is undefined; also ensures the hashes are binary
402
+ # not hex.
403
+ #
404
+ # @param list [Array] the blob versioned hashes.
405
+ # @return [Array] the sanitized blob versioned hashes.
406
+ def sanitize_hashes(list)
407
+ list = [] if list.nil?
408
+ list.each_with_index do |value, index|
409
+ if Util.hex? value
410
+ list[index] = Util.hex_to_bin value
411
+ end
412
+ end
413
+ return list
414
+ end
415
+
375
416
  # Allows to check wether a transaction is signed already.
376
417
  #
377
418
  # @return [Bool] true if transaction is already signed.