SolanaWalletAdapter
A lightweight Rust Solana Wallet that can be used in Rust based frontends and WebAssembly.
Documentation Links
- Usage - How to add this library and required features for
web-syscrate - Initializing - How
AppReadyandRegisterwallet events are initialized - Wallet Storage - How the wallets registered are stored in memory within the dapp
- Connect and Check for Supported Features - How to connect to a browser wallet and check which features the connected wallet supports
- Disconnect - Disconnected an account from the connected wallet
- Sign In With Solana - Sign In With Solana (SIWS)
- Sign Message - Signing a message with a browser wallet
- Sign Transaction - Signing a transaction with a browser wallet
- Sign and Send Transaction - Sign and Send Transaction with a browser wallet
- Examples - Where to find examples
- License - Licensed under Apache-2.0 or MIT
- Features - What features of the wallet standard are supported by this library
- Templates - Which Rust frontend framework templates have been implemented
- Template Examples - Which Rust frontend framework templates examples have been implemented
- Build Requirement - What tools are required to build a working project
- Build and Run A Template - How to build a template or project to WebAssembly and run in the browser
Usage
If the project does not have it's own build tool ensure the Cargo.toml manifest specified that this is a shared library by adding
[]
= ["cdylib", "rlib"]
Add library to Cargo.toml manifest file.
The features for web-sys crate below are required for all features to work.
[]
= "<latest version>"
= "<latest version>"
= { = "<latest version>", = [
"Window",
"Document",
"Event",
"EventTarget",
"CustomEvent",
"CustomEventInit",
"Element",
"HtmlElement",
"Location",
"Request",
"RequestInit",
"RequestMode",
"Response",
"Headers",
"PointerEvent",
"Clipboard",
"Navigator",
"console",
] }
[]
# Tell `rustc` to optimize for small code size.
= "s"
See Template Usage for more details
Initializing Register and AppReady
This is done automatically when calling WalletAdapter::init(). The Register and AppReady events are registered to the browser window and document in the current page allowing browser extension wallet to register themselves as per the wallet standard specification.
#
Alternatively, for some templates where handling events like onclick requires FnMut closures, InitEvents can be used instead.
The wallet_adapter::InitEvents requires an in-memory storage solution to store the registered wallets for retrieval. The wallet_adapter::WalletStorage can be used instead which wraps a HashMap with a Rc<RefCell<>> to allow usage in closures that move variables out of their environment.
#
In-memory storage for registered wallets.
wallet_adapter::WalletStorage handles storage of registered wallets. The in-memory storage is a HashMap<hash, Wallet>
where the hash is the hash of the wallet name.
use WalletStorage;
let storage = default;
// Get all registered wallets
storage.get_wallets;
// Get a wallet by its name
storage.get_wallet;
// Clone the storage inside a closure, method or function that moves variables out of their environment
// `WalletStorage` internally representation is `Rc<RefCell<HashMap<hash, Wallet>>>`
// this makes it cheap to clone `WalletStorage` where one needs to access `HashMap<hash, Wallet>`
storage.clone_inner;
Connecting to a browser extension wallet and checking for features
# async
Disconnecting from the wallet
# async
Sign In With Solana (SIWS)
# async
Sign In With Solana (SIWS) supports more options for the Sign In With Solana Standard. Check the methods on the [SigninInput] struct. NOTE that an error is thrown by the library in case the message signed, public key don't match or if the signature is not valid for the signing public key.
Sign Message
All messages must be UTF-8 encoded string of bytes
# async
NOTE that an error is thrown by the library in case the message, public key don't match or if the signature is not valid for the signing public key.
Sign Transaction
Here, we simulate signing a SOL transfer instruction
# async
Remember to add the necessary dependencies for this part in the Cargo.toml manifest.
[]
# Add these
= "2.1.2"
= "1.3.3"
NOTE that if the signed transaction is verified by the library and an error is thrown in case of signature mismatch.
Sign And Send Transaction
Here, we simulate signing and sending a SOL transfer instruction
# async
Remember to add the necessary dependencies for this part in the Cargo.toml manifest.
[]
# Add these
= "2.1.2"
= "1.3.3"
= "0.12.5"
= "1.0.133"
= { = "1.0.215", = ["derive"] }
NOTE that if the signed transaction is verified by the library and an error is thrown in case of signature mismatch.
Examples
Examples can be found at examples directory
LICENSE
Apache-2.0 OR MIT
Features
- Register
wallet-standard:register-walletcustom event - App Ready
wallet-standard:app-readycustom event - Wallet Info
- Wallet Account parsing
- Wallet Icon
- Chains
- Clusters
- Version (Semver Versionin)
- Features
- Connect Wallet
standard:connect - Disconnect Wallet
standard:disconnect - SignIn (Sign In With Solana SIWS)
- Sign Message
- Sign Transaction
- Sign and Send Transaction
Templates
- WebAssembly (No Frontend Framework)
- Sycamore
- Yew
- Dioxus
Template Examples
- WebAssembly (No Frontend Framework)
- Sycamore
- Yew
- Dioxus
Requirements
- Install
wasm32-unknown-unknowntoolchain to compile against for the browser - Install
wasm-packto build Rust projects for Rust WebAssembly with no framework, Yew and Sycamore templates and examples. Dioxus template and examples require using the Dioxus cli which you can install from Dioxus Website. Follow the instructions from wasm-pack project https://rustwasm.github.io/wasm-pack/installer/
Template Usage
To add the library to any project Run
To get starter code for various templates, first install cargo generate
Then run cargo generate --name temp-wasm https://github.com/JamiiDao/Solana-Rust-Wallet-Adapter-Templates <template subfolder name>
where <template subfolder name> is the name of the directory containing the template. Examples:
- Dioxus template
- Yew template
- Sycamore Template
All templates can be found at [Solana-Rust-Wallet-Adapter-Templates(https://github.com/JamiiDao/Solana-Rust-Wallet-Adapter-Templates)
Running Dioxus examples and templates using Dioxus cli.
-
Install dioxus cli from https://dioxuslabs.com/.
-
Build, run and serve a dioxus project
Templates like Yew and Sycamore don't come with there own build tool like Dioxus. For such templates install a server like miniserve
that supports sending the WebAssembly files to the browser with the MIME application/wasm .
If you decided to use miniserve install with command
Then compile the Rust code to wasm format using
This outputs all the wasm in the ./resources/pkg
Serve files
Install miniserve crate or any other http serve that supports serving wasm files as application/wasm MIME
Now the files are available at port localhost:5500.
The resources/index.html can be used as a reference on how the wasm files are imported an initialized.