Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ERC6492 #2

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ siwe-*.tar

# Temporary files, for example, from tests.
/tmp/

/priv/native/*.so
59 changes: 38 additions & 21 deletions lib/siwe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Siwe do
Siwe provides validation and parsing for Sign-In with Ethereum messages and signatures.
"""

alias Siwe.{Message, Native}
alias Siwe.{AsyncRuntimeOptions, Message, Native, VerifyOptions}

@doc """
Parses a Sign In With Ethereum message string into the Message struct, or reports an error
Expand All @@ -18,27 +18,22 @@ defmodule Siwe do
defdelegate to_str(msg), to: Native

@doc """
Given a Message struct and a signature, returns true if the Message.address
signing the Message would produce the signature.
"""
@spec verify_sig(Message.t(), String.t()) :: boolean()
defdelegate verify_sig(msg, sig), to: Native
Given a Message, signature, and verify options, returns true if:
the current time or timestamp, if provided, is between the messages'
not_before and expiration_time the Message.address signing the Message
would produce the signature.

@doc """
Given a Message, signature, and optionally, domain, nonce and timestamp, returns true if:
the current time or timestamp, if provided, is between the messages' not_before and expiration_time
the Message.address signing the Message would produce the signature.
the domain, if provided, matches Message.domain
The domain, if provided, matches Message.domain
the nonce, if provided, matches Message.nonce
"""
@spec verify(
Message.t(),
String.t(),
String.t() | nil,
String.t() | nil,
String.t() | nil
) :: boolean()
defdelegate verify(msg, sig, domain_binding, match_nonce, timestamp), to: Native
@spec verify(Message.t(), String.t(), Keyword.t() | []) :: boolean()
def verify(msg, sig, opts \\ []) do
:ok = Native.verify(msg, sig, struct(VerifyOptions, opts))

receive do
answer -> answer
end
end

@doc """
Tests that a message and signature pair correspond and that the current
Expand All @@ -61,12 +56,34 @@ defmodule Siwe do
...> "0x8d1327a1abbdf172875e5be41706c50fc3bede8af363b67aefbb543d6d082fb76a22057d7cb6d668ceba883f7d70ab7f1dc015b76b51d226af9d610fa20360ad1c")
{:ok, %Siwe.Message{ address: "0xfA151B5453CE69ABf60f0dbdE71F6C9C5868800E", chain_id: 1, domain: "login.xyz", expiration_time: nil, issued_at: "2021-12-17T00:38:39.834Z", nonce: "ToTaLLyRanDOM", not_before: nil, request_id: nil, resources: [], statement: "Sign-In With Ethereum Example Statement", uri: "https://login.xyz", version: "1" }}
"""
@spec parse_if_valid(String.t(), String.t()) :: {:ok, Message.t()} | {:error, String.t()}
defdelegate parse_if_valid(msg, sig), to: Native
@spec parse_if_valid(String.t(), String.t(), Keyword.t() | []) ::
{:ok, Message.t()} | {:error, String.t()}
def parse_if_valid(msg, sig, opts \\ []) do
:ok = Native.parse_if_valid(msg, sig, struct(VerifyOptions, opts))

receive do
answer -> answer
end
end

@doc """
Generates an alphanumeric nonce for use in SIWE messages.
"""
@spec generate_nonce() :: String.t()
defdelegate generate_nonce, to: Native

def configure_async_runtime! do
case Application.get_env(:siwe, :async_runtime_options, %AsyncRuntimeOptions{}) do
%AsyncRuntimeOptions{} = options ->
options

other ->
raise """
Unexpected async runtime options.

Expected: %Siwe.AsyncRuntimeOptions{}
Found: #{inspect(other)}
"""
end
end
end
7 changes: 7 additions & 0 deletions lib/siwe/async_runtime_options.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Siwe.AsyncRuntimeOptions do
@moduledoc false

defstruct worker_threads: nil,
enable_time: true,
enable_io: true
end
6 changes: 3 additions & 3 deletions lib/siwe/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ defmodule Siwe.Native do
"x86_64-unknown-linux-gnu",
"x86_64-unknown-linux-musl"
],
load_data_fun: {Siwe, :configure_async_runtime!},
version: version

def parse(_msg), do: nif_error()
def to_str(_msg), do: nif_error()
def verify_sig(_msg, _sig), do: nif_error()
def verify(_msg, _sig, _domain_binding, _match_nonce, _timestamp), do: nif_error()
def parse_if_valid(_msg, _sig), do: nif_error()
def verify(_msg, _sig, _opts), do: nif_error()
def parse_if_valid(_msg, _sig, _opts), do: nif_error()
def generate_nonce, do: nif_error()

defp nif_error, do: :erlang.nif_error(:nif_not_loaded)
Expand Down
10 changes: 10 additions & 0 deletions lib/siwe/verify_options.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Siwe.VerifyOptions do
@moduledoc false

@type t :: %__MODULE__{}

defstruct domain: nil,
nonce: nil,
timestamp: nil,
rpc_url: nil
end
File renamed without changes.
Loading
Loading