Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
update abi tool
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminzhe committed Nov 16, 2023
1 parent bd9c5bd commit 612a912
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
25 changes: 11 additions & 14 deletions public/abi.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>ABI Encode/Decode</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/browser.script.iife.js"></script>
<script type="text/ruby" src="./abi.rb"></script>
<script type="text/ruby" src="./flattened_abi_coder-1700126655.rb"></script>
<script type="text/ruby">
require "js"
require "json"
Expand All @@ -20,20 +20,21 @@
end
end

def transform_item(item)
def de_transform_item(item)
if item.is_a?(String)
item.binary? ? ABI.bin_to_hex(item) : item
elsif item.is_a?(Array)
item.map { |i| transform_item(i) }
item.map { |i| de_transform_item(i) }
elsif item.is_a?(Hash)
item.transform_values { |v| transform_item(v) }
item.transform_values { |v| de_transform_item(v) }
elsif item.is_a?(Integer)
item
else
item
end
end


document = JS.global[:document]
btn_encode = document.getElementById "btnEncode"
btn_encode.addEventListener "click" do
Expand All @@ -48,7 +49,7 @@
types = JSON.parse(document.getElementById("types")[:value].to_s)
data = document.getElementById("data")[:value].to_s.strip
result = ABI.decode(types, ABI.hex_to_bin(data))
document.getElementById("values")[:value] = transform_item(result).to_json
document.getElementById("values")[:value] = de_transform_item(result).to_json
end

</script>
Expand Down Expand Up @@ -92,28 +93,24 @@

<hr/>

example 0:<br/>
example0:<br/>
<div style="font-family: monospace">
types(json array): <br>
["uint256[][]", "string[]"]
<br>
["uint256", "(uint256,string)"]
<br>
values(json array): <br>
[
[[1, 2], [3]],
["one", "two", "three"]
]
[1234, [1234, "Hello World"]]
<br>
<br>
<div style="overflow-wrap: break-word;">
data(hex): <br>
000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000036f6e650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000374776f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000057468726565000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000004d2000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000
</div>
</div>

<hr>

example 1:<br/>
example1:<br/>
<div style="font-family: monospace">
types(json array): <br>
["uint256", "(address,uint256)[]", "string"]<br>
Expand Down
42 changes: 10 additions & 32 deletions public/abi.rb → public/flattened_abi_coder-1700126655.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

# ./lib/abi_coder_rb/decode/decode_array.rb
# Generated from https://github.com/wuminzhe/abi_coder_rb
module AbiCoderRb
def decode_array(type, data)
l = decode_uint256(data[0, 32])
Expand All @@ -16,8 +15,6 @@ def decode_array(type, data)
end
end
end

# ./lib/abi_coder_rb/decode/decode_fixed_array.rb
module AbiCoderRb
def decode_fixed_array(type, data)
l = type.dim
Expand All @@ -32,8 +29,6 @@ def decode_fixed_array(type, data)
end
end
end

# ./lib/abi_coder_rb/decode/decode_primitive_type.rb
module AbiCoderRb
def decode_primitive_type(type, data)
case type
Expand Down Expand Up @@ -63,15 +58,11 @@ def decode_uint256(bin)
bin_to_hex(bin).to_i(16)
end
end

# ./lib/abi_coder_rb/decode/decode_tuple.rb
module AbiCoderRb
def decode_tuple(type, data)
decode_types(type.types, data)
end
end

# ./lib/abi_coder_rb/decode.rb
module AbiCoderRb
def decode(types, data)
types = types.map { |type| type.is_a?(Type) ? type : Type.parse(type) }
Expand Down Expand Up @@ -113,8 +104,6 @@ def start_positions(types, data)
start_positions
end
end

# ./lib/abi_coder_rb/encode/encode_array.rb
module AbiCoderRb
def encode_array(type, args)
raise ArgumentError, "arg must be an array" unless args.is_a?(::Array)
Expand All @@ -133,17 +122,13 @@ def encode_array(type, args)
head + tail
end
end

# ./lib/abi_coder_rb/encode/encode_fixed_array.rb
module AbiCoderRb
def encode_fixed_array(type, args)
raise ArgumentError, "arg must be an array" unless args.is_a?(::Array)
raise ArgumentError, "Wrong array size: found #{args.size}, expecting #{type.dim}" unless args.size == type.dim
args.map { |arg| encode_type(type.subtype, arg) }.join
end
end

# ./lib/abi_coder_rb/encode/encode_primitive_type.rb
module AbiCoderRb
def encode_primitive_type(type, arg)
case type
Expand Down Expand Up @@ -184,14 +169,15 @@ def encode_bool(arg)
end
def encode_string(arg)
raise EncodingError, "Expecting string: #{arg}" unless arg.is_a?(::String)
arg = arg.b if arg.encoding != 'BINARY' ## was: name == 'UTF-8', wasm
arg = arg.b if arg.encoding != "BINARY" ## was: name == 'UTF-8', wasm
raise ValueOutOfBounds, "Integer invalid or out of range: #{arg.size}" if arg.size > UINT_MAX
size = lpad_int(arg.size)
value = rpad(arg, ceil32(arg.size))
size + value
end
def encode_bytes(arg, length = nil)
raise EncodingError, "Expecting string: #{arg}" unless arg.is_a?(::String)
arg = hex_to_bin(arg) if hex?(arg)
arg = arg.b if arg.encoding != Encoding::BINARY
if length # fixed length type
raise ValueOutOfBounds, "invalid bytes length #{length}" if arg.size > length
Expand Down Expand Up @@ -231,29 +217,25 @@ def lpad(bin) ## note: same as builtin String#rjust !!!
def lpad_int(n)
raise ArgumentError, "Integer invalid or out of range: #{n}" unless n.is_a?(Integer) && n >= 0 && n <= UINT_MAX
hex = n.to_s(16)
hex = "0#{hex}" if hex.length % 2 != 0 # wasm, no .odd
bin = hex(hex)
hex = "0#{hex}" if hex.length.odd? # wasm, no .odd
bin = hex_to_bin(hex)
lpad(bin)
end
def lpad_hex(hex)
raise TypeError, "Value must be a string" unless hex.is_a?(::String)
raise TypeError, "Non-hexadecimal digit found" unless hex =~ /\A[0-9a-fA-F]*\z/
bin = hex(hex)
bin = hex_to_bin(hex)
lpad(bin)
end
def ceil32(x)
x % 32 == 0 ? x : (x + 32 - x % 32)
end
end

# ./lib/abi_coder_rb/encode/encode_tuple.rb
module AbiCoderRb
def encode_tuple(tuple, args)
encode_types(tuple.types, args)
end
end

# ./lib/abi_coder_rb/encode.rb
module AbiCoderRb
def encode(types, args)
types = types.map { |type| type.is_a?(Type) ? type : Type.parse(type) }
Expand Down Expand Up @@ -289,8 +271,6 @@ def encode_types(types, args)
head + tail
end
end

# ./lib/abi_coder_rb/parser.rb
module AbiCoderRb
class Type
class ParseError < StandardError; end
Expand Down Expand Up @@ -393,8 +373,6 @@ def self._parse_tuple_type(str)
end # class Parser
end # class Type
end # module ABI

# ./lib/abi_coder_rb/types.rb
module AbiCoderRb
class Type
def self.parse(type) ## convenience helper
Expand Down Expand Up @@ -557,13 +535,9 @@ def ==(other)
end
end # class Tuple
end # module ABI

# ./lib/abi_coder_rb/version.rb
module AbiCoderRb
VERSION = "0.1.0"
end

# ./lib/abi_coder_rb.rb
module AbiCoderRb
class DecodingError < StandardError; end
class EncodingError < StandardError; end
Expand All @@ -584,4 +558,8 @@ def bin_to_hex(bin) # convert binary string to hex string
bin.each_byte.map { |byte| "%02x" % byte }.join
end
alias bin bin_to_hex
def hex?(str)
str = str[2..] if %w[0x 0X].include?(str[0, 2]) ## cut-of leading 0x or 0X if present
str.match?(/\A\b[0-9a-fA-F]+\b\z/) && str.length.even?
end
end

0 comments on commit 612a912

Please sign in to comment.