From c0dfe877a1a87e4456829376d8ef80bd494e4c41 Mon Sep 17 00:00:00 2001 From: Izel Nakri Date: Wed, 24 Jan 2018 23:06:44 +0100 Subject: [PATCH] more tests added signing part is stable --- config/dev.exs | 2 + lib/eth.ex | 4 +- lib/eth/transaction.ex | 7 +- lib/eth/transaction/signer.ex | 12 +- lib/eth/transaction_queries.ex | 10 +- mix.exs | 2 +- test/eth/eth_test.exs | 4 +- test/eth/query_test.exs | 8 +- test/eth/transaction/builder_test.exs | 36 +- test/eth/transaction/parser_test.exs | 548 ++++++++++++-------------- test/eth/transaction/signer_test.exs | 210 +++++----- test/eth/transaction_queries_test.exs | 8 +- test/eth/transaction_test.exs | 72 ++-- test/eth/transactions_test.exs | 54 --- test/eth/utils_test.exs | 60 +-- test/eth/wallet_test.exs | 5 +- test/eth_test.exs | 2 +- test/support/eth_client.exs | 4 +- test/test_helper.exs | 8 +- 19 files changed, 490 insertions(+), 566 deletions(-) delete mode 100644 test/eth/transactions_test.exs diff --git a/config/dev.exs b/config/dev.exs index d2d855e..0303066 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -1 +1,3 @@ use Mix.Config + +config :ethereumex, url: "http://localhost:8545" diff --git a/lib/eth.ex b/lib/eth.ex index c6b1ce3..d994c07 100644 --- a/lib/eth.ex +++ b/lib/eth.ex @@ -58,8 +58,8 @@ defmodule ETH do defdelegate build(wallet, params), to: ETH.Transaction.Builder defdelegate build(sender_wallet, receiver_wallet, params_or_value), to: ETH.Transaction.Builder - defdelegate hash(transaction), to: ETH.Transaction - defdelegate hash(transaction, include_signature), to: ETH.Transaction + defdelegate hash_transaction(transaction), to: ETH.Transaction + defdelegate hash_transaction(transaction, include_signature), to: ETH.Transaction defdelegate sign_transaction(transaction, private_key), to: ETH.Transaction.Signer defdelegate decode(rlp_encoded_transaction), to: ETH.Transaction.Signer diff --git a/lib/eth/transaction.ex b/lib/eth/transaction.ex index 85ca4fe..d163640 100644 --- a/lib/eth/transaction.ex +++ b/lib/eth/transaction.ex @@ -9,8 +9,8 @@ defmodule ETH.Transaction do defdelegate build(wallet, params), to: ETH.Transaction.Builder defdelegate build(sender_wallet, receiver_wallet, params_or_value), to: ETH.Transaction.Builder - defdelegate hash(transaction), to: ETH.Transaction.Signer - defdelegate hash(transaction, include_signature), to: ETH.Transaction.Signer + defdelegate hash_transaction(transaction), to: ETH.Transaction.Signer + defdelegate hash_transaction(transaction, include_signature), to: ETH.Transaction.Signer defdelegate sign_transaction(transaction, private_key), to: ETH.Transaction.Signer defdelegate decode(rlp_encoded_transaction), to: ETH.Transaction.Signer @@ -68,6 +68,7 @@ defmodule ETH.Transaction do end def send(signature), do: HttpClient.eth_send_raw_transaction(signature) + def send!(signature) do {:ok, transaction_hash} = HttpClient.eth_send_raw_transaction(signature) transaction_hash @@ -146,7 +147,7 @@ defmodule ETH.Transaction do s ] ) do - message_hash = hash(transaction_list, false) + message_hash = hash_transaction(transaction_list, false) chain_id = get_chain_id(v, Enum.at(transaction_list, 9)) v_int = buffer_to_int(v) target_v = if chain_id > 0, do: v_int - (chain_id * 2 + 8), else: v_int diff --git a/lib/eth/transaction/signer.ex b/lib/eth/transaction/signer.ex index 9a6bf7e..62cc6b8 100644 --- a/lib/eth/transaction/signer.ex +++ b/lib/eth/transaction/signer.ex @@ -13,9 +13,9 @@ defmodule ETH.Transaction.Signer do ExRLP.encode(transaction_list) end - def hash(transaction, include_signature \\ true) + def hash_transaction(transaction, include_signature \\ true) - def hash(transaction_list, include_signature) when is_list(transaction_list) do + def hash_transaction(transaction_list, include_signature) when is_list(transaction_list) do target_list = case include_signature do true -> @@ -37,7 +37,7 @@ defmodule ETH.Transaction.Signer do |> keccak256 end - def hash( + def hash_transaction( transaction = %{ to: _to, value: _value, @@ -54,14 +54,13 @@ defmodule ETH.Transaction.Signer do |> Map.delete(:chain_id) |> TransactionParser.to_list() |> List.insert_at(-1, chain_id) - |> hash(include_signature) + |> hash_transaction(include_signature) end def sign_transaction(transaction, private_key) when is_map(transaction) do transaction |> ETH.Transaction.to_list() |> sign_transaction(private_key) - |> ExRLP.encode() end def sign_transaction( @@ -116,7 +115,7 @@ defmodule ETH.Transaction.Signer do <> ) do chain_id = get_chain_id(v, Enum.at(transaction_list, 9)) - message_hash = hash(transaction_list, false) + message_hash = hash_transaction(transaction_list, false) [signature: signature, recovery: recovery] = secp256k1_signature(message_hash, private_key) @@ -126,5 +125,6 @@ defmodule ETH.Transaction.Signer do sig_v = if chain_id > 0, do: initial_v + (chain_id * 2 + 8), else: initial_v [nonce, gas_price, gas_limit, to, value, data, <>, sig_r, sig_s] + |> ExRLP.encode() end end diff --git a/lib/eth/transaction_queries.ex b/lib/eth/transaction_queries.ex index 2fab02e..c691996 100644 --- a/lib/eth/transaction_queries.ex +++ b/lib/eth/transaction_queries.ex @@ -45,7 +45,8 @@ defmodule ETH.TransactionQueries do end def get_block_transaction_count!(block_hash) do - {:ok, transaction_count} = HttpClient.eth_get_block_transaction_count_by_hash(block_hash) + {:ok, transaction_count} = + HttpClient.eth_get_block_transaction_count_by_hash_transaction(block_hash) transaction_count end @@ -94,7 +95,9 @@ defmodule ETH.TransactionQueries do case HttpClient.eth_get_transaction_receipt(transaction_hash) do {:ok, raw_transaction_receipt} -> {:ok, convert_transaction_receipt(raw_transaction_receipt)} - error -> error + + error -> + error end end @@ -136,7 +139,8 @@ defmodule ETH.TransactionQueries do end def convert_transaction_receipt(result) do - result |> Enum.reduce(%{}, fn tuple, acc -> + result + |> Enum.reduce(%{}, fn tuple, acc -> {key, value} = tuple case key do diff --git a/mix.exs b/mix.exs index 188cc39..6716d06 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Eth.Mixfile do def project do [ app: :eth, - version: "0.3.3", + version: "0.3.4", elixir: "~> 1.6", start_permanent: Mix.env() == :prod, deps: deps(), diff --git a/test/eth/eth_test.exs b/test/eth/eth_test.exs index 03b7749..927cc1d 100644 --- a/test/eth/eth_test.exs +++ b/test/eth/eth_test.exs @@ -50,8 +50,8 @@ defmodule ETH.Test do get_transaction_from_block!: 2, get_transaction_receipt: 1, get_transaction_receipt!: 1, - hash: 1, - hash: 2, + hash_transaction: 1, + hash_transaction: 2, keccak256: 1, pad_to_even: 1, parse: 1, diff --git a/test/eth/query_test.exs b/test/eth/query_test.exs index 7e17095..c55d9d5 100644 --- a/test/eth/query_test.exs +++ b/test/eth/query_test.exs @@ -3,11 +3,11 @@ defmodule ETH.Query.Test do use ExUnit.Case setup_all do - ETH.TestClient.start + ETH.TestClient.start() - on_exit fn -> - ETH.TestClient.stop - end + on_exit(fn -> + ETH.TestClient.stop() + end) :ok end diff --git a/test/eth/transaction/builder_test.exs b/test/eth/transaction/builder_test.exs index 61e8338..c8903f9 100644 --- a/test/eth/transaction/builder_test.exs +++ b/test/eth/transaction/builder_test.exs @@ -6,14 +6,14 @@ defmodule ETH.Transaction.Setter.Test do # make these raw data # [nonce, gas_price, gas_limit, to, value, data, v, r, s] - # @not_signed_transaction_list [ - # "", - # "0x04a817c800", - # "0x5208", - # "0x3535353535353535353535353535353535353535", - # "", - # "" - # ] + @not_signed_transaction_list [ + "", + "0x04a817c800", + "0x5208", + "0x3535353535353535353535353535353535353535", + "", + "" + ] # @signed_transaction_list [ # "", # "0x04a817c800", @@ -45,16 +45,16 @@ defmodule ETH.Transaction.Setter.Test do # s: "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d" # } # - # test "Transaction.set(params) works when params is a list" do - # assert Transaction.set(@not_signed_transaction_list) == %{ - # nonce: "", - # gas_price: to_buffer("0x04a817c800"), - # gas_limit: to_buffer("0x5208"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # value: "", - # data: "" - # } - # end + test "Transaction.set(params) works when params is a list" do + assert Transaction.build(@not_signed_transaction_list) == %{ + nonce: "", + gas_price: to_buffer("0x04a817c800"), + gas_limit: to_buffer("0x5208"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + value: "", + data: "" + } + end # test "Transaction.set(params) works with default values when params is a list" do # diff --git a/test/eth/transaction/parser_test.exs b/test/eth/transaction/parser_test.exs index 1186021..4583d6e 100644 --- a/test/eth/transaction/parser_test.exs +++ b/test/eth/transaction/parser_test.exs @@ -4,296 +4,260 @@ defmodule ETH.Transaction.Parser.Test do alias ETH.Transaction - # @transactions File.read!("test/fixtures/transactions.json") |> Poison.decode!() - # @first_transaction_rlp "0xf864808504a817c800825208943535353535353535353535353535353535353535808025a0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116da0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d" - # @not_signed_transaction_list [ - # "", - # "0x04a817c800", - # "0x5208", - # "0x3535353535353535353535353535353535353535", - # "", - # "" - # ] - # @signed_transaction_list [ - # "", - # "0x04a817c800", - # "0x5208", - # "0x3535353535353535353535353535353535353535", - # "", - # "", - # "0x25", - # "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d", - # "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d" - # ] - # @not_signed_transaction_map %{ - # nonce: "", - # gas_price: "0x04a817c800", - # gas_limit: "0x5208", - # to: "0x3535353535353535353535353535353535353535", - # value: "", - # data: "" - # } - # @signed_transaction_map %{ - # nonce: "", - # gas_price: "0x04a817c800", - # gas_limit: "0x5208", - # to: "0x3535353535353535353535353535353535353535", - # value: "", - # data: "", - # v: "0x25", - # r: "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d", - # s: "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d" - # } - # - # test "parsing encoded transaction rlp returns a transaction map" do - # assert Transaction.parse(@first_transaction_rlp) == %{ - # data: "", - # gas_limit: to_buffer("0x5208"), - # gas_price: to_buffer("0x04a817c800"), - # nonce: "", - # r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # v: to_buffer("0x25"), - # value: "" - # } - # end - # - # # test "parsing decoded transaction rlp buffer retuns a transaction map" do - # # rlp_transaction_buffer = @first_transaction_rlp - # # |> String.slice(2..-1) - # # |> Base.decode16!(case: :mixed) - # # - # # assert ETH.Transaction.parse(rlp_transaction_buffer) == %{ - # # data: "", - # # gas_limit: to_buffer("0x5208"), - # # gas_price: to_buffer("0x04a817c800"), - # # nonce: "", - # # r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # # s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # # to: to_buffer("0x3535353535353535353535353535353535353535"), - # # v: to_buffer("0x25"), - # # value: "" - # # } - # # end - # - # test "parsing a not-signed transaction list returns a transaction map" do - # assert Transaction.parse(@not_signed_transaction_list) == %{ - # nonce: "", - # gas_price: to_buffer("0x04a817c800"), - # gas_limit: to_buffer("0x5208"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # value: "", - # data: "" - # } - # - # buffered_transaction_list = - # @not_signed_transaction_list - # |> Enum.map(fn item -> to_buffer(item) end) - # - # assert buffered_transaction_list |> Transaction.parse() == %{ - # nonce: "", - # gas_price: to_buffer("0x04a817c800"), - # gas_limit: to_buffer("0x5208"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # value: "", - # data: "" - # } - # end - # - # test "parsing a signed transaction list returns a transaction map" do - # assert Transaction.parse(@signed_transaction_list) == %{ - # nonce: "", - # gas_price: to_buffer("0x04a817c800"), - # gas_limit: to_buffer("0x5208"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # value: "", - # data: "", - # v: to_buffer("0x25"), - # r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") - # } - # - # buffered_transaction_list = - # @signed_transaction_list - # |> Enum.map(fn item -> to_buffer(item) end) - # - # assert buffered_transaction_list |> Transaction.parse() == %{ - # nonce: "", - # gas_price: to_buffer("0x04a817c800"), - # gas_limit: to_buffer("0x5208"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # value: "", - # data: "", - # v: to_buffer("0x25"), - # r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") - # } - # end - # - # test "parsing a not-signed transaction map returns a buffered transaction map" do - # assert Transaction.parse(@not_signed_transaction_map) == %{ - # nonce: "", - # gas_price: to_buffer("0x04a817c800"), - # gas_limit: to_buffer("0x5208"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # value: "", - # data: "" - # } - # - # assert Transaction.parse( - # @not_signed_transaction_map |> Map.keys() - # |> Enum.reduce(%{}, fn key, acc -> - # Map.put(acc, key, to_buffer(Map.get(@not_signed_transaction_map, key))) - # end) - # ) == %{ - # nonce: "", - # gas_price: to_buffer("0x04a817c800"), - # gas_limit: to_buffer("0x5208"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # value: "", - # data: "" - # } - # end - # - # test "parsing a signed transaction map returns a buffered transaction map" do - # assert Transaction.parse(@signed_transaction_map) == %{ - # nonce: "", - # gas_price: to_buffer("0x04a817c800"), - # gas_limit: to_buffer("0x5208"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # value: "", - # data: "", - # v: to_buffer("0x25"), - # r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") - # } - # - # assert Transaction.parse( - # @signed_transaction_map |> Map.keys() - # |> Enum.reduce(%{}, fn key, acc -> - # Map.put(acc, key, to_buffer(Map.get(@signed_transaction_map, key))) - # end) - # ) == %{ - # nonce: "", - # gas_price: to_buffer("0x04a817c800"), - # gas_limit: to_buffer("0x5208"), - # to: to_buffer("0x3535353535353535353535353535353535353535"), - # value: "", - # data: "", - # v: to_buffer("0x25"), - # r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") - # } - # end - # - # test "to_list works for encoded rlp transactions" do - # assert Transaction.to_list(@first_transaction_rlp) == [ - # "", - # to_buffer("0x04a817c800"), - # to_buffer("0x5208"), - # to_buffer("0x3535353535353535353535353535353535353535"), - # "", - # "", - # to_buffer("0x25"), - # to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") - # ] - # end - # - # # test "to_list works for buffered rlp transactions" do - # # rlp_transaction_buffer = @first_transaction_rlp - # # |> String.slice(2..-1) - # # |> Base.decode16!(case: :mixed) - # # - # # assert Transaction.to_list(rlp_transaction_buffer) == [ - # # "", - # # to_buffer("0x04a817c800"), - # # to_buffer("0x5208"), - # # to_buffer("0x3535353535353535353535353535353535353535"), - # # "", - # # "", - # # to_buffer("0x25"), - # # to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # # to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") - # # ] - # # end - # - # test "to_list works for signed transactions" do - # assert Transaction.to_list(@signed_transaction_map) == [ - # "", - # to_buffer("0x04a817c800"), - # to_buffer("0x5208"), - # to_buffer("0x3535353535353535353535353535353535353535"), - # "", - # "", - # to_buffer("0x25"), - # to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") - # ] - # - # assert Transaction.to_list( - # @signed_transaction_map |> Map.keys() - # |> Enum.reduce(%{}, fn key, acc -> - # Map.put(acc, key, to_buffer(Map.get(@signed_transaction_map, key))) - # end) - # ) == [ - # "", - # to_buffer("0x04a817c800"), - # to_buffer("0x5208"), - # to_buffer("0x3535353535353535353535353535353535353535"), - # "", - # "", - # to_buffer("0x25"), - # to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), - # to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") - # ] - # end - # - # test "to_list works for not-signed transactions" do - # assert Transaction.to_list(@not_signed_transaction_map) == [ - # "", - # to_buffer("0x04a817c800"), - # to_buffer("0x5208"), - # to_buffer("0x3535353535353535353535353535353535353535"), - # "", - # "", - # <<28>>, - # "", - # "" - # ] - # - # assert Transaction.to_list( - # @not_signed_transaction_map |> Map.keys() - # |> Enum.reduce(%{}, fn key, acc -> - # Map.put(acc, key, to_buffer(Map.get(@not_signed_transaction_map, key))) - # end) - # ) == [ - # "", - # to_buffer("0x04a817c800"), - # to_buffer("0x5208"), - # to_buffer("0x3535353535353535353535353535353535353535"), - # "", - # "", - # <<28>>, - # "", - # "" - # ] - # end - # - # test "parse/1 and to_list/1 works for 0x hexed transactions" do - # @transactions - # |> Enum.slice(0..3) - # |> Enum.map(fn transaction -> transaction["raw"] end) - # |> Enum.each(fn transaction -> - # transaction_list = transaction |> Transaction.parse() |> Transaction.to_list() - # - # transaction_list - # |> Stream.with_index() - # |> Enum.each(fn {_value, index} -> - # encoded_buffer = Enum.at(transaction_list, index) |> Base.encode16(case: :lower) - # assert Enum.at(transaction, index) == "0x#{encoded_buffer}" - # end) - # end) - # end + @transactions File.read!("test/fixtures/transactions.json") |> Poison.decode!() + @first_transaction_rlp "0xf864808504a817c800825208943535353535353535353535353535353535353535808025a0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116da0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d" + @not_signed_transaction_list [ + "", + "0x04a817c800", + "0x5208", + "0x3535353535353535353535353535353535353535", + "", + "" + ] + @signed_transaction_list [ + "", + "0x04a817c800", + "0x5208", + "0x3535353535353535353535353535353535353535", + "", + "", + "0x25", + "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d", + "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d" + ] + @not_signed_transaction_map %{ + nonce: "", + gas_price: "0x04a817c800", + gas_limit: "0x5208", + to: "0x3535353535353535353535353535353535353535", + value: "", + data: "" + } + @signed_transaction_map %{ + nonce: "", + gas_price: "0x04a817c800", + gas_limit: "0x5208", + to: "0x3535353535353535353535353535353535353535", + value: "", + data: "", + v: "0x25", + r: "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d", + s: "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d" + } + + test "parsing encoded transaction rlp returns a transaction map" do + assert Transaction.parse(@first_transaction_rlp) == %{ + data: "", + gas_limit: to_buffer("0x5208"), + gas_price: to_buffer("0x04a817c800"), + nonce: "", + r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), + s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + v: to_buffer("0x25"), + value: "" + } + end + + test "parsing a not-signed transaction list returns a transaction map" do + assert Transaction.parse(@not_signed_transaction_list) == %{ + nonce: "", + gas_price: to_buffer("0x04a817c800"), + gas_limit: to_buffer("0x5208"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + value: "", + data: "" + } + + buffered_transaction_list = + @not_signed_transaction_list + |> Enum.map(fn item -> to_buffer(item) end) + + assert buffered_transaction_list |> Transaction.parse() == %{ + nonce: "", + gas_price: to_buffer("0x04a817c800"), + gas_limit: to_buffer("0x5208"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + value: "", + data: "" + } + end + + test "parsing a signed transaction list returns a transaction map" do + assert Transaction.parse(@signed_transaction_list) == %{ + nonce: "", + gas_price: to_buffer("0x04a817c800"), + gas_limit: to_buffer("0x5208"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + value: "", + data: "", + v: to_buffer("0x25"), + r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), + s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") + } + + buffered_transaction_list = + @signed_transaction_list + |> Enum.map(fn item -> to_buffer(item) end) + + assert buffered_transaction_list |> Transaction.parse() == %{ + nonce: "", + gas_price: to_buffer("0x04a817c800"), + gas_limit: to_buffer("0x5208"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + value: "", + data: "", + v: to_buffer("0x25"), + r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), + s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") + } + end + + test "parsing a not-signed transaction map returns a buffered transaction map" do + assert Transaction.parse(@not_signed_transaction_map) == %{ + nonce: "", + gas_price: to_buffer("0x04a817c800"), + gas_limit: to_buffer("0x5208"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + value: "", + data: "" + } + + assert Transaction.parse( + @not_signed_transaction_map |> Map.keys() + |> Enum.reduce(%{}, fn key, acc -> + Map.put(acc, key, to_buffer(Map.get(@not_signed_transaction_map, key))) + end) + ) == %{ + nonce: "", + gas_price: to_buffer("0x04a817c800"), + gas_limit: to_buffer("0x5208"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + value: "", + data: "" + } + end + + test "parsing a signed transaction map returns a buffered transaction map" do + assert Transaction.parse(@signed_transaction_map) == %{ + nonce: "", + gas_price: to_buffer("0x04a817c800"), + gas_limit: to_buffer("0x5208"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + value: "", + data: "", + v: to_buffer("0x25"), + r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), + s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") + } + + assert Transaction.parse( + @signed_transaction_map |> Map.keys() + |> Enum.reduce(%{}, fn key, acc -> + Map.put(acc, key, to_buffer(Map.get(@signed_transaction_map, key))) + end) + ) == %{ + nonce: "", + gas_price: to_buffer("0x04a817c800"), + gas_limit: to_buffer("0x5208"), + to: to_buffer("0x3535353535353535353535353535353535353535"), + value: "", + data: "", + v: to_buffer("0x25"), + r: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), + s: to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") + } + end + + test "to_list works for encoded rlp transactions" do + assert Transaction.to_list(@first_transaction_rlp) == [ + "", + to_buffer("0x04a817c800"), + to_buffer("0x5208"), + to_buffer("0x3535353535353535353535353535353535353535"), + "", + "", + to_buffer("0x25"), + to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), + to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") + ] + end + + test "to_list works for signed transactions" do + assert Transaction.to_list(@signed_transaction_map) == [ + "", + to_buffer("0x04a817c800"), + to_buffer("0x5208"), + to_buffer("0x3535353535353535353535353535353535353535"), + "", + "", + to_buffer("0x25"), + to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), + to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") + ] + + assert Transaction.to_list( + @signed_transaction_map |> Map.keys() + |> Enum.reduce(%{}, fn key, acc -> + Map.put(acc, key, to_buffer(Map.get(@signed_transaction_map, key))) + end) + ) == [ + "", + to_buffer("0x04a817c800"), + to_buffer("0x5208"), + to_buffer("0x3535353535353535353535353535353535353535"), + "", + "", + to_buffer("0x25"), + to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"), + to_buffer("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d") + ] + end + + test "to_list works for not-signed transactions" do + assert Transaction.to_list(@not_signed_transaction_map) == [ + "", + to_buffer("0x04a817c800"), + to_buffer("0x5208"), + to_buffer("0x3535353535353535353535353535353535353535"), + "", + "", + <<28>>, + "", + "" + ] + + assert Transaction.to_list( + @not_signed_transaction_map |> Map.keys() + |> Enum.reduce(%{}, fn key, acc -> + Map.put(acc, key, to_buffer(Map.get(@not_signed_transaction_map, key))) + end) + ) == [ + "", + to_buffer("0x04a817c800"), + to_buffer("0x5208"), + to_buffer("0x3535353535353535353535353535353535353535"), + "", + "", + <<28>>, + "", + "" + ] + end + + test "parse/1 and to_list/1 works for 0x hexed transactions" do + @transactions + |> Enum.slice(0..3) + |> Enum.map(fn transaction -> transaction["raw"] end) + |> Enum.each(fn transaction -> + transaction_list = transaction |> Transaction.parse() |> Transaction.to_list() + + transaction_list + |> Stream.with_index() + |> Enum.each(fn {_value, index} -> + encoded_buffer = Enum.at(transaction_list, index) |> Base.encode16(case: :lower) + assert Enum.at(transaction, index) == "0x#{encoded_buffer}" + end) + end) + end end diff --git a/test/eth/transaction/signer_test.exs b/test/eth/transaction/signer_test.exs index 1245b14..4daaed2 100644 --- a/test/eth/transaction/signer_test.exs +++ b/test/eth/transaction/signer_test.exs @@ -1,98 +1,118 @@ -require IEx - -defmodule ETH.Transaction.Signer.Test do +defmodule ETH.ETH.Signer.Test do use ExUnit.Case import ETH.Utils - alias ETH.Transaction - + @eip155_transactions File.read!("test/fixtures/eip155_vitalik_tests.json") |> Poison.decode!() @transactions File.read!("test/fixtures/transactions.json") |> Poison.decode!() - @first_example_transaction %{ - nonce: "0x00", - gas_price: "0x09184e72a000", - gas_limit: "0x2710", - to: "0x0000000000000000000000000000000000000000", - value: "0x00", - data: "0x7f7465737432000000000000000000000000000000000000000000000000000000600057", - # EIP 155 chainId - mainnet: 1, ropsten: 3 - chain_id: 3 - } @encoded_example_private_key "75c3b11e480f8ba3db792424bebda1fc8dea2b254287e3a9af9ed50c7d255720" - @decoded_example_private_key Base.decode16!("75c3b11e480f8ba3db792424bebda1fc8dea2b254287e3a9af9ed50c7d255720", case: :mixed) + @decoded_example_private_key Base.decode16!( + "75c3b11e480f8ba3db792424bebda1fc8dea2b254287e3a9af9ed50c7d255720", + case: :mixed + ) setup_all do - ETH.TestClient.start + ETH.TestClient.start() - on_exit fn -> - ETH.TestClient.stop - end + on_exit(fn -> + ETH.TestClient.stop() + end) :ok end test "hash/2 works" do - target_hash = "DF2A7CB6D05278504959987A144C116DBD11CBDC50D6482C5BAE84A7F41E2113" + @eip155_transactions + |> Enum.each(fn transaction -> + target_transaction = + transaction + |> Map.get("transaction") + + transaction_hash = + ETH.parse(%{ + data: target_transaction["data"], + gas_limit: target_transaction["gasLimit"], + gas_price: target_transaction["gasPrice"], + nonce: target_transaction["nonce"], + r: target_transaction["r"], + s: target_transaction["s"], + to: target_transaction["to"], + v: target_transaction["v"], + value: target_transaction["value"] + }) + |> ETH.to_list() + |> ETH.hash_transaction(false) + + assert transaction_hash |> Base.encode16(case: :lower) == transaction |> Map.get("hash") + end) + + example_transaction = + @eip155_transactions + |> Enum.at(0) + |> Map.get("transaction") + + example_transaction_map = + ETH.parse(%{ + data: example_transaction["data"], + gas_limit: example_transaction["gasLimit"], + gas_price: example_transaction["gasPrice"], + nonce: example_transaction["nonce"], + r: example_transaction["r"], + s: example_transaction["s"], + to: example_transaction["to"], + v: example_transaction["v"], + value: example_transaction["value"] + }) - assert Transaction.hash(@first_example_transaction, false) |> Base.encode16() == target_hash - assert @first_example_transaction - |> Transaction.to_list() - |> List.insert_at(-1, @first_example_transaction.chain_id) - |> Transaction.hash(false) - |> Base.encode16() == target_hash + target_hash = "E0BE81F8D506DBE3A5549E720B51EB79492378D6638087740824F168667E5239" - next_target_hash = "B89EE1E3B4FF893AC8C435BE40EA94A1BA0EB3F64B48382DA967780BAFC8DBB1" + assert ETH.hash_transaction(example_transaction_map, false) |> Base.encode16() == target_hash - assert Transaction.hash(@first_example_transaction) |> Base.encode16() == next_target_hash - assert @first_example_transaction - |> Transaction.to_list() - |> List.insert_at(-1, @first_example_transaction.chain_id) - |> Transaction.hash() - |> Base.encode16() == next_target_hash + next_target_hash = "25465F8B50446A4AB4269ACC6DB9D0D48A196C8F7E02B06881FEC6ABFF5C6C12" - assert Transaction.hash(@first_example_transaction, true) |> Base.encode16() == next_target_hash - assert @first_example_transaction - |> Transaction.to_list() - |> List.insert_at(-1, @first_example_transaction.chain_id) - |> Transaction.hash(true) - |> Base.encode16() == next_target_hash + assert ETH.hash_transaction(example_transaction_map) |> Base.encode16() == next_target_hash + assert ETH.hash_transaction(example_transaction_map, true) |> Base.encode16() == + next_target_hash - first_transaction_list = + example_transaction_list = @transactions |> Enum.at(2) |> Map.get("raw") - |> Transaction.parse() - |> Transaction.to_list() - - second_transaction_list = - @transactions - |> Enum.at(3) - |> Map.get("raw") - |> Transaction.parse() - |> Transaction.to_list() + |> ETH.parse() + |> ETH.to_list() - assert Transaction.hash(first_transaction_list) == + assert ETH.hash_transaction(example_transaction_list) == decode16("375a8983c9fc56d7cfd118254a80a8d7403d590a6c9e105532b67aca1efb97aa") - assert Transaction.hash(first_transaction_list, false) == + assert ETH.hash_transaction(example_transaction_list, false) == decode16("61e1ec33764304dddb55348e7883d4437426f44ab3ef65e6da1e025734c03ff0") - assert Transaction.hash(first_transaction_list, true) == + assert ETH.hash_transaction(example_transaction_list, true) == decode16("375a8983c9fc56d7cfd118254a80a8d7403d590a6c9e105532b67aca1efb97aa") + end - assert Transaction.hash(second_transaction_list) == - decode16("0f09dc98ea85b7872f4409131a790b91e7540953992886fc268b7ba5c96820e4") + test "sign_transaction works for transaction maps with encoded private keys" do + output = + ETH.build(%{ + nonce: 1, + to: "0x0dcd857b3c5db88cb7c025f0ef229331cfadffe5", + value: 22, + gas_limit: 100_000, + gas_price: 1000, + from: "0x42c343d8b77a9106d7112b71ba6b3030a34ba560" + }) + |> ETH.sign_transaction(@encoded_example_private_key) + |> Base.encode16(case: :lower) - assert Transaction.hash(second_transaction_list, true) == - decode16("0f09dc98ea85b7872f4409131a790b91e7540953992886fc268b7ba5c96820e4") + serialized_hash = + "f862018203e8830186a0940dcd857b3c5db88cb7c025f0ef229331cfadffe516801ba09b35467cf48151683b41ed8425d59317716f4f639126d7eb69167ac95c8c3ba3a00d5d21f4c6fc400202dadc09a192b011cc16aefa6155d4e5df15d77d9f6c8f9f" - assert Transaction.hash(second_transaction_list, false) == - decode16("f97c73fdca079da7652dbc61a46cd5aeef804008e057be3e712c43eac389aaf0") + assert output == serialized_hash end - test "sign_transaction works for transaction maps with encoded private keys" do + test "sign_transaction works for transaction maps with decoded private keys" do output = - Transaction.build(%{ + ETH.build(%{ nonce: 1, to: "0x0dcd857b3c5db88cb7c025f0ef229331cfadffe5", value: 22, @@ -100,7 +120,7 @@ defmodule ETH.Transaction.Signer.Test do gas_price: 1000, from: "0x42c343d8b77a9106d7112b71ba6b3030a34ba560" }) - |> Transaction.sign_transaction(@encoded_example_private_key) + |> ETH.sign_transaction(@decoded_example_private_key) |> Base.encode16(case: :lower) serialized_hash = @@ -109,9 +129,9 @@ defmodule ETH.Transaction.Signer.Test do assert output == serialized_hash end - test "sign_transaction works for transaction maps with decoded private keys" do + test "sign_transaction works for transaction lists with encoded private keys" do output = - Transaction.build(%{ + ETH.build(%{ nonce: 1, to: "0x0dcd857b3c5db88cb7c025f0ef229331cfadffe5", value: 22, @@ -119,7 +139,8 @@ defmodule ETH.Transaction.Signer.Test do gas_price: 1000, from: "0x42c343d8b77a9106d7112b71ba6b3030a34ba560" }) - |> Transaction.sign_transaction(@decoded_example_private_key) + |> ETH.to_list() + |> ETH.sign_transaction(@encoded_example_private_key) |> Base.encode16(case: :lower) serialized_hash = @@ -128,44 +149,23 @@ defmodule ETH.Transaction.Signer.Test do assert output == serialized_hash end - # TODO: should I add ExRLP.encode() to sign_transaction(transaction_lists) it also changes the test results of above - # test "sign_transaction works for transaction lists with encoded private keys" do - # output = - # Transaction.build(%{ - # nonce: 1, - # to: "0x0dcd857b3c5db88cb7c025f0ef229331cfadffe5", - # value: 22, - # gas_limit: 100_000, - # gas_price: 1000, - # from: "0x42c343d8b77a9106d7112b71ba6b3030a34ba560" - # }) - # |> Transaction.to_list() - # |> Transaction.sign_transaction(@encoded_example_private_key) - # |> Base.encode16(case: :lower) - # - # serialized_hash = - # "f862018203e8830186a0940dcd857b3c5db88cb7c025f0ef229331cfadffe516801ba09b35467cf48151683b41ed8425d59317716f4f639126d7eb69167ac95c8c3ba3a00d5d21f4c6fc400202dadc09a192b011cc16aefa6155d4e5df15d77d9f6c8f9f" - # - # assert output == serialized_hash - # end - # - # test "sign_transaction works for transaction lists with decoded private keys" do - # output = - # Transaction.build(%{ - # nonce: 1, - # to: "0x0dcd857b3c5db88cb7c025f0ef229331cfadffe5", - # value: 22, - # gas_limit: 100_000, - # gas_price: 1000, - # from: "0x42c343d8b77a9106d7112b71ba6b3030a34ba560" - # }) - # |> Transaction.to_list() - # |> Transaction.sign_transaction(@decoded_example_private_key) - # |> Base.encode16(case: :lower) - # - # serialized_hash = - # "f862018203e8830186a0940dcd857b3c5db88cb7c025f0ef229331cfadffe516801ba09b35467cf48151683b41ed8425d59317716f4f639126d7eb69167ac95c8c3ba3a00d5d21f4c6fc400202dadc09a192b011cc16aefa6155d4e5df15d77d9f6c8f9f" - # - # assert output == serialized_hash - # end + test "sign_transaction works for transaction lists with decoded private keys" do + output = + ETH.build(%{ + nonce: 1, + to: "0x0dcd857b3c5db88cb7c025f0ef229331cfadffe5", + value: 22, + gas_limit: 100_000, + gas_price: 1000, + from: "0x42c343d8b77a9106d7112b71ba6b3030a34ba560" + }) + |> ETH.to_list() + |> ETH.sign_transaction(@decoded_example_private_key) + |> Base.encode16(case: :lower) + + serialized_hash = + "f862018203e8830186a0940dcd857b3c5db88cb7c025f0ef229331cfadffe516801ba09b35467cf48151683b41ed8425d59317716f4f639126d7eb69167ac95c8c3ba3a00d5d21f4c6fc400202dadc09a192b011cc16aefa6155d4e5df15d77d9f6c8f9f" + + assert output == serialized_hash + end end diff --git a/test/eth/transaction_queries_test.exs b/test/eth/transaction_queries_test.exs index 07b74bf..cc4e7c3 100644 --- a/test/eth/transaction_queries_test.exs +++ b/test/eth/transaction_queries_test.exs @@ -3,11 +3,11 @@ defmodule ETH.TransactionQueries.Test do use ExUnit.Case setup_all do - ETH.TestClient.start + ETH.TestClient.start() - on_exit fn -> - ETH.TestClient.stop - end + on_exit(fn -> + ETH.TestClient.stop() + end) :ok end diff --git a/test/eth/transaction_test.exs b/test/eth/transaction_test.exs index 8a5ca78..1dc0431 100644 --- a/test/eth/transaction_test.exs +++ b/test/eth/transaction_test.exs @@ -4,14 +4,12 @@ defmodule TransactionTest do use ExUnit.Case import ETH.Utils - alias ETH.Transaction - setup_all do - ETH.TestClient.start + ETH.TestClient.start() - on_exit fn -> - ETH.TestClient.stop - end + on_exit(fn -> + ETH.TestClient.stop() + end) :ok end @@ -47,10 +45,12 @@ defmodule TransactionTest do @eip155_transactions File.read!("test/fixtures/eip155_vitalik_tests.json") |> Poison.decode!() test "send_transaction(wallet, params) works" do - result = Transaction.send_transaction(@first_wallet_in_client, %{ - to: @first_random_wallet.eth_address, - value: 22 - }) + result = + ETH.send_transaction(@first_wallet_in_client, %{ + to: @first_random_wallet.eth_address, + value: 22 + }) + {:ok, transaction_hash} = result assert result == {:ok, "0x5c1cf004a7d239c65e1ef582826258b7835b0301063605c238947682fe3303d8"} @@ -58,19 +58,19 @@ defmodule TransactionTest do Process.sleep(3850) assert ETH.get_transaction!(transaction_hash) |> Map.drop([:block_hash, :block_number]) == %{ - from: "0x051d51ba1e1d58db72efea63549a6792c8f5cb13", - gas: 21000, - gas_price: 20000000000, - hash: "0x5c1cf004a7d239c65e1ef582826258b7835b0301063605c238947682fe3303d8", - input: "0x0", - nonce: 0, - to: "0xdf7a2dc05778d1b507e921fb8ad78cb431590ba7", - transaction_index: 0, - value: 22 - } + from: "0x051d51ba1e1d58db72efea63549a6792c8f5cb13", + gas: 21000, + gas_price: 20_000_000_000, + hash: "0x5c1cf004a7d239c65e1ef582826258b7835b0301063605c238947682fe3303d8", + input: "0x0", + nonce: 0, + to: "0xdf7a2dc05778d1b507e921fb8ad78cb431590ba7", + transaction_index: 0, + value: 22 + } end - # Transaction.build(%{ + # ETH.build(%{ # nonce: 1, # to: "0x0dcd857b3c5db88cb7c025f0ef229331cfadffe5", # value: 22, @@ -78,25 +78,31 @@ defmodule TransactionTest do # gas_price: 1000, # from: "0x42c343d8b77a9106d7112b71ba6b3030a34ba560" # }) - # |> Transaction.sign_transaction( + # |> ETH.sign_transaction( # "75c3b11e480f8ba3db792424bebda1fc8dea2b254287e3a9af9ed50c7d255720" # ) # |> Base.encode16(case: :lower) test "send works" do - result = Transaction.send("f862018203e8830186a0940dcd857b3c5db88cb7c025f0ef229331cfadffe516801ba09b35467cf48151683b41ed8425d59317716f4f639126d7eb69167ac95c8c3ba3a00d5d21f4c6fc400202dadc09a192b011cc16aefa6155d4e5df15d77d9f6c8f9f") + result = + ETH.send( + "f862018203e8830186a0940dcd857b3c5db88cb7c025f0ef229331cfadffe516801ba09b35467cf48151683b41ed8425d59317716f4f639126d7eb69167ac95c8c3ba3a00d5d21f4c6fc400202dadc09a192b011cc16aefa6155d4e5df15d77d9f6c8f9f" + ) assert result == {:ok, "0xfa19fa6afd6c5b5ef9979ecf3b437e0b844484cc3a3b6f97082be60799767510"} end test "send! works" do - result = Transaction.send!("f862018203e8830186a0940dcd857b3c5db88cb7c025f0ef229331cfadffe516801ba09b35467cf48151683b41ed8425d59317716f4f639126d7eb69167ac95c8c3ba3a00d5d21f4c6fc400202dadc09a192b011cc16aefa6155d4e5df15d77d9f6c8f9f") + result = + ETH.send!( + "f862018203e8830186a0940dcd857b3c5db88cb7c025f0ef229331cfadffe516801ba09b35467cf48151683b41ed8425d59317716f4f639126d7eb69167ac95c8c3ba3a00d5d21f4c6fc400202dadc09a192b011cc16aefa6155d4e5df15d77d9f6c8f9f" + ) assert result == "0xfa19fa6afd6c5b5ef9979ecf3b437e0b844484cc3a3b6f97082be60799767510" end # test "send_transaction(wallet, params) works when params is a wallet" do - # result = Transaction.send_transaction(@first_wallet_in_client, %{ + # result = ETH.send_transaction(@first_wallet_in_client, %{ # to: @first_random_wallet.eth_address, # value: 300, # data: "" @@ -120,10 +126,10 @@ defmodule TransactionTest do transaction_list = transaction |> Map.get("raw") - |> Transaction.parse() - |> Transaction.to_list() + |> ETH.parse() + |> ETH.to_list() - result = Transaction.get_sender_address(transaction_list) + result = ETH.get_sender_address(transaction_list) assert result == "0x" <> String.upcase(transaction["sendersAddress"]) end) end @@ -131,21 +137,21 @@ defmodule TransactionTest do test "verify EIP155 Signature based on Vitalik\'s tests" do @eip155_transactions |> Enum.each(fn transaction -> - transaction_list = transaction |> Map.get("rlp") |> Transaction.to_list() + transaction_list = transaction |> Map.get("rlp") |> ETH.to_list() expected_hash = transaction["hash"] |> Base.decode16!(case: :lower) - assert Transaction.hash(transaction_list, false) == expected_hash + assert ETH.hash_transaction(transaction_list, false) == expected_hash sender_address = transaction["sender"] |> String.upcase() - assert Transaction.get_sender_address(transaction_list) == "0x#{sender_address}" + assert ETH.get_sender_address(transaction_list) == "0x#{sender_address}" end) end # NOTE: probably not needed changes th API # test "can sign an empty transaction with right chain id" do - # Transaction.hash_transaction(%{chain_id: 42 }) + # ETH.hash_transaction(%{chain_id: 42 }) # end # test "sign works" do - # signature = Transaction.sign_transaction_list(@first_example_transaction, @first_example_wallet.private_key) + # signature = ETH.sign_transaction_list(@first_example_transaction, @first_example_wallet.private_key) # |> Base.encode16(case: :lower) # assert signature == "f889808609184e72a00082271094000000000000000000000000000000000000000080a47f746573743200000000000000000000000000000000000000000000000000000060005729a0f2d54d3399c9bcd3ac3482a5ffaeddfe68e9a805375f626b4f2f8cf530c2d95aa05b3bb54e6e8db52083a9b674e578c843a87c292f0383ddba168573808d36dc8e" # end diff --git a/test/eth/transactions_test.exs b/test/eth/transactions_test.exs deleted file mode 100644 index fc45d95..0000000 --- a/test/eth/transactions_test.exs +++ /dev/null @@ -1,54 +0,0 @@ -# require IEx -defmodule ETH.TransactionsTest do - use ExUnit.Case - - # @transactions File.read!("test/fixtures/transactions.json") |> Poison.decode!() - # @eip155_transactions File.read!("test/fixtures/eip155_vitalik_tests.json") |> Poison.decode!() - - # test "verify signature" do - # @transactions |> Enum.each(fn(transaction) -> - # hash = # ETH.Transaction.hash() - # # transaction - # - # # ETH.Transaction.verify_signature([v: v, r: r, s: s]) - # - # # signed_transaction - # # assert ExRLP.encode(transaction.raw) == signed_transaction - # - # # NOTE: t - # end) - # - # end - - # test "can get transactions sender address after signing it" do - # @transactions |> Enum.each(fn(transaction) -> - # private_key = Map.get(transaction, "privateKey") - # senders_address = Map.get(transaction, "sendersAddress") - # if private_key != nil do - # decoded_private_key = Base.decode16!(private_key, case: :mixed) - # # IO.inspect(decoded_private_key) - # transaction_params = ETH.Transaction.decode_transaction_list(Map.get(transaction, "raw")) - # # IEx.pry - # signature = ETH.Transaction.sign_transaction(transaction_params, decoded_private_key) - # # IEx.pry - # assert ETH.Transaction.get_sender_address(signature) == senders_address - # end - # end) - # end - - # test "can get transactions sender public key after signing it" do - # - # end - - # NOTE: maybe verify/check_gas function and tests - # NOTE: maybe roundtrip a transaction encoding/decoding - # there are upfront gas/costs tests that we probably dont need - - # test "verify EIP155 Signature based on Vitalik\'s tests" do - # @eip155_transactions |> Enum.each(fn(transaction) -> - # IEx.pry - # end) - # end -end - -# TODO: also implement transaction-runner tests diff --git a/test/eth/utils_test.exs b/test/eth/utils_test.exs index f881e68..75b0093 100644 --- a/test/eth/utils_test.exs +++ b/test/eth/utils_test.exs @@ -3,90 +3,90 @@ defmodule ETH.UtilsTest do use ExUnit.Case test "get_private_key/0 works" do - assert ETH.Utils.get_private_key() |> byte_size == 32 - assert ETH.Utils.get_private_key() != ETH.Utils.get_private_key() + assert ETH.get_private_key() |> byte_size == 32 + assert ETH.get_private_key() != ETH.get_private_key() end test "get_public_key/1 works" do private_key = :crypto.strong_rand_bytes(32) another_private_key = :crypto.strong_rand_bytes(32) - public_key = ETH.Utils.get_public_key(private_key) + public_key = ETH.get_public_key(private_key) assert public_key |> byte_size == 65 - assert public_key == ETH.Utils.get_public_key(private_key) - assert public_key != ETH.Utils.get_public_key(another_private_key) + assert public_key == ETH.get_public_key(private_key) + assert public_key != ETH.get_public_key(another_private_key) 1..1000 |> Enum.each(fn _ -> private_key = :crypto.strong_rand_bytes(32) - ETH.Utils.get_public_key(private_key) + ETH.get_public_key(private_key) end) end test "get_public_key/1 works for encoded private keys" do private_key = :crypto.strong_rand_bytes(32) |> Base.encode16() another_private_key = :crypto.strong_rand_bytes(32) |> Base.encode16() - public_key = ETH.Utils.get_public_key(private_key) + public_key = ETH.get_public_key(private_key) assert public_key |> byte_size == 65 - assert public_key == ETH.Utils.get_public_key(private_key) - assert public_key != ETH.Utils.get_public_key(another_private_key) + assert public_key == ETH.get_public_key(private_key) + assert public_key != ETH.get_public_key(another_private_key) end test "get_address/1 works for private keys" do private_key = :crypto.strong_rand_bytes(32) another_private_key = :crypto.strong_rand_bytes(32) - eth_address = ETH.Utils.get_address(private_key) + eth_address = ETH.get_address(private_key) assert eth_address |> byte_size == 42 - assert eth_address == ETH.Utils.get_address(private_key) + assert eth_address == ETH.get_address(private_key) assert eth_address |> String.slice(0, 2) == "0x" - assert eth_address != ETH.Utils.get_address(another_private_key) + assert eth_address != ETH.get_address(another_private_key) end test "get_address/1 works for encoded private keys" do private_key = :crypto.strong_rand_bytes(32) another_private_key = :crypto.strong_rand_bytes(32) - eth_address = ETH.Utils.get_address(private_key |> Base.encode16()) + eth_address = ETH.get_address(private_key |> Base.encode16()) assert eth_address |> byte_size == 42 - assert eth_address == ETH.Utils.get_address(private_key |> Base.encode16()) + assert eth_address == ETH.get_address(private_key |> Base.encode16()) assert eth_address |> String.slice(0, 2) == "0x" - assert eth_address != ETH.Utils.get_address(another_private_key) + assert eth_address != ETH.get_address(another_private_key) end test "get_address/1 works for public keys" do private_key = :crypto.strong_rand_bytes(32) |> Base.encode16() - public_key = ETH.Utils.get_public_key(private_key) - eth_address = ETH.Utils.get_address(public_key) + public_key = ETH.get_public_key(private_key) + eth_address = ETH.get_address(public_key) another_private_key = :crypto.strong_rand_bytes(32) |> Base.encode16() - another_public_key = ETH.Utils.get_public_key(another_private_key) |> Base.encode16() - another_eth_address = ETH.Utils.get_address(another_public_key) + another_public_key = ETH.get_public_key(another_private_key) |> Base.encode16() + another_eth_address = ETH.get_address(another_public_key) assert eth_address |> byte_size == 42 - assert eth_address == ETH.Utils.get_address(public_key) - assert eth_address == ETH.Utils.get_address(private_key) + assert eth_address == ETH.get_address(public_key) + assert eth_address == ETH.get_address(private_key) assert eth_address |> String.slice(0, 2) == "0x" assert eth_address != another_eth_address - assert eth_address != ETH.Utils.get_address(another_private_key) + assert eth_address != ETH.get_address(another_private_key) end test "get_address/1 works for encoded public keys" do private_key = :crypto.strong_rand_bytes(32) - public_key = ETH.Utils.get_public_key(private_key) - eth_address = ETH.Utils.get_address(public_key |> Base.encode16()) + public_key = ETH.get_public_key(private_key) + eth_address = ETH.get_address(public_key |> Base.encode16()) another_private_key = :crypto.strong_rand_bytes(32) - another_public_key = ETH.Utils.get_public_key(another_private_key) - another_eth_address = ETH.Utils.get_address(another_public_key |> Base.encode16()) + another_public_key = ETH.get_public_key(another_private_key) + another_eth_address = ETH.get_address(another_public_key |> Base.encode16()) assert eth_address |> byte_size == 42 - assert eth_address == ETH.Utils.get_address(public_key |> Base.encode16()) - assert eth_address == ETH.Utils.get_address(private_key |> Base.encode16()) + assert eth_address == ETH.get_address(public_key |> Base.encode16()) + assert eth_address == ETH.get_address(private_key |> Base.encode16()) assert eth_address |> String.slice(0, 2) == "0x" assert eth_address != another_eth_address - assert eth_address != ETH.Utils.get_address(another_private_key |> Base.encode16()) + assert eth_address != ETH.get_address(another_private_key |> Base.encode16()) end test "secp256k1_signature/2 works" do @@ -101,7 +101,7 @@ defmodule ETH.UtilsTest do target_signature = "c2a738b1eb84280399115f4bec9e52b8de494a3ea7d9f069277119a02de4a49876f3168913e968e9484e2e0e447cd7adc56505e25cbc372330793a31f0bf7195" - secp256k1_signature = ETH.Utils.secp256k1_signature(hash, private_key) + secp256k1_signature = ETH.secp256k1_signature(hash, private_key) assert secp256k1_signature[:signature] |> Base.encode16(case: :lower) == target_signature end diff --git a/test/eth/wallet_test.exs b/test/eth/wallet_test.exs index efee592..a5be706 100644 --- a/test/eth/wallet_test.exs +++ b/test/eth/wallet_test.exs @@ -11,12 +11,11 @@ defmodule ETH.WalletTest do assert wallet[:private_key] != second_wallet[:private_key] assert wallet[:public_key] - assert wallet[:public_key] == - ETH.Utils.get_public_key(wallet[:private_key]) |> Base.encode16() + assert wallet[:public_key] == ETH.get_public_key(wallet[:private_key]) |> Base.encode16() assert wallet[:public_key] != second_wallet[:public_key] assert wallet[:eth_address] - assert wallet[:eth_address] == ETH.Utils.get_address(wallet[:public_key]) + assert wallet[:eth_address] == ETH.get_address(wallet[:public_key]) assert wallet[:eth_address] != second_wallet[:eth_address] assert wallet[:mnemonic_phrase] diff --git a/test/eth_test.exs b/test/eth_test.exs index fcf6ddc..ef70533 100644 --- a/test/eth_test.exs +++ b/test/eth_test.exs @@ -24,7 +24,7 @@ defmodule ETHTest do # # test "hash/1 works" do # target_hash = "5C207A650B59A8C2D1271F5CBDA78A658CB411A87271D68062E61AB1A3F85CF9" - # assert ETH.Transaction.hash(@first_transaction_list) |> Base.encode16 == target_hash + # assert ETH.Transaction.hash_transaction(@first_transaction_list) |> Base.encode16 == target_hash # end # # test "secp256k1_signature/2 works" do diff --git a/test/support/eth_client.exs b/test/support/eth_client.exs index f4843ca..35fed23 100644 --- a/test/support/eth_client.exs +++ b/test/support/eth_client.exs @@ -2,7 +2,7 @@ defmodule ETH.TestClient do def start do spawn(fn -> "ganache-cli -b=1 -m=\" parent leopard beauty edit tilt what blast next huge need print advice evolve move explain govern grab raccoon gown gravity gloom walnut silver reopen\"" - |> String.to_charlist + |> String.to_charlist() |> :os.cmd() end) @@ -11,7 +11,7 @@ defmodule ETH.TestClient do def stop do "pkill -f ganache-cli" - |> String.to_charlist + |> String.to_charlist() |> :os.cmd() end end diff --git a/test/test_helper.exs b/test/test_helper.exs index 68146fd..7c29b6b 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -4,9 +4,11 @@ Application.put_env(:ethereumex, :url, Application.get_env(:eth, :url, "http://l {:ok, files} = File.ls("./test/support") -Enum.each files, fn(file) -> - Code.require_file "support/#{file}", __DIR__ -end +Enum.each(files, fn file -> + Code.require_file("support/#{file}", __DIR__) +end) + +ETH.TestClient.stop() ExUnit.start()