Skip to content

Commit

Permalink
major bug fixes for parsing functions!
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Sep 5, 2017
1 parent 20cf997 commit 95bcf71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
26 changes: 12 additions & 14 deletions lib/eth/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ defmodule ETH.Transaction do
def parse([nonce, gas_price, gas_limit, to, value, data]) do
%{
nonce: to_buffer(nonce), gas_price: to_buffer(gas_price), gas_limit: to_buffer(gas_limit),
to: to_buffer(to), value: to_buffer(to), data: to_buffer(data)
to: to_buffer(to), value: to_buffer(value), data: to_buffer(data)
}
end
def parse([nonce, gas_price, gas_limit, to, value, data, v, r, s]) do
%{
nonce: to_buffer(nonce), gas_price: to_buffer(gas_price), gas_limit: to_buffer(gas_limit),
to: to_buffer(to), value: to_buffer(to), data: to_buffer(data), v: to_buffer(v),
to: to_buffer(to), value: to_buffer(value), data: to_buffer(data), v: to_buffer(v),
r: to_buffer(r), s: to_buffer(s)
}
end
def parse(%{
nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: to, data: data
nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data
}) do
%{
nonce: to_buffer(nonce), gas_price: to_buffer(gas_price), gas_limit: to_buffer(gas_limit),
to: to_buffer(to), value: to_buffer(to), data: to_buffer(data)
to: to_buffer(to), value: to_buffer(value), data: to_buffer(data)
}
end
def parse(%{
nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: to, data: data,
nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data,
v: v, r: r, s: s
}) do
%{
nonce: to_buffer(nonce), gas_price: to_buffer(gas_price), gas_limit: to_buffer(gas_limit),
to: to_buffer(to), value: to_buffer(to), data: to_buffer(data), v: to_buffer(v),
to: to_buffer(to), value: to_buffer(value), data: to_buffer(data), v: to_buffer(v),
r: to_buffer(r), s: to_buffer(s)
}
end
Expand Down Expand Up @@ -221,7 +221,7 @@ defmodule ETH.Transaction do

# defp buffer_to_int(""), do: 0
defp buffer_to_int(data) do
{number, _} = Integer.parse(data, 16)
<<number>> = to_buffer(data)
number
end

Expand All @@ -232,20 +232,18 @@ defmodule ETH.Transaction do
"0x" <> Base.encode16(data, case: :mixed)
end

defp to_buffer(nil), do: ""
defp to_buffer(data) when is_number(data) do
IO.puts("NUMBER CALLED")
IEx.pry
pad_to_even(Hexate.encode(data)) |> Base.decode16!(case: :mixed)
def to_buffer(nil), do: ""
def to_buffer(data) when is_number(data) do
pad_to_even(Integer.to_string(data, 16)) |> Base.decode16!(case: :mixed)
end
defp to_buffer("0x" <> data) do
def to_buffer("0x" <> data) do
padded_data = pad_to_even(data)
case Base.decode16(padded_data, case: :mixed) do
{:ok, decoded_binary} -> decoded_binary
_ -> data
end
end
defp to_buffer(data), do: data # NOTE: to_buffer else if (v === null || v === undefined) { v = Buffer.allocUnsafe(0) }
def to_buffer(data), do: data # NOTE: to_buffer else if (v === null || v === undefined) { v = Buffer.allocUnsafe(0) }

def pad_to_even(data) do
if rem(String.length(data), 2) == 1, do: "0#{data}", else: data
Expand Down
16 changes: 11 additions & 5 deletions test/eth/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ defmodule ETH.TransactionTest do
target_hash = "5C207A650B59A8C2D1271F5CBDA78A658CB411A87271D68062E61AB1A3F85CF9"
assert ETH.Transaction.hash(@first_transaction_list) |> Base.encode16 == target_hash

first_transaction_list = @transactions |> Enum.at(2) |> Map.get("raw")
second_transaction_list = @transactions |> Enum.at(3) |> Map.get("raw")
# |> ETH.Transaction.parse |> ETH.Transaction.to_list
IO.inspect(first_transaction_list)
first_transaction_list = @transactions
|> Enum.at(2)
|> Map.get("raw")
|> ETH.Transaction.parse
|> ETH.Transaction.to_list

second_transaction_list = @transactions
|> Enum.at(3)
|> Map.get("raw")
|> ETH.Transaction.parse
|> ETH.Transaction.to_list

assert ETH.Transaction.hash(first_transaction_list) == decode16("375a8983c9fc56d7cfd118254a80a8d7403d590a6c9e105532b67aca1efb97aa")
assert ETH.Transaction.hash(first_transaction_list, false) == decode16("61e1ec33764304dddb55348e7883d4437426f44ab3ef65e6da1e025734c03ff0")
assert ETH.Transaction.hash(first_transaction_list, true) == decode16("375a8983c9fc56d7cfd118254a80a8d7403d590a6c9e105532b67aca1efb97aa")

IEx.pry
assert ETH.Transaction.hash(second_transaction_list) == decode16("0f09dc98ea85b7872f4409131a790b91e7540953992886fc268b7ba5c96820e4")
assert ETH.Transaction.hash(second_transaction_list, true) == decode16("0f09dc98ea85b7872f4409131a790b91e7540953992886fc268b7ba5c96820e4")
assert ETH.Transaction.hash(second_transaction_list, false) == decode16("f97c73fdca079da7652dbc61a46cd5aeef804008e057be3e712c43eac389aaf0")
Expand Down

0 comments on commit 95bcf71

Please # to comment.