|
| 1 | +# SIWE Validator for Clef |
| 2 | + |
| 3 | +This directory implements a minimal Sign-In with Ethereum (SIWE) message validator for Clef, |
| 4 | +designed to verify incoming EIP-4361 formatted messages before approving signing requests. |
| 5 | + |
| 6 | +The validator checks critical fields including: |
| 7 | + |
| 8 | +- **Domain**: Ensures the requested domain matches an expected domain (e.g., `localhost:3000`). |
| 9 | +- **Ethereum Address**: Verifies that a valid `0x` prefixed address is provided. |
| 10 | +- **URI**: Confirms the target URI matches the expected resource. |
| 11 | +- **Version**: Verifies that the SIWE version is `1`. |
| 12 | +- **ChainID**: Ensures the chain ID matches the intended network (e.g., `1` for Ethereum Mainnet). |
| 13 | +- **Nonce**: Checks that a unique nonce is included to prevent replay attacks. |
| 14 | +- **Issued At**: Ensures the issued timestamp follows the ISO 8601/RFC3339 format. |
| 15 | + |
| 16 | +Unlike previous implementations relying on external libraries such as `spruceid/siwe-go`, |
| 17 | +this version introduces a **lightweight internal parser** that directly processes SIWE messages, |
| 18 | +eliminating external dependencies and improving maintainability. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## How It Works |
| 23 | + |
| 24 | +Upon receiving a signing request, Clef will invoke the `siwe-validator` binary, |
| 25 | +passing the SIWE message via standard input (stdin). |
| 26 | + |
| 27 | +The validator parses the message line-by-line and verifies mandatory fields according to EIP-4361 specifications. |
| 28 | + |
| 29 | +If validation passes, Clef proceeds with the signing flow. Otherwise, signing is rejected. |
| 30 | + |
| 31 | +### Manually Testing the Validator |
| 32 | + |
| 33 | +You can manually simulate a Clef signing request by piping a SIWE message into `siwe-validator`. |
| 34 | +For example: |
| 35 | + |
| 36 | +```bash |
| 37 | +echo "localhost:3000 wants you to sign in with your Ethereum account: |
| 38 | +0x32e0556aeC41a34C3002a264f4694193EBCf44F7 |
| 39 | +
|
| 40 | +URI: https://localhost:3000 |
| 41 | +Version: 1 |
| 42 | +ChainID: 1 |
| 43 | +Nonce: 32891756 |
| 44 | +Issued At: 2025-04-26T12:00:00Z" | ./siwe-validator |
| 45 | +``` |
| 46 | + |
| 47 | +If the message is valid, `siwe-validator` will exit silently with code `0`. |
| 48 | +If the message is invalid, an error message will be printed to `stderr`. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## Test Data |
| 53 | + |
| 54 | +The `testdata/genmsg_test.go` file provides a minimal static SIWE message generator for manual testing purposes. |
| 55 | + |
| 56 | +It outputs a standardized EIP-4361 formatted message, allowing developers to easily validate the `siwe-validator` behavior. |
| 57 | + |
| 58 | +This file is intended for manual verification only and is not part of the production codebase or automated tests. |
| 59 | + |
| 60 | +To manually generate and test a SIWE message: |
| 61 | + |
| 62 | +```bash |
| 63 | +cd cmd/clef/siwevalidator |
| 64 | +go run testdata/genmsg_test.go | ./siwe-validator |
| 65 | +``` |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## Notes |
| 70 | + |
| 71 | +- The validator currently supports basic field validation only. |
| 72 | +- Future improvements may include supporting optional fields like `Resources`, `Expiration Time`, and `Request ID`. |
| 73 | +- This implementation follows the EIP-4361. |
| 74 | + |
| 75 | +--- |
0 commit comments