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,328 @@
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 legacy support for transactions on blockchains that do not
22
+ # implement EIP-1559 but still want to utilize EIP-2718 envelopes.
23
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
24
+ class Eip2930
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 gas price for the transaction in Wei.
34
+ attr_reader :gas_price
35
+
36
+ # The gas limit for the transaction.
37
+ attr_reader :gas_limit
38
+
39
+ # The recipient address.
40
+ attr_reader :destination
41
+
42
+ # The transaction amount in Wei.
43
+ attr_reader :amount
44
+
45
+ # The transaction data payload.
46
+ attr_reader :payload
47
+
48
+ # An optional EIP-2930 access list.
49
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
50
+ attr_reader :access_list
51
+
52
+ # The signature's `y`-parity byte (not `v`).
53
+ attr_reader :signature_y_parity
54
+
55
+ # The signature `r` value.
56
+ attr_reader :signature_r
57
+
58
+ # The signature `s` value.
59
+ attr_reader :signature_s
60
+
61
+ # The sender address.
62
+ attr_reader :sender
63
+
64
+ # The transaction type.
65
+ attr_reader :type
66
+
67
+ # Create a legacy type-1 (EIP-2930) transaction payload object that
68
+ # can be prepared for envelope, signature and broadcast. Should not
69
+ # be used unless there is no EIP-1559 support.
70
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
71
+ #
72
+ #
73
+ # @param params [Hash] all necessary transaction fields.
74
+ # @option params [Integer] :chain_id the chain ID.
75
+ # @option params [Integer] :nonce the signer nonce.
76
+ # @option params [Integer] :gas_price the gas price.
77
+ # @option params [Integer] :gas_limit the gas limit.
78
+ # @option params [Eth::Address] :from the sender address.
79
+ # @option params [Eth::Address] :to the reciever address.
80
+ # @option params [Integer] :value the transaction value.
81
+ # @option params [String] :data the transaction data payload.
82
+ # @option params [Array] :access_list an optional access list.
83
+ # @raise [ParameterError] if gas limit is too low.
84
+ def initialize(params)
85
+ fields = { recovery_id: nil, r: 0, s: 0 }.merge params
86
+
87
+ # populate optional fields with serializable empty values
88
+ fields[:chain_id] = Tx.sanitize_chain fields[:chain_id]
89
+ fields[:from] = Tx.sanitize_address fields[:from]
90
+ fields[:to] = Tx.sanitize_address fields[:to]
91
+ fields[:value] = Tx.sanitize_amount fields[:value]
92
+ fields[:data] = Tx.sanitize_data fields[:data]
93
+
94
+ # ensure sane values for all mandatory fields
95
+ fields = Tx.validate_legacy_params fields
96
+ fields[:access_list] = Tx.sanitize_list fields[:access_list]
97
+
98
+ # ensure gas limit is not too low
99
+ minimum_cost = Tx.estimate_intrinsic_gas fields[:data], fields[:access_list]
100
+ raise ParameterError, "Transaction gas limit is too low, try #{minimum_cost}!" if fields[:gas_limit].to_i < minimum_cost
101
+
102
+ # populate class attributes
103
+ @signer_nonce = fields[:nonce].to_i
104
+ @gas_price = fields[:gas_price].to_i
105
+ @gas_limit = fields[:gas_limit].to_i
106
+ @sender = fields[:from].to_s
107
+ @destination = fields[:to].to_s
108
+ @amount = fields[:value].to_i
109
+ @payload = fields[:data]
110
+ @access_list = fields[:access_list]
111
+
112
+ # the signature v is set to the chain id for unsigned transactions
113
+ @signature_y_parity = fields[:recovery_id]
114
+ @chain_id = fields[:chain_id]
115
+
116
+ # the signature fields are empty for unsigned transactions.
117
+ @signature_r = fields[:r]
118
+ @signature_s = fields[:s]
119
+
120
+ # last but not least, set the type.
121
+ @type = TYPE_2930
122
+ end
123
+
124
+ # Overloads the constructor for decoding raw transactions and creating unsigned copies.
125
+ konstructor :decode, :unsigned_copy
126
+
127
+ # Decodes a raw transaction hex into an {Eth::Tx::Eip2930}
128
+ # transaction object.
129
+ #
130
+ # @param hex [String] the raw transaction hex-string.
131
+ # @return [Eth::Tx::Eip2930] transaction payload.
132
+ # @raise [TransactionTypeError] if transaction type is invalid.
133
+ # @raise [ParameterError] if transaction is missing fields.
134
+ # @raise [DecoderError] if transaction decoding fails.
135
+ def decode(hex)
136
+ hex = Util.remove_hex_prefix hex
137
+ type = hex[0, 2]
138
+ raise TransactionTypeError, "Invalid transaction type #{type}!" if type.to_i(16) != TYPE_2930
139
+
140
+ bin = Util.hex_to_bin hex[2..]
141
+ tx = Rlp.decode bin
142
+
143
+ # decoded transactions always have 8 + 3 fields, even if they are empty or zero
144
+ raise ParameterError, "Transaction missing fields!" if tx.size < 8
145
+
146
+ # populate the 8 payload fields
147
+ chain_id = Util.deserialize_big_endian_to_int tx[0]
148
+ nonce = Util.deserialize_big_endian_to_int tx[1]
149
+ gas_price = Util.deserialize_big_endian_to_int tx[2]
150
+ gas_limit = Util.deserialize_big_endian_to_int tx[3]
151
+ to = Util.bin_to_hex tx[4]
152
+ value = Util.deserialize_big_endian_to_int tx[5]
153
+ data = tx[6]
154
+ access_list = tx[7]
155
+
156
+ # populate class attributes
157
+ @chain_id = chain_id.to_i
158
+ @signer_nonce = nonce.to_i
159
+ @gas_price = gas_price.to_i
160
+ @gas_limit = gas_limit.to_i
161
+ @destination = to.to_s
162
+ @amount = value.to_i
163
+ @payload = data
164
+ @access_list = access_list
165
+
166
+ # populate the 3 signature fields
167
+ if tx.size == 8
168
+ _set_signature(nil, 0, 0)
169
+ elsif tx.size == 11
170
+ recovery_id = Util.bin_to_hex(tx[8]).to_i(16)
171
+ r = Util.bin_to_hex tx[9]
172
+ s = Util.bin_to_hex tx[10]
173
+
174
+ # allows us to force-setting a signature if the transaction is signed already
175
+ _set_signature(recovery_id, r, s)
176
+ else
177
+ raise_error DecoderError, "Cannot decode EIP-2930 payload!"
178
+ end
179
+
180
+ # last but not least, set the type.
181
+ @type = TYPE_2930
182
+
183
+ # recover sender address
184
+ v = Chain.to_v recovery_id, chain_id
185
+ public_key = Signature.recover(unsigned_hash, "#{r}#{s}#{v.to_s(16)}", chain_id)
186
+ address = Util.public_key_to_address(public_key).to_s
187
+ @sender = Tx.sanitize_address address
188
+ end
189
+
190
+ # Creates an unsigned copy of a transaction payload.
191
+ #
192
+ # @param tx [Eth::Tx::Eip2930] an EIP-2930 transaction payload.
193
+ # @return [Eth::Tx::Eip2930] an unsigned EIP-2930 transaction payload.
194
+ # @raise [TransactionTypeError] if transaction type does not match.
195
+ def unsigned_copy(tx)
196
+
197
+ # not checking transaction validity unless it's of a different class
198
+ raise TransactionTypeError, "Cannot copy transaction of different payload type!" unless tx.instance_of? Tx::Eip2930
199
+
200
+ # populate class attributes
201
+ @signer_nonce = tx.signer_nonce
202
+ @gas_price = tx.gas_price
203
+ @gas_limit = tx.gas_limit
204
+ @destination = tx.destination
205
+ @amount = tx.amount
206
+ @payload = tx.payload
207
+ @access_list = tx.access_list
208
+ @chain_id = tx.chain_id
209
+
210
+ # force-set signature to unsigned
211
+ _set_signature(nil, 0, 0)
212
+
213
+ # keep the 'from' field blank
214
+ @sender = Tx.sanitize_address nil
215
+
216
+ # last but not least, set the type.
217
+ @type = TYPE_2930
218
+ end
219
+
220
+ # Sign the transaction with a given key.
221
+ #
222
+ # @param key [Eth::Key] the key-pair to use for signing.
223
+ # @return [String] a transaction hash.
224
+ # @raise [Signature::SignatureError] if transaction is already signed.
225
+ # @raise [Signature::SignatureError] if sender address does not match signing key.
226
+ def sign(key)
227
+ if Tx.is_signed? self
228
+ raise Signature::SignatureError, "Transaction is already signed!"
229
+ end
230
+
231
+ # ensure the sender address matches the given key
232
+ unless @sender.nil? or sender.empty?
233
+ signer_address = Tx.sanitize_address key.address.to_s
234
+ from_address = Tx.sanitize_address @sender
235
+ raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
236
+ end
237
+
238
+ # sign a keccak hash of the unsigned, encoded transaction
239
+ signature = key.sign(unsigned_hash, @chain_id)
240
+ r, s, v = Signature.dissect signature
241
+ recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
242
+ @signature_y_parity = recovery_id
243
+ @signature_r = r
244
+ @signature_s = s
245
+ return hash
246
+ end
247
+
248
+ # Encodes a raw transaction object, wraps it in an EIP-2718 envelope
249
+ # with an EIP-2930 type prefix.
250
+ #
251
+ # @return [String] a raw, RLP-encoded EIP-2930 type transaction object.
252
+ # @raise [Signature::SignatureError] if the transaction is not yet signed.
253
+ def encoded
254
+ unless Tx.is_signed? self
255
+ raise Signature::SignatureError, "Transaction is not signed!"
256
+ end
257
+ tx_data = []
258
+ tx_data.push Util.serialize_int_to_big_endian @chain_id
259
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
260
+ tx_data.push Util.serialize_int_to_big_endian @gas_price
261
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
262
+ tx_data.push Util.hex_to_bin @destination
263
+ tx_data.push Util.serialize_int_to_big_endian @amount
264
+ tx_data.push Rlp::Sedes.binary.serialize @payload
265
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
266
+ tx_data.push Util.serialize_int_to_big_endian @signature_y_parity
267
+ tx_data.push Util.serialize_int_to_big_endian @signature_r
268
+ tx_data.push Util.serialize_int_to_big_endian @signature_s
269
+ tx_encoded = Rlp.encode tx_data
270
+
271
+ # create an EIP-2718 envelope with EIP-2930 type payload
272
+ tx_type = Util.serialize_int_to_big_endian @type
273
+ return "#{tx_type}#{tx_encoded}"
274
+ end
275
+
276
+ # Gets the encoded, enveloped, raw transaction hex.
277
+ #
278
+ # @return [String] the raw transaction hex.
279
+ def hex
280
+ Util.bin_to_hex encoded
281
+ end
282
+
283
+ # Gets the transaction hash.
284
+ #
285
+ # @return [String] the transaction hash.
286
+ def hash
287
+ Util.bin_to_hex Util.keccak256 encoded
288
+ end
289
+
290
+ # Encodes the unsigned transaction payload in an EIP-2930 envelope,
291
+ # required for signing.
292
+ #
293
+ # @return [String] an RLP-encoded, unsigned, enveloped EIP-2930 transaction.
294
+ def unsigned_encoded
295
+ tx_data = []
296
+ tx_data.push Util.serialize_int_to_big_endian @chain_id
297
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
298
+ tx_data.push Util.serialize_int_to_big_endian @gas_price
299
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
300
+ tx_data.push Util.hex_to_bin @destination
301
+ tx_data.push Util.serialize_int_to_big_endian @amount
302
+ tx_data.push Rlp::Sedes.binary.serialize @payload
303
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
304
+ tx_encoded = Rlp.encode tx_data
305
+
306
+ # create an EIP-2718 envelope with EIP-2930 type payload (unsigned)
307
+ tx_type = Util.serialize_int_to_big_endian @type
308
+ return "#{tx_type}#{tx_encoded}"
309
+ end
310
+
311
+ # Gets the sign-hash required to sign a raw transaction.
312
+ #
313
+ # @return [String] a Keccak-256 hash of an unsigned transaction.
314
+ def unsigned_hash
315
+ Util.keccak256 unsigned_encoded
316
+ end
317
+
318
+ private
319
+
320
+ # Force-sets an existing signature of a decoded transaction.
321
+ def _set_signature(recovery_id, r, s)
322
+ @signature_y_parity = recovery_id
323
+ @signature_r = r
324
+ @signature_s = s
325
+ end
326
+ end
327
+ end
328
+ end
@@ -0,0 +1,296 @@
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 legacy support for transactions on blockchains that do not
22
+ # implement EIP-1559, EIP-2718, or EIP-2930.
23
+ class Legacy
24
+
25
+ # The transaction nonce provided by the signer.
26
+ attr_reader :signer_nonce
27
+
28
+ # The gas price for the transaction in Wei.
29
+ attr_reader :gas_price
30
+
31
+ # The gas limit for the transaction.
32
+ attr_reader :gas_limit
33
+
34
+ # The recipient address.
35
+ attr_reader :destination
36
+
37
+ # The transaction amount in Wei.
38
+ attr_reader :amount
39
+
40
+ # The transaction data payload.
41
+ attr_reader :payload
42
+
43
+ # The signature `v` byte.
44
+ attr_reader :signature_v
45
+
46
+ # The signature `r` value.
47
+ attr_reader :signature_r
48
+
49
+ # The signature `s` value.
50
+ attr_reader :signature_s
51
+
52
+ # The EIP-155 chain ID field.
53
+ # Ref: https://eips.ethereum.org/EIPS/eip-155
54
+ attr_reader :chain_id
55
+
56
+ # The sender address.
57
+ attr_reader :sender
58
+
59
+ # The transaction type.
60
+ attr_reader :type
61
+
62
+ # Create a legacy transaction object that can be prepared for
63
+ # signature and broadcast. Should not be used unless there is
64
+ # no EIP-1559 support.
65
+ #
66
+ # @param params [Hash] all necessary transaction fields.
67
+ # @option params [Integer] :nonce the signer nonce.
68
+ # @option params [Integer] :gas_price the gas price.
69
+ # @option params [Integer] :gas_limit the gas limit.
70
+ # @option params [Eth::Address] :from the sender address.
71
+ # @option params [Eth::Address] :to the reciever address.
72
+ # @option params [Integer] :value the transaction value.
73
+ # @option params [String] :data the transaction data payload.
74
+ # @param chain_id [Integer] the EIP-155 Chain ID.
75
+ # @raise [ParameterError] if gas limit is too low.
76
+ def initialize(params, chain_id = Chain::ETHEREUM)
77
+ fields = { v: chain_id, r: 0, s: 0 }.merge params
78
+
79
+ # populate optional fields with serializable empty values
80
+ fields[:value] = Tx.sanitize_amount fields[:value]
81
+ fields[:from] = Tx.sanitize_address fields[:from]
82
+ fields[:to] = Tx.sanitize_address fields[:to]
83
+ fields[:data] = Tx.sanitize_data fields[:data]
84
+
85
+ # ensure sane values for all mandatory fields
86
+ fields = Tx.validate_legacy_params fields
87
+
88
+ # ensure gas limit is not too low
89
+ minimum_cost = Tx.estimate_intrinsic_gas fields[:data]
90
+ raise ParameterError, "Transaction gas limit is too low, try #{minimum_cost}!" if fields[:gas_limit].to_i < minimum_cost
91
+
92
+ # populate class attributes
93
+ @signer_nonce = fields[:nonce].to_i
94
+ @gas_price = fields[:gas_price].to_i
95
+ @gas_limit = fields[:gas_limit].to_i
96
+ @sender = fields[:from].to_s
97
+ @destination = fields[:to].to_s
98
+ @amount = fields[:value].to_i
99
+ @payload = fields[:data]
100
+
101
+ # the signature v is set to the chain id for unsigned transactions
102
+ @signature_v = fields[:v]
103
+ @chain_id = chain_id
104
+
105
+ # the signature fields are empty for unsigned transactions.
106
+ @signature_r = fields[:r]
107
+ @signature_s = fields[:s]
108
+
109
+ # last but not least, set the type.
110
+ @type = TYPE_LEGACY
111
+ end
112
+
113
+ # overloads the constructor for decoding raw transactions and creating unsigned copies
114
+ konstructor :decode, :unsigned_copy
115
+
116
+ # Decodes a raw transaction hex into an {Eth::Tx::Legacy}
117
+ # transaction object.
118
+ #
119
+ # @param hex [String] the raw transaction hex-string.
120
+ # @return [Eth::Tx::Legacy] transaction object.
121
+ # @raise [ParameterError] if transaction misses fields.
122
+ def decode(hex)
123
+ bin = Util.hex_to_bin hex
124
+ tx = Rlp.decode bin
125
+
126
+ # decoded transactions always have 9 fields, even if they are empty or zero
127
+ raise ParameterError, "Transaction missing fields!" if tx.size < 9
128
+
129
+ # populate the 9 fields
130
+ nonce = Util.deserialize_big_endian_to_int tx[0]
131
+ gas_price = Util.deserialize_big_endian_to_int tx[1]
132
+ gas_limit = Util.deserialize_big_endian_to_int tx[2]
133
+ to = Util.bin_to_hex tx[3]
134
+ value = Util.deserialize_big_endian_to_int tx[4]
135
+ data = tx[5]
136
+ v = Util.bin_to_hex tx[6]
137
+ r = Util.bin_to_hex tx[7]
138
+ s = Util.bin_to_hex tx[8]
139
+
140
+ # try to recover the chain id from v
141
+ chain_id = Chain.to_chain_id Util.deserialize_big_endian_to_int tx[6]
142
+
143
+ # populate class attributes
144
+ @signer_nonce = nonce.to_i
145
+ @gas_price = gas_price.to_i
146
+ @gas_limit = gas_limit.to_i
147
+ @destination = to.to_s
148
+ @amount = value.to_i
149
+ @payload = data
150
+ @chain_id = chain_id
151
+
152
+ # allows us to force-setting a signature if the transaction is signed already
153
+ _set_signature(v, r, s)
154
+
155
+ unless chain_id.nil?
156
+
157
+ # recover sender address
158
+ public_key = Signature.recover(unsigned_hash, "#{r}#{s}#{v}", chain_id)
159
+ address = Util.public_key_to_address(public_key).to_s
160
+ @sender = Tx.sanitize_address address
161
+ else
162
+
163
+ # keep the 'from' field blank
164
+ @sender = Tx.sanitize_address nil
165
+ end
166
+
167
+ # last but not least, set the type.
168
+ @type = TYPE_LEGACY
169
+ end
170
+
171
+ # Creates an unsigned copy of a transaction.
172
+ #
173
+ # @param tx [Eth::Tx::Legacy] an legacy transaction object.
174
+ # @return [Eth::Tx::Legacy] an unsigned transaction object.
175
+ # @raise [TransactionTypeError] if transaction type does not match.
176
+ def unsigned_copy(tx)
177
+
178
+ # not checking transaction validity unless it's of a different class
179
+ raise TransactionTypeError, "Cannot copy transaction of different type!" unless tx.instance_of? Tx::Legacy
180
+
181
+ # populate class attributes
182
+ @signer_nonce = tx.signer_nonce
183
+ @gas_price = tx.gas_price
184
+ @gas_limit = tx.gas_limit
185
+ @destination = tx.destination
186
+ @amount = tx.amount
187
+ @payload = tx.payload
188
+ @chain_id = tx.chain_id
189
+
190
+ # force-set signature to unsigned
191
+ _set_signature(tx.chain_id, 0, 0)
192
+
193
+ # keep the 'from' field blank
194
+ @sender = Tx.sanitize_address nil
195
+
196
+ # last but not least, set the type.
197
+ @type = TYPE_LEGACY
198
+ end
199
+
200
+ # Sign the transaction with a given key.
201
+ #
202
+ # @param key [Eth::Key] the key-pair to use for signing.
203
+ # @return [String] a transaction hash.
204
+ # @raise [Signature::SignatureError] if transaction is already signed.
205
+ # @raise [Signature::SignatureError] if sender address does not match signing key.
206
+ def sign(key)
207
+ if Tx.is_signed? self
208
+ raise Signature::SignatureError, "Transaction is already signed!"
209
+ end
210
+
211
+ # ensure the sender address matches the given key
212
+ unless @sender.nil? or sender.empty?
213
+ signer_address = Tx.sanitize_address key.address.to_s
214
+ from_address = Tx.sanitize_address @sender
215
+ raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
216
+ end
217
+
218
+ # sign a keccak hash of the unsigned, encoded transaction
219
+ signature = key.sign(unsigned_hash, @chain_id)
220
+ r, s, v = Signature.dissect signature
221
+ @signature_v = v
222
+ @signature_r = r
223
+ @signature_s = s
224
+ return hash
225
+ end
226
+
227
+ # Encodes a raw transaction object.
228
+ #
229
+ # @return [String] a raw, RLP-encoded legacy transaction.
230
+ # @raise [Signature::SignatureError] if the transaction is not yet signed.
231
+ def encoded
232
+ unless Tx.is_signed? self
233
+ raise Signature::SignatureError, "Transaction is not signed!"
234
+ end
235
+ tx_data = []
236
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
237
+ tx_data.push Util.serialize_int_to_big_endian @gas_price
238
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
239
+ tx_data.push Util.hex_to_bin @destination
240
+ tx_data.push Util.serialize_int_to_big_endian @amount
241
+ tx_data.push Rlp::Sedes.binary.serialize @payload
242
+ tx_data.push Util.serialize_int_to_big_endian @signature_v
243
+ tx_data.push Util.serialize_int_to_big_endian @signature_r
244
+ tx_data.push Util.serialize_int_to_big_endian @signature_s
245
+ Rlp.encode tx_data
246
+ end
247
+
248
+ # Gets the encoded, raw transaction hex.
249
+ #
250
+ # @return [String] the raw transaction hex.
251
+ def hex
252
+ Util.bin_to_hex encoded
253
+ end
254
+
255
+ # Gets the transaction hash.
256
+ #
257
+ # @return [String] the transaction hash.
258
+ def hash
259
+ Util.bin_to_hex Util.keccak256 encoded
260
+ end
261
+
262
+ # Encodes the unsigned transaction object, required for signing.
263
+ #
264
+ # @return [String] an RLP-encoded, unsigned transaction.
265
+ def unsigned_encoded
266
+ tx_data = []
267
+ tx_data.push Util.serialize_int_to_big_endian @signer_nonce
268
+ tx_data.push Util.serialize_int_to_big_endian @gas_price
269
+ tx_data.push Util.serialize_int_to_big_endian @gas_limit
270
+ tx_data.push Util.hex_to_bin @destination
271
+ tx_data.push Util.serialize_int_to_big_endian @amount
272
+ tx_data.push Rlp::Sedes.binary.serialize @payload
273
+ tx_data.push Util.serialize_int_to_big_endian @chain_id
274
+ tx_data.push Util.serialize_int_to_big_endian 0
275
+ tx_data.push Util.serialize_int_to_big_endian 0
276
+ Rlp.encode tx_data
277
+ end
278
+
279
+ # Gets the sign-hash required to sign a raw transaction.
280
+ #
281
+ # @return [String] a Keccak-256 hash of an unsigned transaction.
282
+ def unsigned_hash
283
+ Util.keccak256 unsigned_encoded
284
+ end
285
+
286
+ private
287
+
288
+ # Force-sets an existing signature of a decoded transaction.
289
+ def _set_signature(v, r, s)
290
+ @signature_v = v
291
+ @signature_r = r
292
+ @signature_s = s
293
+ end
294
+ end
295
+ end
296
+ end