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
data/lib/eth/abi/type.rb CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  # -*- encoding : ascii-8bit -*-
16
16
 
17
- # Provides the `Eth` module.
17
+ # Provides the {Eth} module.
18
18
  module Eth
19
19
 
20
20
  # Provides a Ruby implementation of the Ethereum Applicatoin Binary Interface (ABI).
@@ -26,7 +26,7 @@ module Eth
26
26
  # Provides a specific parser error if type cannot be determined.
27
27
  class ParseError < StandardError; end
28
28
 
29
- # The base attribute, e.g., `string` or `bytes`
29
+ # The base attribute, e.g., `string` or `bytes`.
30
30
  attr :base_type
31
31
 
32
32
  # The sub-type attribute, e.g., `256` as size of an uint256.
@@ -36,7 +36,7 @@ module Eth
36
36
  attr :dimensions
37
37
 
38
38
  # Create a new Type object for base types, sub types, and dimensions.
39
- # Should use the `Type.parse` parser.
39
+ # Should not be used; use {Type.parse} instead.
40
40
  #
41
41
  # @param base_type [String] the base-type attribute.
42
42
  # @param sub_type [String] the sub-type attribute.
@@ -75,7 +75,7 @@ module Eth
75
75
  @dimensions = dims.map { |x| x[1...-1].to_i }
76
76
  end
77
77
 
78
- # Creata new uint256 type used for size.
78
+ # Creates a new uint256 type used for size.
79
79
  #
80
80
  # @return [Eth::Abi::Type] a uint256 size type.
81
81
  def self.size_type
@@ -85,7 +85,7 @@ module Eth
85
85
  # Compares two types for their attributes.
86
86
  #
87
87
  # @param another_type [Eth::Abi::Type] another type to be compared.
88
- # @return [Bool] true if all attributes match.
88
+ # @return [Boolean] true if all attributes match.
89
89
  def ==(another_type)
90
90
  base_type == another_type.base_type and
91
91
  sub_type == another_type.sub_type and
@@ -111,9 +111,9 @@ module Eth
111
111
  @size ||= s
112
112
  end
113
113
 
114
- # Helper to determine whether array is of dynamic size.
114
+ # Helpes to determine whether array is of dynamic size.
115
115
  #
116
- # @return [Bool] true if array is of dynamic size.
116
+ # @return [Boolean] true if array is of dynamic size.
117
117
  def is_dynamic?
118
118
  size.nil?
119
119
  end
@@ -127,6 +127,7 @@ module Eth
127
127
 
128
128
  private
129
129
 
130
+ # Validates all known base types and raises if an issue occurs.
130
131
  def validate_base_type(base_type, sub_type)
131
132
  case base_type
132
133
  when "string"
data/lib/eth/abi.rb CHANGED
@@ -16,17 +16,16 @@
16
16
 
17
17
  require "konstructor"
18
18
 
19
- require "eth/abi/constant"
19
+ require "eth/abi/event"
20
20
  require "eth/abi/type"
21
21
 
22
- # Provides the `Eth` module.
22
+ # Provides the {Eth} module.
23
23
  module Eth
24
24
 
25
25
  # Provides a Ruby implementation of the Ethereum Applicatoin Binary Interface (ABI).
26
26
  # ref: https://docs.soliditylang.org/en/develop/abi-spec.html
27
27
  module Abi
28
28
  extend self
29
- include Constant
30
29
 
31
30
  # Provides a special encoding error if anything fails to encode.
32
31
  class EncodingError < StandardError; end
@@ -71,7 +70,7 @@ module Eth
71
70
  # Encodes a specific value, either static or dynamic.
72
71
  #
73
72
  # @param type [Eth::Abi::Type] type to be encoded.
74
- # @param arg [String, Number] value to be encoded.
73
+ # @param arg [String|Number] value to be encoded.
75
74
  # @return [String] the encoded type.
76
75
  # @raise [EncodingError] if value does not match type.
77
76
  def encode_type(type, arg)
@@ -80,7 +79,7 @@ module Eth
80
79
 
81
80
  # encodes strings and bytes
82
81
  size = encode_type Type.size_type, arg.size
83
- padding = BYTE_ZERO * (Util.ceil32(arg.size) - arg.size)
82
+ padding = Constant::BYTE_ZERO * (Util.ceil32(arg.size) - arg.size)
84
83
  return "#{size}#{arg}#{padding}"
85
84
  elsif type.is_dynamic?
86
85
  raise EncodingError, "Argument must be an Array" unless arg.instance_of? Array
@@ -113,7 +112,7 @@ module Eth
113
112
  # Encodes primitive types.
114
113
  #
115
114
  # @param type [Eth::Abi::Type] type to be encoded.
116
- # @param arg [String, Number] value to be encoded.
115
+ # @param arg [String|Number] value to be encoded.
117
116
  # @return [String] the encoded primitive type.
118
117
  # @raise [EncodingError] if value does not match type.
119
118
  # @raise [ValueOutOfBounds] if value is out of bounds for type.
@@ -286,41 +285,57 @@ module Eth
286
285
  when "bool"
287
286
 
288
287
  # decoded boolean
289
- return data[-1] == BYTE_ONE
288
+ return data[-1] == Constant::BYTE_ONE
290
289
  else
291
290
  raise DecodingError, "Unknown primitive type: #{type.base_type}"
292
291
  end
293
292
  end
294
293
 
294
+ # Build event signature string from ABI interface.
295
+ #
296
+ # @param interface [Hash] ABI event interface.
297
+ # @return [String] interface signature string.
298
+ def signature(interface)
299
+ name = interface.fetch("name")
300
+ inputs = interface.fetch("inputs", [])
301
+ types = inputs.map { |i| i.fetch("type") }
302
+ "#{name}(#{types.join(",")})"
303
+ end
304
+
295
305
  private
296
306
 
307
+ # Properly encodes unsigned integers.
297
308
  def encode_uint(arg, type)
298
- raise ValueOutOfBounds, "Number out of range: #{arg}" if arg > UINT_MAX or arg < UINT_MIN
309
+ raise ValueOutOfBounds, "Number out of range: #{arg}" if arg > Constant::UINT_MAX or arg < Constant::UINT_MIN
299
310
  real_size = type.sub_type.to_i
300
311
  i = arg.to_i
301
312
  raise ValueOutOfBounds, arg unless i >= 0 and i < 2 ** real_size
302
313
  return Util.zpad_int i
303
314
  end
304
315
 
316
+ # Properly encodes signed integers.
305
317
  def encode_int(arg, type)
306
- raise ValueOutOfBounds, "Number out of range: #{arg}" if arg > INT_MAX or arg < INT_MIN
318
+ raise ValueOutOfBounds, "Number out of range: #{arg}" if arg > Constant::INT_MAX or arg < Constant::INT_MIN
307
319
  real_size = type.sub_type.to_i
308
320
  i = arg.to_i
309
321
  raise ValueOutOfBounds, arg unless i >= -2 ** (real_size - 1) and i < 2 ** (real_size - 1)
310
322
  return Util.zpad_int(i % 2 ** type.sub_type.to_i)
311
323
  end
312
324
 
325
+ # Properly encodes booleans.
313
326
  def encode_bool(arg)
314
327
  raise EncodingError, "Argument is not bool: #{arg}" unless arg.instance_of? TrueClass or arg.instance_of? FalseClass
315
328
  return Util.zpad_int(arg ? 1 : 0)
316
329
  end
317
330
 
331
+ # Properly encodes unsigned fixed-point numbers.
318
332
  def encode_ufixed(arg, type)
319
333
  high, low = type.sub_type.split("x").map(&:to_i)
320
334
  raise ValueOutOfBounds, arg unless arg >= 0 and arg < 2 ** high
321
335
  return Util.zpad_int((arg * 2 ** low).to_i)
322
336
  end
323
337
 
338
+ # Properly encodes signed fixed-point numbers.
324
339
  def encode_fixed(arg, type)
325
340
  high, low = type.sub_type.split("x").map(&:to_i)
326
341
  raise ValueOutOfBounds, arg unless arg >= -2 ** (high - 1) and arg < 2 ** (high - 1)
@@ -328,23 +343,25 @@ module Eth
328
343
  return Util.zpad_int(i % 2 ** (high + low))
329
344
  end
330
345
 
346
+ # Properly encodes byte-strings.
331
347
  def encode_bytes(arg, type)
332
348
  raise EncodingError, "Expecting String: #{arg}" unless arg.instance_of? String
333
349
  if type.sub_type.empty?
334
350
  size = Util.zpad_int arg.size
335
- padding = BYTE_ZERO * (Util.ceil32(arg.size) - arg.size)
351
+ padding = Constant::BYTE_ZERO * (Util.ceil32(arg.size) - arg.size)
336
352
 
337
353
  # variable length string/bytes
338
354
  return "#{size}#{arg}#{padding}"
339
355
  else
340
356
  raise ValueOutOfBounds, arg unless arg.size <= type.sub_type.to_i
341
- padding = BYTE_ZERO * (32 - arg.size)
357
+ padding = Constant::BYTE_ZERO * (32 - arg.size)
342
358
 
343
359
  # fixed length string/bytes
344
360
  return "#{arg}#{padding}"
345
361
  end
346
362
  end
347
363
 
364
+ # Properly encodes hash-strings.
348
365
  def encode_hash(arg, type)
349
366
  size = type.sub_type.to_i
350
367
  raise EncodingError, "Argument too long: #{arg}" unless size > 0 and size <= 32
@@ -365,6 +382,7 @@ module Eth
365
382
  end
366
383
  end
367
384
 
385
+ # Properly encodes addresses.
368
386
  def encode_address(arg)
369
387
  if arg.is_a? Integer
370
388
 
data/lib/eth/address.rb CHANGED
@@ -12,10 +12,10 @@
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
- # The `Eth::Address` class to handle checksummed Ethereum addresses.
18
+ # The {Eth::Address} class to handle checksummed Ethereum addresses.
19
19
  class Address
20
20
 
21
21
  # Provides a special checksum error if EIP-55 is violated.
@@ -24,7 +24,7 @@ module Eth
24
24
  # The prefixed and checksummed Ethereum address.
25
25
  attr_reader :address
26
26
 
27
- # Constructor of the `Eth::Address` class. Creates a new hex
27
+ # Constructor of the {Eth::Address} class. Creates a new hex
28
28
  # prefixed address.
29
29
  #
30
30
  # @param address [String] hex string representing an ethereum address.
@@ -38,9 +38,9 @@ module Eth
38
38
  end
39
39
  end
40
40
 
41
- # Check that the address is valid.
41
+ # Checks that the address is valid.
42
42
  #
43
- # @return [Bool] true if valid address.
43
+ # @return [Boolean] true if valid address.
44
44
  def valid?
45
45
  if !matches_any_format?
46
46
  false
@@ -68,30 +68,37 @@ module Eth
68
68
 
69
69
  private
70
70
 
71
+ # Checks whether the address checksum matches.
71
72
  def checksum_matches?
72
73
  address == checksummed
73
74
  end
74
75
 
76
+ # Checks whether the address is not checksummed.
75
77
  def not_checksummed?
76
78
  all_uppercase? || all_lowercase?
77
79
  end
78
80
 
81
+ # Checks whether the address is all upper-case.
79
82
  def all_uppercase?
80
83
  address.match /(?:0[xX])[A-F0-9]{40}/
81
84
  end
82
85
 
86
+ # Checks whether the address is all lower-case.
83
87
  def all_lowercase?
84
88
  address.match /(?:0[xX])[a-f0-9]{40}/
85
89
  end
86
90
 
91
+ # Checks whether the address matches any known format.
87
92
  def matches_any_format?
88
93
  address.match /\A(?:0[xX])[a-fA-F0-9]{40}\z/
89
94
  end
90
95
 
96
+ # Computes the checksum of the address.
91
97
  def checksum
92
98
  Util.bin_to_hex Util.keccak256 unprefixed.downcase
93
99
  end
94
100
 
101
+ # Removes the hex prefix.
95
102
  def unprefixed
96
103
  Util.remove_hex_prefix address
97
104
  end
data/lib/eth/api.rb ADDED
@@ -0,0 +1,223 @@
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 `Eth::Api` module grouping known RPC commands.
19
+ module Api
20
+
21
+ # Implements the available RPC-APIs provided by Geth version 1.10.15.
22
+ COMMANDS = [
23
+ "admin_addPeer",
24
+ "admin_addTrustedPeer",
25
+ "admin_clearHistory",
26
+ "admin_datadir",
27
+ "admin_exportChain",
28
+ "admin_getDatadir",
29
+ "admin_getNodeInfo",
30
+ "admin_getPeers",
31
+ "admin_importChain",
32
+ "admin_nodeInfo",
33
+ "admin_peers",
34
+ "admin_removePeer",
35
+ "admin_removeTrustedPeer",
36
+ "admin_sleep",
37
+ "admin_sleepBlocks",
38
+ "admin_startHTTP",
39
+ "admin_startRPC",
40
+ "admin_startWS",
41
+ "admin_stopHTTP",
42
+ "admin_stopRPC",
43
+ "admin_stopWS",
44
+ "clique_discard",
45
+ "clique_getProposals",
46
+ "clique_getSigner",
47
+ "clique_getSigners",
48
+ "clique_getSignersAtHash",
49
+ "clique_getSnapshot",
50
+ "clique_getSnapshotAtHash",
51
+ "clique_proposals",
52
+ "clique_propose",
53
+ "clique_status",
54
+ "debug_accountRange",
55
+ "debug_backtraceAt",
56
+ "debug_blockProfile",
57
+ "debug_chaindbCompact",
58
+ "debug_chaindbProperty",
59
+ "debug_cpuProfile",
60
+ "debug_dumpBlock",
61
+ "debug_freeOSMemory",
62
+ "debug_freezeClient",
63
+ "debug_gcStats",
64
+ "debug_getAccessibleState",
65
+ "debug_getBadBlocks",
66
+ "debug_getBlockRlp",
67
+ "debug_getHeaderRlp",
68
+ "debug_getModifiedAccountsByHash",
69
+ "debug_getModifiedAccountsByNumber",
70
+ "debug_goTrace",
71
+ "debug_intermediateRoots",
72
+ "debug_memStats",
73
+ "debug_mutexProfile",
74
+ "debug_preimage",
75
+ "debug_printBlock",
76
+ "debug_seedHash",
77
+ "debug_setBlockProfileRate",
78
+ "debug_setGCPercent",
79
+ "debug_setHead",
80
+ "debug_setMutexProfileFraction",
81
+ "debug_stacks",
82
+ "debug_standardTraceBadBlockToFile",
83
+ "debug_standardTraceBlockToFile",
84
+ "debug_startCPUProfile",
85
+ "debug_startGoTrace",
86
+ "debug_stopCPUProfile",
87
+ "debug_stopGoTrace",
88
+ "debug_storageRangeAt",
89
+ "debug_testSignCliqueBlock",
90
+ "debug_traceBadBlock",
91
+ "debug_traceBlock",
92
+ "debug_traceBlockByHash",
93
+ "debug_traceBlockByNumber",
94
+ "debug_traceBlockFromFile",
95
+ "debug_traceCall",
96
+ "debug_traceTransaction",
97
+ "debug_verbosity",
98
+ "debug_vmodule",
99
+ "debug_writeBlockProfile",
100
+ "debug_writeMemProfile",
101
+ "debug_writeMutexProfile",
102
+ "eth_accounts",
103
+ "eth_blockNumber",
104
+ "eth_call",
105
+ "eth_chainId",
106
+ "eth_coinbase",
107
+ "eth_compile",
108
+ "eth_contract",
109
+ "eth_createAccessList",
110
+ "eth_defaultAccount",
111
+ "eth_defaultBlock",
112
+ "eth_estimateGas",
113
+ "eth_feeHistory",
114
+ "eth_fillTransaction",
115
+ "eth_filter",
116
+ "eth_gasPrice",
117
+ "eth_getAccounts",
118
+ "eth_getBalance",
119
+ "eth_getBlock",
120
+ "eth_getBlockByHash",
121
+ "eth_getBlockByNumber",
122
+ "eth_getBlockNumber",
123
+ "eth_getBlockTransactionCount",
124
+ "eth_getBlockTransactionCountByHash",
125
+ "eth_getBlockTransactionCountByNumber",
126
+ "eth_getBlockUncleCount",
127
+ "eth_getCode",
128
+ "eth_getCoinbase",
129
+ "eth_getCompilers",
130
+ "eth_getFilterChanges",
131
+ "eth_getFilterLogs",
132
+ "eth_getGasPrice",
133
+ "eth_getHashrate",
134
+ "eth_getHeaderByHash",
135
+ "eth_getHeaderByNumber",
136
+ "eth_getLogs",
137
+ "eth_getMaxPriorityFeePerGas",
138
+ "eth_getMining",
139
+ "eth_getPendingTransactions",
140
+ "eth_getProof",
141
+ "eth_getProtocolVersion",
142
+ "eth_getRawTransaction",
143
+ "eth_getRawTransactionFromBlock",
144
+ "eth_getStorageAt",
145
+ "eth_getSyncing",
146
+ "eth_getTransaction",
147
+ "eth_getTransactionByBlockHashAndIndex",
148
+ "eth_getTransactionByBlockNumberAndIndex",
149
+ "eth_getTransactionByHash",
150
+ "eth_getTransactionCount",
151
+ "eth_getTransactionFromBlock",
152
+ "eth_getTransactionReceipt",
153
+ "eth_getUncle",
154
+ "eth_getUncleCountByBlockHash",
155
+ "eth_getUncleCountByBlockNumber",
156
+ "eth_getWork",
157
+ "eth_hashrate",
158
+ "eth_iban",
159
+ "eth_icapNamereg",
160
+ "eth_isSyncing",
161
+ "eth_maxPriorityFeePerGas",
162
+ "eth_mining",
163
+ "eth_namereg",
164
+ "eth_newBlockFilter",
165
+ "eth_newFilter",
166
+ "eth_newPendingTransactionFilter",
167
+ "eth_pendingTransactions",
168
+ "eth_protocolVersion",
169
+ "eth_resend",
170
+ "eth_sendIBANTransaction",
171
+ "eth_sendRawTransaction",
172
+ "eth_sendTransaction",
173
+ "eth_sign",
174
+ "eth_signTransaction",
175
+ "eth_submitHashrate",
176
+ "eth_submitTransaction",
177
+ "eth_submitWork",
178
+ "eth_subscribe",
179
+ "eth_syncing",
180
+ "eth_uninstallFilter",
181
+ "eth_unsubscribe",
182
+ "les_addBalance",
183
+ "les_clientInfo",
184
+ "les_getCheckpoint",
185
+ "les_getCheckpointContractAddress",
186
+ "les_latestCheckpoint",
187
+ "les_priorityClientInfo",
188
+ "les_serverInfo",
189
+ "les_setClientParams",
190
+ "les_setDefaultParams",
191
+ "miner_getHashrate",
192
+ "miner_setEtherbase",
193
+ "miner_setExtra",
194
+ "miner_setGasLimit",
195
+ "miner_setGasPrice",
196
+ "miner_start",
197
+ "miner_stop",
198
+ "personal_deriveAccount",
199
+ "personal_ecRecover",
200
+ "personal_getListAccounts",
201
+ "personal_getListWallets",
202
+ "personal_importRawKey",
203
+ "personal_initializeWallet",
204
+ "personal_listAccounts",
205
+ "personal_listWallets",
206
+ "personal_lockAccount",
207
+ "personal_newAccount",
208
+ "personal_openWallet",
209
+ "personal_sendTransaction",
210
+ "personal_sign",
211
+ "personal_signTransaction",
212
+ "personal_unlockAccount",
213
+ "personal_unpair",
214
+ "txpool_content",
215
+ "txpool_contentFrom",
216
+ "txpool_getContent",
217
+ "txpool_getInspect",
218
+ "txpool_getStatus",
219
+ "txpool_inspect",
220
+ "txpool_status",
221
+ ]
222
+ end
223
+ end
data/lib/eth/chain.rb CHANGED
@@ -12,82 +12,85 @@
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
- # Encapsulates `Chain` IDs and utilities for EIP-155 compatibility.
19
- # ref: https://eips.ethereum.org/EIPS/eip-155
18
+ # Encapsulates {Eth::Chain} IDs and utilities for EIP-155 compatibility.
19
+ # Ref: https://eips.ethereum.org/EIPS/eip-155
20
20
  module Chain
21
21
  extend self
22
22
 
23
23
  # Provides a special replay protection error if EIP-155 is violated.
24
24
  class ReplayProtectionError < StandardError; end
25
25
 
26
- # Chain ID for Ethereum mainnet
26
+ # Chain ID for Ethereum mainnet.
27
27
  ETHEREUM = 1.freeze
28
28
 
29
- # Chain ID for Expanse mainnet
29
+ # Chain ID for Expanse mainnet.
30
30
  EXPANSE = 2.freeze
31
31
 
32
- # Chain ID for Optimistic Ethereum mainnet
32
+ # Chain ID for Optimistic Ethereum mainnet.
33
33
  OPTIMISM = 10.freeze
34
34
 
35
- # Chain ID for Ethereum Classic mainnet
35
+ # Chain ID for Ethereum Classic mainnet.
36
36
  CLASSIC = 61.freeze
37
37
 
38
- # Chain ID for POA Network mainnet
38
+ # Chain ID for POA Network mainnet.
39
39
  POA_NET = 99.freeze
40
40
 
41
- # Chain ID for xDAI mainnet
41
+ # Chain ID for Gnosis mainnet.
42
42
  XDAI = 100.freeze
43
43
 
44
- # Chain ID for Arbitrum mainnet
44
+ # Chain ID for Arbitrum mainnet.
45
45
  ARBITRUM = 42161.freeze
46
46
 
47
- # Chain ID for Morden (Ethereum) testnet
47
+ # Chain ID for Morden (Ethereum) testnet.
48
48
  MORDEN = 2.freeze
49
49
 
50
- # Chain ID for Ropsten testnet
50
+ # Chain ID for Ropsten testnet.
51
51
  ROPSTEN = 3.freeze
52
52
 
53
- # Chain ID for Rinkeby testnet
53
+ # Chain ID for Rinkeby testnet.
54
54
  RINKEBY = 4.freeze
55
55
 
56
- # Chain ID for Goerli testnet
56
+ # Chain ID for Goerli testnet.
57
57
  GOERLI = 5.freeze
58
58
 
59
- # Chain ID for Kotti testnet
59
+ # Chain ID for Kotti testnet.
60
60
  KOTTI = 6.freeze
61
61
 
62
- # Chain ID for Kovan testnet
62
+ # Chain ID for Kovan testnet.
63
63
  KOVAN = 42.freeze
64
64
 
65
- # Chain ID for Morden (Classic) testnet
65
+ # Chain ID for Morden (Classic) testnet.
66
66
  MORDEN_CLASSIC = 62.freeze
67
67
 
68
- # Chain ID for Mordor testnet
68
+ # Chain ID for Mordor testnet.
69
69
  MORDOR = 63.freeze
70
70
 
71
- # Chain ID for Optimistik Kovan testnet
71
+ # Chain ID for Optimistik Kovan testnet.
72
72
  KOVAN_OPTIMISM = 69.freeze
73
73
 
74
- # Chain ID for Arbitrum xDAI testnet
74
+ # Chain ID for Arbitrum xDAI testnet.
75
75
  XDAI_ARBITRUM = 200.freeze
76
76
 
77
- # Chain ID for Optimistic Goerli testnet
77
+ # Chain ID for Optimistic Goerli testnet.
78
78
  GOERLI_OPTIMISM = 420.freeze
79
79
 
80
- # Chain ID for Arbitrum Rinkeby testnet
80
+ # Chain ID for Arbitrum Rinkeby testnet.
81
81
  RINKEBY_ARBITRUM = 421611.freeze
82
82
 
83
- # Chain ID for the geth private network preset
83
+ # Chain ID for Sepolia testnet.
84
+ SEPOLIA = 11155111.freeze
85
+
86
+ # Chain ID for the geth private network preset.
84
87
  PRIVATE_GETH = 1337.freeze
85
88
 
86
89
  # Indicates wether the given `v` indicates a legacy chain value
87
90
  # without EIP-155 replay protection.
88
91
  #
89
- # @param v [Integer] the signature's `v` value
90
- # @return [Boolean] true if legacy value
92
+ # @param v [Integer] the signature's `v` value.
93
+ # @return [Boolean] true if legacy value.
91
94
  def is_legacy?(v)
92
95
  [27, 28].include? v
93
96
  end
@@ -95,7 +98,7 @@ module Eth
95
98
  # Convert a given `v` value to an ECDSA recovery id for the given
96
99
  # EIP-155 chain ID.
97
100
  #
98
- # @param v [Integer] the signature's `v` value
101
+ # @param v [Integer] the signature's `v` value.
99
102
  # @param chain_id [Integer] the chain id the signature was generated on.
100
103
  # @return [Integer] the recovery id corresponding to `v`.
101
104
  # @raise [ReplayProtectionError] if the given `v` is invalid.
@@ -134,10 +137,10 @@ module Eth
134
137
  end
135
138
 
136
139
  # Converst a `v` value into a chain ID. This does not work for legacy signatures
137
- # with v < 36 that do not conform with EIP-155.
140
+ # with `v < 36` that do not conform with EIP-155.
138
141
  #
139
142
  # @param v [Integer] the signature's `v` value.
140
- # @return [Integer] the chain id as per EIP-155 or nil if there is no replay protection.
143
+ # @return [Integer] the chain id as per EIP-155 or `nil` if there is no replay protection.
141
144
  def to_chain_id(v)
142
145
  return nil if v < 36
143
146
  chain_id = (v - 35) / 2
@@ -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
+ require "net/http"
16
+
17
+ # Provides the {Eth} module.
18
+ module Eth
19
+
20
+ # Provides an HTTP/S-RPC client.
21
+ class Client::Http < Client
22
+
23
+ # The host of the HTTP endpoint.
24
+ attr_reader :host
25
+
26
+ # The port of the HTTP endpoint.
27
+ attr_reader :port
28
+
29
+ # The full URI of the HTTP endpoint, including path.
30
+ attr_reader :uri
31
+
32
+ # Attribute indicator for SSL.
33
+ attr_reader :ssl
34
+
35
+ # Constructor for the HTTP Client. Should not be used; use
36
+ # {Client.create} intead.
37
+ #
38
+ # @param host [String] an URI pointing to an HTTP RPC-API.
39
+ def initialize(host)
40
+ super
41
+ uri = URI.parse(host)
42
+ raise ArgumentError, "Unable to parse the HTTP-URI!" unless ["http", "https"].include? uri.scheme
43
+ @host = uri.host
44
+ @port = uri.port
45
+ @ssl = uri.scheme == "https"
46
+ @uri = URI("#{uri.scheme}://#{@host}:#{@port}#{uri.path}")
47
+ end
48
+
49
+ # Sends an RPC request to the connected HTTP client.
50
+ #
51
+ # @param payload [Hash] the RPC request parameters.
52
+ # @return [String] a JSON-encoded response.
53
+ def send(payload)
54
+ http = Net::HTTP.new(@host, @port)
55
+ http.use_ssl = @ssl
56
+ header = { "Content-Type" => "application/json" }
57
+ request = Net::HTTP::Post.new(@uri, header)
58
+ request.body = payload
59
+ response = http.request(request)
60
+ response.body
61
+ end
62
+ end
63
+ end