Before you start
KeySigner source checkout and Rust 1.98 or later
- Use disposable software keys for this offline exercise. It checks one legacy System Program transfer with one signer and uses no funded account.
Data and access
Set up a scratch project
Set KEYSIGNER_SOURCE to the absolute path of your KeySigner source checkout first. Create the example project outside that checkout.
cargo new --bin verify-sol-transfer cd verify-sol-transfer cargo add --path "$KEYSIGNER_SOURCE/crates/solana-native" cargo add ed25519-dalek@=2.2.0 --features rand_core cargo add rand@=0.8.8 bincode@=1.3.3 cargo add solana-transaction@=3.1.0 --features bincode,verifyCheck. Cargo adds keysigner-solana-native from your source checkout and resolves the example’s public dependencies.
Save the transfer check
Save the example as src/main.rs. It generates disposable keys, signs the prepared message and uses Solana’s transaction library to decode the transfer and verify the signature.
Download verify-sol-transfer.rs
use ed25519_dalek::{Signer, SigningKey}; use keysigner_solana_native::{ ApprovedTransfer, SolanaError, assemble, check_message, prepare_transfer, }; use rand::rngs::OsRng; use solana_transaction::Transaction; fn main() -> Result<(), Box<dyn std::error::Error>> { let sender = SigningKey::generate(&mut OsRng); let recipient = SigningKey::generate(&mut OsRng); let other_key = SigningKey::generate(&mut OsRng); let approved = ApprovedTransfer { sender: sender.verifying_key().to_bytes(), recipient: recipient.verifying_key().to_bytes(), lamports: 10_000_000, recent_blockhash: [9; 32], // A fixed blockhash for this offline exercise. }; let message = prepare_transfer(&approved)?; check_message(&message, &approved)?; let signature = sender.sign(&message).to_bytes(); let wire = assemble(&message, &approved, &signature)?; let transaction: Transaction = bincode::deserialize(&wire)?; transaction.verify()?; let [instruction] = transaction.message.instructions.as_slice() else { return Err("expected one instruction".into()); }; let accounts = &transaction.message.account_keys; assert_eq!(accounts[0].to_bytes(), approved.sender); assert_eq!( accounts[usize::from(instruction.program_id_index)].to_bytes(), [0; 32] ); assert_eq!( accounts[usize::from(instruction.accounts[0])].to_bytes(), approved.sender ); assert_eq!( accounts[usize::from(instruction.accounts[1])].to_bytes(), approved.recipient ); assert_eq!(instruction.data.len(), 12); assert_eq!(u32::from_le_bytes(instruction.data[..4].try_into()?), 2); let amount = u64::from_le_bytes(instruction.data[4..].try_into()?); assert_eq!(amount, 10_000_000); println!("Verified one signed transfer of {amount} lamports."); let changed_amount = prepare_transfer(&ApprovedTransfer { lamports: amount + 1, ..approved })?; let changed_recipient = prepare_transfer(&ApprovedTransfer { recipient: other_key.verifying_key().to_bytes(), ..approved })?; for changed in [changed_amount, changed_recipient] { assert_eq!( check_message(&changed, &approved), Err(SolanaError::NotApprovedTransfer) ); } let wrong_signature = other_key.sign(&message).to_bytes(); assert_eq!( assemble(&message, &approved, &wrong_signature), Err(SolanaError::SignatureNotFromSender), ); println!("Rejected a changed amount, a changed recipient and the wrong signer."); Ok(()) }Check. The decoded transaction contains one System Program transfer from the sending key to the chosen recipient.
Run the acceptance and rejection checks
The example verifies a 0.01 SOL transfer. Then it submits a changed amount, a changed recipient and a signature from a different key to the same local checks.
cargo runCheck. The output confirms the signed transfer amount and reports that all three changes were rejected.
Check your result
The example verifies one signed transfer of 10,000,000 lamports and rejects the three changes. The fixed blockhash and disposable keys are for offline testing. Use the simulation guide to check projected balances.
What we tested
Tested on . We ran the downloadable example with Rust 1.98.1 against KeySigner source revision 1abf4d835. Solana’s transaction library decoded and verified the signed transfer. KeySigner rejected the changed amount, changed recipient and wrong signature. This exercise used software test keys on a local computer.
If you get stuck
Cargo can’t load the KeySigner library
Check that KEYSIGNER_SOURCE is set to an accessible source checkout that contains crates/solana-native. Run rustc --version to confirm that you use Rust 1.98 or later.