SIWS - Sign in With Solana Rust Library
A simple Rust implementation of CAIP-122 (Sign in With X) for Solana, following the Solana Wallet Standard and Phantom Wallet's Sign In With Solana protocol.
Installation
SIWS can be easily installed by including the siws crate as a dependency inside your project's Cargo.toml:
[]
# ...other dependencies
= "0.0.1"
# ...other dependencies
Usage
SIWS exposes two main structs - SiwsMessage for message validation, and SiwsOutput for sign-in verification.
SiwsMessage is analogous to Solana Wallet Standard's SolanaSignInInput, while SiwsOutput is analogous to SolanaSignInOutput.
Using these, you can verify the sign in request, and validate the sign-in message.
You will mainly want to use the SiwsOutput struct, as its primary purpose is to provide you with simple methods to verify its signature.
However, if you wish to validate the SIWS Message (which you should), you can extract it from SiwsOutput's signed_message field using SiwsMessage::try_from.
An End-to-end example
The below example code shows a complete Rust program using actix-web, time, and siws to receive a JSON object containing the SIWS Output, creating a SIWS message from it, verifying the signature and validating the message.
Cargo.toml:
[]
= "siws-server-example"
= "0.1.0"
= "2021"
[]
= "4.5.1"
= { = "../../siws-rs" }
= "0.3.36"
src/main.rs
use ;
use ;
use SiwsOutput;
use OffsetDateTime;
async
async
SIWS Output derives serde's Serialize and Deserialize traits, and also automatically renames all of its fields as camelCase for simpler Solana Wallet support.
Verify sign-in with SIWS Output
Whenever you have a SIWS Output, all you need to do is call its verify method to verify its signature. You can construct a SIWS Output by parsing a JSON string.
See tests/integration_tests.rs for details.
Validate SIWS Message from SIWS Output
From the previous example, if you wanted to also validate the SIWS Message against a certain domain, nonce, or time, you can do the following:
let message = try_from.map_err?;
message.validate?; // Result<(), ValidateError>
SIWS Message
The SiwsMessage struct is used to serialize/deserialize the SIWS Message from/to its ABNF form.
Additional methods are implemented to support parsing it from a &Vec<u8> and &[u8], as Solana Wallet-signed messages usually come as UTF-8 byte arrays.
Parse SIWS message from string
You can parse a SIWS message from any string that adheres to its specified ABNF:
Serialize the SIWS message according to its ABNF
You can get the ABNF-compliant string for your SIWS Message by using String::from:
let siws_message = SiwsMessage ;
let message_string = Stringfrom;
print!;
Contributing
This project aims to provide basic functionality of Sign in With Solana to Rust developers. As such, it's intended to be kept small and manageable.
Contributing to this repository is highly encouraged.
If you find any bugs, please try cloning the repository and fixing them yourself, then opening a PR with your proposed fixes.
The project is also open to new features, however feature requests should be discussed through issues beforehand to align with the minimalist nature of the project.
Security
This library has not undergone security audits.
If you or anyone you know wants to audit siws-rs, please contact the authors directly.