eth 0.4.12 → 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.12"
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/sha3'
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,31 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.12
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Ellis
8
- autorequire:
8
+ - Afri Schoedon
9
+ autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2020-03-04 00:00:00.000000000 Z
12
+ date: 2022-01-17 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
- name: digest-sha3-patched
15
+ name: keccak
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
18
  - - "~>"
18
19
  - !ruby/object:Gem::Version
19
- version: '1.1'
20
+ version: '1.3'
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
25
  - - "~>"
25
26
  - !ruby/object:Gem::Version
26
- version: '1.1'
27
+ version: '1.3'
27
28
  - !ruby/object:Gem::Dependency
28
- name: ffi
29
+ name: konstructor
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
32
  - - "~>"
@@ -39,100 +40,76 @@ dependencies:
39
40
  - !ruby/object:Gem::Version
40
41
  version: '1.0'
41
42
  - !ruby/object:Gem::Dependency
42
- name: money-tree
43
+ name: rbsecp256k1
43
44
  requirement: !ruby/object:Gem::Requirement
44
45
  requirements:
45
46
  - - "~>"
46
47
  - !ruby/object:Gem::Version
47
- version: 0.10.0
48
+ version: '5.1'
48
49
  type: :runtime
49
50
  prerelease: false
50
51
  version_requirements: !ruby/object:Gem::Requirement
51
52
  requirements:
52
53
  - - "~>"
53
54
  - !ruby/object:Gem::Version
54
- version: 0.10.0
55
+ version: '5.1'
55
56
  - !ruby/object:Gem::Dependency
56
57
  name: rlp
57
58
  requirement: !ruby/object:Gem::Requirement
58
59
  requirements:
59
60
  - - "~>"
60
61
  - !ruby/object:Gem::Version
61
- version: 0.7.3
62
+ version: '0.7'
62
63
  type: :runtime
63
64
  prerelease: false
64
65
  version_requirements: !ruby/object:Gem::Requirement
65
66
  requirements:
66
67
  - - "~>"
67
68
  - !ruby/object:Gem::Version
68
- version: 0.7.3
69
+ version: '0.7'
69
70
  - !ruby/object:Gem::Dependency
70
- name: scrypt
71
+ name: openssl
71
72
  requirement: !ruby/object:Gem::Requirement
72
73
  requirements:
73
74
  - - "~>"
74
75
  - !ruby/object:Gem::Version
75
- version: 3.0.6
76
+ version: '3.0'
76
77
  type: :runtime
77
78
  prerelease: false
78
79
  version_requirements: !ruby/object:Gem::Requirement
79
80
  requirements:
80
81
  - - "~>"
81
82
  - !ruby/object:Gem::Version
82
- version: 3.0.6
83
- - !ruby/object:Gem::Dependency
84
- name: pry
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '0.1'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '0.1'
97
- - !ruby/object:Gem::Dependency
98
- name: rake
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: 12.3.3
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: 12.3.3
83
+ version: '3.0'
111
84
  - !ruby/object:Gem::Dependency
112
- name: rspec
85
+ name: scrypt
113
86
  requirement: !ruby/object:Gem::Requirement
114
87
  requirements:
115
88
  - - "~>"
116
89
  - !ruby/object:Gem::Version
117
90
  version: '3.0'
118
- type: :development
91
+ type: :runtime
119
92
  prerelease: false
120
93
  version_requirements: !ruby/object:Gem::Requirement
121
94
  requirements:
122
95
  - - "~>"
123
96
  - !ruby/object:Gem::Version
124
97
  version: '3.0'
125
- description: Library to build, parse, and sign Ethereum transactions.
98
+ description: Library to handle Ethereum accounts, messages, and transactions.
126
99
  email:
127
100
  - email@steveell.is
101
+ - ruby@q9f.cc
128
102
  executables: []
129
103
  extensions: []
130
104
  extra_rdoc_files: []
131
105
  files:
106
+ - ".github/workflows/codeql.yml"
107
+ - ".github/workflows/docs.yml"
108
+ - ".github/workflows/spec.yml"
132
109
  - ".gitignore"
133
110
  - ".gitmodules"
134
111
  - ".rspec"
135
- - ".travis.yml"
112
+ - AUTHORS.txt
136
113
  - CHANGELOG.md
137
114
  - Gemfile
138
115
  - LICENSE.txt
@@ -142,22 +119,32 @@ files:
142
119
  - bin/setup
143
120
  - eth.gemspec
144
121
  - lib/eth.rb
122
+ - lib/eth/abi.rb
123
+ - lib/eth/abi/constant.rb
124
+ - lib/eth/abi/type.rb
145
125
  - lib/eth/address.rb
146
- - lib/eth/gas.rb
126
+ - lib/eth/chain.rb
127
+ - lib/eth/eip712.rb
147
128
  - lib/eth/key.rb
148
129
  - lib/eth/key/decrypter.rb
149
130
  - lib/eth/key/encrypter.rb
150
- - lib/eth/open_ssl.rb
151
- - lib/eth/secp256k1.rb
152
- - lib/eth/sedes.rb
131
+ - lib/eth/signature.rb
153
132
  - lib/eth/tx.rb
154
- - 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
155
138
  - lib/eth/version.rb
156
- homepage: https://github.com/se3000/ruby-eth
139
+ homepage: https://github.com/q9f/eth.rb
157
140
  licenses:
158
- - MIT
159
- metadata: {}
160
- post_install_message:
141
+ - Apache-2.0
142
+ 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
+ bug_tracker_uri: https://github.com/q9f/eth.rb/issues
147
+ post_install_message:
161
148
  rdoc_options: []
162
149
  require_paths:
163
150
  - lib
@@ -165,16 +152,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
165
152
  requirements:
166
153
  - - ">="
167
154
  - !ruby/object:Gem::Version
168
- version: '0'
155
+ version: '2.6'
156
+ - - "<"
157
+ - !ruby/object:Gem::Version
158
+ version: '4.0'
169
159
  required_rubygems_version: !ruby/object:Gem::Requirement
170
160
  requirements:
171
161
  - - ">="
172
162
  - !ruby/object:Gem::Version
173
163
  version: '0'
174
164
  requirements: []
175
- rubyforge_project:
176
- rubygems_version: 2.6.8
177
- signing_key:
165
+ rubygems_version: 3.2.29
166
+ signing_key:
178
167
  specification_version: 4
179
- summary: Simple API to sign Ethereum transactions.
168
+ summary: Ruby Ethereum library.
180
169
  test_files: []
data/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- sudo: false
2
- dist: xenial
3
- language: ruby
4
- rvm:
5
- - 2.2.0
6
- - 2.3.0
7
- - 2.4.0
8
- - 2.5.3
9
- - 2.6.0
10
- before_install: gem install bundler -v 1.12.3
data/lib/eth/gas.rb DELETED
@@ -1,9 +0,0 @@
1
- module Eth
2
- class Gas
3
-
4
- GTXCOST = 21000 # TX BASE GAS COST
5
- GTXDATANONZERO = 68 # TX DATA NON ZERO BYTE GAS COST
6
- GTXDATAZERO = 4 # TX DATA ZERO BYTE GAS COST
7
-
8
- end
9
- end