Skip to content

Commit

Permalink
Tidies some types and adds documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesduncombe committed Mar 21, 2024
1 parent 2965f16 commit 12be5b0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
6 changes: 5 additions & 1 deletion lib/tt_eth/protocols/wallet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ defprotocol TTEth.Protocols.Wallet do
Protocol for wallet adapters.
"""

@typedoc """
All the types that implement this protocol.
"""
@type t :: any()

@doc """
Expand All @@ -13,5 +16,6 @@ defprotocol TTEth.Protocols.Wallet do
@doc """
Returns a signature.
"""
def sign(t, hash_digest)
@spec sign(t, binary()) :: binary()
def sign(t, digest)
end
14 changes: 8 additions & 6 deletions lib/tt_eth/secp256k1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ defmodule TTEth.Secp256k1 do

@valid_recovery_ids [0, 1]

@type recovery_id :: 0 | 1
@type compact_signature_return :: {:ok, {binary, recovery_id}} | {:error, atom}

@doc """
Delegates to `ExSecp256k1.recover_compact/3` with guards around the `recovery_id` value.
"""
@spec ecdsa_recover_compact(binary(), binary(), non_neg_integer()) ::
{:ok, binary()} | {:error, atom()}
def ecdsa_recover_compact(hash, sig, recovery_id) when recovery_id in @valid_recovery_ids,
do: ExSecp256k1.recover_compact(hash, sig, recovery_id)
def ecdsa_recover_compact(hash, signature, recovery_id) when recovery_id in @valid_recovery_ids,
do: ExSecp256k1.recover_compact(hash, signature, recovery_id)

def ecdsa_recover_compact(_hash, _sig, _recovery_id),
do: {:error, :invalid_recovery_id}

@doc """
Delegates to `ExSecp256k1.sign_compact/2`.
"""
@spec ecdsa_sign_compact(binary(), binary()) ::
{:ok, {binary(), binary()}} | {:error, atom()}
@spec ecdsa_sign_compact(binary(), binary()) :: compact_signature_return
def ecdsa_sign_compact(hash, private_key),
do: ExSecp256k1.sign_compact(hash, private_key)

@doc """
Delegates to `ExSecp256k1.create_public_key/1`.
"""
@spec ec_pubkey_create(binary()) :: {:ok, binary()} | atom()
def ec_pubkey_create(priv),
do: ExSecp256k1.create_public_key(priv)
def ec_pubkey_create(private_key),
do: ExSecp256k1.create_public_key(private_key)
end
8 changes: 5 additions & 3 deletions lib/tt_eth/type/signature.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ defmodule TTEth.Type.Signature do
SEE: https://ethereum.org/en/developers/docs/apis/json-rpc#eth_sign
"""
@deprecated "Use Wallet.personal_sign/2 instead."
@spec sign(message :: binary, private_key :: binary) ::
{:ok, components} | {:error, :cannot_sign}
@spec sign(message :: binary, private_key :: binary) :: Secp256k1.compact_signature_return()
def sign(message, private_key),
do:
message
Expand Down Expand Up @@ -90,13 +89,16 @@ defmodule TTEth.Type.Signature do
@doc """
Takes the compact signature and returns the components with `v` added.
"""
@spec compact_to_components(Secp256k1.compact_signature_return()) ::
{:ok, components} | {:error, :cannot_sign}

def compact_to_components({:ok, {<<r::size(256), s::size(256)>>, recovery_id}}),
do: {:ok, {@base_v + recovery_id, r, s}}

def compact_to_components({:error, _}),
do: {:error, :cannot_sign}

@spec components(binary) :: {:error, :invalid_signature} | {:ok, components}
@spec components(binary) :: {:ok, components} | {:error, :invalid_signature}
def components(signature) do
signature
|> from_human()
Expand Down

0 comments on commit 12be5b0

Please sign in to comment.