From bd9c5bddc240b4d6479b8e56bea88006705d5e12 Mon Sep 17 00:00:00 2001 From: Aki Wu Date: Thu, 16 Nov 2023 15:25:15 +0800 Subject: [PATCH] abi tool --- public/abi.html | 98 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 8 deletions(-) diff --git a/public/abi.html b/public/abi.html index 10bc967..3c79519 100644 --- a/public/abi.html +++ b/public/abi.html @@ -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 +