From 0fc71f52fa6f44fc1925b4dd6137b434f9fba8e0 Mon Sep 17 00:00:00 2001 From: matteo-cristino Date: Fri, 31 Mar 2023 13:03:32 +0200 Subject: [PATCH 1/3] =?UTF-8?q?fix(ethereum):=20=F0=9F=90=9B=20improve=20h?= =?UTF-8?q?andling=20of=20cid=20using=20codec?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/crypto_ethereum.lua | 14 ++++++++++++++ src/lua/zencode_ethereum.lua | 11 +++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/lua/crypto_ethereum.lua b/src/lua/crypto_ethereum.lua index f2402a6ac..b53cde650 100644 --- a/src/lua/crypto_ethereum.lua +++ b/src/lua/crypto_ethereum.lua @@ -254,6 +254,20 @@ function ETH.verify_from_address(add, txSigned) return (valid and ETH.address_from_public_key(pk) == add) end +function ETH.parse_chainid(chainid, cid, enc) + if not enc then enc = "string" end + if cid then + if enc == "string" then + cid = tonumber(cid:str()) or cid:octet() + elseif enc ~= "integer" then + return nil + end + else + cid = tonumber(chainid) or O.from_string(chainid) + end + return INT.new(cid) +end + -- Assume we are given a smart contract with a function with the -- following signature -- function writeString(string memory) diff --git a/src/lua/zencode_ethereum.lua b/src/lua/zencode_ethereum.lua index 21a6b4469..e6691c9f1 100644 --- a/src/lua/zencode_ethereum.lua +++ b/src/lua/zencode_ethereum.lua @@ -209,14 +209,9 @@ When("create the signed ethereum transaction for chain ''", function(chainid) local sk = havekey'ethereum' local tx = have'ethereum transaction' - local cid = mayhave(chainid) - if cid then - cid = tonumber(cid) or cid:octet() - else - cid = tonumber(chainid) or O.from_string(chainid) - end - cid = INT.new(cid) - ZEN.assert(cid, "Invalid chain id: "..chainid) + local cid, cid_codec = mayhave(chainid) + cid = ETH.parse_chainid(chainid, cid, cid_codec.encoding) + ZEN.assert(cid, "Invalid chain id encoding: "..cid_codec.encoding) if not tx.data then tx.data = O.new() end if not tx.value then tx.value = O.new() end tx.v = cid From 0b359778534d1fd837afc592ffb87435d790fcb9 Mon Sep 17 00:00:00 2001 From: Alberto Lerda Date: Fri, 31 Mar 2023 14:38:24 +0200 Subject: [PATCH 2/3] fix: improve chain id import in ethereum transaction Now chain id can also be imported from KEYS or DATA. Chain ID can have encoding 'string' or 'integer'. When the chain id is set, we look at ACK and see if the variable references a value which is loaded. In this case: - if the encoding is 'integer' his numeric value is imported. - if the encoding is 'string' we try to import its numeric value, if it fails we import it as `O.from_string` (backward compatibility) If the name is not loaded from keys or data, we use the value inside the zencode script --- .editorconfig | 4 +++ src/lua/crypto_ethereum.lua | 14 --------- src/lua/zencode.lua | 1 + src/lua/zencode_ethereum.lua | 14 +++++++-- test/zencode/ethereum.bats | 60 ++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 16 deletions(-) diff --git a/.editorconfig b/.editorconfig index 311ab3f1c..bd12df3da 100644 --- a/.editorconfig +++ b/.editorconfig @@ -24,6 +24,10 @@ indent_size = 4 indent_style = space indent_size = 4 +[test/**.bats] +indent_style = space +indent_size = 4 + [src/lua/crypto_ethereum.lua] indent_style = space indent_size = 3 diff --git a/src/lua/crypto_ethereum.lua b/src/lua/crypto_ethereum.lua index b53cde650..f2402a6ac 100644 --- a/src/lua/crypto_ethereum.lua +++ b/src/lua/crypto_ethereum.lua @@ -254,20 +254,6 @@ function ETH.verify_from_address(add, txSigned) return (valid and ETH.address_from_public_key(pk) == add) end -function ETH.parse_chainid(chainid, cid, enc) - if not enc then enc = "string" end - if cid then - if enc == "string" then - cid = tonumber(cid:str()) or cid:octet() - elseif enc ~= "integer" then - return nil - end - else - cid = tonumber(chainid) or O.from_string(chainid) - end - return INT.new(cid) -end - -- Assume we are given a smart contract with a function with the -- following signature -- function writeString(string memory) diff --git a/src/lua/zencode.lua b/src/lua/zencode.lua index 7e05182df..afc978a3d 100644 --- a/src/lua/zencode.lua +++ b/src/lua/zencode.lua @@ -489,6 +489,7 @@ function mayhave(obj) local name = uscore(trim(obj)) res = ACK[name] if not res then + I.warn(name .. " not found in DATA or KEYS") return nil end local codec = ZEN.CODEC[name] diff --git a/src/lua/zencode_ethereum.lua b/src/lua/zencode_ethereum.lua index e6691c9f1..21b730648 100644 --- a/src/lua/zencode_ethereum.lua +++ b/src/lua/zencode_ethereum.lua @@ -210,8 +210,18 @@ function(chainid) local sk = havekey'ethereum' local tx = have'ethereum transaction' local cid, cid_codec = mayhave(chainid) - cid = ETH.parse_chainid(chainid, cid, cid_codec.encoding) - ZEN.assert(cid, "Invalid chain id encoding: "..cid_codec.encoding) + if cid then + local enc = cid_codec.encoding + if enc == "string" then + cid = tonumber(cid:str()) or cid:octet() + elseif enc ~= "integer" then + error("Invalid chain id encoding: "..cid_codec.encoding) + end + else + cid = tonumber(chainid) or O.from_string(chainid) + end + cid = INT.new(cid) + ZEN.assert(cid, "Invalid chain id") if not tx.data then tx.data = O.new() end if not tx.value then tx.value = O.new() end tx.v = cid diff --git a/test/zencode/ethereum.bats b/test/zencode/ethereum.bats index 7b6a7e2e8..10ff8185d 100644 --- a/test/zencode/ethereum.bats +++ b/test/zencode/ethereum.bats @@ -474,3 +474,63 @@ Then print the 'data retrieved' as 'string' EOF save_output 'doc_retrieved_data.json' } + +@test "Import transaction" { + cat < Date: Fri, 31 Mar 2023 15:18:42 +0200 Subject: [PATCH 3/3] =?UTF-8?q?fix(ethereum):=20=F0=9F=90=9B=20avoid=20glo?= =?UTF-8?q?bal=20function=20inside=20crypto=5Fethereum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/crypto_ethereum.lua | 46 +++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/lua/crypto_ethereum.lua b/src/lua/crypto_ethereum.lua index f2402a6ac..fc2e2d805 100644 --- a/src/lua/crypto_ethereum.lua +++ b/src/lua/crypto_ethereum.lua @@ -325,7 +325,30 @@ function ETH.address_from_public_key(pk) return H:process(pk:sub(2, #pk)):sub(13, 32) end -function encode(t, arg) +local function encode_uint(val) + return BIG.new(val):fixed(32) +end + +-- The following types are called “dynamic”: +-- bytes +-- string +-- T[] for any T +-- T[k] for any dynamic T and any k >= 0 +-- (T1,...,Tk) if Ti is dynamic for some 1 <= i <= k +local function is_dynamic(t) + local dyn = t == 'string' or t == 'bytes' or string.match(t, '[a-zA-Z0-9]+%[%]') + if not dyn then + t = string.match(t, '([a-zA-Z]+)%[%d+%]') + if t then + dyn = is_dynamic(t) + end + end + return dyn +end + +local encode_tuple + +local function encode(t, arg) local res if type(t) == "string" then if string.match(t, 'uint%d+$') or t == 'address' then @@ -391,27 +414,6 @@ function encode(t, arg) return res end -function encode_uint(val) - return BIG.new(val):fixed(32) -end - --- The following types are called “dynamic”: --- bytes --- string --- T[] for any T --- T[k] for any dynamic T and any k >= 0 --- (T1,...,Tk) if Ti is dynamic for some 1 <= i <= k -function is_dynamic(t) - local dyn = t == 'string' or t == 'bytes' or string.match(t, '[a-zA-Z0-9]+%[%]') - if not dyn then - t = string.match(t, '([a-zA-Z]+)%[%d+%]') - if t then - dyn = is_dynamic(t) - end - end - return dyn -end - function encode_tuple(params, args) local res = O.empty()