From 9d78e6e6472ecb0e5129056ea99d3f9cec432294 Mon Sep 17 00:00:00 2001 From: James Duncombe Date: Wed, 15 Nov 2023 17:11:34 +0000 Subject: [PATCH] Adds Signer behaviour. --- config/config.exs | 3 ++- lib/tt_eth.ex | 4 ++++ lib/tt_eth/behaviours/signer.ex | 9 +++++++++ lib/tt_eth/secp256k1.ex | 6 ++++++ lib/tt_eth/transactions/eip1559_transaction.ex | 5 +++-- 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 lib/tt_eth/behaviours/signer.ex diff --git a/config/config.exs b/config/config.exs index a7c9a72..57a2438 100644 --- a/config/config.exs +++ b/config/config.exs @@ -10,4 +10,5 @@ config :tt_eth, secondary: "0xe2187dc017e880dded10c403f7c0d397afb11736ac027c1202e318b0dd345086", ternary: "0xfa015243f2e6d8694ab037a7987dc73b1630fc8cb1ce82860344684c15d24026" ], - transaction_module: TTEth.Transactions.LegacyTransaction + transaction_module: TTEth.Transactions.LegacyTransaction, + signer_module: TTEth.Secp256k1 diff --git a/lib/tt_eth.ex b/lib/tt_eth.ex index f3eece7..ccb87b8 100644 --- a/lib/tt_eth.ex +++ b/lib/tt_eth.ex @@ -123,6 +123,10 @@ defmodule TTEth do def transaction_module(), do: :tt_eth |> get_env(:transaction_module, TTEth.Transactions.EIP1559Transaction) + @spec signer_module() :: module + def signer_module(), + do: :tt_eth |> get_env(:signer_module, TTEth.Secp256k1) + ## Mocks related stuff. @doc false diff --git a/lib/tt_eth/behaviours/signer.ex b/lib/tt_eth/behaviours/signer.ex new file mode 100644 index 0000000..2bc0dad --- /dev/null +++ b/lib/tt_eth/behaviours/signer.ex @@ -0,0 +1,9 @@ +defmodule TTEth.Behaviours.Signer do + @moduledoc """ + Defines a behaviour to encapsulate a signer. + """ + + @callback sign_transaction(transaction :: binary(), private_key :: binary()) :: + {:ok, {r_s :: binary(), v :: non_neg_integer()}} + | {:error, term()} +end diff --git a/lib/tt_eth/secp256k1.ex b/lib/tt_eth/secp256k1.ex index 28b184c..5309fba 100644 --- a/lib/tt_eth/secp256k1.ex +++ b/lib/tt_eth/secp256k1.ex @@ -3,6 +3,12 @@ defmodule TTEth.Secp256k1 do Wrapper around `ExSecp256k1` functions. """ + @behaviour TTEth.Behaviours.Signer + + @impl TTEth.Behaviours.Signer + def sign_transaction(transaction, private_key), + do: ecdsa_sign_compact(transaction, private_key) + @doc """ Delegates to `ExSecp256k1.recover_compact/3` with guards around the `recovery_id` value. """ diff --git a/lib/tt_eth/transactions/eip1559_transaction.ex b/lib/tt_eth/transactions/eip1559_transaction.ex index 9cf2243..6e3db14 100644 --- a/lib/tt_eth/transactions/eip1559_transaction.ex +++ b/lib/tt_eth/transactions/eip1559_transaction.ex @@ -4,9 +4,10 @@ defmodule TTEth.Transactions.EIP1559Transaction do SEE: https://eips.ethereum.org/EIPS/eip-1559 """ - alias TTEth.{BitHelper, Secp256k1} + alias TTEth.BitHelper alias TTEth.Behaviours.Transaction import BitHelper, only: [encode_unsigned: 1] + import TTEth, only: [signer_module: 0] @behaviour Transaction @@ -89,7 +90,7 @@ defmodule TTEth.Transactions.EIP1559Transaction do Returns a ECDSA signature (v,r,s) for a given hashed value. """ def sign_hash(hash, private_key) do - {:ok, {<>, v}} = Secp256k1.ecdsa_sign_compact(hash, private_key) + {:ok, {<>, v}} = signer_module().sign_compact(hash, private_key) {v, r, s} end