klay 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/klay/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 Klay
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 Klay
17
+
18
+ # Defines the version of the {Eth} module.
19
+ VERSION = "0.0.1".freeze
20
+ end
data/lib/klay.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 Klay
17
+ end
18
+
19
+ # Loads the {Eth} module classes.
20
+ require "klay/abi"
21
+ require "klay/api"
22
+ require "klay/address"
23
+ require "klay/chain"
24
+ require "klay/constant"
25
+ require "klay/client"
26
+ require "klay/client/http"
27
+ require "klay/client/ipc"
28
+ require "klay/eip712"
29
+ require "klay/key"
30
+ require "klay/rlp"
31
+ require "klay/signature"
32
+ require "klay/tx"
33
+ require "klay/unit"
34
+ require "klay/util"
35
+ require "klay/version"
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: klay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sehan Park
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: keccak
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: konstructor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rbsecp256k1
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: openssl
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: scrypt
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Library to handle Klaytn accounts, messages, and transactions.
84
+ email:
85
+ - ianparkfirst@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".github/workflows/codeql.yml"
91
+ - ".github/workflows/docs.yml"
92
+ - ".github/workflows/spec.yml"
93
+ - ".gitignore"
94
+ - ".gitmodules"
95
+ - ".yardopts"
96
+ - AUTHORS.txt
97
+ - CHANGELOG.md
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - klay.gemspec
105
+ - lib/klay.rb
106
+ - lib/klay/abi.rb
107
+ - lib/klay/abi/type.rb
108
+ - lib/klay/address.rb
109
+ - lib/klay/api.rb
110
+ - lib/klay/chain.rb
111
+ - lib/klay/client.rb
112
+ - lib/klay/client/http.rb
113
+ - lib/klay/client/ipc.rb
114
+ - lib/klay/constant.rb
115
+ - lib/klay/eip712.rb
116
+ - lib/klay/key.rb
117
+ - lib/klay/key/decrypter.rb
118
+ - lib/klay/key/encrypter.rb
119
+ - lib/klay/rlp.rb
120
+ - lib/klay/rlp/decoder.rb
121
+ - lib/klay/rlp/encoder.rb
122
+ - lib/klay/rlp/sedes.rb
123
+ - lib/klay/rlp/sedes/big_endian_int.rb
124
+ - lib/klay/rlp/sedes/binary.rb
125
+ - lib/klay/rlp/sedes/list.rb
126
+ - lib/klay/signature.rb
127
+ - lib/klay/tx.rb
128
+ - lib/klay/tx/eip1559.rb
129
+ - lib/klay/tx/eip2930.rb
130
+ - lib/klay/tx/legacy.rb
131
+ - lib/klay/unit.rb
132
+ - lib/klay/util.rb
133
+ - lib/klay/version.rb
134
+ homepage: https://github.com/noMacGuffins/klay
135
+ licenses:
136
+ - Apache-2.0
137
+ metadata:
138
+ bug_tracker_uri: https://github.com/noMacGuffins/klay/issues
139
+ changelog_uri: https://github.com/noMacGuffins/klay/blob/main/CHANGELOG.md
140
+ github_repo: https://github.com/noMacGuffins/klay
141
+ source_code_uri: https://github.com/noMacGuffins/klay
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '2.6'
151
+ - - "<"
152
+ - !ruby/object:Gem::Version
153
+ version: '4.0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubygems_version: 3.0.3
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Ruby Klaytn library.
164
+ test_files: []