|
| 1 | +defmodule Ethers.SiweVerifyTest.RaisingRpcModule do |
| 2 | + @moduledoc false |
| 3 | + # Used to prove code paths that must not hit the network. |
| 4 | + |
| 5 | + def eth_call(_params, _block, _opts) do |
| 6 | + raise "eth_call must not be called in this code path" |
| 7 | + end |
| 8 | +end |
| 9 | + |
| 10 | +defmodule Ethers.SiweVerifyTest.ErrorRpcModule do |
| 11 | + @moduledoc false |
| 12 | + # Simulates an RPC transport failure. |
| 13 | + |
| 14 | + def eth_call(_params, _block, _opts), do: {:error, :nxdomain} |
| 15 | +end |
| 16 | + |
| 17 | +defmodule Ethers.Contract.Test.SiweERC1271WalletContract do |
| 18 | + @moduledoc false |
| 19 | + use Ethers.Contract, abi_file: "tmp/erc1271_wallet_abi.json" |
| 20 | +end |
| 21 | + |
| 22 | +defmodule Ethers.SiweVerifyTest do |
| 23 | + use ExUnit.Case |
| 24 | + |
| 25 | + import Ethers.TestHelpers |
| 26 | + |
| 27 | + alias Ethers.Contract.Test.SiweERC1271WalletContract |
| 28 | + alias Ethers.Siwe |
| 29 | + alias Ethers.Siwe.Message |
| 30 | + alias Ethers.SiweVerifyTest.ErrorRpcModule |
| 31 | + alias Ethers.SiweVerifyTest.RaisingRpcModule |
| 32 | + |
| 33 | + # 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1 (same key used across the suite) |
| 34 | + @owner_private_key "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d" |
| 35 | + @owner "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1" |
| 36 | + # First anvil dev account (funded, unlocked) — used to send deployment transactions |
| 37 | + @from "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" |
| 38 | + @other_private_key "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" |
| 39 | + |
| 40 | + defp build_message(overrides \\ []) do |
| 41 | + Siwe.new!( |
| 42 | + Keyword.merge( |
| 43 | + [ |
| 44 | + domain: "example.com", |
| 45 | + address: @owner, |
| 46 | + statement: "Sign in to Example", |
| 47 | + uri: "https://example.com/login", |
| 48 | + chain_id: 1, |
| 49 | + nonce: "32891756", |
| 50 | + issued_at: "2021-09-30T16:25:24.000Z" |
| 51 | + ], |
| 52 | + overrides |
| 53 | + ) |
| 54 | + ) |
| 55 | + end |
| 56 | + |
| 57 | + defp sign(raw_message, private_key) do |
| 58 | + Ethers.personal_sign!(raw_message, |
| 59 | + signer: Ethers.Signer.Local, |
| 60 | + signer_opts: [private_key: private_key] |
| 61 | + ) |
| 62 | + end |
| 63 | + |
| 64 | + describe "verify/3 with EOA signatures" do |
| 65 | + test "verifies a raw message string without any RPC call" do |
| 66 | + raw = Siwe.to_message(build_message()) |
| 67 | + signature = sign(raw, @owner_private_key) |
| 68 | + |
| 69 | + assert {:ok, %Message{address: @owner, domain: "example.com"}} = |
| 70 | + Siwe.verify(raw, signature, |
| 71 | + domain: "example.com", |
| 72 | + nonce: "32891756", |
| 73 | + rpc_client: RaisingRpcModule |
| 74 | + ) |
| 75 | + end |
| 76 | + |
| 77 | + test "verifies an already-parsed message struct" do |
| 78 | + message = build_message() |
| 79 | + signature = sign(Siwe.to_message(message), @owner_private_key) |
| 80 | + |
| 81 | + assert {:ok, ^message} = Siwe.verify(message, signature, rpc_client: RaisingRpcModule) |
| 82 | + end |
| 83 | + |
| 84 | + test "rejects a signature by a different key" do |
| 85 | + raw = Siwe.to_message(build_message()) |
| 86 | + signature = sign(raw, @other_private_key) |
| 87 | + |
| 88 | + assert {:error, :invalid_signature} = Siwe.verify(raw, signature) |
| 89 | + end |
| 90 | + |
| 91 | + test "rejects a signature over a different message" do |
| 92 | + raw = Siwe.to_message(build_message()) |
| 93 | + signature = sign("something else entirely", @owner_private_key) |
| 94 | + |
| 95 | + assert {:error, :invalid_signature} = Siwe.verify(raw, signature) |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + describe "verify/3 validation errors" do |
| 100 | + test "propagates field validation errors before checking the signature" do |
| 101 | + message = build_message(expiration_time: "2021-09-30T17:00:00Z") |
| 102 | + raw = Siwe.to_message(message) |
| 103 | + signature = sign(raw, @owner_private_key) |
| 104 | + |
| 105 | + # rpc_client would raise if the signature check ran — validation fails first |
| 106 | + assert {:error, :expired} = |
| 107 | + Siwe.verify(raw, signature, |
| 108 | + time: ~U[2022-01-01 00:00:00Z], |
| 109 | + rpc_client: RaisingRpcModule |
| 110 | + ) |
| 111 | + |
| 112 | + assert {:error, :domain_mismatch} = |
| 113 | + Siwe.verify(raw, signature, |
| 114 | + domain: "evil.com", |
| 115 | + time: ~U[2021-09-30 16:30:00Z], |
| 116 | + rpc_client: RaisingRpcModule |
| 117 | + ) |
| 118 | + |
| 119 | + assert {:error, :nonce_mismatch} = |
| 120 | + Siwe.verify(raw, signature, |
| 121 | + nonce: "deadbeef", |
| 122 | + time: ~U[2021-09-30 16:30:00Z], |
| 123 | + rpc_client: RaisingRpcModule |
| 124 | + ) |
| 125 | + end |
| 126 | + |
| 127 | + test "propagates parse errors" do |
| 128 | + assert {:error, :invalid_message_format} = Siwe.verify("not a siwe message", "0x1234") |
| 129 | + end |
| 130 | + |
| 131 | + test "propagates RPC transport errors from the signature check" do |
| 132 | + raw = Siwe.to_message(build_message()) |
| 133 | + signature = sign(raw, @other_private_key) |
| 134 | + |
| 135 | + assert {:error, :nxdomain} = Siwe.verify(raw, signature, rpc_client: ErrorRpcModule) |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + describe "verify/3 with a smart-contract wallet (ERC-1271)" do |
| 140 | + test "verifies a wallet-owner signature against the wallet address" do |
| 141 | + encoded_constructor = SiweERC1271WalletContract.constructor(@owner) |
| 142 | + |
| 143 | + wallet = |
| 144 | + deploy(SiweERC1271WalletContract, encoded_constructor: encoded_constructor, from: @from) |
| 145 | + |
| 146 | + raw = Siwe.to_message(build_message(address: wallet)) |
| 147 | + owner_signature = sign(raw, @owner_private_key) |
| 148 | + other_signature = sign(raw, @other_private_key) |
| 149 | + |
| 150 | + assert {:ok, %Message{address: address}} = Siwe.verify(raw, owner_signature) |
| 151 | + assert String.downcase(address) == String.downcase(wallet) |
| 152 | + |
| 153 | + assert {:error, :invalid_signature} = Siwe.verify(raw, other_signature) |
| 154 | + end |
| 155 | + end |
| 156 | +end |
0 commit comments