Skip to content

Commit

Permalink
query, utils and wallet tests are added
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Aug 30, 2017
1 parent fa589b6 commit 0f7fe0e
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 52 deletions.
8 changes: 8 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2017 Izel Nakri

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ use Mix.Config
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

config :ethereumex,
scheme: "http",
host: "localhost",
port: 8545

# You can configure your application as:
#
# config :eth, key: :value
Expand Down
42 changes: 20 additions & 22 deletions lib/eth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,14 @@ defmodule ETH do
"""

def get_private_key do
:crypto.strong_rand_bytes(32)
end

def private_key_to_address(<< private_key :: binary-size(32) >>) do
private_key
|> get_public_key()
|> public_key_to_address()
end

def get_public_key(<< private_key :: binary-size(32) >>) do
{public_key, ^private_key} = :crypto.generate_key(:ecdh, :secp256k1, private_key)
public_key
end

def public_key_to_address(<< 4 :: size(8), key :: binary-size(64) >>) do
<< _ :: binary-size(12), address :: binary-size(20) >> = keccak256(key)
address
end

def sign_transaction(
source_wallet, value, target_wallet, options \\ [gas_price: 100, gas_limit: 1000, data: "", chain_id: 3]
) do
gas_price = options.gas_price |> Hexate.encode
gas_limit = options.gas_limit |> Hexate.encode
data = data |> Hexate.encode
data = options.data |> Hexate.encode

Ethereumex.HttpClient.eth_get_transaction_count([first[:eth_address]]) |> elem(1) |> Map.get("result")
Ethereumex.HttpClient.eth_get_transaction_count([source_wallet[:eth_address]]) |> elem(1) |> Map.get("result")

# NOTE: calc nonce
%{
Expand Down Expand Up @@ -113,6 +93,24 @@ defmodule ETH do
[nonce, gas_price, gas_limit, to, value, data, v, r, s]
end

def convert(number, magnitute \\ :ether) do
denomination = [
wei: 1,
kwei: 1000,
mwei: 1000000,
gwei: 1000000000,
shannon: 1000000000,
nano: 1000000000,
szabo: 1000000000000,
micro: 1000000000000,
finney: 1000000000000000,
milli: 1000000000000000,
ether: 1000000000000000000,
] |> List.keyfind(magnitute, 0) |> elem(1)

number / denomination
end

def keccak256(data), do: :keccakf1600.hash(:sha3_256, data)
defp encode16(value), do: Base.encode16(value, case: :lower)
defp default_is_empty(map, key), do: if Map.get(map, key), do: Map.get(map, key), else: ""
Expand Down
18 changes: 18 additions & 0 deletions lib/eth/query.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule ETH.Query do
def get_accounts do
Ethereumex.HttpClient.eth_accounts
|> elem(1)
|> Map.get("result")
end

def get_balance(eth_address, denomination \\ :ether) do
result = Ethereumex.HttpClient.eth_get_balance([eth_address])
|> elem(1)
|> Map.get("result")

result
|> String.slice(2..String.length(result))
|> Hexate.to_integer
|> ETH.convert(denomination)
end
end
9 changes: 9 additions & 0 deletions lib/eth/transaction.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule ETH.Transaction do
# def set([]) do # maybe better name
#
# end

# def send(transaction) do
#
# end
end
39 changes: 39 additions & 0 deletions lib/eth/utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule ETH.Utils do

def get_private_key do
:crypto.strong_rand_bytes(32)
end

def get_public_key(<< private_key :: binary-size(32) >>) do
{public_key, ^private_key} = :crypto.generate_key(:ecdh, :secp256k1, private_key)
public_key
end
def get_public_key(<< encoded_private_key :: binary-size(64) >>) do
private_key = Base.decode16!(encoded_private_key, case: :mixed)
{public_key, ^private_key} = :crypto.generate_key(:ecdh, :secp256k1, private_key)
public_key
end

def get_address(<< private_key :: binary-size(32) >>) do
public_key = private_key |> get_public_key()
<< _ :: binary-size(12), eth_address :: binary-size(20) >> = keccak256(public_key)
"0x#{Base.encode16(eth_address)}"
end
def get_address(<< encoded_private_key :: binary-size(64) >>) do
public_key = Base.decode16!(encoded_private_key, case: :mixed) |> get_public_key()
<< _ :: binary-size(12), eth_address :: binary-size(20) >> = keccak256(public_key)
"0x#{Base.encode16(eth_address)}"
end
def get_address(<< public_key :: binary-size(65) >>) do
<< _ :: binary-size(12), eth_address :: binary-size(20) >> = keccak256(public_key)
"0x#{Base.encode16(eth_address)}"
end
def get_address(<< encoded_public_key :: binary-size(130) >>) do
public_key = Base.decode16!(encoded_public_key, case: :mixed)
<< _ :: binary-size(12), eth_address :: binary-size(20) >> = keccak256(public_key)
"0x#{Base.encode16(eth_address)}"
end

def keccak256(data), do: :keccakf1600.hash(:sha3_256, data)
defp encode16(value), do: Base.encode16(value, case: :lower)
end
39 changes: 13 additions & 26 deletions lib/eth/wallet.ex
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
defmodule ETH.Wallet do
def create(private_key \\ :crypto.strong_rand_bytes(32)) do # pattern match on the base16 version as well
{public_key, _} = :crypto.generate_key(:ecdh, :secp256k1, private_key)
<< 4 :: size(8), key :: binary-size(64) >> = public_key
<< _ :: binary-size(12), address :: binary-size(20) >> = keccak256(key)
import ETH.Utils

%{private_key: Base.encode16(private_key), public_key: Base.encode16(public_key),
eth_address: "0x#{Base.encode16(address)}"}
end

# def get_public_key(<< private_key :: binary-size(32) >>) do
# {public_key, ^private_key} = :crypto.generate_key(:ecdh, :secp256k1, private_key)
# public_key
# end
#
# def private_key_to_address(<< private_key :: binary-size(32) >>) do
# private_key
# |> get_public_key()
# |> public_key_to_address()
# end
#
# def public_key_to_address(<< 4 :: size(8), key :: binary-size(64) >>) do
# << _ :: binary-size(12), address :: binary-size(20) >> = keccak256(key)
# address
# end
def create(private_key \\ :crypto.strong_rand_bytes(32))
def create(<< encoded_private_key :: binary-size(64) >>) do
public_key = get_public_key(encoded_private_key)
eth_address = get_address(public_key)

def balance do
# Ethereumex
%{private_key: encoded_private_key, public_key: Base.encode16(public_key),
eth_address: eth_address}
end
def create(private_key) do
public_key = get_public_key(private_key)
eth_address = get_address(public_key)

defp keccak256(data), do: :keccakf1600.hash(:sha3_256, data)
%{private_key: Base.encode16(private_key), public_key: Base.encode16(public_key),
eth_address: eth_address}
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ defmodule Eth.Mixfile do
{:keccakf1600, git: "https://github.com/jur0/erlang-keccakf1600", branch: "original-keccak"},
{:ex_rlp, "~> 0.2.1"},
{:hexate, "~> 0.6.1"},
{:ethereumex, "~> 0.1.0"}
{:ethereumex, "~> 0.1.0"},
{:ex_doc, ">= 0.0.0", only: :dev}
]
end

Expand Down
16 changes: 14 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
%{"ex_rlp": {:hex, :ex_rlp, "0.2.1", "bd320900d6316cdfe01d365d4bda22eb2f39b359798daeeffd3bd1ca7ba958ec", [:mix], []},
%{"certifi": {:hex, :certifi, "1.2.1", "c3904f192bd5284e5b13f20db3ceac9626e14eeacfbb492e19583cf0e37b22be", [:rebar3], []},
"earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [:mix], []},
"ethereumex": {:hex, :ethereumex, "0.1.0", "05764dfdfcc3c98a0cd2c5452f8bd086f77187df5994d07ebbb761a189f1afc8", [:mix], [{:httpoison, "~> 0.11.1", [hex: :httpoison, optional: false]}, {:poison, "~> 3.1.0", [hex: :poison, optional: false]}]},
"ex_doc": {:hex, :ex_doc, "0.16.3", "cd2a4cfe5d26e37502d3ec776702c72efa1adfa24ed9ce723bb565f4c30bd31a", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
"ex_rlp": {:hex, :ex_rlp, "0.2.1", "bd320900d6316cdfe01d365d4bda22eb2f39b359798daeeffd3bd1ca7ba958ec", [:mix], []},
"hackney": {:hex, :hackney, "1.8.6", "21a725db3569b3fb11a6af17d5c5f654052ce9624219f1317e8639183de4a423", [:rebar3], [{:certifi, "1.2.1", [hex: :certifi, optional: false]}, {:idna, "5.0.2", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"hexate": {:hex, :hexate, "0.6.1", "1cea42e462c1daa32223127d4752e71016c3d933d492b9bb7fa4709a4a0fd50d", [:mix], []},
"httpoison": {:hex, :httpoison, "0.11.2", "9e59f17a473ef6948f63c51db07320477bad8ba88cf1df60a3eee01150306665", [:mix], [{:hackney, "~> 1.8.0", [hex: :hackney, optional: false]}]},
"idna": {:hex, :idna, "5.0.2", "ac203208ada855d95dc591a764b6e87259cb0e2a364218f215ad662daa8cd6b4", [:rebar3], [{:unicode_util_compat, "0.2.0", [hex: :unicode_util_compat, optional: false]}]},
"keccakf1600": {:git, "https://github.com/jur0/erlang-keccakf1600", "a87d31d32250091262e0da5ee2c2a4535bc50945", [branch: "original-keccak"]},
"libsecp256k1": {:git, "https://github.com/mbrix/libsecp256k1.git", "671be513a6c19db47fbeea0ceefbf61421d196cc", []}}
"libsecp256k1": {:git, "https://github.com/mbrix/libsecp256k1.git", "671be513a6c19db47fbeea0ceefbf61421d196cc", []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.2.0", "dbbccf6781821b1c0701845eaf966c9b6d83d7c3bfc65ca2b78b88b8678bfa35", [:rebar3], []}}
30 changes: 30 additions & 0 deletions test/eth/query_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule ETH.QueryTest do
use ExUnit.Case

test "get_accounts/0 works" do
accounts = ETH.Query.get_accounts

assert accounts

accounts |> Enum.each(fn(x) ->
assert String.length(x) == 42
assert String.slice(x, 0..1) == "0x"
end)
end

test "get_balance/1 returns the balance of an ethereum address in ether by default" do
address_with_balance = ETH.Query.get_accounts |> List.first
address_with_no_balance = ETH.Wallet.create |> Map.get(:eth_address)

assert ETH.Query.get_balance(address_with_balance) == 100.0
assert ETH.Query.get_balance(address_with_no_balance) == 0.0
end

test "balance/1 returns the balance of an ethereum address with specific denomination" do
address_with_balance = ETH.Query.get_accounts |> List.first
address_with_no_balance = ETH.Wallet.create |> Map.get(:eth_address)

assert ETH.Query.get_balance(address_with_balance, :wei) == 1.0e20
assert ETH.Query.get_balance(address_with_no_balance, :wei) == 0.0
end
end
84 changes: 84 additions & 0 deletions test/eth/utils_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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
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)

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)
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)

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)
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)

assert eth_address |> byte_size == 42
assert eth_address == ETH.Utils.get_address(private_key)
assert eth_address |> String.slice(0, 2) == "0x"
assert eth_address != ETH.Utils.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)

assert eth_address |> byte_size == 42
assert eth_address == ETH.Utils.get_address(private_key |> Base.encode16)
assert eth_address |> String.slice(0, 2) == "0x"
assert eth_address != ETH.Utils.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)

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)

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 |> String.slice(0, 2) == "0x"
assert eth_address != another_eth_address
assert eth_address != ETH.Utils.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)

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)

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 |> String.slice(0, 2) == "0x"
assert eth_address != another_eth_address
assert eth_address != ETH.Utils.get_address(another_private_key |> Base.encode16)
end
end
43 changes: 43 additions & 0 deletions test/eth/wallet_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule ETH.WalletTest do
use ExUnit.Case

test "create/1 without private_key creates a random wallet" do
wallet = ETH.Wallet.create()
second_wallet = ETH.Wallet.create()

assert wallet[:private_key]
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] != second_wallet[:public_key]
assert wallet[:eth_address]
assert wallet[:eth_address] == ETH.Utils.get_address(wallet[:public_key])
assert wallet[:eth_address] != second_wallet[:eth_address]
end

test "create/1 without a specific raw private_key returns a specific wallet" do
private_key = :crypto.strong_rand_bytes(32)
wallet = ETH.Wallet.create(private_key)
second_wallet = ETH.Wallet.create(Base.decode16!(wallet[:private_key]))

assert wallet[:private_key]
assert wallet[:private_key] == second_wallet[:private_key]
assert wallet[:public_key]
assert wallet[:public_key] == second_wallet[:public_key]
assert wallet[:eth_address]
assert wallet[:eth_address] == second_wallet[:eth_address]
end

test "create/1 without a specific base16 encoded private_key returns a specific wallet" do
private_key = :crypto.strong_rand_bytes(32)
wallet = ETH.Wallet.create(private_key)
second_wallet = ETH.Wallet.create(wallet[:private_key])

assert wallet[:private_key]
assert wallet[:private_key] == second_wallet[:private_key]
assert wallet[:public_key]
assert wallet[:public_key] == second_wallet[:public_key]
assert wallet[:eth_address]
assert wallet[:eth_address] == second_wallet[:eth_address]
end
end
Loading

0 comments on commit 0f7fe0e

Please # to comment.