Skip to content

Commit

Permalink
more tests added signing part is stable
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jan 24, 2018
1 parent 3620c6e commit c0dfe87
Show file tree
Hide file tree
Showing 19 changed files with 490 additions and 566 deletions.
2 changes: 2 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
use Mix.Config

config :ethereumex, url: "http://localhost:8545"
4 changes: 2 additions & 2 deletions lib/eth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/eth/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/eth/transaction/signer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -37,7 +37,7 @@ defmodule ETH.Transaction.Signer do
|> keccak256
end

def hash(
def hash_transaction(
transaction = %{
to: _to,
value: _value,
Expand All @@ -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(
Expand Down Expand Up @@ -116,7 +115,7 @@ defmodule ETH.Transaction.Signer do
<<private_key::binary-size(32)>>
) 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)

Expand All @@ -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_v>>, sig_r, sig_s]
|> ExRLP.encode()
end
end
10 changes: 7 additions & 3 deletions lib/eth/transaction_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions test/eth/eth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions test/eth/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 18 additions & 18 deletions test/eth/transaction/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
#
Expand Down
Loading

0 comments on commit c0dfe87

Please # to comment.