eth 0.4.18 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/eth/util.rb ADDED
@@ -0,0 +1,178 @@
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
+ require "rlp"
17
+
18
+ # Provides the `Eth` module.
19
+ module Eth
20
+
21
+ # Defines handy tools for the `Eth` gem for convenience.
22
+ module Util
23
+ extend self
24
+
25
+ # Generates an Ethereum address from a given compressed or
26
+ # uncompressed binary or hexadecimal public key string.
27
+ #
28
+ # @param str [String] the public key to be converted.
29
+ # @return [Eth::Address] an Ethereum address.
30
+ def public_key_to_address(str)
31
+ str = hex_to_bin str if is_hex? str
32
+ bytes = keccak256(str[1..-1])[-20..-1]
33
+ Address.new bin_to_prefixed_hex bytes
34
+ end
35
+
36
+ # Hashes a string with the Keccak-256 algorithm.
37
+ #
38
+ # @param str [String] a string to be hashed.
39
+ # @return [String] a Keccak-256 hash of the given string.
40
+ def keccak256(str)
41
+ Digest::Keccak.new(256).digest str
42
+ end
43
+
44
+ # Unpacks a binary string to a hexa-decimal string.
45
+ #
46
+ # @param bin [String] a binary string to be unpacked.
47
+ # @return [String] a hexa-decimal string.
48
+ # @raise [TypeError] if value is not a string.
49
+ def bin_to_hex(bin)
50
+ raise TypeError, "Value must be an instance of String" unless bin.instance_of? String
51
+ bin.unpack("H*").first
52
+ end
53
+
54
+ # Packs a hexa-decimal string into a binary string. Also works with
55
+ # `0x`-prefixed strings.
56
+ #
57
+ # @param hex [String] a hexa-decimal string to be packed.
58
+ # @return [String] a packed binary string.
59
+ # @raise [TypeError] if value is not a string or string is not hex.
60
+ def hex_to_bin(hex)
61
+ raise TypeError, "Value must be an instance of String" unless hex.instance_of? String
62
+ hex = remove_hex_prefix hex
63
+ raise TypeError, "Non-hexadecimal digit found" unless is_hex? hex
64
+ [hex].pack("H*")
65
+ end
66
+
67
+ # Prefixes a hexa-decimal string with `0x`.
68
+ #
69
+ # @param hex [String] a hex-string to be prefixed.
70
+ # @return [String] a prefixed hex-string.
71
+ def prefix_hex(hex)
72
+ return hex if is_prefixed? hex
73
+ return "0x#{hex}"
74
+ end
75
+
76
+ # Removes the `0x` prefix of a hexa-decimal string.
77
+ #
78
+ # @param hex [String] a prefixed hex-string.
79
+ # @return [String] an unprefixed hex-string.
80
+ def remove_hex_prefix(hex)
81
+ return hex[2..-1] if is_prefixed? hex
82
+ return hex
83
+ end
84
+
85
+ # Unpacks a binary string to a prefixed hexa-decimal string.
86
+ #
87
+ # @param bin [String] a binary string to be unpacked.
88
+ # @return [String] a prefixed hexa-decimal string.
89
+ def bin_to_prefixed_hex(bin)
90
+ prefix_hex bin_to_hex bin
91
+ end
92
+
93
+ # Checks if a string is hex-adecimal.
94
+ #
95
+ # @param str [String] a string to be checked.
96
+ # @return [String] a match if true; nil if not.
97
+ def is_hex?(str)
98
+ return false unless str.is_a? String
99
+ str = remove_hex_prefix str
100
+ str.match /\A[0-9a-fA-F]*\z/
101
+ end
102
+
103
+ # Checks if a string is prefixed with `0x`.
104
+ #
105
+ # @param hex [String] a string to be checked.
106
+ # @return [String] a match if true; nil if not.
107
+ def is_prefixed?(hex)
108
+ hex.match /\A0x/
109
+ end
110
+
111
+ # Serializes an unsigned integer to big endian.
112
+ #
113
+ # @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.
116
+ def serialize_int_to_big_endian(num)
117
+ num = num.to_i(16) if is_hex? num
118
+ unless num.is_a? Integer and num >= 0 and num <= Abi::UINT_MAX
119
+ raise ArgumentError, "Integer invalid or out of range: #{num}"
120
+ end
121
+ RLP::Sedes.big_endian_int.serialize num
122
+ end
123
+
124
+ # Deserializes big endian data string to integer.
125
+ #
126
+ # @param str [String] serialized big endian integer string.
127
+ # @return [Integer] an deserialized unsigned integer.
128
+ def deserialize_big_endian_to_int(str)
129
+ RLP::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "")
130
+ end
131
+
132
+ # Ceil and integer to the next multiple of 32 bytes.
133
+ #
134
+ # @param num [Integer] the number to ciel up.
135
+ # @return [Integer] the ceiled to 32 integer.
136
+ def ceil32(num)
137
+ num % 32 == 0 ? num : (num + 32 - num % 32)
138
+ end
139
+
140
+ # Left-pad a number with a symbol.
141
+ #
142
+ # @param str [String] a serialized string to be padded.
143
+ # @param sym [String] a symbol used for left-padding.
144
+ # @param len [Integer] number of symbols for the final string.
145
+ # @return [String] a left-padded serialized string of wanted size.
146
+ def lpad(str, sym, len)
147
+ return str if str.size >= len
148
+ sym * (len - str.size) + str
149
+ end
150
+
151
+ # Left-pad a serialized string with zeros.
152
+ #
153
+ # @param str [String] a serialized string to be padded.
154
+ # @param len [Integer] number of symbols for the final string.
155
+ # @return [String] a zero-padded serialized string of wanted size.
156
+ def zpad(str, len)
157
+ lpad str, Abi::BYTE_ZERO, len
158
+ end
159
+
160
+ # Left-pad a hex number with zeros.
161
+ #
162
+ # @param hex [String] a hex-string to be padded.
163
+ # @param len [Integer] number of symbols for the final string.
164
+ # @return [String] a zero-padded serialized string of wanted size.
165
+ def zpad_hex(hex, len = 32)
166
+ zpad hex_to_bin(hex), len
167
+ end
168
+
169
+ # Left-pad an unsigned integer with zeros.
170
+ #
171
+ # @param num [Integer] an unsigned integer to be padded.
172
+ # @param len [Integer] number of symbols for the final string.
173
+ # @return [String] a zero-padded serialized string of wanted size.
174
+ def zpad_int(num, len = 32)
175
+ zpad serialize_int_to_big_endian(num), len
176
+ end
177
+ end
178
+ end
data/lib/eth/version.rb CHANGED
@@ -1,3 +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.
1
16
  module Eth
2
- VERSION = "0.4.18"
17
+
18
+ # Defines the version of the `Eth` module.
19
+ VERSION = "0.5.0".freeze
3
20
  end
data/lib/eth.rb CHANGED
@@ -1,69 +1,29 @@
1
- require "digest/keccak"
2
- require "ffi"
3
- require "money-tree"
4
- require "rlp"
5
-
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.
6
16
  module Eth
7
- BYTE_ZERO = "\x00".freeze
8
- UINT_MAX = 2 ** 256 - 1
9
-
10
- autoload :Address, "eth/address"
11
- autoload :Gas, "eth/gas"
12
- autoload :Key, "eth/key"
13
- autoload :OpenSsl, "eth/open_ssl"
14
- autoload :Secp256k1, "eth/secp256k1"
15
- autoload :Sedes, "eth/sedes"
16
- autoload :Tx, "eth/tx"
17
- autoload :Utils, "eth/utils"
18
-
19
- class << self
20
- def configure
21
- yield(configuration)
22
- end
23
-
24
- def replayable_chain_id
25
- 27
26
- end
27
-
28
- def chain_id
29
- configuration.chain_id
30
- end
31
-
32
- def v_base
33
- replayable_chain_id
34
- end
35
-
36
- def replayable_v?(v)
37
- [replayable_chain_id, replayable_chain_id + 1].include? v
38
- end
39
-
40
- def tx_data_hex?
41
- !!configuration.tx_data_hex
42
- end
43
-
44
- def chain_id_from_signature(signature)
45
- return nil if Eth.replayable_v?(signature[:v])
46
-
47
- cid = (signature[:v] - 35) / 2
48
- (cid < 1) ? nil : cid
49
- end
50
-
51
- private
52
-
53
- def configuration
54
- @configuration ||= Configuration.new
55
- end
56
- end
57
-
58
- class Configuration
59
- attr_accessor :chain_id, :tx_data_hex
60
-
61
- def initialize
62
- self.chain_id = nil
63
- self.tx_data_hex = true
64
- end
65
- end
66
-
67
- class ValidationError < StandardError; end
68
- class InvalidTransaction < ValidationError; end
69
17
  end
18
+
19
+ # Loads the `Eth` module classes.
20
+ require "eth/abi"
21
+ require "eth/address"
22
+ require "eth/chain"
23
+ require "eth/eip712"
24
+ require "eth/key"
25
+ require "eth/signature"
26
+ require "eth/tx"
27
+ require "eth/unit"
28
+ require "eth/util"
29
+ require "eth/version"
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.4.18
4
+ version: 0.5.0
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-04 00:00:00.000000000 Z
12
+ date: 2022-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: keccak
@@ -26,47 +26,33 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.3'
28
28
  - !ruby/object:Gem::Dependency
29
- name: ffi
29
+ name: konstructor
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '1.15'
34
+ version: '1.0'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '1.15'
41
+ version: '1.0'
42
42
  - !ruby/object:Gem::Dependency
43
- name: money-tree
43
+ name: rbsecp256k1
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0.11'
48
+ version: '5.1'
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0.11'
56
- - !ruby/object:Gem::Dependency
57
- name: openssl
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - "~>"
61
- - !ruby/object:Gem::Version
62
- version: '3.0'
63
- type: :runtime
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - "~>"
68
- - !ruby/object:Gem::Version
69
- version: '3.0'
55
+ version: '5.1'
70
56
  - !ruby/object:Gem::Dependency
71
57
  name: rlp
72
58
  requirement: !ruby/object:Gem::Requirement
@@ -82,7 +68,7 @@ dependencies:
82
68
  - !ruby/object:Gem::Version
83
69
  version: '0.7'
84
70
  - !ruby/object:Gem::Dependency
85
- name: scrypt
71
+ name: openssl
86
72
  requirement: !ruby/object:Gem::Requirement
87
73
  requirements:
88
74
  - - "~>"
@@ -96,62 +82,20 @@ dependencies:
96
82
  - !ruby/object:Gem::Version
97
83
  version: '3.0'
98
84
  - !ruby/object:Gem::Dependency
99
- name: bundler
100
- requirement: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - "~>"
103
- - !ruby/object:Gem::Version
104
- version: '2.2'
105
- type: :development
106
- prerelease: false
107
- version_requirements: !ruby/object:Gem::Requirement
108
- requirements:
109
- - - "~>"
110
- - !ruby/object:Gem::Version
111
- version: '2.2'
112
- - !ruby/object:Gem::Dependency
113
- name: pry
114
- requirement: !ruby/object:Gem::Requirement
115
- requirements:
116
- - - "~>"
117
- - !ruby/object:Gem::Version
118
- version: '0.14'
119
- type: :development
120
- prerelease: false
121
- version_requirements: !ruby/object:Gem::Requirement
122
- requirements:
123
- - - "~>"
124
- - !ruby/object:Gem::Version
125
- version: '0.14'
126
- - !ruby/object:Gem::Dependency
127
- name: rake
128
- requirement: !ruby/object:Gem::Requirement
129
- requirements:
130
- - - "~>"
131
- - !ruby/object:Gem::Version
132
- version: '13.0'
133
- type: :development
134
- prerelease: false
135
- version_requirements: !ruby/object:Gem::Requirement
136
- requirements:
137
- - - "~>"
138
- - !ruby/object:Gem::Version
139
- version: '13.0'
140
- - !ruby/object:Gem::Dependency
141
- name: rspec
85
+ name: scrypt
142
86
  requirement: !ruby/object:Gem::Requirement
143
87
  requirements:
144
88
  - - "~>"
145
89
  - !ruby/object:Gem::Version
146
- version: '3.10'
147
- type: :development
90
+ version: '3.0'
91
+ type: :runtime
148
92
  prerelease: false
149
93
  version_requirements: !ruby/object:Gem::Requirement
150
94
  requirements:
151
95
  - - "~>"
152
96
  - !ruby/object:Gem::Version
153
- version: '3.10'
154
- description: Library to build, parse, and sign Ethereum transactions.
97
+ version: '3.0'
98
+ description: Library to handle Ethereum accounts, messages, and transactions.
155
99
  email:
156
100
  - email@steveell.is
157
101
  - ruby@q9f.cc
@@ -159,12 +103,13 @@ executables: []
159
103
  extensions: []
160
104
  extra_rdoc_files: []
161
105
  files:
162
- - ".github/workflows/build.yml"
163
106
  - ".github/workflows/codeql.yml"
164
107
  - ".github/workflows/docs.yml"
108
+ - ".github/workflows/spec.yml"
165
109
  - ".gitignore"
166
110
  - ".gitmodules"
167
111
  - ".rspec"
112
+ - AUTHORS.txt
168
113
  - CHANGELOG.md
169
114
  - Gemfile
170
115
  - LICENSE.txt
@@ -174,25 +119,31 @@ files:
174
119
  - bin/setup
175
120
  - eth.gemspec
176
121
  - lib/eth.rb
122
+ - lib/eth/abi.rb
123
+ - lib/eth/abi/constant.rb
124
+ - lib/eth/abi/type.rb
177
125
  - lib/eth/address.rb
178
- - lib/eth/gas.rb
126
+ - lib/eth/chain.rb
127
+ - lib/eth/eip712.rb
179
128
  - lib/eth/key.rb
180
129
  - lib/eth/key/decrypter.rb
181
130
  - lib/eth/key/encrypter.rb
182
- - lib/eth/open_ssl.rb
183
- - lib/eth/secp256k1.rb
184
- - lib/eth/sedes.rb
131
+ - lib/eth/signature.rb
185
132
  - lib/eth/tx.rb
186
- - lib/eth/utils.rb
133
+ - lib/eth/tx/eip1559.rb
134
+ - lib/eth/tx/eip2930.rb
135
+ - lib/eth/tx/legacy.rb
136
+ - lib/eth/unit.rb
137
+ - lib/eth/util.rb
187
138
  - lib/eth/version.rb
188
- homepage: https://github.com/se3000/ruby-eth
139
+ homepage: https://github.com/q9f/eth.rb
189
140
  licenses:
190
- - MIT
141
+ - Apache-2.0
191
142
  metadata:
192
- homepage_uri: https://github.com/se3000/ruby-eth
193
- source_code_uri: https://github.com/se3000/ruby-eth
194
- github_repo: https://github.com/se3000/ruby-eth
195
- bug_tracker_uri: https://github.com/se3000/ruby-eth/issues
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
+ bug_tracker_uri: https://github.com/q9f/eth.rb/issues
196
147
  post_install_message:
197
148
  rdoc_options: []
198
149
  require_paths:
@@ -211,8 +162,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
162
  - !ruby/object:Gem::Version
212
163
  version: '0'
213
164
  requirements: []
214
- rubygems_version: 3.2.32
165
+ rubygems_version: 3.2.29
215
166
  signing_key:
216
167
  specification_version: 4
217
- summary: Simple API to sign Ethereum transactions.
168
+ summary: Ruby Ethereum library.
218
169
  test_files: []
data/lib/eth/gas.rb DELETED
@@ -1,7 +0,0 @@
1
- module Eth
2
- class Gas
3
- GTXCOST = 21000 # TX BASE GAS COST
4
- GTXDATANONZERO = 68 # TX DATA NON ZERO BYTE GAS COST
5
- GTXDATAZERO = 4 # TX DATA ZERO BYTE GAS COST
6
- end
7
- end