eth-custom 0.5.7

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 (57) hide show
  1. checksums.yaml +7 -0
  2. data/.github/dependabot.yml +18 -0
  3. data/.github/workflows/codeql.yml +48 -0
  4. data/.github/workflows/docs.yml +26 -0
  5. data/.github/workflows/spec.yml +52 -0
  6. data/.gitignore +43 -0
  7. data/.gitmodules +3 -0
  8. data/.rspec +4 -0
  9. data/.yardopts +1 -0
  10. data/AUTHORS.txt +29 -0
  11. data/CHANGELOG.md +218 -0
  12. data/Gemfile +17 -0
  13. data/LICENSE.txt +202 -0
  14. data/README.md +347 -0
  15. data/Rakefile +6 -0
  16. data/bin/console +10 -0
  17. data/bin/setup +9 -0
  18. data/codecov.yml +6 -0
  19. data/eth.gemspec +51 -0
  20. data/lib/eth/abi/event.rb +137 -0
  21. data/lib/eth/abi/type.rb +178 -0
  22. data/lib/eth/abi.rb +446 -0
  23. data/lib/eth/address.rb +106 -0
  24. data/lib/eth/api.rb +223 -0
  25. data/lib/eth/chain.rb +157 -0
  26. data/lib/eth/client/http.rb +63 -0
  27. data/lib/eth/client/ipc.rb +50 -0
  28. data/lib/eth/client.rb +499 -0
  29. data/lib/eth/constant.rb +71 -0
  30. data/lib/eth/contract/event.rb +42 -0
  31. data/lib/eth/contract/function.rb +57 -0
  32. data/lib/eth/contract/function_input.rb +38 -0
  33. data/lib/eth/contract/function_output.rb +37 -0
  34. data/lib/eth/contract/initializer.rb +47 -0
  35. data/lib/eth/contract.rb +143 -0
  36. data/lib/eth/eip712.rb +184 -0
  37. data/lib/eth/key/decrypter.rb +146 -0
  38. data/lib/eth/key/encrypter.rb +207 -0
  39. data/lib/eth/key.rb +167 -0
  40. data/lib/eth/rlp/decoder.rb +114 -0
  41. data/lib/eth/rlp/encoder.rb +78 -0
  42. data/lib/eth/rlp/sedes/big_endian_int.rb +66 -0
  43. data/lib/eth/rlp/sedes/binary.rb +97 -0
  44. data/lib/eth/rlp/sedes/list.rb +84 -0
  45. data/lib/eth/rlp/sedes.rb +74 -0
  46. data/lib/eth/rlp.rb +63 -0
  47. data/lib/eth/signature.rb +163 -0
  48. data/lib/eth/solidity.rb +75 -0
  49. data/lib/eth/tx/eip1559.rb +337 -0
  50. data/lib/eth/tx/eip2930.rb +329 -0
  51. data/lib/eth/tx/legacy.rb +297 -0
  52. data/lib/eth/tx.rb +322 -0
  53. data/lib/eth/unit.rb +49 -0
  54. data/lib/eth/util.rb +235 -0
  55. data/lib/eth/version.rb +20 -0
  56. data/lib/eth.rb +35 -0
  57. metadata +184 -0
data/lib/eth/util.rb ADDED
@@ -0,0 +1,235 @@
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 "digest/keccak"
16
+
17
+ # Provides the {Eth} module.
18
+ module Eth
19
+
20
+ # Defines handy tools for the {Eth} gem for convenience.
21
+ module Util
22
+ extend self
23
+
24
+ # Generates an Ethereum address from a given compressed or
25
+ # uncompressed binary or hexadecimal public key string.
26
+ #
27
+ # @param str [String] the public key to be converted.
28
+ # @return [Eth::Address] an Ethereum address.
29
+ def public_key_to_address(str)
30
+ str = hex_to_bin str if is_hex? str
31
+ bytes = keccak256(str[1..-1])[-20..-1]
32
+ Address.new bin_to_prefixed_hex bytes
33
+ end
34
+
35
+ # Hashes a string with the Keccak-256 algorithm.
36
+ #
37
+ # @param str [String] a string to be hashed.
38
+ # @return [String] a Keccak-256 hash of the given string.
39
+ def keccak256(str)
40
+ Digest::Keccak.new(256).digest str
41
+ end
42
+
43
+ # Unpacks a binary string to a hexa-decimal string.
44
+ #
45
+ # @param bin [String] a binary string to be unpacked.
46
+ # @return [String] a hexa-decimal string.
47
+ # @raise [TypeError] if value is not a string.
48
+ def bin_to_hex(bin)
49
+ raise TypeError, "Value must be an instance of String" unless bin.instance_of? String
50
+ bin.unpack("H*").first
51
+ end
52
+
53
+ # Packs a hexa-decimal string into a binary string. Also works with
54
+ # `0x`-prefixed strings.
55
+ #
56
+ # @param hex [String] a hexa-decimal string to be packed.
57
+ # @return [String] a packed binary string.
58
+ # @raise [TypeError] if value is not a string or string is not hex.
59
+ def hex_to_bin(hex)
60
+ raise TypeError, "Value must be an instance of String" unless hex.instance_of? String
61
+ hex = remove_hex_prefix hex
62
+ raise TypeError, "Non-hexadecimal digit found" unless is_hex? hex
63
+ [hex].pack("H*")
64
+ end
65
+
66
+ # Prefixes a hexa-decimal string with `0x`.
67
+ #
68
+ # @param hex [String] a hex-string to be prefixed.
69
+ # @return [String] a prefixed hex-string.
70
+ def prefix_hex(hex)
71
+ return hex if is_prefixed? hex
72
+ return "0x#{hex}"
73
+ end
74
+
75
+ # Removes the `0x` prefix of a hexa-decimal string.
76
+ #
77
+ # @param hex [String] a prefixed hex-string.
78
+ # @return [String] an unprefixed hex-string.
79
+ def remove_hex_prefix(hex)
80
+ return hex[2..-1] if is_prefixed? hex
81
+ return hex
82
+ end
83
+
84
+ # Unpacks a binary string to a prefixed hexa-decimal string.
85
+ #
86
+ # @param bin [String] a binary string to be unpacked.
87
+ # @return [String] a prefixed hexa-decimal string.
88
+ def bin_to_prefixed_hex(bin)
89
+ prefix_hex bin_to_hex bin
90
+ end
91
+
92
+ # Checks if a string is hex-adecimal.
93
+ #
94
+ # @param str [String] a string to be checked.
95
+ # @return [String] a match if true; `nil` if not.
96
+ def is_hex?(str)
97
+ return false unless str.is_a? String
98
+ str = remove_hex_prefix str
99
+ str.match /\A[0-9a-fA-F]*\z/
100
+ end
101
+
102
+ # Checks if a string is prefixed with `0x`.
103
+ #
104
+ # @param hex [String] a string to be checked.
105
+ # @return [String] a match if true; `nil` if not.
106
+ def is_prefixed?(hex)
107
+ hex.match /\A0x/
108
+ end
109
+
110
+ # Serializes an unsigned integer to big endian.
111
+ #
112
+ # @param num [Integer] unsigned integer to be serialized.
113
+ # @return [String] serialized big endian integer string.
114
+ # @raise [ArgumentError] if unsigned integer is out of bounds.
115
+ def serialize_int_to_big_endian(num)
116
+ num = num.to_i(16) if is_hex? num
117
+ unless num.is_a? Integer and num >= 0 and num <= Constant::UINT_MAX
118
+ raise ArgumentError, "Integer invalid or out of range: #{num}"
119
+ end
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
131
+ end
132
+
133
+ # Deserializes big endian data string to integer.
134
+ #
135
+ # @param str [String] serialized big endian integer string.
136
+ # @return [Integer] an deserialized unsigned integer.
137
+ def deserialize_big_endian_to_int(str)
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)
187
+ end
188
+
189
+ # Ceil and integer to the next multiple of 32 bytes.
190
+ #
191
+ # @param num [Integer] the number to ciel up.
192
+ # @return [Integer] the ceiled to 32 integer.
193
+ def ceil32(num)
194
+ num % 32 == 0 ? num : (num + 32 - num % 32)
195
+ end
196
+
197
+ # Left-pad a number with a symbol.
198
+ #
199
+ # @param str [String] a serialized string to be padded.
200
+ # @param sym [String] a symbol used for left-padding.
201
+ # @param len [Integer] number of symbols for the final string.
202
+ # @return [String] a left-padded serialized string of wanted size.
203
+ def lpad(str, sym, len)
204
+ return str if str.size >= len
205
+ sym * (len - str.size) + str
206
+ end
207
+
208
+ # Left-pad a serialized string with zeros.
209
+ #
210
+ # @param str [String] a serialized string to be padded.
211
+ # @param len [Integer] number of symbols for the final string.
212
+ # @return [String] a zero-padded serialized string of wanted size.
213
+ def zpad(str, len)
214
+ lpad str, Constant::BYTE_ZERO, len
215
+ end
216
+
217
+ # Left-pad a hex number with zeros.
218
+ #
219
+ # @param hex [String] a hex-string to be padded.
220
+ # @param len [Integer] number of symbols for the final string.
221
+ # @return [String] a zero-padded serialized string of wanted size.
222
+ def zpad_hex(hex, len = 32)
223
+ zpad hex_to_bin(hex), len
224
+ end
225
+
226
+ # Left-pad an unsigned integer with zeros.
227
+ #
228
+ # @param num [Integer] an unsigned integer to be padded.
229
+ # @param len [Integer] number of symbols for the final string.
230
+ # @return [String] a zero-padded serialized string of wanted size.
231
+ def zpad_int(num, len = 32)
232
+ zpad serialize_int_to_big_endian(num), len
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,20 @@
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
+ # Defines the version of the {Eth} module.
19
+ VERSION = "0.5.7".freeze
20
+ end
data/lib/eth.rb ADDED
@@ -0,0 +1,35 @@
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
+ end
18
+
19
+ # Loads the {Eth} module classes.
20
+ require "eth/abi"
21
+ require "eth/api"
22
+ require "eth/address"
23
+ require "eth/chain"
24
+ require "eth/constant"
25
+ require "eth/contract"
26
+ require "eth/client"
27
+ require "eth/eip712"
28
+ require "eth/key"
29
+ require "eth/rlp"
30
+ require "eth/signature"
31
+ require "eth/solidity"
32
+ require "eth/tx"
33
+ require "eth/unit"
34
+ require "eth/util"
35
+ require "eth/version"
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eth-custom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.7
5
+ platform: ruby
6
+ authors:
7
+ - Steve Ellis
8
+ - Afri Schoedon
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2022-10-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: keccak
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: konstructor
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rbsecp256k1
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 5.0.1
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 5.0.1
56
+ - !ruby/object:Gem::Dependency
57
+ name: openssl
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '1.1'
63
+ - - "<"
64
+ - !ruby/object:Gem::Version
65
+ version: '4.0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '1.1'
73
+ - - "<"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ - !ruby/object:Gem::Dependency
77
+ name: scrypt
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.7
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.7
90
+ description: Library to handle Ethereum accounts, messages, and transactions.
91
+ email:
92
+ - email@steveell.is
93
+ - ruby@q9f.cc
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - ".github/dependabot.yml"
99
+ - ".github/workflows/codeql.yml"
100
+ - ".github/workflows/docs.yml"
101
+ - ".github/workflows/spec.yml"
102
+ - ".gitignore"
103
+ - ".gitmodules"
104
+ - ".rspec"
105
+ - ".yardopts"
106
+ - AUTHORS.txt
107
+ - CHANGELOG.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - codecov.yml
115
+ - eth.gemspec
116
+ - lib/eth.rb
117
+ - lib/eth/abi.rb
118
+ - lib/eth/abi/event.rb
119
+ - lib/eth/abi/type.rb
120
+ - lib/eth/address.rb
121
+ - lib/eth/api.rb
122
+ - lib/eth/chain.rb
123
+ - lib/eth/client.rb
124
+ - lib/eth/client/http.rb
125
+ - lib/eth/client/ipc.rb
126
+ - lib/eth/constant.rb
127
+ - lib/eth/contract.rb
128
+ - lib/eth/contract/event.rb
129
+ - lib/eth/contract/function.rb
130
+ - lib/eth/contract/function_input.rb
131
+ - lib/eth/contract/function_output.rb
132
+ - lib/eth/contract/initializer.rb
133
+ - lib/eth/eip712.rb
134
+ - lib/eth/key.rb
135
+ - lib/eth/key/decrypter.rb
136
+ - lib/eth/key/encrypter.rb
137
+ - lib/eth/rlp.rb
138
+ - lib/eth/rlp/decoder.rb
139
+ - lib/eth/rlp/encoder.rb
140
+ - lib/eth/rlp/sedes.rb
141
+ - lib/eth/rlp/sedes/big_endian_int.rb
142
+ - lib/eth/rlp/sedes/binary.rb
143
+ - lib/eth/rlp/sedes/list.rb
144
+ - lib/eth/signature.rb
145
+ - lib/eth/solidity.rb
146
+ - lib/eth/tx.rb
147
+ - lib/eth/tx/eip1559.rb
148
+ - lib/eth/tx/eip2930.rb
149
+ - lib/eth/tx/legacy.rb
150
+ - lib/eth/unit.rb
151
+ - lib/eth/util.rb
152
+ - lib/eth/version.rb
153
+ homepage: https://github.com/aravinth-elangovan/eth.rb
154
+ licenses:
155
+ - Apache-2.0
156
+ metadata:
157
+ bug_tracker_uri: https://github.com/q9f/eth.rb/issues
158
+ changelog_uri: https://github.com/q9f/eth.rb/blob/main/CHANGELOG.md
159
+ documentation_uri: https://q9f.github.io/eth.rb/
160
+ github_repo: https://github.com/aravinth-elangovan/eth.rb
161
+ source_code_uri: https://github.com/aravinth-elangovan/eth.rb
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '2.2'
171
+ - - "<"
172
+ - !ruby/object:Gem::Version
173
+ version: '4.0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubygems_version: 3.0.9
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Ruby Ethereum library.
184
+ test_files: []