WalletAdapter
struct
Contains types and implements methods to dispatch events, listen for events and perform actions specified by the wallet-standard
.
Structure
window
Represents an optional Browser Window Object object.
Handles both Dispatch (wallet-standard:app-ready
) and listen (wallet-standard:register-wallet
) events.
If the Browser window is not found the WalletError::MissingAccessToBrowserWindow
error is returned.
document
Represent the Browser Document Object.
If the document object is not found the WalletError::MissingAccessToBrowserDocument
is returned.
storage field
WalletStorage is where the registered wallets are stored. It is an in-memory store of the registered wallets whose key is a hash that ensures no two wallets with the same name can be registered. All wallet names are case-insensitive. The internal structure is:
Only the register event adds to the storage.
- Methods on storage
connection_info field
This field represents the ConnectionInfo struct which contains the connected wallet and connected account.
It's internal structure:
wallet
field holds a connected wallet. WhenWalletAdapter.connect()
method is called and a connection with a browser wallet is established successfully, this field isOption::Some(Wallet)
else it isOption::None
account
field holds a connected account. WhenWalletAdapter.connect()
method is called and a connection with a browser wallet is established successfully, this field isOption::Some(WalletAccount)
else it isOption::None
previous_accounts
field contains a sequence of connected accounts. Some browser extension wallets emit events that can emit a connected event without disconnecting previous accounts. This account can be used to check if it is part of the sequence in theprevious_accounts
field therefore theWalletAdapter
can emit the eventsReconnected
andAccountChanged
.
wallet_events field
An asynchronous listener of type:
It is an asynchronous type that can be used to listen for events emitted by the WalletAdapter and can be accessed using the WalletAdapter.events() method.
wallet_events_sender
An async sender of type:
It is an asynchronous type that is used to send wallet events to an asynchronous listener.
Usage
Initializing a WalletAdapter struct
Fetching the browser extension wallets that registered themselves
Listen for WalletEvents
Connect a wallet
Disconnect a wallet
Get the connection information
Check if a wallet is connected
Perform operations as defined by the wallet-standard features
With the connection information, we no longer need to mutate anything within the WalletAdapter
allowing for easy use of the operations event inside types that move other types outside of their scope. Remember, ConnectionInfo
is wrapped in a Arc<async_lock::RwLock<T>>
meaning that is is cheap to clone and safe to use in background tasks.
Authenticate/Authorize a user with Sign In With Solana (SIWS)
This method requires a SigninInput and a 32 byte Ed25519 public key and it returns a SignInOutput if the data signed by the browser wallet is the same as the data requested, otherwise an error is returned.
You can read more about all the options available for the SigninInput
from the Sign In With Solana (SIWS) standard.
NOTE: Some wallets require the address
field of the SigninInput
or an error MessageResponseMismatch
which is as a result of the sent message not corresponding with the signed message
Sign a Message
Sign a message encoded as bytes. This takes in some bytes and returns SignedMessageOutput containing the signed message. If the signed message doesn't match the requested message an error is returned.
Sign a Transaction
This takes in an serialized transaction as bytes and returns a Vector of bytes of the signed transaction. If the signed transaction does not match then an error is returned. Let's simulate transfer of lamports transaction.
Add bincode
to the dependencies in Cargo.toml
file
Sign and send a Transaction
This takes in an serialized transaction as bytes, a cluster and SendOptions and returns an Ed25519 Signature of the signed transaction. If the signed transaction does not match then an error is returned.
The SendOptions include the max retries
, preflight_commitment
and skip_preflight
fields.
Let's simulate transfer of lamports transaction.
Add bincode
to the dependencies in Cargo.toml
file
Verification of signin, sign message and sign transaction requests
All sign requests are verified using the public key of the connected account. If the signature fails then an error informing the user of signature mismatch is returned.
Checking for features supported by the connected wallet
Checking for supported chains e.g. solana:mainnet