@@ -2,9 +2,9 @@ use solana_client::nonblocking::rpc_client::RpcClient;
2
2
use solana_sdk:: {
3
3
commitment_config:: CommitmentConfig ,
4
4
instruction:: { AccountMeta , CompiledInstruction , Instruction } ,
5
- message:: Message ,
5
+ message:: { Message , VersionedMessage } ,
6
6
pubkey:: Pubkey ,
7
- transaction:: Transaction ,
7
+ transaction:: { Transaction , VersionedTransaction } ,
8
8
} ;
9
9
10
10
use crate :: {
@@ -89,33 +89,46 @@ pub async fn sign_and_send_transaction(
89
89
}
90
90
91
91
pub fn encode_b64_transaction ( transaction : & Transaction ) -> Result < String , KoraError > {
92
- let serialized = bincode:: serialize ( transaction) . map_err ( |e| {
93
- KoraError :: SerializationError ( format ! ( "Base64 serialization failed: {}" , e) )
94
- } ) ?;
92
+ let serialized = bincode:: serialize ( transaction)
93
+ . map_err ( |e| KoraError :: SerializationError ( format ! ( "Base64 serialization failed: {e}" ) ) ) ?;
95
94
Ok ( STANDARD . encode ( serialized) )
96
95
}
97
96
98
97
pub fn encode_b64_message ( message : & Message ) -> Result < String , KoraError > {
99
- let serialized = bincode:: serialize ( message) . map_err ( |e| {
100
- KoraError :: SerializationError ( format ! ( "Base64 serialization failed: {}" , e) )
101
- } ) ?;
98
+ let serialized = bincode:: serialize ( message)
99
+ . map_err ( |e| KoraError :: SerializationError ( format ! ( "Base64 serialization failed: {e}" ) ) ) ?;
102
100
Ok ( STANDARD . encode ( serialized) )
103
101
}
104
102
105
103
pub fn decode_b64_transaction ( encoded : & str ) -> Result < Transaction , KoraError > {
106
104
let decoded = STANDARD . decode ( encoded) . map_err ( |e| {
107
- KoraError :: InvalidTransaction ( format ! ( "Failed to decode base64 transaction: {}" , e ) )
105
+ KoraError :: InvalidTransaction ( format ! ( "Failed to decode base64 transaction: {e}" ) )
108
106
} ) ?;
109
107
108
+ // For now we don't support versioned transactions, will be added in the future (where it checks the addresses in the lookup tables)
109
+ if let Ok ( versioned_tx) = bincode:: deserialize :: < VersionedTransaction > ( & decoded) {
110
+ if let VersionedMessage :: V0 ( _) = versioned_tx. message {
111
+ return Err ( KoraError :: InvalidTransaction (
112
+ "Versioned transaction not supported" . to_string ( ) ,
113
+ ) ) ;
114
+ }
115
+ }
116
+
110
117
bincode:: deserialize ( & decoded) . map_err ( |e| {
111
- KoraError :: InvalidTransaction ( format ! ( "Failed to deserialize transaction: {}" , e ) )
118
+ KoraError :: InvalidTransaction ( format ! ( "Failed to deserialize transaction: {e}" ) )
112
119
} )
113
120
}
114
121
115
122
#[ cfg( test) ]
116
123
mod tests {
117
124
use super :: * ;
118
- use solana_sdk:: { hash:: Hash , message:: Message , signature:: Keypair , signer:: Signer as _} ;
125
+ use solana_sdk:: {
126
+ hash:: Hash ,
127
+ message:: { v0:: Message as V0Message , Message , VersionedMessage } ,
128
+ signature:: Keypair ,
129
+ signer:: Signer as _,
130
+ transaction:: VersionedTransaction ,
131
+ } ;
119
132
120
133
#[ test]
121
134
fn test_encode_decode_b64_transaction ( ) {
@@ -184,4 +197,30 @@ mod tests {
184
197
assert_eq ! ( uncompiled. accounts[ 1 ] . pubkey, account2) ;
185
198
assert_eq ! ( uncompiled. data, vec![ 1 , 2 , 3 ] ) ;
186
199
}
200
+
201
+ #[ test]
202
+ fn test_encode_decode_b64_versioned_transaction_unsupported ( ) {
203
+ let keypair = Keypair :: new ( ) ;
204
+ let instruction = Instruction :: new_with_bytes (
205
+ Pubkey :: new_unique ( ) ,
206
+ & [ 1 , 2 , 3 ] ,
207
+ vec ! [ AccountMeta :: new( keypair. pubkey( ) , true ) ] ,
208
+ ) ;
209
+ let tx = VersionedTransaction :: try_new (
210
+ VersionedMessage :: V0 (
211
+ V0Message :: try_compile ( & keypair. pubkey ( ) , & [ instruction] , & [ ] , Hash :: default ( ) )
212
+ . unwrap ( ) ,
213
+ ) ,
214
+ & [ & keypair] ,
215
+ )
216
+ . unwrap ( ) ;
217
+
218
+ let serialized = bincode:: serialize ( & tx) . unwrap ( ) ;
219
+ let encoded = STANDARD . encode ( serialized) ;
220
+
221
+ assert ! ( matches!(
222
+ decode_b64_transaction( & encoded) ,
223
+ Err ( KoraError :: InvalidTransaction ( e) ) if e == "Versioned transaction not supported"
224
+ ) ) ;
225
+ }
187
226
}
0 commit comments