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

Commit

Permalink
abi tool
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminzhe committed Nov 16, 2023
1 parent dc62692 commit bd9c5bd
Showing 1 changed file with 90 additions and 8 deletions.
98 changes: 90 additions & 8 deletions public/abi.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,46 @@
extend AbiCoderRb
end

class String
def binary?
self.each_char do |char|
return true if char.ord < 32 || char.ord > 126
end
false
end
end

def 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) }
elsif item.is_a?(Hash)
item.transform_values { |v| transform_item(v) }
elsif item.is_a?(Integer)
item
else
item
end
end

document = JS.global[:document]
search_button = document.getElementById "btnEncode"
search_button.addEventListener "click" do
btn_encode = document.getElementById "btnEncode"
btn_encode.addEventListener "click" do
types = JSON.parse(document.getElementById("types")[:value].to_s)
values = JSON.parse(document.getElementById("values")[:value].to_s)
result = ABI.encode(types, values)
document.getElementById("result")[:innerHTML] = ABI.bin_to_hex(result)
document.getElementById("data")[:value] = ABI.bin_to_hex(result)
end

btn_decode = document.getElementById "btnDecode"
btn_decode.addEventListener "click" do
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
end

</script>

<style type="text/css">
Expand All @@ -38,18 +70,68 @@
<form>
<p>
<div>types: </div>
<input type="text" id="types" name="types" value="[&quot;uint256&quot;, &quot;(uint256,string)&quot;]" style="width: 100%"></input>

<input type="text" id="types" name="types" value='["bytes32","(address,uint256,uint256,address,uint256,address,uint256,bytes)"]' style="width: 100%"></input>

</p>
<p>
<div>values: </div>
<input type="text" id="values" name="values" value="[1234, [1234, &quot;Hello World&quot;]]" style="width: 100%">
<div>decoded values: </div>
<textarea id="values" name="values" value="" style="width: 100%" rows=8></textarea>
</p>
<p>
<div>encoded data: </div>
<textarea id="data" name="data" value="" style="width: 100%" rows="8">
2628abe10aaf809f0ea9a33fb15782582e8d8353ea15698d7067b057748581a4000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000001523057a05d6293c1e5171ee33ee0a00000000000000000000000000000000000000000000000000000000000000c30000000000000000000000000000000000000000000000000000000000066eee0000000000000000000000000000000000d2de3e2444926c4577b0a59f1dd8bc000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000d2de3e2444926c4577b0a59f1dd8bc00000000000000000000000000000000000000000000000000000000000739a4000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000a4394d1bca0000000000000000000000000b001c95e86d64c1ad6e43944c568a6c31b538870000000000000000000000000b001c95e86d64c1ad6e43944c568a6c31b5388700000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000009084128719162207770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
</textarea>
</p>
<p>
<button type="button" id="btnEncode">encode</button>
<button type="button" id="btnDecode">decode</button>
</p>
</form>
<pre style="width: 100%;" id="result"></pre>


<hr/>

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

<hr>

example 1:<br/>
<div style="font-family: monospace">
types(json array): <br>
["uint256", "(address,uint256)[]", "string"]<br>
<br>
values(json array): <br>
[
66,
[["18a475d6741215709ed6cc5f4d064732379b5a58", 1]],
"QmWBiSE9ByR6vrx4hvrjqS3SG5r6wE4SRq7CP2RVpafZWV"
]
<br>
<br>
<div style="overflow-wrap: break-word;">
data(hex): <br>0000000000000000000000000000000000000000000000000000000000000042000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000018a475d6741215709ed6cc5f4d064732379b5a580000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002e516d57426953453942795236767278346876726a71533353473572367745345352713743503252567061665a5756000000000000000000000000000000000000
</div>
</div>

<br>
more examples: <a href="https://github.com/wuminzhe/abi_coder_rb/tree/main/spec" target="_blank">abi_coder_rb</a>
</body>
</html>

0 comments on commit bd9c5bd

Please sign in to comment.