-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ongoing refactoring/implementation, bugfixes on transaction hashing
- Loading branch information
Showing
8 changed files
with
267 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
require IEx | ||
# elliptic curve cryptography library for signing transactions in Ethereum | ||
defmodule ETH do | ||
@moduledoc """ | ||
Documentation for Eth. | ||
""" | ||
|
||
@doc """ | ||
Hello world. | ||
## Examples | ||
iex> Eth.hello | ||
:world | ||
""" | ||
|
||
# def sign_transaction( | ||
# source_eth_address, value, target_eth_address, | ||
# 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 = options[:data] |> Hexate.encode | ||
# | ||
# nonce = case options[:nonce] do | ||
# nil -> ETH.Query.get_transaction_count(source_eth_address) | ||
# _ -> options[:nonce] | ||
# end | ||
# | ||
# # NOTE: calc nonce | ||
# %{ | ||
# to: target_eth_address, value: Hexate.encode(value), gas_price: gas_price, | ||
# gas_limit: gas_limit, data: data, chain_id: 3 | ||
# } | ||
# # get nonce and make a transaction map -> sign_transaction -> send it to client | ||
# end | ||
|
||
|
||
# def sign_transaction(transaction, private_key) do # must have chain_id | ||
# hash = hash_transaction(transaction) | ||
# decoded_private_key = Base.decode16!(private_key, case: :lower) | ||
# [signature: signature, recovery: recovery] = secp256k1_signature(hash, decoded_private_key) | ||
# | ||
# << r :: binary-size(32) >> <> << s :: binary-size(32) >> = signature | ||
# | ||
# transaction | ||
# |> Map.merge(%{r: encode16(r), s: encode16(s), v: encode16(<<recovery + 27>>)}) | ||
# |> adjust_v_for_chain_id | ||
# |> transaction_list | ||
# |> Enum.map(fn(x) -> Base.decode16!(x, case: :lower) end) | ||
# |> ExRLP.encode | ||
# end | ||
# | ||
# def adjust_v_for_chain_id(transaction) do | ||
# if transaction.chain_id > 0 do | ||
# current_v_bytes = Base.decode16!(transaction.v, case: :lower) |> :binary.decode_unsigned | ||
# target_v_bytes = current_v_bytes + (transaction.chain_id * 2 + 8) | ||
# transaction |> Map.merge(%{v: encode16(<< target_v_bytes >>) }) | ||
# else | ||
# transaction | ||
# end | ||
# end | ||
# | ||
# def secp256k1_signature(hash, private_key) do | ||
# {:ok, signature, recovery} = :libsecp256k1.ecdsa_sign_compact(hash, private_key, :default, <<>>) | ||
# [signature: signature, recovery: recovery] | ||
# end | ||
# | ||
# # must have [nonce, gasPrice, gasLimit, to, value, data] # and chainId inside the transaction? | ||
# def hash_transaction(transaction) do | ||
# # NOTE: if transaction is decoded no need to encode | ||
# # EIP155 spec: | ||
# # when computing the hash of a transaction for purposes of signing or recovering, | ||
# # instead of hashing only the first six elements (ie. nonce, gasprice, startgas, to, value, data), | ||
# # hash nine elements, with v replaced by CHAIN_ID, r = 0 and s = 0 | ||
# transaction |> Map.merge(%{v: encode16(<<transaction.chain_id>>), r: <<>>, s: <<>> }) | ||
# |> transaction_list | ||
# |> Enum.map(fn(x) -> Base.decode16!(x, case: :lower) end) | ||
# |> hash | ||
# end | ||
# | ||
# def hash(transaction_list) do | ||
# transaction_list | ||
# |> ExRLP.encode | ||
# |> keccak256 | ||
# end | ||
# | ||
# def transaction_list(transaction \\ %{}) do | ||
# %{ | ||
# nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data | ||
# } = transaction | ||
# | ||
# v = if Map.get(transaction, :v), do: transaction.v, else: Base.encode16(<<28>>, case: :lower) | ||
# r = Map.get(transaction, :r, "") | ||
# s = Map.get(transaction, :s, "") | ||
# [nonce, gas_price, gas_limit, to, value, data, v, r, s] | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 | ||
# | ||
# end | ||
|
||
# test "can get transactions sender public kye 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 | ||
# | ||
# end | ||
end | ||
|
||
# TODO: also implement transaction-runner tests |