eth 0.5.13 → 0.5.15
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.
- checksums.yaml +4 -4
- data/.github/workflows/codeql.yml +1 -1
- data/.github/workflows/docs.yml +2 -2
- data/.github/workflows/spec.yml +1 -3
- data/CHANGELOG.md +33 -0
- data/CODE_OF_CONDUCT.md +3 -5
- data/Gemfile +3 -3
- data/LICENSE.txt +1 -1
- data/README.md +6 -6
- data/SECURITY.md +2 -2
- data/eth.gemspec +4 -1
- data/lib/eth/abi/decoder.rb +18 -7
- data/lib/eth/abi/encoder.rb +14 -26
- data/lib/eth/abi/event.rb +5 -1
- data/lib/eth/abi/function.rb +124 -0
- data/lib/eth/abi/packed/encoder.rb +196 -0
- data/lib/eth/abi/type.rb +77 -16
- data/lib/eth/abi.rb +29 -2
- data/lib/eth/address.rb +3 -1
- data/lib/eth/api.rb +1 -1
- data/lib/eth/chain.rb +9 -1
- data/lib/eth/client/http.rb +7 -3
- data/lib/eth/client/ipc.rb +1 -1
- data/lib/eth/client.rb +38 -37
- data/lib/eth/constant.rb +1 -1
- data/lib/eth/contract/error.rb +62 -0
- data/lib/eth/contract/event.rb +69 -16
- data/lib/eth/contract/function.rb +22 -1
- data/lib/eth/contract/function_input.rb +1 -1
- data/lib/eth/contract/function_output.rb +12 -4
- data/lib/eth/contract/initializer.rb +1 -1
- data/lib/eth/contract.rb +56 -5
- data/lib/eth/eip712.rb +49 -13
- data/lib/eth/ens/coin_type.rb +1 -1
- data/lib/eth/ens/resolver.rb +1 -1
- data/lib/eth/ens.rb +1 -1
- data/lib/eth/key/decrypter.rb +1 -1
- data/lib/eth/key/encrypter.rb +1 -1
- data/lib/eth/key.rb +2 -2
- data/lib/eth/rlp/decoder.rb +1 -1
- data/lib/eth/rlp/encoder.rb +1 -1
- data/lib/eth/rlp/sedes/big_endian_int.rb +1 -1
- data/lib/eth/rlp/sedes/binary.rb +1 -1
- data/lib/eth/rlp/sedes/list.rb +1 -1
- data/lib/eth/rlp/sedes.rb +1 -1
- data/lib/eth/rlp.rb +1 -1
- data/lib/eth/signature.rb +1 -1
- data/lib/eth/solidity.rb +1 -1
- data/lib/eth/tx/eip1559.rb +33 -8
- data/lib/eth/tx/eip2930.rb +32 -7
- data/lib/eth/tx/eip4844.rb +389 -0
- data/lib/eth/tx/eip7702.rb +520 -0
- data/lib/eth/tx/legacy.rb +31 -7
- data/lib/eth/tx.rb +88 -1
- data/lib/eth/unit.rb +1 -1
- data/lib/eth/util.rb +20 -8
- data/lib/eth/version.rb +2 -2
- data/lib/eth.rb +1 -1
- metadata +26 -16
@@ -0,0 +1,520 @@
|
|
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 receiver 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_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
|
+
to = Util.bin_to_hex tx[5]
|
278
|
+
value = Util.deserialize_rlp_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
|
+
# 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
|
+
|
417
|
+
# Encodes a raw transaction object, wraps it in an EIP-2718 envelope
|
418
|
+
# with an EIP-7702 type prefix.
|
419
|
+
#
|
420
|
+
# @return [String] a raw, RLP-encoded EIP-7702 type transaction object.
|
421
|
+
# @raise [Signature::SignatureError] if the transaction is not yet signed.
|
422
|
+
def encoded
|
423
|
+
unless Tx.signed? self
|
424
|
+
raise Signature::SignatureError, "Transaction is not signed!"
|
425
|
+
end
|
426
|
+
tx_data = []
|
427
|
+
tx_data.push Util.serialize_int_to_big_endian @chain_id
|
428
|
+
tx_data.push Util.serialize_int_to_big_endian @signer_nonce
|
429
|
+
tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
|
430
|
+
tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
|
431
|
+
tx_data.push Util.serialize_int_to_big_endian @gas_limit
|
432
|
+
tx_data.push Util.hex_to_bin @destination
|
433
|
+
tx_data.push Util.serialize_int_to_big_endian @amount
|
434
|
+
tx_data.push Rlp::Sedes.binary.serialize @payload
|
435
|
+
tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
|
436
|
+
|
437
|
+
authorization_list = @authorization_list.map { |authorization| authorization.raw }
|
438
|
+
tx_data.push Rlp::Sedes.infer(authorization_list).serialize authorization_list
|
439
|
+
|
440
|
+
tx_data.push Util.serialize_int_to_big_endian @signature_y_parity
|
441
|
+
tx_data.push Util.serialize_int_to_big_endian @signature_r
|
442
|
+
tx_data.push Util.serialize_int_to_big_endian @signature_s
|
443
|
+
tx_encoded = Rlp.encode tx_data
|
444
|
+
|
445
|
+
# create an EIP-2718 envelope with EIP-7702 type payload
|
446
|
+
tx_type = Util.serialize_int_to_big_endian @type
|
447
|
+
return "#{tx_type}#{tx_encoded}"
|
448
|
+
end
|
449
|
+
|
450
|
+
# Gets the encoded, enveloped, raw transaction hex.
|
451
|
+
#
|
452
|
+
# @return [String] the raw transaction hex.
|
453
|
+
def hex
|
454
|
+
Util.bin_to_hex encoded
|
455
|
+
end
|
456
|
+
|
457
|
+
# Gets the transaction hash.
|
458
|
+
#
|
459
|
+
# @return [String] the transaction hash.
|
460
|
+
def hash
|
461
|
+
Util.bin_to_hex Util.keccak256 encoded
|
462
|
+
end
|
463
|
+
|
464
|
+
# Encodes the unsigned transaction payload in an EIP-7702 envelope,
|
465
|
+
# required for signing.
|
466
|
+
#
|
467
|
+
# @return [String] an RLP-encoded, unsigned, enveloped EIP-7702 transaction.
|
468
|
+
def unsigned_encoded
|
469
|
+
tx_data = []
|
470
|
+
tx_data.push Util.serialize_int_to_big_endian @chain_id
|
471
|
+
tx_data.push Util.serialize_int_to_big_endian @signer_nonce
|
472
|
+
tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
|
473
|
+
tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
|
474
|
+
tx_data.push Util.serialize_int_to_big_endian @gas_limit
|
475
|
+
tx_data.push Util.hex_to_bin @destination
|
476
|
+
tx_data.push Util.serialize_int_to_big_endian @amount
|
477
|
+
tx_data.push Rlp::Sedes.binary.serialize @payload
|
478
|
+
tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
|
479
|
+
|
480
|
+
authorization_list = @authorization_list.map { |authorization| authorization.raw }
|
481
|
+
tx_data.push Rlp::Sedes.infer(authorization_list).serialize authorization_list
|
482
|
+
|
483
|
+
tx_encoded = Rlp.encode tx_data
|
484
|
+
|
485
|
+
# create an EIP-2718 envelope with EIP-7702 type payload (unsigned)
|
486
|
+
tx_type = Util.serialize_int_to_big_endian @type
|
487
|
+
|
488
|
+
return "#{tx_type}#{tx_encoded}"
|
489
|
+
end
|
490
|
+
|
491
|
+
# Gets the sign-hash required to sign a raw transaction.
|
492
|
+
#
|
493
|
+
# @return [String] a Keccak-256 hash of an unsigned transaction.
|
494
|
+
def unsigned_hash
|
495
|
+
Util.keccak256 unsigned_encoded
|
496
|
+
end
|
497
|
+
|
498
|
+
private
|
499
|
+
|
500
|
+
def deserialize_authorizations(authorization_list)
|
501
|
+
authorization_list.map do |authorization_tuple|
|
502
|
+
chain_id = Util.deserialize_rlp_int authorization_tuple[0]
|
503
|
+
address = Util.bin_to_hex authorization_tuple[1]
|
504
|
+
nonce = Util.deserialize_rlp_int authorization_tuple[2]
|
505
|
+
recovery_id = Util.bin_to_hex(authorization_tuple[3]).to_i(16)
|
506
|
+
r = Util.bin_to_hex authorization_tuple[4]
|
507
|
+
s = Util.bin_to_hex authorization_tuple[5]
|
508
|
+
Authorization.new(chain_id: chain_id, address: address, nonce: nonce, recovery_id: recovery_id, r: r, s: s)
|
509
|
+
end
|
510
|
+
end
|
511
|
+
|
512
|
+
# Force-sets an existing signature of a decoded transaction.
|
513
|
+
def _set_signature(recovery_id, r, s)
|
514
|
+
@signature_y_parity = recovery_id
|
515
|
+
@signature_r = r
|
516
|
+
@signature_s = s
|
517
|
+
end
|
518
|
+
end
|
519
|
+
end
|
520
|
+
end
|
data/lib/eth/tx/legacy.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2016-
|
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.
|
@@ -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
|
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.
|
132
|
-
gas_price = Util.
|
133
|
-
gas_limit = Util.
|
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.
|
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.
|
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.
|