Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
scrogson committed Jul 24, 2024
1 parent a45c091 commit 705d047
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
1 change: 0 additions & 1 deletion lib/siwe/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ defmodule Siwe.Native do

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, _opts), do: nif_error()
def parse_if_valid(_msg, _sig, _opts), do: nif_error()
def generate_nonce, do: nif_error()
Expand Down
14 changes: 7 additions & 7 deletions native/siwe_native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions native/siwe_native/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use alloy::primitives::{hex::FromHex, Address};
use alloy::primitives::{
hex::{self, FromHex},
Address,
};
use http::uri::Authority;
use iri_string::types::UriString;
use rustler::{types::atom::ok, Atom, Encoder, Env, NifStruct, OwnedEnv};
Expand All @@ -8,7 +11,7 @@ use time::{format_description::well_known::Rfc3339, OffsetDateTime};

mod runtime;

#[derive(NifStruct)]
#[derive(Debug, NifStruct)]
#[module = "Siwe.Message"]
pub struct Parsed {
pub domain: String,
Expand All @@ -25,7 +28,7 @@ pub struct Parsed {
pub resources: Vec<String>,
}

#[derive(NifStruct)]
#[derive(Debug, NifStruct)]
#[module = "Siwe.VerifyOptions"]
pub struct VerifyOptions {
pub domain: Option<String>,
Expand All @@ -47,6 +50,7 @@ impl Into<VerificationOpts> for VerifyOptions {
.map(|timestamp| OffsetDateTime::parse(&timestamp, &Rfc3339).ok())
.flatten(),
rpc_provider: self.rpc_url.map(|rpc_url| {
dbg!(&rpc_url);
let rpc_url = rpc_url.parse().unwrap();
alloy::providers::ProviderBuilder::new().on_http(rpc_url)
}),
Expand Down Expand Up @@ -143,17 +147,28 @@ fn to_str(message: Parsed) -> Result<String, String> {
#[rustler::nif]
fn verify(env: Env, message: Parsed, sig: String, opts: VerifyOptions) -> Atom {
let caller = env.pid();

dbg!(&message);
dbg!(&sig);

let opts: VerificationOpts = opts.into();

dbg!("verify");

runtime::spawn(async move {
let result = match message.to_eip4361_message() {
Ok(m) => match <[u8; 65]>::from_hex(sig.chars().skip(2).collect::<String>()) {
Ok(s) => m.verify(&s, &opts).await.is_ok(),
Ok(m) => match hex::decode(sig) {
Ok(s) => {
dbg!(&s);
m.verify(&s.to_vec(), &opts).await.is_ok()
}
Err(_) => false,
},
Err(_) => false,
};

dbg!(&result);

let _ = OwnedEnv::new().send_and_clear(&caller, move |env| result.encode(env));
});

Expand Down
Binary file modified priv/native/libsiwe_native.so
Binary file not shown.
20 changes: 20 additions & 0 deletions test/siwe_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,24 @@ Expiration Time: 2021-12-18T20:21:39.907Z"

{:error, _} = Siwe.parse_if_valid(msg, sig)
end

test "verifies smart wallet signature on base-sepolia" do

Check failure on line 135 in test/siwe_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.14.3 / OTP 25.2.1

test verifies smart wallet signature on base-sepolia (SiweTest)

Check failure on line 135 in test/siwe_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.6 / OTP 26.1

test verifies smart wallet signature on base-sepolia (SiweTest)
msg = "login.xyz wants you to sign in with your Ethereum account:
0x3e5608baE5e195F7EB12792f0Ba1aEf911F364E9
Sign-In With Ethereum Example Statement
URI: https://login.xyz
Version: 1
Chain ID: 84532
Nonce: ToTaLLyRanDOM
Issued At: 2021-12-16T20:21:39.911Z"

assert {:ok, msg} = Siwe.parse(msg)

sig =
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000017000000000000000000000000000000000000000000000000000000000000000111d8c5e6edcd92aeac9ee215372ebf3264300a429ae6dd79f93d71781d1a397d747b83a603182db717314be83bf9ae0d18d0652294a09d8d472a0cdd6f4a11c80000000000000000000000000000000000000000000000000000000000000025f9351d6932d2b1c9aca4fee013c4ff2672f712f28e9c37330bac4fa19292b8351d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008f7b2274797065223a22776562617574686e2e676574222c226368616c6c656e6765223a2274337659526847564b6258504a476c506e51676d79495950573079442d356b497675536e497a494d485a4d222c226f726967696e223a2268747470733a2f2f6b6579732d626574612e636f696e626173652e636f6d222c2263726f73734f726967696e223a66616c73657d0000000000000000000000000000000000"

assert Siwe.verify(msg, sig, rpc_url: "https://sepolia.base.org") == true
end
end

0 comments on commit 705d047

Please sign in to comment.