Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
LiAohua authored and LiAohua committed Jan 3, 2024
1 parent 1c0f6ff commit 74a7ae1
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
Binary file removed .swp
Binary file not shown.
109 changes: 109 additions & 0 deletions lib/components/transaction.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
defmodule Components.Transaction do
alias Ethereumex.HttpClient
defstruct from: <<>>, to: <<>>, gas_price: 0, gas_limit: 0, value: 0, init: <<>>, data: <<>>

@base_recovery_id_eip_155 35

def send(chain_id, priv_key, tx, nonce, others) do
items = prepare_items(tx, nonce, others)

# Refer to EIP-155, we SHOULD hash nine rlp encoded elements:
# (nonce, gasprice, startgas, to, value, data, chainid, 0, 0)
hashed_tx = hash(items ++ [encode_unsigned(chain_id), <<>>, <<>>])
{v, r, s} = sign(hashed_tx, priv_key, chain_id)
signature = [
encode_unsigned(v),
encode_unsigned(r),
encode_unsigned(s)
]

raw_tx =
(items ++ signature)
|> ExRLP.encode(encoding: :hex)
HttpClient.eth_send_raw_transaction("0x" <> raw_tx, others)
end

def send(chain_id, priv_key, tx, others) do
items = prepare_items(tx, others)

# Refer to EIP-155, we SHOULD hash nine rlp encoded elements:
# (nonce, gasprice, startgas, to, value, data, chainid, 0, 0)
hashed_tx = hash(items ++ [encode_unsigned(chain_id), <<>>, <<>>])
{v, r, s} = sign(hashed_tx, priv_key, chain_id)
signature = [
encode_unsigned(v),
encode_unsigned(r),
encode_unsigned(s)
]

raw_tx =
(items ++ signature)
|> ExRLP.encode(encoding: :hex)
HttpClient.eth_send_raw_transaction("0x" <> raw_tx, others)
end

def get_gas(contract_address, behaviour, payloads, others) do
transaction = %{
"to" => contract_address,
"data" => TypeTranslator.get_data(behaviour, payloads)
}

{:ok, gas_limit} = HttpClient.eth_estimate_gas(transaction, others)
{:ok, gas_price} = HttpClient.eth_gas_price(others)

{
TypeTranslator.hex_to_int(gas_limit),
TypeTranslator.hex_to_int(gas_price)
}
end

defp prepare_items(tx, nonce, _others) do

[
encode_unsigned(nonce),
encode_unsigned(tx.gas_price),
encode_unsigned(tx.gas_limit),
tx.to |> String.replace("0x", "") |> Base.decode16!(case: :mixed),
encode_unsigned(tx.value || 0),
if(tx.to == <<>>, do: <<>>, else: tx.data)
]
end

defp prepare_items(tx, others) do
nonce = get_nonce(tx.from, others)

[
encode_unsigned(nonce),
encode_unsigned(tx.gas_price),
encode_unsigned(tx.gas_limit),
tx.to |> String.replace("0x", "") |> Base.decode16!(case: :mixed),
encode_unsigned(tx.value || 0),
if(tx.to == <<>>, do: <<>>, else: tx.data)
]
end

defp hash(items) do
items
|> ExRLP.encode(encoding: :binary)
|> ExKeccak.hash_256()
end

defp sign(hashed_tx, priv_key, chain_id) do
{:ok, {<<r::size(256), s::size(256)>>, recovery_id}} =
ExSecp256k1.sign_compact(hashed_tx, priv_key)

# Refer to EIP-155
recovery_id = chain_id * 2 + @base_recovery_id_eip_155 + recovery_id

{recovery_id, r, s}
end

def get_nonce(wallet_address, others) do
{:ok, hex} = HttpClient.eth_get_transaction_count(wallet_address, "latest", others)

TypeTranslator.hex_to_int(hex)
end

defp encode_unsigned(0), do: <<>>
defp encode_unsigned(number), do: :binary.encode_unsigned(number)
end
3 changes: 2 additions & 1 deletion lib/components/verifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ defmodule Components.Verifier do

defp verify_signature(hash, signature) do
{r, s, v} = destructure_sig(signature)
:libsecp256k1.ecdsa_recover_compact(hash, r <> s, :uncompressed, v)
# :libsecp256k1.ecdsa_recover_compact(hash, r <> s, :uncompressed, v)
ExSecp256k1.recover_compact(hash, r <> s, v)
end
end
7 changes: 7 additions & 0 deletions lib/utils/type_translator.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
defmodule TypeTranslator do
@doc """
TODO: impl the cases.
iex(34)> ABI.encode("buy(uint256,uint256)", [13721, 10000000000000000])
iex(33)> ABI.decode("buy(uint256,uint256)", "d6febde80000000000000000000000000000000000000000000000000000000000003599000000000000000000000000000000000000000000000000002386f26fc10000" |> Base.decode16!(case: :lower))
[97245039436039886927109785377494219820477888259653562449568703439859083116544,
369917428219973928622626114008996352472975258621679195013222636231821042]
"""

def str_to_module(class, mod_name) do
"Elixir.TaiShangWorldGenerator.#{class}.#{mod_name}"
Expand Down
3 changes: 3 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ defmodule TaiShangMicroFaasSystem.MixProject do
# graphql client
{:neuron, "~> 5.1.0"},

# decimal
{:decimal, "~> 2.0"}

]
end

Expand Down

0 comments on commit 74a7ae1

Please sign in to comment.