Skip to content

Commit

Permalink
finalizing the public api
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Sep 7, 2017
1 parent cac79a4 commit d1e4d73
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 59 deletions.
105 changes: 46 additions & 59 deletions lib/eth/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ defmodule ETH.Transaction do

alias ETH.Query

defdelegate parse(data), to: ETH.Transaction.Parser
defdelegate parse(data), to: ETH.Transaction.Parser # NOTE: improve this one
defdelegate to_list(data), to: ETH.Transaction.Parser
defdelegate hash_transaction(transaction), to: ETH.Transaction.Signer
defdelegate hash_transaction(transaction, include_signature), to: ETH.Transaction.Signer
defdelegate hash_transaction_list(transaction_list), to: ETH.Transaction.Signer
defdelegate hash_transaction_list(transaction_list, include_signature), to: ETH.Transaction.Signer
# TODO: allow ETH.Transaction.Wallet
# TODO: allow ETH.Transaction.Wallet for signing
defdelegate sign_transaction(transaction, private_key), to: ETH.Transaction.Signer
defdelegate sign_transaction_list(transaction_list, private_key), to: ETH.Transaction.Signer
defdelegate decode(rlp_encoded_transaction), to: ETH.Transaction.Signer
defdelegate encode(signed_transaction_list), to: ETH.Transaction.Signer

def hash(transaction, include_signature \\ true)
def hash(transaction, include_signature) when is_list(transaction) do
Expand All @@ -22,40 +24,49 @@ defmodule ETH.Transaction do
ETH.Transaction.Signer.hash_transaction(transaction, include_signature)
end

# def send_transaction(params = [from: _from, to: _to, value: _value], private_key) do
# set(params)
# |> sign_transaction_list(private_key)
# |> send
# end
# def send_transaction(params = %{from: _from, to: _to, value: _value}, private_key) do
# Map.to_list(params)
# |> set
# |> sign_transaction_list(private_key)
# |> send
# end
# TODO: what if its a wallet
def set(params = %{from: from, to: to, value: value}) do
gas_price = Keyword.get(params, :gas_price, ETH.Query.gas_price())
data = Keyword.get(params, :data, "")
nonce = Keyword.get(params, :nonce, ETH.Query.get_transaction_count(from))
chain_id = Keyword.get(params, :chain_id, 3)
gas_limit = Keyword.get(params, :gas_limit, ETH.Query.estimate_gas(%{
to: to, value: value, data: data, nonce: nonce, chain_id: chain_id
}))

%{nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data}
|> parse
end
def set(params = [from: from, to: to, value: value]) do
gas_price = Keyword.get(params, :gas_price, ETH.Query.gas_price())
data = Keyword.get(params, :data, "")
nonce = Keyword.get(params, :nonce, ETH.Query.get_transaction_count(from))
chain_id = Keyword.get(params, :chain_id, 3)
gas_limit = Keyword.get(params, :gas_limit, ETH.Query.estimate_gas(%{
to: to, value: value, data: data, nonce: nonce, chain_id: chain_id
}))

%{nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data}
|> parse
end

# def set(params = [from: from, to: to, value: value]) do
# gas_price = Keyword.get(params, :gas_price, ETH.Query.gas_price())
# data = Keyword.get(params, :data, "")
# nonce = Keyword.get(params, :nonce, ETH.Query.get_transaction_count(from))
# chain_id = Keyword.get(params, :chain_id, 3)
# gas_limit = Keyword.get(params, :gas_limit, ETH.Query.estimate_gas(%{
# to: to, value: value, data: data, nonce: nonce, chain_id: chain_id
# }))
#
# [
# to: to, value: value, data: data, gas_price: gas_price, gas_limit: gas_limit, nonce: nonce
# ]
# |> Enum.map(fn(x) ->
# {key, value} = x
# {key, to_buffer(value)}
# end)
# |> Enum.into(%{})
# end
# TODO: what if its a wallet
def send_transaction(params = %{from: _from, to: _to, value: _value}, private_key) do
params
|> set
|> to_list
|> sign_transaction_list(private_key)
|> send
end
def send_transaction(params = [from: _from, to: _to, value: _value], private_key) do
params
|> set
|> to_list
|> sign_transaction_list(private_key)
|> send
end

# def sign_transaction(transaction=%{}) do
# transaction_list =
# end
def send(signature), do: Ethereumex.HttpClient.eth_send_raw_transaction([signature])

def get_senders_public_key("0x" <> rlp_encoded_transaction_list) do # NOTE: not tested
rlp_encoded_transaction_list
Expand All @@ -68,18 +79,14 @@ defmodule ETH.Transaction do

def get_sender_address("0x" <> rlp_encoded_transaction_list) do # NOTE: not tested
rlp_encoded_transaction_list
|> ExRLP.decode
|> get_senders_public_key
|> get_address
end
def get_sender_address(transaction_list = [
nonce, gas_price, gas_limit, to, value, data, v, r, s
]), do: get_senders_public_key(transaction_list) |> get_address

def send(signature), do: Ethereumex.HttpClient.eth_send_raw_transaction([signature])

# NOTE: maybe add encode / decode


defp to_senders_public_key(transaction_list = [
nonce, gas_price, gas_limit, to, value, data, v, r, s
]) do
Expand All @@ -95,23 +102,3 @@ defmodule ETH.Transaction do
public_key
end
end



# def get_sender_address(signature) do
# transaction_list = signature
# |> ExRLP.decode
# |> Enum.map(fn(value) -> "0x" <> Base.encode16(value) end)
#
# v = transaction_list |> Enum.at(6) |> String.slice(2..-1) |> Hexate.to_integer
# r = transaction_list |> Enum.at(7) |> String.slice(2..-1)
# s = transaction_list |> Enum.at(8) |> String.slice(2..-1)
#
# message_hash = hash(transaction_list, false)
# signature = r <> s
# recovery_id = v - 27
#
# {:ok, public_key} = :libsecp256k1.ecdsa_recover_compact(message_hash, signature, :uncompressed, recovery_id)
#
# get_address(public_key)
# end
7 changes: 7 additions & 0 deletions lib/eth/transaction/signer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule ETH.Transaction.Signer do
|> List.insert_at(-1, chain_id)
|> hash_transaction_list(include_signature)
end

# def hash_transaction(transaction=%{}, include_signature) do # TODO: check if this is necessary
# chain_id = get_chain_id(Map.get(transaction, :v, <<28>>), Map.get(transaction, :chain_id))
#
Expand Down Expand Up @@ -47,6 +48,12 @@ defmodule ETH.Transaction.Signer do
|> keccak256
end

def decode(<<rlp_encoded_transaction_list>>), do: ExRLP.decode(rlp_encoded_transaction_list)

def encode(transaction_list = [_nonce, _gas_price, _gas_limit, _to, _value, _data, _v, _r, _s]) do
ExRLP.encode(transaction_list)
end

def sign_transaction(transaction, private_key) when is_map(transaction) do
transaction
|> ETH.Transaction.to_list
Expand Down

0 comments on commit d1e4d73

Please # to comment.