abi_coder_rb 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.DS_Store +0 -0
- data/Gemfile.lock +3 -1
- data/README.md +60 -2
- data/flatten.rb +8 -0
- data/flattened_abi_coder.rb +669 -0
- data/lib/.DS_Store +0 -0
- data/lib/abi_coder_rb/decode/decode_array.rb +6 -6
- data/lib/abi_coder_rb/decode/decode_primitive_type.rb +27 -22
- data/lib/abi_coder_rb/decode/decode_tuple.rb +29 -0
- data/lib/abi_coder_rb/decode.rb +3 -33
- data/lib/abi_coder_rb/encode/encode_fixed_array.rb +15 -1
- data/lib/abi_coder_rb/encode/encode_primitive_type.rb +20 -8
- data/lib/abi_coder_rb/encode/encode_tuple.rb +32 -0
- data/lib/abi_coder_rb/encode.rb +13 -43
- data/lib/abi_coder_rb/parser.rb +8 -1
- data/lib/abi_coder_rb/types.rb +10 -3
- data/lib/abi_coder_rb/utils.rb +111 -0
- data/lib/abi_coder_rb/version.rb +1 -1
- data/lib/abi_coder_rb.rb +20 -12
- metadata +20 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1df03ba4a67d09da186cb3326798ef5f2bbbebb48dedb7fb3f30170eac01283e
|
4
|
+
data.tar.gz: 92cada88c754227fb404dc6ae6198be216eb1db19c39d47e69e15b210fd58095
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d1621232bdabc3ee622786cafcef42260fac404df806b53a534791fed8a97884d3d0faca5ab153332b1b02665e95465667678e02d20098218178ef909f09f66
|
7
|
+
data.tar.gz: 93f19691b3d14cfeaac06b001f24e5910b4160acb90bc06df5066616666b7148701c8d62d5511bacfd716143f49299fc039b46c1896c61f84505cf8179deae01
|
data/.DS_Store
CHANGED
Binary file
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
abi_coder_rb (0.
|
4
|
+
abi_coder_rb (0.2.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -9,6 +9,7 @@ GEM
|
|
9
9
|
ast (2.4.2)
|
10
10
|
diff-lcs (1.5.0)
|
11
11
|
json (2.6.3)
|
12
|
+
keccak (1.3.1)
|
12
13
|
language_server-protocol (3.17.0.3)
|
13
14
|
parallel (1.23.0)
|
14
15
|
parser (3.2.2.4)
|
@@ -53,6 +54,7 @@ PLATFORMS
|
|
53
54
|
|
54
55
|
DEPENDENCIES
|
55
56
|
abi_coder_rb!
|
57
|
+
keccak (~> 1.3)
|
56
58
|
rake (~> 13.0)
|
57
59
|
rspec (~> 3.0)
|
58
60
|
rubocop (~> 1.21)
|
data/README.md
CHANGED
@@ -4,7 +4,13 @@ modified from https://github.com/rubycocos/blockchain/blob/master/abicoder
|
|
4
4
|
|
5
5
|
for better readability code and deep learning abi codec.
|
6
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.
|
7
|
+
1. 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
|
+
2. Added type encoding pre- and type decoding post- callbacks to facilitate transforming before encoding and after decoding. See [test](./spec/web3_js_abitests_spec.rb)
|
10
|
+
|
11
|
+
3. pass all web3.js tests in [encodeDecodeParams.test.ts](https://github.com/web3/web3.js/blob/c490c1814da646a83c6a5f7fee643e35507c9344/packages/web3-eth-abi/test/unit/encodeDecodeParams.test.ts). That is about 1024 unit tests from fixture [abitestsdata.json](https://github.com/web3/web3.js/blob/c490c1814da646a83c6a5f7fee643e35507c9344/packages/web3-eth-abi/test/fixtures/abitestsdata.json).
|
12
|
+
|
13
|
+
Also, some code was modified to compile to wasm. Try it online: https://wuminzhe.github.io/abi.html
|
8
14
|
|
9
15
|
## Installation
|
10
16
|
|
@@ -18,7 +24,59 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
18
24
|
|
19
25
|
## Usage
|
20
26
|
|
21
|
-
|
27
|
+
### Way 1: extend AbiCoderRb
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'abi_coder_rb'
|
31
|
+
|
32
|
+
module ABI
|
33
|
+
extend AbiCoderRb
|
34
|
+
end
|
35
|
+
|
36
|
+
type = "(bytes4)"
|
37
|
+
value = ["\x124Vx"] # or ABI.hex "0x12345678"
|
38
|
+
data = ABI.hex "1234567800000000000000000000000000000000000000000000000000000000"
|
39
|
+
ABI.decode(type, data) == value # => true
|
40
|
+
ABI.encode(type, value) == data # => true
|
41
|
+
```
|
42
|
+
|
43
|
+
You can transform the value according to the type before encoding it. For example, you can convert the hex string to bytes before encoding it. Here is same example but the value for "bytes4" is a hex string.
|
44
|
+
```ruby
|
45
|
+
require 'abi_coder_rb'
|
46
|
+
|
47
|
+
module ABI
|
48
|
+
extend AbiCoderRb
|
49
|
+
|
50
|
+
before_encoding ->(type, value) {
|
51
|
+
if type.start_with?("bytes")
|
52
|
+
hex(value)
|
53
|
+
else
|
54
|
+
value
|
55
|
+
end
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
type = "(bytes4)"
|
60
|
+
value = ["0x12345678"]
|
61
|
+
data = ABI.hex "1234567800000000000000000000000000000000000000000000000000000000"
|
62
|
+
ABI.encode(type, value) == data # => true
|
63
|
+
```
|
64
|
+
|
65
|
+
### Way 2: include AbiCoderRb
|
66
|
+
```ruby
|
67
|
+
class Hello
|
68
|
+
include AbiCoderRb
|
69
|
+
|
70
|
+
def world
|
71
|
+
data = hex "0000000000000000000000000000000000000000000000000000000000000020" \
|
72
|
+
"000000000000000000000000000000000000000000000000000000000000000b" \
|
73
|
+
"48656c6c6f20576f726c64000000000000000000000000000000000000000000"
|
74
|
+
decode("(string)", data)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
Hello.new.world # => ["Hello World"]
|
79
|
+
```
|
22
80
|
|
23
81
|
## Development
|
24
82
|
|
data/flatten.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
content = "# Generated from https://github.com/wuminzhe/abi_coder_rb\n"
|
2
|
+
Dir["./lib/**/*.rb"].each do |f|
|
3
|
+
File.open(f, "r").each_line do |line|
|
4
|
+
content += line unless line.include?("require") || line.strip.start_with?("#") || line.strip.empty?
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
puts content
|