|
| 1 | +//! Solana transaction deserialization. |
| 2 | +//! |
| 3 | +//! Wraps `solana_transaction::Transaction` for WASM compatibility. |
| 4 | +//! |
| 5 | +//! # Wire Format |
| 6 | +//! |
| 7 | +//! Solana transactions use a compact binary format: |
| 8 | +//! - Signatures (variable length array) |
| 9 | +//! - Message (contains instructions, accounts, blockhash) |
| 10 | +//! |
| 11 | +//! This module deserializes base64-encoded transactions as used by |
| 12 | +//! `@solana/web3.js` `Transaction.from()`. |
| 13 | +
|
| 14 | +use crate::error::WasmSolanaError; |
| 15 | + |
| 16 | +/// Re-export the underlying Solana Transaction type. |
| 17 | +pub use solana_transaction::Transaction; |
| 18 | + |
| 19 | +/// Extension trait for Transaction to add WASM-friendly methods. |
| 20 | +pub trait TransactionExt { |
| 21 | + /// Deserialize a transaction from base64-encoded wire format. |
| 22 | + fn from_base64(base64_str: &str) -> Result<Transaction, WasmSolanaError>; |
| 23 | + |
| 24 | + /// Deserialize a transaction from raw bytes (wire format). |
| 25 | + fn from_bytes(bytes: &[u8]) -> Result<Transaction, WasmSolanaError>; |
| 26 | + |
| 27 | + /// Get the fee payer address as base58 string. |
| 28 | + fn fee_payer_string(&self) -> Option<String>; |
| 29 | + |
| 30 | + /// Get the recent blockhash as base58 string. |
| 31 | + fn blockhash_string(&self) -> String; |
| 32 | + |
| 33 | + /// Get the number of instructions. |
| 34 | + fn num_instructions(&self) -> usize; |
| 35 | + |
| 36 | + /// Get the number of signatures. |
| 37 | + fn num_signatures(&self) -> usize; |
| 38 | + |
| 39 | + /// Get the signable message bytes (what gets signed). |
| 40 | + fn signable_payload(&self) -> Vec<u8>; |
| 41 | + |
| 42 | + /// Serialize transaction to bytes (wire format). |
| 43 | + fn to_bytes(&self) -> Result<Vec<u8>, WasmSolanaError>; |
| 44 | + |
| 45 | + /// Serialize transaction to base64. |
| 46 | + fn to_base64(&self) -> Result<String, WasmSolanaError>; |
| 47 | +} |
| 48 | + |
| 49 | +impl TransactionExt for Transaction { |
| 50 | + fn from_base64(base64_str: &str) -> Result<Transaction, WasmSolanaError> { |
| 51 | + // Decode base64 |
| 52 | + use base64::prelude::*; |
| 53 | + let bytes = BASE64_STANDARD |
| 54 | + .decode(base64_str) |
| 55 | + .map_err(|e| WasmSolanaError::new(&format!("Invalid base64: {}", e)))?; |
| 56 | + |
| 57 | + Self::from_bytes(&bytes) |
| 58 | + } |
| 59 | + |
| 60 | + fn from_bytes(bytes: &[u8]) -> Result<Transaction, WasmSolanaError> { |
| 61 | + bincode::deserialize(bytes) |
| 62 | + .map_err(|e| WasmSolanaError::new(&format!("Failed to deserialize transaction: {}", e))) |
| 63 | + } |
| 64 | + |
| 65 | + fn fee_payer_string(&self) -> Option<String> { |
| 66 | + self.message.account_keys.first().map(|p| p.to_string()) |
| 67 | + } |
| 68 | + |
| 69 | + fn blockhash_string(&self) -> String { |
| 70 | + self.message.recent_blockhash.to_string() |
| 71 | + } |
| 72 | + |
| 73 | + fn num_instructions(&self) -> usize { |
| 74 | + self.message.instructions.len() |
| 75 | + } |
| 76 | + |
| 77 | + fn num_signatures(&self) -> usize { |
| 78 | + self.signatures.len() |
| 79 | + } |
| 80 | + |
| 81 | + fn signable_payload(&self) -> Vec<u8> { |
| 82 | + self.message.serialize() |
| 83 | + } |
| 84 | + |
| 85 | + fn to_bytes(&self) -> Result<Vec<u8>, WasmSolanaError> { |
| 86 | + bincode::serialize(self) |
| 87 | + .map_err(|e| WasmSolanaError::new(&format!("Failed to serialize transaction: {}", e))) |
| 88 | + } |
| 89 | + |
| 90 | + fn to_base64(&self) -> Result<String, WasmSolanaError> { |
| 91 | + use base64::prelude::*; |
| 92 | + let bytes = self.to_bytes()?; |
| 93 | + Ok(BASE64_STANDARD.encode(&bytes)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod tests { |
| 99 | + use super::*; |
| 100 | + |
| 101 | + // Test transaction from @solana/web3.js - a simple SOL transfer |
| 102 | + // This is a real transaction serialized with Transaction.serialize() |
| 103 | + const TEST_TX_BASE64: &str = "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDFVMqpim7tqEi2XL8R6KKkP0DYJvY3eiRXLlL1P9EjYgXKQC+k0FKnqyC4AZGJR7OhJXfpPP3NHOhS8t/6G7bLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/1c7Oaj3RbyLIjU0/ZPpsmVfVUWAzc8g36fK5g6A0JoBAgIAAQwCAAAAoIYBAAAAAAA="; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_deserialize_transaction() { |
| 107 | + let tx = Transaction::from_base64(TEST_TX_BASE64).unwrap(); |
| 108 | + |
| 109 | + // Check we got valid data |
| 110 | + assert!(tx.num_signatures() > 0); |
| 111 | + assert!(tx.num_instructions() > 0); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn test_fee_payer() { |
| 116 | + let tx = Transaction::from_base64(TEST_TX_BASE64).unwrap(); |
| 117 | + let fee_payer = tx.fee_payer_string(); |
| 118 | + assert!(fee_payer.is_some()); |
| 119 | + // Fee payer should be a valid base58 Solana address |
| 120 | + let payer = fee_payer.unwrap(); |
| 121 | + assert!(payer.len() >= 32 && payer.len() <= 44); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn test_blockhash() { |
| 126 | + let tx = Transaction::from_base64(TEST_TX_BASE64).unwrap(); |
| 127 | + let blockhash = tx.blockhash_string(); |
| 128 | + // Blockhash should be a valid base58 string |
| 129 | + assert!(blockhash.len() >= 32 && blockhash.len() <= 44); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_roundtrip() { |
| 134 | + let tx = Transaction::from_base64(TEST_TX_BASE64).unwrap(); |
| 135 | + let serialized = tx.to_base64().unwrap(); |
| 136 | + |
| 137 | + // Deserialize again |
| 138 | + let tx2 = Transaction::from_base64(&serialized).unwrap(); |
| 139 | + assert_eq!(tx.num_signatures(), tx2.num_signatures()); |
| 140 | + assert_eq!(tx.num_instructions(), tx2.num_instructions()); |
| 141 | + assert_eq!(tx.blockhash_string(), tx2.blockhash_string()); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn test_signable_payload() { |
| 146 | + let tx = Transaction::from_base64(TEST_TX_BASE64).unwrap(); |
| 147 | + let payload = tx.signable_payload(); |
| 148 | + // Message should have some content |
| 149 | + assert!(!payload.is_empty()); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test_invalid_base64() { |
| 154 | + let result = Transaction::from_base64("not valid base64!!!"); |
| 155 | + assert!(result.is_err()); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn test_invalid_transaction() { |
| 160 | + let result = Transaction::from_bytes(&[0, 1, 2, 3]); |
| 161 | + assert!(result.is_err()); |
| 162 | + } |
| 163 | +} |
0 commit comments