Skip to content

Commit

Permalink
Adds Signer behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesduncombe committed Nov 15, 2023
1 parent 527d605 commit 74b3740
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/tt_eth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/tt_eth/behaviours/signer.ex
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions lib/tt_eth/secp256k1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
6 changes: 4 additions & 2 deletions lib/tt_eth/transactions/eip1559_transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -89,7 +90,8 @@ 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, {<<r::size(256), s::size(256)>>, v}} = Secp256k1.ecdsa_sign_compact(hash, private_key)
{:ok, {<<r::size(256), s::size(256)>>, v}} =
signer_module().sign_transaction(hash, private_key)

{v, r, s}
end
Expand Down

0 comments on commit 74b3740

Please sign in to comment.