eth 0.5.0 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/codeql.yml +4 -0
  3. data/.github/workflows/spec.yml +14 -3
  4. data/.yardopts +1 -0
  5. data/AUTHORS.txt +14 -1
  6. data/CHANGELOG.md +63 -13
  7. data/README.md +121 -18
  8. data/bin/console +2 -1
  9. data/bin/setup +3 -4
  10. data/codecov.yml +6 -0
  11. data/eth.gemspec +5 -7
  12. data/lib/eth/abi/event.rb +137 -0
  13. data/lib/eth/abi/type.rb +8 -7
  14. data/lib/eth/abi.rb +29 -11
  15. data/lib/eth/address.rb +12 -5
  16. data/lib/eth/api.rb +223 -0
  17. data/lib/eth/chain.rb +31 -28
  18. data/lib/eth/client/http.rb +63 -0
  19. data/lib/eth/client/ipc.rb +50 -0
  20. data/lib/eth/client.rb +468 -0
  21. data/lib/eth/constant.rb +71 -0
  22. data/lib/eth/contract/event.rb +41 -0
  23. data/lib/eth/contract/function.rb +56 -0
  24. data/lib/eth/contract/function_input.rb +36 -0
  25. data/lib/eth/contract/function_output.rb +32 -0
  26. data/lib/eth/contract/initializer.rb +46 -0
  27. data/lib/eth/contract.rb +120 -0
  28. data/lib/eth/eip712.rb +2 -2
  29. data/lib/eth/key/decrypter.rb +16 -13
  30. data/lib/eth/key/encrypter.rb +27 -25
  31. data/lib/eth/key.rb +21 -16
  32. data/lib/eth/rlp/decoder.rb +114 -0
  33. data/lib/eth/rlp/encoder.rb +78 -0
  34. data/lib/eth/rlp/sedes/big_endian_int.rb +66 -0
  35. data/lib/eth/rlp/sedes/binary.rb +97 -0
  36. data/lib/eth/rlp/sedes/list.rb +84 -0
  37. data/lib/eth/rlp/sedes.rb +74 -0
  38. data/lib/eth/rlp.rb +63 -0
  39. data/lib/eth/signature.rb +11 -8
  40. data/lib/eth/solidity.rb +75 -0
  41. data/lib/eth/tx/eip1559.rb +22 -14
  42. data/lib/eth/tx/eip2930.rb +23 -15
  43. data/lib/eth/tx/legacy.rb +14 -10
  44. data/lib/eth/tx.rb +28 -34
  45. data/lib/eth/unit.rb +1 -1
  46. data/lib/eth/util.rb +68 -11
  47. data/lib/eth/version.rb +3 -3
  48. data/lib/eth.rb +15 -2
  49. metadata +31 -23
  50. data/lib/eth/abi/constant.rb +0 -63
@@ -12,7 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- # Provides the `Eth` module.
15
+ # Provides the {Eth} module.
16
16
  module Eth
17
17
 
18
18
  # Provides the `Tx` module supporting various transaction types.
@@ -20,9 +20,11 @@ module Eth
20
20
 
21
21
  # Provides legacy support for transactions on blockchains that do not
22
22
  # implement EIP-1559 but still want to utilize EIP-2718 envelopes.
23
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
23
24
  class Eip2930
24
25
 
25
26
  # The EIP-155 Chain ID.
27
+ # Ref: https://eips.ethereum.org/EIPS/eip-155
26
28
  attr_reader :chain_id
27
29
 
28
30
  # The transaction nonce provided by the signer.
@@ -44,9 +46,10 @@ module Eth
44
46
  attr_reader :payload
45
47
 
46
48
  # An optional EIP-2930 access list.
49
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
47
50
  attr_reader :access_list
48
51
 
49
- # The signature's y-parity byte (not v).
52
+ # The signature's `y`-parity byte (not `v`).
50
53
  attr_reader :signature_y_parity
51
54
 
52
55
  # The signature `r` value.
@@ -64,9 +67,11 @@ module Eth
64
67
  # Create a legacy type-1 (EIP-2930) transaction payload object that
65
68
  # can be prepared for envelope, signature and broadcast. Should not
66
69
  # be used unless there is no EIP-1559 support.
70
+ # Ref: https://eips.ethereum.org/EIPS/eip-2930
67
71
  #
68
72
  #
69
73
  # @param params [Hash] all necessary transaction fields.
74
+ # @option params [Integer] :chain_id the chain ID.
70
75
  # @option params [Integer] :nonce the signer nonce.
71
76
  # @option params [Integer] :gas_price the gas price.
72
77
  # @option params [Integer] :gas_limit the gas limit.
@@ -75,6 +80,7 @@ module Eth
75
80
  # @option params [Integer] :value the transaction value.
76
81
  # @option params [String] :data the transaction data payload.
77
82
  # @option params [Array] :access_list an optional access list.
83
+ # @raise [ParameterError] if gas limit is too low.
78
84
  def initialize(params)
79
85
  fields = { recovery_id: nil, r: 0, s: 0 }.merge params
80
86
 
@@ -86,6 +92,7 @@ module Eth
86
92
  fields[:data] = Tx.sanitize_data fields[:data]
87
93
 
88
94
  # ensure sane values for all mandatory fields
95
+ fields = Tx.validate_params fields
89
96
  fields = Tx.validate_legacy_params fields
90
97
  fields[:access_list] = Tx.sanitize_list fields[:access_list]
91
98
 
@@ -118,7 +125,7 @@ module Eth
118
125
  # Overloads the constructor for decoding raw transactions and creating unsigned copies.
119
126
  konstructor :decode, :unsigned_copy
120
127
 
121
- # Decodes a raw transaction hex into an Eth::Tx::Eip2930
128
+ # Decodes a raw transaction hex into an {Eth::Tx::Eip2930}
122
129
  # transaction object.
123
130
  #
124
131
  # @param hex [String] the raw transaction hex-string.
@@ -132,7 +139,7 @@ module Eth
132
139
  raise TransactionTypeError, "Invalid transaction type #{type}!" if type.to_i(16) != TYPE_2930
133
140
 
134
141
  bin = Util.hex_to_bin hex[2..]
135
- tx = RLP.decode(bin)
142
+ tx = Rlp.decode bin
136
143
 
137
144
  # decoded transactions always have 8 + 3 fields, even if they are empty or zero
138
145
  raise ParameterError, "Transaction missing fields!" if tx.size < 8
@@ -215,8 +222,8 @@ module Eth
215
222
  #
216
223
  # @param key [Eth::Key] the key-pair to use for signing.
217
224
  # @return [String] a transaction hash.
218
- # @raise [SignatureError] if transaction is already signed.
219
- # @raise [SignatureError] if sender address does not match signing key.
225
+ # @raise [Signature::SignatureError] if transaction is already signed.
226
+ # @raise [Signature::SignatureError] if sender address does not match signing key.
220
227
  def sign(key)
221
228
  if Tx.is_signed? self
222
229
  raise Signature::SignatureError, "Transaction is already signed!"
@@ -243,7 +250,7 @@ module Eth
243
250
  # with an EIP-2930 type prefix.
244
251
  #
245
252
  # @return [String] a raw, RLP-encoded EIP-2930 type transaction object.
246
- # @raise [SignatureError] if the transaction is not yet signed.
253
+ # @raise [Signature::SignatureError] if the transaction is not yet signed.
247
254
  def encoded
248
255
  unless Tx.is_signed? self
249
256
  raise Signature::SignatureError, "Transaction is not signed!"
@@ -255,12 +262,12 @@ module Eth
255
262
  tx_data.push Util.serialize_int_to_big_endian @gas_limit
256
263
  tx_data.push Util.hex_to_bin @destination
257
264
  tx_data.push Util.serialize_int_to_big_endian @amount
258
- tx_data.push @payload
259
- tx_data.push @access_list
265
+ tx_data.push Rlp::Sedes.binary.serialize @payload
266
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
260
267
  tx_data.push Util.serialize_int_to_big_endian @signature_y_parity
261
- tx_data.push Util.hex_to_bin @signature_r
262
- tx_data.push Util.hex_to_bin @signature_s
263
- tx_encoded = RLP.encode tx_data
268
+ tx_data.push Util.serialize_int_to_big_endian @signature_r
269
+ tx_data.push Util.serialize_int_to_big_endian @signature_s
270
+ tx_encoded = Rlp.encode tx_data
264
271
 
265
272
  # create an EIP-2718 envelope with EIP-2930 type payload
266
273
  tx_type = Util.serialize_int_to_big_endian @type
@@ -293,9 +300,9 @@ module Eth
293
300
  tx_data.push Util.serialize_int_to_big_endian @gas_limit
294
301
  tx_data.push Util.hex_to_bin @destination
295
302
  tx_data.push Util.serialize_int_to_big_endian @amount
296
- tx_data.push @payload
297
- tx_data.push @access_list
298
- tx_encoded = RLP.encode tx_data
303
+ tx_data.push Rlp::Sedes.binary.serialize @payload
304
+ tx_data.push Rlp::Sedes.infer(@access_list).serialize @access_list
305
+ tx_encoded = Rlp.encode tx_data
299
306
 
300
307
  # create an EIP-2718 envelope with EIP-2930 type payload (unsigned)
301
308
  tx_type = Util.serialize_int_to_big_endian @type
@@ -311,6 +318,7 @@ module Eth
311
318
 
312
319
  private
313
320
 
321
+ # Force-sets an existing signature of a decoded transaction.
314
322
  def _set_signature(recovery_id, r, s)
315
323
  @signature_y_parity = recovery_id
316
324
  @signature_r = r
data/lib/eth/tx/legacy.rb CHANGED
@@ -12,7 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- # Provides the `Eth` module.
15
+ # Provides the {Eth} module.
16
16
  module Eth
17
17
 
18
18
  # Provides the `Tx` module supporting various transaction types.
@@ -50,6 +50,7 @@ module Eth
50
50
  attr_reader :signature_s
51
51
 
52
52
  # The EIP-155 chain ID field.
53
+ # Ref: https://eips.ethereum.org/EIPS/eip-155
53
54
  attr_reader :chain_id
54
55
 
55
56
  # The sender address.
@@ -71,6 +72,7 @@ module Eth
71
72
  # @option params [Integer] :value the transaction value.
72
73
  # @option params [String] :data the transaction data payload.
73
74
  # @param chain_id [Integer] the EIP-155 Chain ID.
75
+ # @raise [ParameterError] if gas limit is too low.
74
76
  def initialize(params, chain_id = Chain::ETHEREUM)
75
77
  fields = { v: chain_id, r: 0, s: 0 }.merge params
76
78
 
@@ -81,6 +83,7 @@ module Eth
81
83
  fields[:data] = Tx.sanitize_data fields[:data]
82
84
 
83
85
  # ensure sane values for all mandatory fields
86
+ fields = Tx.validate_params fields
84
87
  fields = Tx.validate_legacy_params fields
85
88
 
86
89
  # ensure gas limit is not too low
@@ -111,7 +114,7 @@ module Eth
111
114
  # overloads the constructor for decoding raw transactions and creating unsigned copies
112
115
  konstructor :decode, :unsigned_copy
113
116
 
114
- # Decodes a raw transaction hex into an Eth::Tx::Legacy
117
+ # Decodes a raw transaction hex into an {Eth::Tx::Legacy}
115
118
  # transaction object.
116
119
  #
117
120
  # @param hex [String] the raw transaction hex-string.
@@ -119,7 +122,7 @@ module Eth
119
122
  # @raise [ParameterError] if transaction misses fields.
120
123
  def decode(hex)
121
124
  bin = Util.hex_to_bin hex
122
- tx = RLP.decode(bin)
125
+ tx = Rlp.decode bin
123
126
 
124
127
  # decoded transactions always have 9 fields, even if they are empty or zero
125
128
  raise ParameterError, "Transaction missing fields!" if tx.size < 9
@@ -199,8 +202,8 @@ module Eth
199
202
  #
200
203
  # @param key [Eth::Key] the key-pair to use for signing.
201
204
  # @return [String] a transaction hash.
202
- # @raise [SignatureError] if transaction is already signed.
203
- # @raise [SignatureError] if sender address does not match signing key.
205
+ # @raise [Signature::SignatureError] if transaction is already signed.
206
+ # @raise [Signature::SignatureError] if sender address does not match signing key.
204
207
  def sign(key)
205
208
  if Tx.is_signed? self
206
209
  raise Signature::SignatureError, "Transaction is already signed!"
@@ -225,7 +228,7 @@ module Eth
225
228
  # Encodes a raw transaction object.
226
229
  #
227
230
  # @return [String] a raw, RLP-encoded legacy transaction.
228
- # @raise [SignatureError] if the transaction is not yet signed.
231
+ # @raise [Signature::SignatureError] if the transaction is not yet signed.
229
232
  def encoded
230
233
  unless Tx.is_signed? self
231
234
  raise Signature::SignatureError, "Transaction is not signed!"
@@ -236,11 +239,11 @@ module Eth
236
239
  tx_data.push Util.serialize_int_to_big_endian @gas_limit
237
240
  tx_data.push Util.hex_to_bin @destination
238
241
  tx_data.push Util.serialize_int_to_big_endian @amount
239
- tx_data.push @payload
242
+ tx_data.push Rlp::Sedes.binary.serialize @payload
240
243
  tx_data.push Util.serialize_int_to_big_endian @signature_v
241
244
  tx_data.push Util.serialize_int_to_big_endian @signature_r
242
245
  tx_data.push Util.serialize_int_to_big_endian @signature_s
243
- RLP.encode tx_data
246
+ Rlp.encode tx_data
244
247
  end
245
248
 
246
249
  # Gets the encoded, raw transaction hex.
@@ -267,11 +270,11 @@ module Eth
267
270
  tx_data.push Util.serialize_int_to_big_endian @gas_limit
268
271
  tx_data.push Util.hex_to_bin @destination
269
272
  tx_data.push Util.serialize_int_to_big_endian @amount
270
- tx_data.push @payload
273
+ tx_data.push Rlp::Sedes.binary.serialize @payload
271
274
  tx_data.push Util.serialize_int_to_big_endian @chain_id
272
275
  tx_data.push Util.serialize_int_to_big_endian 0
273
276
  tx_data.push Util.serialize_int_to_big_endian 0
274
- RLP.encode tx_data
277
+ Rlp.encode tx_data
275
278
  end
276
279
 
277
280
  # Gets the sign-hash required to sign a raw transaction.
@@ -283,6 +286,7 @@ module Eth
283
286
 
284
287
  private
285
288
 
289
+ # Force-sets an existing signature of a decoded transaction.
286
290
  def _set_signature(v, r, s)
287
291
  @signature_v = v
288
292
  @signature_r = r
data/lib/eth/tx.rb CHANGED
@@ -12,7 +12,6 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require "rlp"
16
15
  require "konstructor"
17
16
 
18
17
  require "eth/chain"
@@ -21,14 +20,14 @@ require "eth/tx/eip2930"
21
20
  require "eth/tx/legacy"
22
21
  require "eth/unit"
23
22
 
24
- # Provides the `Eth` module.
23
+ # Provides the {Eth} module.
25
24
  module Eth
26
25
 
27
26
  # Provides the `Tx` module supporting various transaction types.
28
27
  module Tx
29
28
  extend self
30
29
 
31
- # Provides a special transactoin error if transaction type is unknown.
30
+ # Provides a special transaction error if transaction type is unknown.
32
31
  class TransactionTypeError < TypeError; end
33
32
 
34
33
  # Provides an decoder error if transaction cannot be decoded.
@@ -70,6 +69,9 @@ module Eth
70
69
  # The zero byte is 0x00.
71
70
  ZERO_BYTE = "\x00".freeze
72
71
 
72
+ # Smart contract transaction gas cost
73
+ CREATE_GAS = 32_000.freeze
74
+
73
75
  # Creates a new transaction of any type for given parameters and chain ID.
74
76
  # Required parameters are (optional in brackets):
75
77
  # - EIP-1559: chain_id, nonce, priority_fee, max_gas_fee, gas_limit(, from, to,
@@ -185,28 +187,20 @@ module Eth
185
187
  return gas
186
188
  end
187
189
 
188
- # Validates the common type-2 transaction fields such as nonce, priority
189
- # fee, max gas fee, gas limit, amount, and access list.
190
+ # Validates the common transaction fields such as nonce, gas limit,
191
+ # amount, and access list.
190
192
  #
191
193
  # @param fields [Hash] the transaction fields.
192
194
  # @return [Hash] the validated transaction fields.
193
195
  # @raise [ParameterError] if nonce is an invalid integer.
194
- # @raise [ParameterError] if priority fee is invalid.
195
- # @raise [ParameterError] if max gas fee is invalid.
196
196
  # @raise [ParameterError] if gas limit is invalid.
197
197
  # @raise [ParameterError] if amount is invalid.
198
198
  # @raise [ParameterError] if access list is invalid.
199
199
  def validate_params(fields)
200
- unless fields[:nonce] >= 0
200
+ if fields[:nonce].nil? or fields[:nonce] < 0
201
201
  raise ParameterError, "Invalid signer nonce #{fields[:nonce]}!"
202
202
  end
203
- unless fields[:priority_fee] >= 0
204
- raise ParameterError, "Invalid gas priority fee #{fields[:priority_fee]}!"
205
- end
206
- unless fields[:max_gas_fee] >= 0
207
- raise ParameterError, "Invalid max gas fee #{fields[:max_gas_fee]}!"
208
- end
209
- unless fields[:gas_limit] >= DEFAULT_GAS_LIMIT and fields[:gas_limit] <= BLOCK_GAS_LIMIT
203
+ if fields[:gas_limit].nil? or fields[:gas_limit] < DEFAULT_GAS_LIMIT or fields[:gas_limit] > BLOCK_GAS_LIMIT
210
204
  raise ParameterError, "Invalid gas limit #{fields[:gas_limit]}!"
211
205
  end
212
206
  unless fields[:value] >= 0
@@ -218,32 +212,32 @@ module Eth
218
212
  return fields
219
213
  end
220
214
 
221
- # Validates the common legacy transaction fields such as nonce, gas
222
- # price, gas limit, amount, and access list.
215
+ # Validates the common type-2 transaction fields such as priority
216
+ # fee and max gas fee.
217
+ #
218
+ # @param fields [Hash] the transaction fields.
219
+ # @return [Hash] the validated transaction fields.
220
+ # @raise [ParameterError] if priority fee is invalid.
221
+ # @raise [ParameterError] if max gas fee is invalid.
222
+ def validate_eip1559_params(fields)
223
+ if fields[:priority_fee].nil? or fields[:priority_fee] < 0
224
+ raise ParameterError, "Invalid gas priority fee #{fields[:priority_fee]}!"
225
+ end
226
+ if fields[:max_gas_fee].nil? or fields[:max_gas_fee] < 0
227
+ raise ParameterError, "Invalid max gas fee #{fields[:max_gas_fee]}!"
228
+ end
229
+ return fields
230
+ end
231
+
232
+ # Validates the common legacy transaction fields such as gas price.
223
233
  #
224
234
  # @param fields [Hash] the transaction fields.
225
235
  # @return [Hash] the validated transaction fields.
226
- # @raise [ParameterError] if nonce is an invalid integer.
227
236
  # @raise [ParameterError] if gas price is invalid.
228
- # @raise [ParameterError] if gas limit is invalid.
229
- # @raise [ParameterError] if amount is invalid.
230
- # @raise [ParameterError] if access list is invalid.
231
237
  def validate_legacy_params(fields)
232
- unless fields[:nonce] >= 0
233
- raise ParameterError, "Invalid signer nonce #{fields[:nonce]}!"
234
- end
235
- unless fields[:gas_price] >= 0
238
+ if fields[:gas_price].nil? or fields[:gas_price] < 0
236
239
  raise ParameterError, "Invalid gas price #{fields[:gas_price]}!"
237
240
  end
238
- unless fields[:gas_limit] >= DEFAULT_GAS_LIMIT and fields[:gas_limit] <= BLOCK_GAS_LIMIT
239
- raise ParameterError, "Invalid gas limit #{fields[:gas_limit]}!"
240
- end
241
- unless fields[:value] >= 0
242
- raise ParameterError, "Invalid transaction value #{fields[:value]}!"
243
- end
244
- unless fields[:access_list].nil? or fields[:access_list].is_a? Array
245
- raise ParameterError, "Invalid access list #{fields[:access_list]}!"
246
- end
247
241
  return fields
248
242
  end
249
243
 
data/lib/eth/unit.rb CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  require "bigdecimal"
16
16
 
17
- # Provides the `Eth` module.
17
+ # Provides the {Eth} module.
18
18
  module Eth
19
19
 
20
20
  # Provides constants for common Ethereum units.
data/lib/eth/util.rb CHANGED
@@ -13,12 +13,11 @@
13
13
  # limitations under the License.
14
14
 
15
15
  require "digest/keccak"
16
- require "rlp"
17
16
 
18
- # Provides the `Eth` module.
17
+ # Provides the {Eth} module.
19
18
  module Eth
20
19
 
21
- # Defines handy tools for the `Eth` gem for convenience.
20
+ # Defines handy tools for the {Eth} gem for convenience.
22
21
  module Util
23
22
  extend self
24
23
 
@@ -93,7 +92,7 @@ module Eth
93
92
  # Checks if a string is hex-adecimal.
94
93
  #
95
94
  # @param str [String] a string to be checked.
96
- # @return [String] a match if true; nil if not.
95
+ # @return [String] a match if true; `nil` if not.
97
96
  def is_hex?(str)
98
97
  return false unless str.is_a? String
99
98
  str = remove_hex_prefix str
@@ -103,7 +102,7 @@ module Eth
103
102
  # Checks if a string is prefixed with `0x`.
104
103
  #
105
104
  # @param hex [String] a string to be checked.
106
- # @return [String] a match if true; nil if not.
105
+ # @return [String] a match if true; `nil` if not.
107
106
  def is_prefixed?(hex)
108
107
  hex.match /\A0x/
109
108
  end
@@ -111,14 +110,24 @@ module Eth
111
110
  # Serializes an unsigned integer to big endian.
112
111
  #
113
112
  # @param num [Integer] unsigned integer to be serialized.
114
- # return [String] serialized big endian integer string.
115
- # raises [ArgumentError] if unsigned integer is out of bounds.
113
+ # @return [String] serialized big endian integer string.
114
+ # @raise [ArgumentError] if unsigned integer is out of bounds.
116
115
  def serialize_int_to_big_endian(num)
117
116
  num = num.to_i(16) if is_hex? num
118
- unless num.is_a? Integer and num >= 0 and num <= Abi::UINT_MAX
117
+ unless num.is_a? Integer and num >= 0 and num <= Constant::UINT_MAX
119
118
  raise ArgumentError, "Integer invalid or out of range: #{num}"
120
119
  end
121
- RLP::Sedes.big_endian_int.serialize num
120
+ Rlp::Sedes.big_endian_int.serialize num
121
+ end
122
+
123
+ # Converts an integer to big endian.
124
+ #
125
+ # @param num [Integer] integer to be converted.
126
+ # @return [String] packed, big-endian integer string.
127
+ def int_to_big_endian(num)
128
+ hex = num.to_s(16) unless is_hex? num
129
+ hex = "0#{hex}" if hex.size.odd?
130
+ hex_to_bin hex
122
131
  end
123
132
 
124
133
  # Deserializes big endian data string to integer.
@@ -126,7 +135,55 @@ module Eth
126
135
  # @param str [String] serialized big endian integer string.
127
136
  # @return [Integer] an deserialized unsigned integer.
128
137
  def deserialize_big_endian_to_int(str)
129
- RLP::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "")
138
+ Rlp::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "")
139
+ end
140
+
141
+ # Converts a big endian to an interger.
142
+ #
143
+ # @param str [String] big endian to be converted.
144
+ # @return [Integer] an unpacked integer number.
145
+ def big_endian_to_int(str)
146
+ str.unpack("H*").first.to_i(16)
147
+ end
148
+
149
+ # Converts a binary string to bytes.
150
+ #
151
+ # @param str [String] binary string to be converted.
152
+ # @return [Object] the string bytes.
153
+ def str_to_bytes(str)
154
+ is_bytes?(str) ? str : str.b
155
+ end
156
+
157
+ # Converts bytes to a binary string.
158
+ #
159
+ # @param bin [Object] bytes to be converted.
160
+ # @return [String] a packed binary string.
161
+ def bytes_to_str(bin)
162
+ bin.unpack("U*").pack("U*")
163
+ end
164
+
165
+ # Checks if a string is a byte-string.
166
+ #
167
+ # @param str [String] a string to check.
168
+ # @return [Boolean] true if it's an ASCII-8bit encoded byte-string.
169
+ def is_bytes?(str)
170
+ str && str.instance_of?(String) && str.encoding.name == Constant::BINARY_ENCODING
171
+ end
172
+
173
+ # Checks if the given item is a string primitive.
174
+ #
175
+ # @param item [Object] the item to check.
176
+ # @return [Boolean] true if it's a string primitive.
177
+ def is_primitive?(item)
178
+ item.instance_of?(String)
179
+ end
180
+
181
+ # Checks if the given item is a list.
182
+ #
183
+ # @param item [Object] the item to check.
184
+ # @return [Boolean] true if it's a list.
185
+ def is_list?(item)
186
+ !is_primitive?(item) && item.respond_to?(:each)
130
187
  end
131
188
 
132
189
  # Ceil and integer to the next multiple of 32 bytes.
@@ -154,7 +211,7 @@ module Eth
154
211
  # @param len [Integer] number of symbols for the final string.
155
212
  # @return [String] a zero-padded serialized string of wanted size.
156
213
  def zpad(str, len)
157
- lpad str, Abi::BYTE_ZERO, len
214
+ lpad str, Constant::BYTE_ZERO, len
158
215
  end
159
216
 
160
217
  # Left-pad a hex number with zeros.
data/lib/eth/version.rb CHANGED
@@ -12,9 +12,9 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- # Provides the `Eth` module.
15
+ # Provides the {Eth} module.
16
16
  module Eth
17
17
 
18
- # Defines the version of the `Eth` module.
19
- VERSION = "0.5.0".freeze
18
+ # Defines the version of the {Eth} module.
19
+ VERSION = "0.5.3".freeze
20
20
  end
data/lib/eth.rb CHANGED
@@ -12,17 +12,30 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- # Provides the `Eth` module.
15
+ # Provides the {Eth} module.
16
16
  module Eth
17
17
  end
18
18
 
19
- # Loads the `Eth` module classes.
19
+ # Loads the {Eth} module classes.
20
20
  require "eth/abi"
21
+ require "eth/api"
21
22
  require "eth/address"
22
23
  require "eth/chain"
24
+ require "eth/constant"
25
+ require "eth/contract"
26
+ require "eth/contract/event"
27
+ require "eth/contract/function"
28
+ require "eth/contract/function_input"
29
+ require "eth/contract/function_output"
30
+ require "eth/contract/initializer"
31
+ require "eth/client"
32
+ require "eth/client/http"
33
+ require "eth/client/ipc"
23
34
  require "eth/eip712"
24
35
  require "eth/key"
36
+ require "eth/rlp"
25
37
  require "eth/signature"
38
+ require "eth/solidity"
26
39
  require "eth/tx"
27
40
  require "eth/unit"
28
41
  require "eth/util"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Ellis
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-01-17 00:00:00.000000000 Z
12
+ date: 2022-05-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: keccak
@@ -53,34 +53,20 @@ dependencies:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '5.1'
56
- - !ruby/object:Gem::Dependency
57
- name: rlp
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - "~>"
61
- - !ruby/object:Gem::Version
62
- version: '0.7'
63
- type: :runtime
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - "~>"
68
- - !ruby/object:Gem::Version
69
- version: '0.7'
70
56
  - !ruby/object:Gem::Dependency
71
57
  name: openssl
72
58
  requirement: !ruby/object:Gem::Requirement
73
59
  requirements:
74
60
  - - "~>"
75
61
  - !ruby/object:Gem::Version
76
- version: '3.0'
62
+ version: '2.2'
77
63
  type: :runtime
78
64
  prerelease: false
79
65
  version_requirements: !ruby/object:Gem::Requirement
80
66
  requirements:
81
67
  - - "~>"
82
68
  - !ruby/object:Gem::Version
83
- version: '3.0'
69
+ version: '2.2'
84
70
  - !ruby/object:Gem::Dependency
85
71
  name: scrypt
86
72
  requirement: !ruby/object:Gem::Requirement
@@ -109,6 +95,7 @@ files:
109
95
  - ".gitignore"
110
96
  - ".gitmodules"
111
97
  - ".rspec"
98
+ - ".yardopts"
112
99
  - AUTHORS.txt
113
100
  - CHANGELOG.md
114
101
  - Gemfile
@@ -117,18 +104,38 @@ files:
117
104
  - Rakefile
118
105
  - bin/console
119
106
  - bin/setup
107
+ - codecov.yml
120
108
  - eth.gemspec
121
109
  - lib/eth.rb
122
110
  - lib/eth/abi.rb
123
- - lib/eth/abi/constant.rb
111
+ - lib/eth/abi/event.rb
124
112
  - lib/eth/abi/type.rb
125
113
  - lib/eth/address.rb
114
+ - lib/eth/api.rb
126
115
  - lib/eth/chain.rb
116
+ - lib/eth/client.rb
117
+ - lib/eth/client/http.rb
118
+ - lib/eth/client/ipc.rb
119
+ - lib/eth/constant.rb
120
+ - lib/eth/contract.rb
121
+ - lib/eth/contract/event.rb
122
+ - lib/eth/contract/function.rb
123
+ - lib/eth/contract/function_input.rb
124
+ - lib/eth/contract/function_output.rb
125
+ - lib/eth/contract/initializer.rb
127
126
  - lib/eth/eip712.rb
128
127
  - lib/eth/key.rb
129
128
  - lib/eth/key/decrypter.rb
130
129
  - lib/eth/key/encrypter.rb
130
+ - lib/eth/rlp.rb
131
+ - lib/eth/rlp/decoder.rb
132
+ - lib/eth/rlp/encoder.rb
133
+ - lib/eth/rlp/sedes.rb
134
+ - lib/eth/rlp/sedes/big_endian_int.rb
135
+ - lib/eth/rlp/sedes/binary.rb
136
+ - lib/eth/rlp/sedes/list.rb
131
137
  - lib/eth/signature.rb
138
+ - lib/eth/solidity.rb
132
139
  - lib/eth/tx.rb
133
140
  - lib/eth/tx/eip1559.rb
134
141
  - lib/eth/tx/eip2930.rb
@@ -140,10 +147,11 @@ homepage: https://github.com/q9f/eth.rb
140
147
  licenses:
141
148
  - Apache-2.0
142
149
  metadata:
143
- homepage_uri: https://github.com/q9f/eth.rb
144
- source_code_uri: https://github.com/q9f/eth.rb
145
- github_repo: https://github.com/q9f/eth.rb
146
150
  bug_tracker_uri: https://github.com/q9f/eth.rb/issues
151
+ changelog_uri: https://github.com/q9f/eth.rb/blob/main/CHANGELOG.md
152
+ documentation_uri: https://q9f.github.io/eth.rb/
153
+ github_repo: https://github.com/q9f/eth.rb
154
+ source_code_uri: https://github.com/q9f/eth.rb
147
155
  post_install_message:
148
156
  rdoc_options: []
149
157
  require_paths:
@@ -162,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
170
  - !ruby/object:Gem::Version
163
171
  version: '0'
164
172
  requirements: []
165
- rubygems_version: 3.2.29
173
+ rubygems_version: 3.3.8
166
174
  signing_key:
167
175
  specification_version: 4
168
176
  summary: Ruby Ethereum library.