Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ethereum): 🐛 accept any zenroom type in the store of data #620

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/lua/crypto_ethereum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,14 @@ function ETH.make_storage_data(src)
offset = O.from_hex('0000000000000000000000000000000000000000000000000000000000000020')

-- length as a 256 unsigned integer
bytLen = INT.new(#src):octet()
local src_o = src
if iszen(type(src_o)) then
src_o = src_o:octet()
end
bytLen = INT.new(#src_o):octet()
paddingLength = 32-#bytLen
paddingLen = O.zero(paddingLength)
paddingLength = #src % 32
paddingLength = #src_o % 32
if paddingLength > 0 then
paddingLength = 32 - paddingLength
padding = O.zero(paddingLength)
Expand All @@ -297,7 +301,7 @@ function ETH.make_storage_data(src)
end
-- done with padding

return fId .. offset .. paddingLen .. bytLen .. src .. padding
return fId .. offset .. paddingLen .. bytLen .. src_o .. padding
end

-- generate an ethereum keypair
Expand Down Expand Up @@ -398,7 +402,7 @@ end
-- 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)
dyn = t == 'string' or t == 'bytes' or string.match(t, '[a-zA-Z0-9]+%[%]')
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
Expand All @@ -411,22 +415,18 @@ end
function encode_tuple(params, args)
local res = O.empty()

if type(params) == 'string' then
elseif type(params) ~= 'table' then
if type(params) ~= 'table' then
error("Type not encodable as tuple")
end

local tails = {}
local heads = {}
local head
local tail

local head_size = 0;
local tail_size = 0;
-- check if there are dynamic types and compute tails
for i, v in ipairs(params) do
if is_dynamic(v) then
local arg = nil;
head_size = head_size + 32
table.insert(tails, encode(v, args[i]))
else
Expand All @@ -453,10 +453,10 @@ function encode_tuple(params, args)
end

-- implement O.concat for memory efficient concat
for i, v in pairs(heads) do
for _, v in pairs(heads) do
res = res .. v
end
for i, v in pairs(tails) do
for _, v in pairs(tails) do
res = res .. v
end
return res
Expand Down
17 changes: 17 additions & 0 deletions test/zencode/ethereum.bats
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ EOF
save_output 'alice_storage_tx.json'
}

@test "When I use the ethereum transaction to store ecp point" {
cat <<EOF | zexe transaction_storage_ecp.zen alice_nonce_eth.json storage_contract.json
Scenario ethereum
Given I have a 'ethereum address' named 'storage contract'
# here we assume bob is a storage contract
and I have a 'gas price'
and I have a 'gas limit'
and I have an 'ethereum nonce'
When I set 'data' to 'Hello, it is me Mario!' as 'string'
When I create the hash to point 'ecp' of 'data'
When I create the ethereum transaction to 'storage contract'
and I use the ethereum transaction to store 'hash to point'
Then print the 'ethereum transaction'
EOF
save_output 'alice_storage_tx.json'
}

@test "When I create the signed ethereum transaction for chain '' (again)" {
cat <<eof | zexe sign_transaction_chainid.zen alice_storage_tx.json alice_keys.json
scenario ethereum
Expand Down