abi_coder_rb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +61 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +12 -0
- data/lib/.DS_Store +0 -0
- data/lib/abi_coder_rb/.DS_Store +0 -0
- data/lib/abi_coder_rb/decode/decode_array.rb +21 -0
- data/lib/abi_coder_rb/decode/decode_fixed_array.rb +16 -0
- data/lib/abi_coder_rb/decode/decode_primitive_type.rb +35 -0
- data/lib/abi_coder_rb/decode/decode_tuple.rb +5 -0
- data/lib/abi_coder_rb/decode.rb +57 -0
- data/lib/abi_coder_rb/encode/encode_array.rb +22 -0
- data/lib/abi_coder_rb/encode/encode_fixed_array.rb +8 -0
- data/lib/abi_coder_rb/encode/encode_primitive_type.rb +146 -0
- data/lib/abi_coder_rb/encode/encode_tuple.rb +5 -0
- data/lib/abi_coder_rb/encode.rb +59 -0
- data/lib/abi_coder_rb/parser.rb +149 -0
- data/lib/abi_coder_rb/types.rb +230 -0
- data/lib/abi_coder_rb/version.rb +5 -0
- data/lib/abi_coder_rb.rb +43 -0
- data/sig/abi_coder_rb.rbs +4 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 72266c6750d4c8e137acebdc790350c7862d9c84453f1af99b1aaa9ee5e775f7
|
4
|
+
data.tar.gz: 045aa60cc688c8537290ffff047df35ceb17db5de978d8943d7db9b0725ca9af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 19822e060b76e17667c3deccf9ba9f2697b51b1ec0825fda594a6d4fcc3f3ed617ca15db6354b4e8702e994cadc58ee221b2b4823b5e93371bc2effd4858b3fc
|
7
|
+
data.tar.gz: 863775a35f5819bddee87bcfe0cbaafc0b59042c98eeff8c9ceaa254ae3e9543c2a8df95660a40e4ac25aa8902f04b50b8493762e729f8141bc35b216e3ea704
|
data/.DS_Store
ADDED
Binary file
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at wuminzhe@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
abi_coder_rb (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
json (2.6.3)
|
12
|
+
language_server-protocol (3.17.0.3)
|
13
|
+
parallel (1.23.0)
|
14
|
+
parser (3.2.2.4)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
racc
|
17
|
+
racc (1.7.3)
|
18
|
+
rainbow (3.1.1)
|
19
|
+
rake (13.1.0)
|
20
|
+
regexp_parser (2.8.2)
|
21
|
+
rexml (3.2.6)
|
22
|
+
rspec (3.12.0)
|
23
|
+
rspec-core (~> 3.12.0)
|
24
|
+
rspec-expectations (~> 3.12.0)
|
25
|
+
rspec-mocks (~> 3.12.0)
|
26
|
+
rspec-core (3.12.2)
|
27
|
+
rspec-support (~> 3.12.0)
|
28
|
+
rspec-expectations (3.12.3)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.12.0)
|
31
|
+
rspec-mocks (3.12.6)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.12.0)
|
34
|
+
rspec-support (3.12.1)
|
35
|
+
rubocop (1.57.2)
|
36
|
+
json (~> 2.3)
|
37
|
+
language_server-protocol (>= 3.17.0)
|
38
|
+
parallel (~> 1.10)
|
39
|
+
parser (>= 3.2.2.4)
|
40
|
+
rainbow (>= 2.2.2, < 4.0)
|
41
|
+
regexp_parser (>= 1.8, < 3.0)
|
42
|
+
rexml (>= 3.2.5, < 4.0)
|
43
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
44
|
+
ruby-progressbar (~> 1.7)
|
45
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
46
|
+
rubocop-ast (1.30.0)
|
47
|
+
parser (>= 3.2.1.0)
|
48
|
+
ruby-progressbar (1.13.0)
|
49
|
+
unicode-display_width (2.5.0)
|
50
|
+
|
51
|
+
PLATFORMS
|
52
|
+
arm64-darwin-21
|
53
|
+
|
54
|
+
DEPENDENCIES
|
55
|
+
abi_coder_rb!
|
56
|
+
rake (~> 13.0)
|
57
|
+
rspec (~> 3.0)
|
58
|
+
rubocop (~> 1.21)
|
59
|
+
|
60
|
+
BUNDLED WITH
|
61
|
+
2.4.13
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Aki Wu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# AbiCoderRb
|
2
|
+
|
3
|
+
modified from https://github.com/rubycocos/blockchain/blob/master/abicoder
|
4
|
+
|
5
|
+
for better readability code and deep learning abi codec.
|
6
|
+
|
7
|
+
The most significant difference from the original code is that 'data' to decode in every decode_* function is no longer exact but now includes both the data needed for decoding and the remaining data. This change means that in the entry point('AbiCoderRb.decode'), there's no longer a need to calculate the precise data required for decoding for each type. This simplification streamlines the code.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add abi_coder_rb
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install abi_coder_rb
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
See tests
|
22
|
+
|
23
|
+
## Development
|
24
|
+
|
25
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
26
|
+
|
27
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wuminzhe/abi_coder_rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/abi_coder_rb/blob/main/CODE_OF_CONDUCT.md).
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
36
|
+
|
37
|
+
## Code of Conduct
|
38
|
+
|
39
|
+
Everyone interacting in the AbiCoderRb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/abi_coder_rb/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/lib/.DS_Store
ADDED
Binary file
|
Binary file
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module AbiCoderRb
|
2
|
+
def decode_array(type, data)
|
3
|
+
l = decode_uint256(data[0, 32])
|
4
|
+
raise DecodingError, "Too long length: #{l}" if l > 100_000
|
5
|
+
|
6
|
+
subtype = type.subtype
|
7
|
+
|
8
|
+
if subtype.dynamic?
|
9
|
+
raise DecodingError, "Not enough data for head" unless data.size >= 32 + 32 * l
|
10
|
+
|
11
|
+
start_positions = (1..l).map { |i| 32 + decode_uint256(data[32 * i, 32]) }
|
12
|
+
start_positions.push(data.size)
|
13
|
+
|
14
|
+
outputs = (0...l).map { |i| data[start_positions[i]...start_positions[i + 1]] }
|
15
|
+
|
16
|
+
outputs.map { |out| decode_type(subtype, out) }
|
17
|
+
else
|
18
|
+
(0...l).map { |i| decode_type(subtype, data[(32 + subtype.size * i)..]) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module AbiCoderRb
|
2
|
+
def decode_fixed_array(type, data)
|
3
|
+
l = type.dim
|
4
|
+
subtype = type.subtype
|
5
|
+
if subtype.dynamic?
|
6
|
+
start_positions = (0...l).map { |i| decode_uint256(data[32 * i, 32]) }
|
7
|
+
start_positions.push(data.size)
|
8
|
+
|
9
|
+
outputs = (0...l).map { |i| data[start_positions[i]...start_positions[i + 1]] }
|
10
|
+
|
11
|
+
outputs.map { |out| decode_type(subtype, out) }
|
12
|
+
else
|
13
|
+
(0...l).map { |i| decode_type(subtype, data[subtype.size * i, subtype.size]) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module AbiCoderRb
|
2
|
+
def decode_primitive_type(type, data)
|
3
|
+
case type
|
4
|
+
when Uint
|
5
|
+
decode_uint256(data[0, 32])
|
6
|
+
when Int
|
7
|
+
u = decode_uint256(data[0, 32])
|
8
|
+
u >= 2**(type.bits - 1) ? (u - 2**type.bits) : u
|
9
|
+
when Bool
|
10
|
+
data[31] == BYTE_ONE
|
11
|
+
when String
|
12
|
+
size = decode_uint256(data[0, 32])
|
13
|
+
data[32...(32 + size)].force_encoding(Encoding::UTF_8)
|
14
|
+
when Bytes
|
15
|
+
size = decode_uint256(data[0, 32])
|
16
|
+
data[32...(32 + size)]
|
17
|
+
when FixedBytes
|
18
|
+
data[0, type.length]
|
19
|
+
when Address
|
20
|
+
data[12...32].unpack1("H*").force_encoding(Encoding::UTF_8)
|
21
|
+
else
|
22
|
+
raise DecodingError, "Unknown primitive type: #{type.class.name} #{type.format}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def decode_uint256(bin)
|
29
|
+
# bin = bin.sub( /\A(\x00)+/, '' ) ## keep "performance" shortcut - why? why not?
|
30
|
+
### todo/check - allow nil - why? why not?
|
31
|
+
## raise DeserializationError, "Invalid serialization (not minimal length)" if !@size && serial.size > 0 && serial[0] == BYTE_ZERO
|
32
|
+
# bin = bin || BYTE_ZERO
|
33
|
+
bin.unpack1("H*").to_i(16)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative "decode/decode_tuple"
|
2
|
+
require_relative "decode/decode_fixed_array"
|
3
|
+
require_relative "decode/decode_array"
|
4
|
+
require_relative "decode/decode_primitive_type"
|
5
|
+
|
6
|
+
module AbiCoderRb
|
7
|
+
def decode(types, data)
|
8
|
+
# Convert types to ABI::Type if they are not already
|
9
|
+
types = types.map { |type| type.is_a?(Type) ? type : Type.parse(type) }
|
10
|
+
|
11
|
+
decode_types(types, data)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def decode_type(type, data)
|
17
|
+
return nil if data.nil? || data.empty?
|
18
|
+
|
19
|
+
case type
|
20
|
+
when Tuple ## todo: support empty (unit) tuple - why? why not?
|
21
|
+
decode_tuple(type, data)
|
22
|
+
when FixedArray # static-sized arrays
|
23
|
+
decode_fixed_array(type, data)
|
24
|
+
when Array
|
25
|
+
decode_array(type, data)
|
26
|
+
else
|
27
|
+
decode_primitive_type(type, data)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def decode_types(types, data)
|
32
|
+
start_positions = start_positions(types, data)
|
33
|
+
|
34
|
+
types.map.with_index do |type, index|
|
35
|
+
start_position = start_positions[index]
|
36
|
+
decode_type(type, data[start_position..])
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def start_positions(types, data)
|
41
|
+
start_positions = ::Array.new(types.size)
|
42
|
+
offset = 0
|
43
|
+
|
44
|
+
types.each_with_index do |type, index|
|
45
|
+
if type.dynamic?
|
46
|
+
# 读取动态类型的偏移量
|
47
|
+
start_positions[index] = decode_uint256(data[offset, 32])
|
48
|
+
offset += 32
|
49
|
+
else
|
50
|
+
start_positions[index] = offset
|
51
|
+
offset += type.size
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
start_positions
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module AbiCoderRb
|
2
|
+
def encode_array(type, args)
|
3
|
+
raise ArgumentError, "arg must be an array" unless args.is_a?(::Array)
|
4
|
+
|
5
|
+
head = "".b
|
6
|
+
tail = "".b # 使用二进制字符串
|
7
|
+
|
8
|
+
head += encode_uint256(args.size)
|
9
|
+
|
10
|
+
subtype = type.subtype
|
11
|
+
args.each do |arg|
|
12
|
+
if subtype.dynamic?
|
13
|
+
head += encode_uint256(32 * args.size + tail.size)
|
14
|
+
tail += encode_type(subtype, arg)
|
15
|
+
else
|
16
|
+
head += encode_type(subtype, arg)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
head + tail
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
module AbiCoderRb
|
2
|
+
def encode_fixed_array(type, args)
|
3
|
+
raise ArgumentError, "arg must be an array" unless args.is_a?(::Array)
|
4
|
+
raise ArgumentError, "Wrong array size: found #{args.size}, expecting #{type.dim}" unless args.size == type.dim
|
5
|
+
|
6
|
+
args.map { |arg| encode_type(type.subtype, arg) }.join
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
module AbiCoderRb
|
2
|
+
def encode_primitive_type(type, arg)
|
3
|
+
# 根据类型选择相应的编码方法
|
4
|
+
case type
|
5
|
+
when Uint
|
6
|
+
# NOTE: for now size in bits always required
|
7
|
+
encode_uint(arg, type.bits)
|
8
|
+
when Int
|
9
|
+
# NOTE: for now size in bits always required
|
10
|
+
encode_int(arg, type.bits)
|
11
|
+
when Bool
|
12
|
+
encode_bool(arg)
|
13
|
+
when String
|
14
|
+
encode_string(arg)
|
15
|
+
when FixedBytes
|
16
|
+
encode_bytes(arg, type.length)
|
17
|
+
when Bytes
|
18
|
+
encode_bytes(arg)
|
19
|
+
when Address
|
20
|
+
encode_address(arg)
|
21
|
+
else
|
22
|
+
raise EncodingError, "Unknown type: #{type}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def encode_uint(arg, bits)
|
27
|
+
raise ArgumentError, "arg is not integer: #{arg}" unless arg.is_a?(Integer)
|
28
|
+
raise ValueOutOfBounds, arg unless arg >= 0 && arg < 2**bits
|
29
|
+
|
30
|
+
lpad_int(arg)
|
31
|
+
end
|
32
|
+
|
33
|
+
def encode_uint256(arg)
|
34
|
+
encode_uint(arg, 256)
|
35
|
+
end
|
36
|
+
|
37
|
+
def encode_int(arg, bits)
|
38
|
+
## raise EncodingError or ArgumentError - why? why not?
|
39
|
+
raise ArgumentError, "arg is not integer: #{arg}" unless arg.is_a?(Integer)
|
40
|
+
raise ValueOutOfBounds, arg unless arg >= -2**(bits - 1) && arg < 2**(bits - 1)
|
41
|
+
|
42
|
+
lpad_int(arg % 2**bits)
|
43
|
+
end
|
44
|
+
|
45
|
+
def encode_bool(arg)
|
46
|
+
## raise EncodingError or ArgumentError - why? why not?
|
47
|
+
raise ArgumentError, "arg is not bool: #{arg}" unless arg.is_a?(TrueClass) || arg.is_a?(FalseClass)
|
48
|
+
|
49
|
+
lpad(arg ? BYTE_ONE : BYTE_ZERO) ## was lpad_int( arg ? 1 : 0 )
|
50
|
+
end
|
51
|
+
|
52
|
+
def encode_string(arg)
|
53
|
+
## raise EncodingError or ArgumentError - why? why not?
|
54
|
+
raise EncodingError, "Expecting string: #{arg}" unless arg.is_a?(::String)
|
55
|
+
|
56
|
+
arg = arg.b if arg.encoding != Encoding::BINARY ## was: name == 'UTF-8'
|
57
|
+
|
58
|
+
raise ValueOutOfBounds, "Integer invalid or out of range: #{arg.size}" if arg.size > UINT_MAX
|
59
|
+
|
60
|
+
size = lpad_int(arg.size)
|
61
|
+
value = rpad(arg, ceil32(arg.size))
|
62
|
+
size + value
|
63
|
+
end
|
64
|
+
|
65
|
+
def encode_bytes(arg, length = nil)
|
66
|
+
## raise EncodingError or ArgumentError - why? why not?
|
67
|
+
raise EncodingError, "Expecting string: #{arg}" unless arg.is_a?(::String)
|
68
|
+
|
69
|
+
arg = arg.b if arg.encoding != Encoding::BINARY
|
70
|
+
|
71
|
+
if length # fixed length type
|
72
|
+
raise ValueOutOfBounds, "invalid bytes length #{length}" if arg.size > length
|
73
|
+
raise ValueOutOfBounds, "invalid bytes length #{length}" if length < 0 || length > 32
|
74
|
+
|
75
|
+
rpad(arg)
|
76
|
+
else # variable length type (if length is nil)
|
77
|
+
raise ValueOutOfBounds, "Integer invalid or out of range: #{arg.size}" if arg.size > UINT_MAX
|
78
|
+
|
79
|
+
size = lpad_int(arg.size)
|
80
|
+
value = rpad(arg, ceil32(arg.size))
|
81
|
+
size + value
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def encode_address(arg)
|
86
|
+
if arg.is_a?(Integer)
|
87
|
+
lpad_int(arg)
|
88
|
+
elsif arg.size == 20
|
89
|
+
## note: make sure encoding is always binary!!!
|
90
|
+
arg = arg.b if arg.encoding != Encoding::BINARY
|
91
|
+
lpad(arg)
|
92
|
+
elsif arg.size == 40
|
93
|
+
lpad_hex(arg)
|
94
|
+
elsif arg.size == 42 && arg[0, 2] == "0x" ## todo/fix: allow 0X too - why? why not?
|
95
|
+
lpad_hex(arg[2..-1])
|
96
|
+
else
|
97
|
+
raise EncodingError, "Could not parse address: #{arg}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
###########
|
104
|
+
# encoding helpers / utils
|
105
|
+
# with "hard-coded" fill symbol as BYTE_ZERO
|
106
|
+
|
107
|
+
def rpad(bin, l = 32) ## note: same as builtin String#ljust !!!
|
108
|
+
# note: default l word is 32 bytes
|
109
|
+
return bin if bin.size >= l
|
110
|
+
|
111
|
+
bin + BYTE_ZERO * (l - bin.size)
|
112
|
+
end
|
113
|
+
|
114
|
+
## rename to lpad32 or such - why? why not?
|
115
|
+
def lpad(bin) ## note: same as builtin String#rjust !!!
|
116
|
+
l = 32 # NOTE: default l word is 32 bytes
|
117
|
+
return bin if bin.size >= l
|
118
|
+
|
119
|
+
BYTE_ZERO * (l - bin.size) + bin
|
120
|
+
end
|
121
|
+
|
122
|
+
## rename to lpad32_int or such - why? why not?
|
123
|
+
def lpad_int(n)
|
124
|
+
raise ArgumentError, "Integer invalid or out of range: #{n}" unless n.is_a?(Integer) && n >= 0 && n <= UINT_MAX
|
125
|
+
|
126
|
+
hex = n.to_s(16)
|
127
|
+
hex = "0" + hex if hex.size.odd?
|
128
|
+
bin = [hex].pack("H*")
|
129
|
+
|
130
|
+
lpad(bin)
|
131
|
+
end
|
132
|
+
|
133
|
+
## rename to lpad32_hex or such - why? why not?
|
134
|
+
def lpad_hex(hex)
|
135
|
+
raise TypeError, "Value must be a string" unless hex.is_a?(::String)
|
136
|
+
raise TypeError, "Non-hexadecimal digit found" unless hex =~ /\A[0-9a-fA-F]*\z/
|
137
|
+
|
138
|
+
bin = [hex].pack("H*")
|
139
|
+
|
140
|
+
lpad(bin)
|
141
|
+
end
|
142
|
+
|
143
|
+
def ceil32(x)
|
144
|
+
x % 32 == 0 ? x : (x + 32 - x % 32)
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require_relative "encode/encode_tuple"
|
2
|
+
require_relative "encode/encode_fixed_array"
|
3
|
+
require_relative "encode/encode_array"
|
4
|
+
require_relative "encode/encode_primitive_type"
|
5
|
+
|
6
|
+
module AbiCoderRb
|
7
|
+
##
|
8
|
+
# Encodes multiple arguments using the head/tail mechanism.
|
9
|
+
# returns binary string (with BINARY / ASCII_8BIT encoding)
|
10
|
+
#
|
11
|
+
def encode(types, args)
|
12
|
+
# 如果 types 是字符串,则转换为 ABI::Type 实例
|
13
|
+
types = types.map { |type| type.is_a?(Type) ? type : Type.parse(type) }
|
14
|
+
|
15
|
+
encode_types(types, args)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def encode_type(type, arg)
|
21
|
+
if type.is_a?(Tuple)
|
22
|
+
encode_tuple(type, arg)
|
23
|
+
elsif type.is_a?(Array) || type.is_a?(FixedArray)
|
24
|
+
type.dynamic? ? encode_array(type, arg) : encode_fixed_array(type, arg)
|
25
|
+
else
|
26
|
+
encode_primitive_type(type, arg)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def encode_types(types, args)
|
31
|
+
raise ArgumentError, "args must be an array" unless args.is_a?(::Array)
|
32
|
+
|
33
|
+
unless args.size == types.size
|
34
|
+
raise ArgumentError,
|
35
|
+
"Wrong number of args: found #{args.size}, expecting #{types.size}"
|
36
|
+
end
|
37
|
+
|
38
|
+
# 计算头部大小
|
39
|
+
head_size = types.map { |type| type.size || 32 }.sum
|
40
|
+
|
41
|
+
# 初始化头部和尾部
|
42
|
+
head = "".b
|
43
|
+
tail = "".b # 使用二进制字符串
|
44
|
+
|
45
|
+
# 遍历类型并编码
|
46
|
+
types.each_with_index do |type, i|
|
47
|
+
if type.dynamic?
|
48
|
+
# 动态类型: 更新头部和尾部
|
49
|
+
head += encode_uint256(head_size + tail.size)
|
50
|
+
tail += encode_type(type, args[i])
|
51
|
+
else
|
52
|
+
# 静态类型: 只更新头部
|
53
|
+
head += encode_type(type, args[i])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
head + tail
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
module AbiCoderRb
|
2
|
+
class Type
|
3
|
+
class ParseError < StandardError; end
|
4
|
+
|
5
|
+
## nested inside Type - why? why not?
|
6
|
+
class Parser
|
7
|
+
TUPLE_TYPE_RX = /^\((.*)\)
|
8
|
+
((\[[0-9]*\])*)
|
9
|
+
/x
|
10
|
+
|
11
|
+
def self.parse(type)
|
12
|
+
if type =~ TUPLE_TYPE_RX
|
13
|
+
types = _parse_tuple_type(::Regexp.last_match(1))
|
14
|
+
dims = _parse_dims(::Regexp.last_match(2))
|
15
|
+
|
16
|
+
parsed_types = types.map { |t| parse(t) }
|
17
|
+
|
18
|
+
return _parse_array_type(Tuple.new(parsed_types), dims)
|
19
|
+
end
|
20
|
+
|
21
|
+
base, sub, dims = _parse_base_type(type)
|
22
|
+
|
23
|
+
_validate_base_type(base, sub)
|
24
|
+
|
25
|
+
subtype = case base
|
26
|
+
when "string" then String.new
|
27
|
+
when "bytes" then sub ? FixedBytes.new(sub) : Bytes.new
|
28
|
+
when "uint" then Uint.new(sub)
|
29
|
+
when "int" then Int.new(sub)
|
30
|
+
when "address" then Address.new
|
31
|
+
when "bool" then Bool.new
|
32
|
+
else
|
33
|
+
## puts " type: >#{type}<"
|
34
|
+
raise ParseError, "Unrecognized type base: #{base}"
|
35
|
+
end
|
36
|
+
|
37
|
+
_parse_array_type(subtype, dims)
|
38
|
+
end
|
39
|
+
|
40
|
+
##
|
41
|
+
# Crazy regexp to seperate out base type component (eg. uint), size (eg.
|
42
|
+
# 256, 128, nil), array component (eg. [], [45], nil)
|
43
|
+
#
|
44
|
+
BASE_TYPE_RX = /([a-z]*)
|
45
|
+
([0-9]*)
|
46
|
+
((\[[0-9]*\])*)
|
47
|
+
/x
|
48
|
+
|
49
|
+
def self._parse_base_type(str)
|
50
|
+
_, base, subscript, dimension = BASE_TYPE_RX.match(str).to_a
|
51
|
+
|
52
|
+
## note: use [Integer,Integer] array in the future for sub
|
53
|
+
## for fixed (e.g. 128x128 => [128,128]) or such
|
54
|
+
## for now always assume single integer (as string)
|
55
|
+
sub = subscript == "" ? nil : subscript.to_i
|
56
|
+
|
57
|
+
## e.g. turn "[][1][2]" into [-1,1,2]
|
58
|
+
## or "" into [] -- that is, empty array
|
59
|
+
dims = _parse_dims(dimension)
|
60
|
+
|
61
|
+
[base, sub, dims]
|
62
|
+
end
|
63
|
+
|
64
|
+
def self._parse_dims(str)
|
65
|
+
dims = str.scan(/\[[0-9]*\]/)
|
66
|
+
|
67
|
+
## note: return -1 for dynamic array size e.g. []
|
68
|
+
## e.g. "[]"[1...-1] => ""
|
69
|
+
## "[0]"[1...-1] => "0"
|
70
|
+
## "[1]"[1...-1] => "1"
|
71
|
+
dims.map do |dim|
|
72
|
+
size = dim[1...-1]
|
73
|
+
size == "" ? -1 : size.to_i
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def self._parse_array_type(subtype, dims)
|
78
|
+
##
|
79
|
+
## todo/check - double check if the order in reverse
|
80
|
+
## in solidity / abi encoding / decoding?
|
81
|
+
##
|
82
|
+
dims.each do |dim|
|
83
|
+
subtype = if dim == -1
|
84
|
+
Array.new(subtype)
|
85
|
+
else
|
86
|
+
FixedArray.new(subtype, dim)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
subtype
|
91
|
+
end
|
92
|
+
|
93
|
+
def self._validate_base_type(base, sub)
|
94
|
+
case base
|
95
|
+
when "string"
|
96
|
+
# NOTE: string can not have any suffix
|
97
|
+
raise ParseError, "String cannot have suffix" if sub
|
98
|
+
when "bytes"
|
99
|
+
raise ParseError, "Maximum 32 bytes for fixed-length bytes" if sub && sub > 32
|
100
|
+
when "uint", "int"
|
101
|
+
raise ParseError, "Integer type must have numerical suffix" unless sub
|
102
|
+
raise ParseError, "Integer size out of bounds" unless sub >= 8 && sub <= 256
|
103
|
+
raise ParseError, "Integer size must be multiple of 8" unless sub % 8 == 0
|
104
|
+
when "address"
|
105
|
+
raise ParseError, "Address cannot have suffix" if sub
|
106
|
+
when "bool"
|
107
|
+
raise ParseError, "Bool cannot have suffix" if sub
|
108
|
+
else
|
109
|
+
## puts " type: >#{type}<"
|
110
|
+
raise ParseError, "Unrecognized type base: #{base}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def self._parse_tuple_type(str)
|
115
|
+
## note: types assumes string WITHOUT enclosing () e.g.
|
116
|
+
## tuple(string,string,bool) => expected as "string,string,bool"
|
117
|
+
|
118
|
+
depth = 0
|
119
|
+
collected = []
|
120
|
+
current = ""
|
121
|
+
|
122
|
+
### todo/fix: replace with a simple parser!!!
|
123
|
+
## allow () and move verbose tuple() too!!!
|
124
|
+
str.each_char do |c|
|
125
|
+
case c
|
126
|
+
when ","
|
127
|
+
if depth == 0
|
128
|
+
collected << current
|
129
|
+
current = ""
|
130
|
+
else
|
131
|
+
current += c
|
132
|
+
end
|
133
|
+
when "("
|
134
|
+
depth += 1
|
135
|
+
current += c
|
136
|
+
when ")"
|
137
|
+
depth -= 1
|
138
|
+
current += c
|
139
|
+
else
|
140
|
+
current += c
|
141
|
+
end
|
142
|
+
end
|
143
|
+
collected << current unless current.empty?
|
144
|
+
|
145
|
+
collected
|
146
|
+
end
|
147
|
+
end # class Parser
|
148
|
+
end # class Type
|
149
|
+
end # module ABI
|
@@ -0,0 +1,230 @@
|
|
1
|
+
module AbiCoderRb
|
2
|
+
#######
|
3
|
+
## for now use (get inspired)
|
4
|
+
## by the type names used by abi coder in rust
|
5
|
+
## see https://github.com/rust-ethereum/ethabi/blob/master/ethabi/src/param_type/param_type.rs
|
6
|
+
|
7
|
+
class Type
|
8
|
+
def self.parse(type) ## convenience helper
|
9
|
+
Parser.parse(type)
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# Get the static size of a type, or nil if dynamic.
|
14
|
+
#
|
15
|
+
# @return [Integer, NilClass] size of static type, or nil for dynamic type
|
16
|
+
#
|
17
|
+
def size
|
18
|
+
## check/todo: what error to raise for not implemented / method not defined???
|
19
|
+
raise ArgumentError, "no required size method defined for Type subclass #{self.class.name}; sorry"
|
20
|
+
end
|
21
|
+
|
22
|
+
def dynamic?
|
23
|
+
size.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def format
|
27
|
+
## check/todo: what error to raise for not implemented / method not defined???
|
28
|
+
raise ArgumentError, "no required format method defined for Type subclass #{self.class.name}; sorry"
|
29
|
+
end
|
30
|
+
|
31
|
+
####
|
32
|
+
## default implementation
|
33
|
+
## assume equal if class match (e.g. Address == Address)
|
34
|
+
## - use format string for generic compare - why? why not?
|
35
|
+
end
|
36
|
+
|
37
|
+
class Address < Type
|
38
|
+
## note: address is always 20 bytes; BUT uses 32 bytes (with padding)
|
39
|
+
def size
|
40
|
+
32
|
41
|
+
end
|
42
|
+
|
43
|
+
def format
|
44
|
+
"address"
|
45
|
+
end
|
46
|
+
|
47
|
+
def ==(other)
|
48
|
+
other.is_a?(Address)
|
49
|
+
end
|
50
|
+
end # class Address
|
51
|
+
|
52
|
+
class Bytes < Type
|
53
|
+
## note: dynamic (not known at compile-time)
|
54
|
+
def size
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
58
|
+
def format
|
59
|
+
"bytes"
|
60
|
+
end
|
61
|
+
|
62
|
+
def ==(other)
|
63
|
+
other.is_a?(Bytes)
|
64
|
+
end
|
65
|
+
end # class Bytes
|
66
|
+
|
67
|
+
class FixedBytes < Type
|
68
|
+
attr_reader :length
|
69
|
+
|
70
|
+
def initialize(length)
|
71
|
+
@length = length # in bytes (1,2,...32)
|
72
|
+
end
|
73
|
+
|
74
|
+
## note: always uses 32 bytes (with padding)
|
75
|
+
def size
|
76
|
+
32
|
77
|
+
end
|
78
|
+
|
79
|
+
def format
|
80
|
+
"bytes#{@length}"
|
81
|
+
end
|
82
|
+
|
83
|
+
def ==(other)
|
84
|
+
other.is_a?(FixedBytes) && @length == other.length
|
85
|
+
end
|
86
|
+
end # class FixedBytes
|
87
|
+
|
88
|
+
class Int < Type
|
89
|
+
attr_reader :bits
|
90
|
+
|
91
|
+
def initialize(bits = 256)
|
92
|
+
@bits = bits # in bits (8,16,...256)
|
93
|
+
end
|
94
|
+
|
95
|
+
## note: always uses 32 bytes (with padding)
|
96
|
+
def size
|
97
|
+
32
|
98
|
+
end
|
99
|
+
|
100
|
+
def format
|
101
|
+
"int#{@bits}"
|
102
|
+
end
|
103
|
+
|
104
|
+
def ==(other)
|
105
|
+
other.is_a?(Int) && @bits == other.bits
|
106
|
+
end
|
107
|
+
end # class Int
|
108
|
+
|
109
|
+
class Uint < Type
|
110
|
+
attr_reader :bits
|
111
|
+
|
112
|
+
def initialize(bits = 256)
|
113
|
+
@bits = bits # in bits (8,16,...256)
|
114
|
+
end
|
115
|
+
|
116
|
+
## note: always uses 32 bytes (with padding)
|
117
|
+
def size
|
118
|
+
32
|
119
|
+
end
|
120
|
+
|
121
|
+
def format
|
122
|
+
"uint#{@bits}"
|
123
|
+
end
|
124
|
+
|
125
|
+
def ==(other)
|
126
|
+
other.is_a?(Uint) && @bits == other.bits
|
127
|
+
end
|
128
|
+
end # class Uint
|
129
|
+
|
130
|
+
class Bool < Type
|
131
|
+
## note: always uses 32 bytes (with padding)
|
132
|
+
def size
|
133
|
+
32
|
134
|
+
end
|
135
|
+
|
136
|
+
def format
|
137
|
+
"bool"
|
138
|
+
end
|
139
|
+
|
140
|
+
def ==(other)
|
141
|
+
other.is_a?(Bool)
|
142
|
+
end
|
143
|
+
end # class Bool
|
144
|
+
|
145
|
+
class String < Type
|
146
|
+
## note: dynamic (not known at compile-time)
|
147
|
+
def size
|
148
|
+
nil
|
149
|
+
end
|
150
|
+
|
151
|
+
def format
|
152
|
+
"string"
|
153
|
+
end
|
154
|
+
|
155
|
+
def ==(other)
|
156
|
+
other.is_a?(String)
|
157
|
+
end
|
158
|
+
end # class String
|
159
|
+
|
160
|
+
class Array < Type
|
161
|
+
attr_reader :subtype
|
162
|
+
|
163
|
+
def initialize(subtype)
|
164
|
+
@subtype = subtype
|
165
|
+
end
|
166
|
+
|
167
|
+
## note: dynamic (not known at compile-time)
|
168
|
+
def size
|
169
|
+
nil
|
170
|
+
end
|
171
|
+
|
172
|
+
def format
|
173
|
+
"#{@subtype.format}[]"
|
174
|
+
end
|
175
|
+
|
176
|
+
def ==(other)
|
177
|
+
other.is_a?(Array) && @subtype == other.subtype
|
178
|
+
end
|
179
|
+
end # class Array
|
180
|
+
|
181
|
+
class FixedArray < Type
|
182
|
+
attr_reader :subtype, :dim
|
183
|
+
|
184
|
+
def initialize(subtype, dim)
|
185
|
+
@subtype = subtype
|
186
|
+
@dim = dim
|
187
|
+
end
|
188
|
+
|
189
|
+
def size
|
190
|
+
@subtype.dynamic? ? nil : @dim * subtype.size
|
191
|
+
end
|
192
|
+
|
193
|
+
def format
|
194
|
+
"#{@subtype.format}[#{@dim}]"
|
195
|
+
end
|
196
|
+
|
197
|
+
def ==(other)
|
198
|
+
other.is_a?(FixedArray) &&
|
199
|
+
@dim == other.dim &&
|
200
|
+
@subtype == other.subtype
|
201
|
+
end
|
202
|
+
end # class FixedArray
|
203
|
+
|
204
|
+
class Tuple < Type
|
205
|
+
attr_reader :types
|
206
|
+
|
207
|
+
def initialize(types)
|
208
|
+
@types = types
|
209
|
+
end
|
210
|
+
|
211
|
+
def size
|
212
|
+
s = 0
|
213
|
+
@types.each do |type|
|
214
|
+
ts = type.size
|
215
|
+
return nil if ts.nil?
|
216
|
+
|
217
|
+
s += ts
|
218
|
+
end
|
219
|
+
s
|
220
|
+
end
|
221
|
+
|
222
|
+
def format
|
223
|
+
"(#{@types.map { |t| t.format }.join(",")})" ## rebuild minimal string
|
224
|
+
end
|
225
|
+
|
226
|
+
def ==(other)
|
227
|
+
other.is_a?(Tuple) && @types == other.types
|
228
|
+
end
|
229
|
+
end # class Tuple
|
230
|
+
end # module ABI
|
data/lib/abi_coder_rb.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "abi_coder_rb/version"
|
4
|
+
|
5
|
+
require_relative "abi_coder_rb/parser"
|
6
|
+
require_relative "abi_coder_rb/types"
|
7
|
+
require_relative "abi_coder_rb/decode"
|
8
|
+
require_relative "abi_coder_rb/encode"
|
9
|
+
|
10
|
+
module AbiCoderRb
|
11
|
+
class DecodingError < StandardError; end
|
12
|
+
class EncodingError < StandardError; end
|
13
|
+
class ValueError < StandardError; end
|
14
|
+
class ValueOutOfBounds < ValueError; end
|
15
|
+
|
16
|
+
###################
|
17
|
+
### some (shared) constants (move to constants.rb or such - why? why not?)
|
18
|
+
|
19
|
+
## todo/check: use auto-freeze string literals magic comment - why? why not?
|
20
|
+
##
|
21
|
+
## todo/fix: move BYTE_EMPTY, BYTE_ZERO, BYTE_ONE to upstream to bytes gem
|
22
|
+
## and make "global" constants - why? why not?
|
23
|
+
|
24
|
+
## BYTE_EMPTY = "".b.freeze
|
25
|
+
BYTE_ZERO = "\x00".b.freeze
|
26
|
+
BYTE_ONE = "\x01".b.freeze ## note: used for encoding bool for now
|
27
|
+
|
28
|
+
UINT_MAX = 2**256 - 1 ## same as 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
|
29
|
+
UINT_MIN = 0
|
30
|
+
INT_MAX = 2**255 - 1 ## same as 57896044618658097711785492504343953926634992332820282019728792003956564819967
|
31
|
+
INT_MIN = -2**255 ## same as -57896044618658097711785492504343953926634992332820282019728792003956564819968
|
32
|
+
|
33
|
+
def hex_to_bin(hex) # convert hex(adecimal) string to binary string
|
34
|
+
hex = hex[2..] if %w[0x 0X].include?(hex[0, 2]) ## cut-of leading 0x or 0X if present
|
35
|
+
[hex].pack("H*")
|
36
|
+
end
|
37
|
+
alias hex hex_to_bin
|
38
|
+
|
39
|
+
def bin_to_hex(bin) # convert binary string to hex string
|
40
|
+
bin.unpack1("H*")
|
41
|
+
end
|
42
|
+
alias bin bin_to_hex
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: abi_coder_rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aki Wu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-11-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: An EVM ABI encoding and decoding tool
|
14
|
+
email:
|
15
|
+
- wuminzhe@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".DS_Store"
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- CODE_OF_CONDUCT.md
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- lib/.DS_Store
|
30
|
+
- lib/abi_coder_rb.rb
|
31
|
+
- lib/abi_coder_rb/.DS_Store
|
32
|
+
- lib/abi_coder_rb/decode.rb
|
33
|
+
- lib/abi_coder_rb/decode/decode_array.rb
|
34
|
+
- lib/abi_coder_rb/decode/decode_fixed_array.rb
|
35
|
+
- lib/abi_coder_rb/decode/decode_primitive_type.rb
|
36
|
+
- lib/abi_coder_rb/decode/decode_tuple.rb
|
37
|
+
- lib/abi_coder_rb/encode.rb
|
38
|
+
- lib/abi_coder_rb/encode/encode_array.rb
|
39
|
+
- lib/abi_coder_rb/encode/encode_fixed_array.rb
|
40
|
+
- lib/abi_coder_rb/encode/encode_primitive_type.rb
|
41
|
+
- lib/abi_coder_rb/encode/encode_tuple.rb
|
42
|
+
- lib/abi_coder_rb/parser.rb
|
43
|
+
- lib/abi_coder_rb/types.rb
|
44
|
+
- lib/abi_coder_rb/version.rb
|
45
|
+
- sig/abi_coder_rb.rbs
|
46
|
+
homepage: https://github.com/wuminzhe/abi_coder_rb
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata:
|
50
|
+
homepage_uri: https://github.com/wuminzhe/abi_coder_rb
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 2.6.0
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.3.7
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: An EVM ABI encoding and decoding tool
|
70
|
+
test_files: []
|