eth 0.4.16 → 0.5.1

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 (51) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/codeql.yml +15 -5
  3. data/.github/workflows/docs.yml +26 -0
  4. data/.github/workflows/spec.yml +52 -0
  5. data/.gitignore +24 -24
  6. data/.gitmodules +3 -3
  7. data/.yardopts +1 -0
  8. data/AUTHORS.txt +21 -0
  9. data/CHANGELOG.md +65 -10
  10. data/Gemfile +13 -2
  11. data/LICENSE.txt +202 -22
  12. data/README.md +199 -74
  13. data/bin/console +4 -4
  14. data/bin/setup +5 -4
  15. data/eth.gemspec +34 -29
  16. data/lib/eth/abi/type.rb +178 -0
  17. data/lib/eth/abi.rb +396 -0
  18. data/lib/eth/address.rb +55 -11
  19. data/lib/eth/api.rb +223 -0
  20. data/lib/eth/chain.rb +151 -0
  21. data/lib/eth/client/http.rb +63 -0
  22. data/lib/eth/client/ipc.rb +47 -0
  23. data/lib/eth/client.rb +232 -0
  24. data/lib/eth/constant.rb +71 -0
  25. data/lib/eth/eip712.rb +184 -0
  26. data/lib/eth/key/decrypter.rb +121 -88
  27. data/lib/eth/key/encrypter.rb +178 -99
  28. data/lib/eth/key.rb +136 -48
  29. data/lib/eth/rlp/decoder.rb +109 -0
  30. data/lib/eth/rlp/encoder.rb +78 -0
  31. data/lib/eth/rlp/sedes/big_endian_int.rb +66 -0
  32. data/lib/eth/rlp/sedes/binary.rb +97 -0
  33. data/lib/eth/rlp/sedes/list.rb +84 -0
  34. data/lib/eth/rlp/sedes.rb +74 -0
  35. data/lib/eth/rlp.rb +63 -0
  36. data/lib/eth/signature.rb +163 -0
  37. data/lib/eth/tx/eip1559.rb +336 -0
  38. data/lib/eth/tx/eip2930.rb +328 -0
  39. data/lib/eth/tx/legacy.rb +296 -0
  40. data/lib/eth/tx.rb +273 -143
  41. data/lib/eth/unit.rb +49 -0
  42. data/lib/eth/util.rb +235 -0
  43. data/lib/eth/version.rb +18 -1
  44. data/lib/eth.rb +33 -67
  45. metadata +50 -85
  46. data/.github/workflows/build.yml +0 -26
  47. data/lib/eth/gas.rb +0 -9
  48. data/lib/eth/open_ssl.rb +0 -264
  49. data/lib/eth/secp256k1.rb +0 -7
  50. data/lib/eth/sedes.rb +0 -40
  51. data/lib/eth/utils.rb +0 -130
@@ -0,0 +1,74 @@
1
+ # Copyright (c) 2016-2022 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
+ # -*- encoding : ascii-8bit -*-
16
+
17
+ require "eth/rlp/sedes/big_endian_int"
18
+ require "eth/rlp/sedes/binary"
19
+ require "eth/rlp/sedes/list"
20
+
21
+ # Provides the {Eth} module.
22
+ module Eth
23
+
24
+ # Provides an recursive-length prefix (RLP) encoder and decoder.
25
+ module Rlp
26
+
27
+ # Provides serializable and deserializable types (SeDes).
28
+ module Sedes
29
+
30
+ # Provides a singleton {Eth::Rlp::Sedes} class to infer objects and types.
31
+ class << self
32
+
33
+ # Tries to find a sedes objects suitable for a given Ruby object.
34
+ #
35
+ # The sedes objects considered are `obj`'s class, {big_endian_int} and
36
+ # {binary}. If `obj` is a list, an {Eth::Rlp::Sedes::List} will be
37
+ # constructed recursively.
38
+ #
39
+ # @param obj [Object] the Ruby object for which to find a sedes object.
40
+ # @raise [TypeError] if no appropriate sedes could be found.
41
+ def infer(obj)
42
+ return obj.class if is_sedes? obj.class
43
+ return big_endian_int if obj.is_a?(Integer) && obj >= 0
44
+ return binary if Binary.valid_type? obj
45
+ return List.new(elements: obj.map { |item| infer item }) if Util.is_list? obj
46
+ raise TypeError, "Did not find sedes handling type #{obj.class.name}"
47
+ end
48
+
49
+ # Determines if an object is a sedes object.
50
+ #
51
+ # @param obj [Object] the object to check.
52
+ # @return [Boolean] true if it's serializable and deserializable.
53
+ def is_sedes?(obj)
54
+ obj.respond_to?(:serialize) && obj.respond_to?(:deserialize)
55
+ end
56
+
57
+ # A utility to use a big-endian, unsigned integer sedes type with
58
+ # unspecified length.
59
+ #
60
+ # @return [Eth::Rlp::Sedes::BigEndianInt] a big-endian, unsigned integer sedes.
61
+ def big_endian_int
62
+ @big_endian_int ||= BigEndianInt.new
63
+ end
64
+
65
+ # A utility to use a binary sedes type.
66
+ #
67
+ # @return [Eth::Rlp::Sedes::Binary] a binary sedes.
68
+ def binary
69
+ @binary ||= Binary.new
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
data/lib/eth/rlp.rb ADDED
@@ -0,0 +1,63 @@
1
+ # Copyright (c) 2016-2022 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
+ # -*- encoding : ascii-8bit -*-
16
+
17
+ require "eth/rlp/decoder"
18
+ require "eth/rlp/encoder"
19
+ require "eth/rlp/sedes"
20
+ require "eth/util"
21
+
22
+ # Provides the {Eth} module.
23
+ module Eth
24
+
25
+ # Provides an recursive-length prefix (RLP) encoder and decoder.
26
+ module Rlp
27
+ extend self
28
+
29
+ # The Rlp module exposes a variety of exceptions grouped as {RlpException}.
30
+ class RlpException < StandardError; end
31
+
32
+ # An error-type to point out RLP-encoding errors.
33
+ class EncodingError < RlpException; end
34
+
35
+ # An error-type to point out RLP-decoding errors.
36
+ class DecodingError < RlpException; end
37
+
38
+ # An error-type to point out RLP-type serialization errors.
39
+ class SerializationError < RlpException; end
40
+
41
+ # An error-type to point out RLP-type serialization errors.
42
+ class DeserializationError < RlpException; end
43
+
44
+ # A wrapper to represent already RLP-encoded data.
45
+ class Data < String; end
46
+
47
+ # Performes an {Eth::Rlp::Encoder} on any ruby object.
48
+ #
49
+ # @param obj [Object] any ruby object.
50
+ # @return [String] a packed, RLP-encoded item.
51
+ def encode(obj)
52
+ Rlp::Encoder.perform obj
53
+ end
54
+
55
+ # Performes an {Eth::Rlp::Decoder} on any RLP-encoded item.
56
+ #
57
+ # @param rlp [String] a packed, RLP-encoded item.
58
+ # @return [Object] a decoded ruby object.
59
+ def decode(rlp)
60
+ Rlp::Decoder.perform rlp
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,163 @@
1
+ # Copyright (c) 2016-2022 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
+ require "rbsecp256k1"
16
+
17
+ # Provides the {Eth} module.
18
+ module Eth
19
+
20
+ # Defines handy tools for verifying and recovering signatures.
21
+ module Signature
22
+ extend self
23
+
24
+ # Provides a special signature error if signature is invalid.
25
+ class SignatureError < StandardError; end
26
+
27
+ # EIP-191 prefix byte 0x19
28
+ EIP191_PREFIX_BYTE = "\x19".freeze
29
+
30
+ # EIP-712 version byte 0x01
31
+ EIP712_VERSION_BYTE = "\x01".freeze
32
+
33
+ # Prefix message as per EIP-191 with `0x19` to ensure the data is not
34
+ # valid RLP and thus not mistaken for a transaction.
35
+ # EIP-191 Version byte: `0x45` (`E`)
36
+ # Ref: https://eips.ethereum.org/EIPS/eip-191
37
+ #
38
+ # @param message [String] the message string to be prefixed.
39
+ # @return [String] an EIP-191 prefixed string.
40
+ def prefix_message(message)
41
+ "#{EIP191_PREFIX_BYTE}Ethereum Signed Message:\n#{message.size}#{message}"
42
+ end
43
+
44
+ # Dissects a signature blob of 65+ bytes into its `r`, `s`, and `v`
45
+ # values.
46
+ #
47
+ # @param signature [String] a concatenated Secp256k1 signature string.
48
+ # @return [String, String, String] the `r`, `s`, and `v` values.
49
+ # @raise [SignatureError] if signature is of unknown size.
50
+ def dissect(signature)
51
+ signature = Util.bin_to_hex signature unless Util.is_hex? signature
52
+ signature = Util.remove_hex_prefix signature
53
+ if signature.size < 130
54
+ raise SignatureError, "Unknown signature length #{signature.size}!"
55
+ end
56
+ r = signature[0, 64]
57
+ s = signature[64, 64]
58
+ v = signature[128..]
59
+ return r, s, v
60
+ end
61
+
62
+ # Recovers a signature from arbitrary data without validation on a given chain.
63
+ #
64
+ # @param blob [String] that arbitrary data to be recovered.
65
+ # @param signature [String] the hex string containing the signature.
66
+ # @param chain_id [Integer] the chain ID the signature should be recovered from.
67
+ # @return [String] a hexa-decimal, uncompressed public key.
68
+ # @raise [SignatureError] if signature is of invalid size or invalid v.
69
+ def recover(blob, signature, chain_id = Chain::ETHEREUM)
70
+ context = Secp256k1::Context.new
71
+ r, s, v = dissect signature
72
+ v = v.to_i(16)
73
+ raise SignatureError, "Invalid signature v byte #{v} for chain ID #{chain_id}!" if v < chain_id
74
+ recovery_id = Chain.to_recovery_id v, chain_id
75
+ signature_rs = Util.hex_to_bin "#{r}#{s}"
76
+ recoverable_signature = context.recoverable_signature_from_compact signature_rs, recovery_id
77
+ public_key = recoverable_signature.recover_public_key blob
78
+ Util.bin_to_hex public_key.uncompressed
79
+ end
80
+
81
+ # Recovers a public key from a prefixed, personal message and
82
+ # a signature on a given chain. (EIP-191)
83
+ # Ref: https://eips.ethereum.org/EIPS/eip-191
84
+ #
85
+ # @param message [String] the message string.
86
+ # @param signature [String] the hex string containing the signature.
87
+ # @param chain_id [Integer] the chain ID the signature should be recovered from.
88
+ # @return [String] a hexa-decimal, uncompressed public key.
89
+ def personal_recover(message, signature, chain_id = Chain::ETHEREUM)
90
+ prefixed_message = prefix_message message
91
+ hashed_message = Util.keccak256 prefixed_message
92
+ recover hashed_message, signature, chain_id
93
+ end
94
+
95
+ # Recovers a public key from a typed data structure and a signature
96
+ # on a given chain. (EIP-712)
97
+ # Ref: https://eips.ethereum.org/EIPS/eip-712
98
+ #
99
+ # @param typed_data [Array] all the data in the typed data structure to be recovered.
100
+ # @param signature [String] the hex string containing the signature.
101
+ # @param chain_id [Integer] the chain ID the signature should be recovered from.
102
+ # @return [String] a hexa-decimal, uncompressed public key.
103
+ def recover_typed_data(typed_data, signature, chain_id = Chain::ETHEREUM)
104
+ hash_to_sign = Eip712.hash typed_data
105
+ recover hash_to_sign, signature, chain_id
106
+ end
107
+
108
+ # Verifies a signature for a given public key or address.
109
+ #
110
+ # @param blob [String] that arbitrary data to be verified.
111
+ # @param signature [String] the hex string containing the signature.
112
+ # @param public_key [String] either a public key or an Ethereum address.
113
+ # @param chain_id [Integer] the chain ID used to sign.
114
+ # @return [Boolean] true if signature matches provided public key.
115
+ # @raise [SignatureError] if it cannot determine the type of data or public key.
116
+ def verify(blob, signature, public_key, chain_id = Chain::ETHEREUM)
117
+ recovered_key = nil
118
+ if blob.instance_of? Array or blob.instance_of? Hash
119
+
120
+ # recover Array from sign_typed_data
121
+ recovered_key = recover_typed_data blob, signature, chain_id
122
+ elsif blob.instance_of? String and blob.encoding != Encoding::ASCII_8BIT
123
+
124
+ # recover message from personal_sign
125
+ recovered_key = personal_recover blob, signature, chain_id
126
+ elsif blob.instance_of? String and (Util.is_hex? blob or blob.encoding == Encoding::ASCII_8BIT)
127
+
128
+ # if nothing else, recover from arbitrary signature
129
+ recovered_key = recover blob, signature, chain_id
130
+ end
131
+
132
+ # raise if we cannot determine the data format
133
+ raise SignatureError, "Unknown data format to verify: #{blob}" if recovered_key.nil?
134
+
135
+ if public_key.instance_of? Address
136
+
137
+ # recovering using an Eth::Address
138
+ address = public_key.to_s
139
+ recovered_address = Util.public_key_to_address(recovered_key).to_s
140
+ return address == recovered_address
141
+ elsif public_key.instance_of? Secp256k1::PublicKey
142
+
143
+ # recovering using an Secp256k1::PublicKey
144
+ public_hex = Util.bin_to_hex public_key.uncompressed
145
+ return public_hex == recovered_key
146
+ elsif public_key.size == 42
147
+
148
+ # recovering using an address String
149
+ address = Address.new(public_key).to_s
150
+ recovered_address = Util.public_key_to_address(recovered_key).to_s
151
+ return address == recovered_address
152
+ elsif public_key.size == 130
153
+
154
+ # recovering using an uncompressed public key String
155
+ return public_key == recovered_key
156
+ else
157
+
158
+ # raise if we cannot determine the public key format used
159
+ raise SignatureError, "Invalid public key or address supplied #{public_key}!"
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,336 @@
1
+ # Copyright (c) 2016-2022 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-1559 transactions utilizing EIP-2718
22
+ # types and envelopes.
23
+ # Ref: https://eips.ethereum.org/EIPS/eip-1559
24
+ class Eip1559
25
+
26
+ # The EIP-155 Chain ID.
27
+ # Ref: https://eips.ethereum.org/EIPS/eip-155
28
+ attr_reader :chain_id
29
+
30
+ # The transaction nonce provided by the signer.
31
+ attr_reader :signer_nonce
32
+
33
+ # The transaction max priority fee per gas in Wei.
34
+ attr_reader :max_priority_fee_per_gas
35
+
36
+ # The transaction max fee per gas in Wei.
37
+ attr_reader :max_fee_per_gas
38
+
39
+ # The gas limit for the transaction.
40
+ attr_reader :gas_limit
41
+
42
+ # The recipient address.
43
+ attr_reader :destination
44
+
45
+ # The transaction amount in Wei.
46
+ attr_reader :amount
47
+
48
+ # The transaction data payload.
49
+ attr_reader :payload
50
+
51
+ # An optional EIP-2930 access list.
52
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
53
+ attr_reader :access_list
54
+
55
+ # The signature's y-parity byte (not v).
56
+ attr_reader :signature_y_parity
57
+
58
+ # The signature `r` value.
59
+ attr_reader :signature_r
60
+
61
+ # The signature `s` value.
62
+ attr_reader :signature_s
63
+
64
+ # The sender address.
65
+ attr_reader :sender
66
+
67
+ # The transaction type.
68
+ attr_reader :type
69
+
70
+ # Create a type-2 (EIP-1559) transaction payload object that
71
+ # can be prepared for envelope, signature and broadcast.
72
+ # Ref: https://eips.ethereum.org/EIPS/eip-1559
73
+ #
74
+ # @param params [Hash] all necessary transaction fields.
75
+ # @option params [Integer] :chain_id the chain ID.
76
+ # @option params [Integer] :nonce the signer nonce.
77
+ # @option params [Integer] :priority_fee the max priority fee per gas.
78
+ # @option params [Integer] :max_gas_fee the max transaction fee per gas.
79
+ # @option params [Integer] :gas_limit the gas limit.
80
+ # @option params [Eth::Address] :from the sender address.
81
+ # @option params [Eth::Address] :to the reciever address.
82
+ # @option params [Integer] :value the transaction value.
83
+ # @option params [String] :data the transaction data payload.
84
+ # @option params [Array] :access_list an optional access list.
85
+ # @raise [ParameterError] if gas limit is too low.
86
+ def initialize(params)
87
+ fields = { recovery_id: nil, r: 0, s: 0 }.merge params
88
+
89
+ # populate optional fields with serializable empty values
90
+ fields[:chain_id] = Tx.sanitize_chain fields[:chain_id]
91
+ fields[:from] = Tx.sanitize_address fields[:from]
92
+ fields[:to] = Tx.sanitize_address fields[:to]
93
+ fields[:value] = Tx.sanitize_amount fields[:value]
94
+ fields[:data] = Tx.sanitize_data fields[:data]
95
+
96
+ # ensure sane values for all mandatory fields
97
+ fields = Tx.validate_params fields
98
+ fields[:access_list] = Tx.sanitize_list fields[:access_list]
99
+
100
+ # ensure gas limit is not too low
101
+ minimum_cost = Tx.estimate_intrinsic_gas fields[:data], fields[:access_list]
102
+ raise ParameterError, "Transaction gas limit is too low, try #{minimum_cost}!" if fields[:gas_limit].to_i < minimum_cost
103
+
104
+ # populate class attributes
105
+ @signer_nonce = fields[:nonce].to_i
106
+ @max_priority_fee_per_gas = fields[:priority_fee].to_i
107
+ @max_fee_per_gas = fields[:max_gas_fee].to_i
108
+ @gas_limit = fields[:gas_limit].to_i
109
+ @sender = fields[:from].to_s
110
+ @destination = fields[:to].to_s
111
+ @amount = fields[:value].to_i
112
+ @payload = fields[:data]
113
+ @access_list = fields[:access_list]
114
+
115
+ # the signature v is set to the chain id for unsigned transactions
116
+ @signature_y_parity = fields[:recovery_id]
117
+ @chain_id = fields[:chain_id]
118
+
119
+ # the signature fields are empty for unsigned transactions.
120
+ @signature_r = fields[:r]
121
+ @signature_s = fields[:s]
122
+
123
+ # last but not least, set the type.
124
+ @type = TYPE_1559
125
+ end
126
+
127
+ # Overloads the constructor for decoding raw transactions and creating unsigned copies.
128
+ konstructor :decode, :unsigned_copy
129
+
130
+ # Decodes a raw transaction hex into an {Eth::Tx::Eip1559}
131
+ # transaction object.
132
+ #
133
+ # @param hex [String] the raw transaction hex-string.
134
+ # @return [Eth::Tx::Eip1559] transaction payload.
135
+ # @raise [TransactionTypeError] if transaction type is invalid.
136
+ # @raise [ParameterError] if transaction is missing fields.
137
+ # @raise [DecoderError] if transaction decoding fails.
138
+ def decode(hex)
139
+ hex = Util.remove_hex_prefix hex
140
+ type = hex[0, 2]
141
+ raise TransactionTypeError, "Invalid transaction type #{type}!" if type.to_i(16) != TYPE_1559
142
+
143
+ bin = Util.hex_to_bin hex[2..]
144
+ tx = Rlp.decode bin
145
+
146
+ # decoded transactions always have 9 + 3 fields, even if they are empty or zero
147
+ raise ParameterError, "Transaction missing fields!" if tx.size < 9
148
+
149
+ # populate the 9 payload fields
150
+ chain_id = Util.deserialize_big_endian_to_int tx[0]
151
+ nonce = Util.deserialize_big_endian_to_int tx[1]
152
+ priority_fee = Util.deserialize_big_endian_to_int tx[2]
153
+ max_gas_fee = Util.deserialize_big_endian_to_int tx[3]
154
+ gas_limit = Util.deserialize_big_endian_to_int tx[4]
155
+ to = Util.bin_to_hex tx[5]
156
+ value = Util.deserialize_big_endian_to_int tx[6]
157
+ data = tx[7]
158
+ access_list = tx[8]
159
+
160
+ # populate class attributes
161
+ @chain_id = chain_id.to_i
162
+ @signer_nonce = nonce.to_i
163
+ @max_priority_fee_per_gas = priority_fee.to_i
164
+ @max_fee_per_gas = max_gas_fee.to_i
165
+ @gas_limit = gas_limit.to_i
166
+ @destination = to.to_s
167
+ @amount = value.to_i
168
+ @payload = data
169
+ @access_list = access_list
170
+
171
+ # populate the 3 signature fields
172
+ if tx.size == 9
173
+ _set_signature(nil, 0, 0)
174
+ elsif tx.size == 12
175
+ recovery_id = Util.bin_to_hex(tx[9]).to_i(16)
176
+ r = Util.bin_to_hex tx[10]
177
+ s = Util.bin_to_hex tx[11]
178
+
179
+ # allows us to force-setting a signature if the transaction is signed already
180
+ _set_signature(recovery_id, r, s)
181
+ else
182
+ raise_error DecoderError, "Cannot decode EIP-1559 payload!"
183
+ end
184
+
185
+ # last but not least, set the type.
186
+ @type = TYPE_1559
187
+
188
+ # recover sender address
189
+ v = Chain.to_v recovery_id, chain_id
190
+ public_key = Signature.recover(unsigned_hash, "#{r}#{s}#{v.to_s(16)}", chain_id)
191
+ address = Util.public_key_to_address(public_key).to_s
192
+ @sender = Tx.sanitize_address address
193
+ end
194
+
195
+ # Creates an unsigned copy of a transaction payload.
196
+ #
197
+ # @param tx [Eth::Tx::Eip1559] an EIP-1559 transaction payload.
198
+ # @return [Eth::Tx::Eip1559] an unsigned EIP-1559 transaction payload.
199
+ # @raise [TransactionTypeError] if transaction type does not match.
200
+ def unsigned_copy(tx)
201
+
202
+ # not checking transaction validity unless it's of a different class
203
+ raise TransactionTypeError, "Cannot copy transaction of different payload type!" unless tx.instance_of? Tx::Eip1559
204
+
205
+ # populate class attributes
206
+ @signer_nonce = tx.signer_nonce
207
+ @max_priority_fee_per_gas = tx.max_priority_fee_per_gas
208
+ @max_fee_per_gas = tx.max_fee_per_gas
209
+ @gas_limit = tx.gas_limit
210
+ @destination = tx.destination
211
+ @amount = tx.amount
212
+ @payload = tx.payload
213
+ @access_list = tx.access_list
214
+ @chain_id = tx.chain_id
215
+
216
+ # force-set signature to unsigned
217
+ _set_signature(nil, 0, 0)
218
+
219
+ # keep the 'from' field blank
220
+ @sender = Tx.sanitize_address nil
221
+
222
+ # last but not least, set the type.
223
+ @type = TYPE_1559
224
+ end
225
+
226
+ # Sign the transaction with a given key.
227
+ #
228
+ # @param key [Eth::Key] the key-pair to use for signing.
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 signing key.
232
+ def sign(key)
233
+ if Tx.is_signed? self
234
+ raise Signature::SignatureError, "Transaction is already signed!"
235
+ end
236
+
237
+ # ensure the sender address matches the given key
238
+ unless @sender.nil? or sender.empty?
239
+ signer_address = Tx.sanitize_address key.address.to_s
240
+ from_address = Tx.sanitize_address @sender
241
+ raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
242
+ end
243
+
244
+ # sign a keccak hash of the unsigned, encoded transaction
245
+ signature = key.sign(unsigned_hash, @chain_id)
246
+ r, s, v = Signature.dissect signature
247
+ recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
248
+ @signature_y_parity = recovery_id
249
+ @signature_r = r
250
+ @signature_s = s
251
+ return hash
252
+ end
253
+
254
+ # Encodes a raw transaction object, wraps it in an EIP-2718 envelope
255
+ # with an EIP-1559 type prefix.
256
+ #
257
+ # @return [String] a raw, RLP-encoded EIP-1559 type transaction object.
258
+ # @raise [Signature::SignatureError] if the transaction is not yet signed.
259
+ def encoded
260
+ unless Tx.is_signed? self
261
+ raise Signature::SignatureError, "Transaction is not signed!"
262
+ end
263
+ tx_data = []
264
+ tx_data.push Util.serialize_int_to_big_endian @chain_id
265
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
266
+ tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
267
+ tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
268
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
269
+ tx_data.push Util.hex_to_bin @destination
270
+ tx_data.push Util.serialize_int_to_big_endian @amount
271
+ tx_data.push Rlp::Sedes.binary.serialize @payload
272
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
273
+ tx_data.push Util.serialize_int_to_big_endian @signature_y_parity
274
+ tx_data.push Util.serialize_int_to_big_endian @signature_r
275
+ tx_data.push Util.serialize_int_to_big_endian @signature_s
276
+ tx_encoded = Rlp.encode tx_data
277
+
278
+ # create an EIP-2718 envelope with EIP-1559 type payload
279
+ tx_type = Util.serialize_int_to_big_endian @type
280
+ return "#{tx_type}#{tx_encoded}"
281
+ end
282
+
283
+ # Gets the encoded, enveloped, raw transaction hex.
284
+ #
285
+ # @return [String] the raw transaction hex.
286
+ def hex
287
+ Util.bin_to_hex encoded
288
+ end
289
+
290
+ # Gets the transaction hash.
291
+ #
292
+ # @return [String] the transaction hash.
293
+ def hash
294
+ Util.bin_to_hex Util.keccak256 encoded
295
+ end
296
+
297
+ # Encodes the unsigned transaction payload in an EIP-1559 envelope,
298
+ # required for signing.
299
+ #
300
+ # @return [String] an RLP-encoded, unsigned, enveloped EIP-1559 transaction.
301
+ def unsigned_encoded
302
+ tx_data = []
303
+ tx_data.push Util.serialize_int_to_big_endian @chain_id
304
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
305
+ tx_data.push Util.serialize_int_to_big_endian @max_priority_fee_per_gas
306
+ tx_data.push Util.serialize_int_to_big_endian @max_fee_per_gas
307
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
308
+ tx_data.push Util.hex_to_bin @destination
309
+ tx_data.push Util.serialize_int_to_big_endian @amount
310
+ tx_data.push Rlp::Sedes.binary.serialize @payload
311
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
312
+ tx_encoded = Rlp.encode tx_data
313
+
314
+ # create an EIP-2718 envelope with EIP-1559 type payload (unsigned)
315
+ tx_type = Util.serialize_int_to_big_endian @type
316
+ return "#{tx_type}#{tx_encoded}"
317
+ end
318
+
319
+ # Gets the sign-hash required to sign a raw transaction.
320
+ #
321
+ # @return [String] a Keccak-256 hash of an unsigned transaction.
322
+ def unsigned_hash
323
+ Util.keccak256 unsigned_encoded
324
+ end
325
+
326
+ private
327
+
328
+ # Force-sets an existing signature of a decoded transaction.
329
+ def _set_signature(recovery_id, r, s)
330
+ @signature_y_parity = recovery_id
331
+ @signature_r = r
332
+ @signature_s = s
333
+ end
334
+ end
335
+ end
336
+ end