use std::str::FromStr;
use crate::{
AuthorizationContext, PrivySignedApiError,
generated::{
Error, ResponseValue,
types::{
SolanaSignAndSendTransactionRpcInput, SolanaSignAndSendTransactionRpcInputCaip2,
SolanaSignAndSendTransactionRpcInputMethod, SolanaSignAndSendTransactionRpcInputParams,
SolanaSignAndSendTransactionRpcInputParamsEncoding, SolanaSignMessageRpcInput,
SolanaSignMessageRpcInputMethod, SolanaSignMessageRpcInputParams,
SolanaSignMessageRpcInputParamsEncoding, SolanaSignTransactionRpcInput,
SolanaSignTransactionRpcInputMethod, SolanaSignTransactionRpcInputParams,
SolanaSignTransactionRpcInputParamsEncoding, WalletRpcBody, WalletRpcResponse,
},
},
};
pub struct SolanaService {
wallets_client: crate::subclients::WalletsClient,
}
impl SolanaService {
pub(crate) fn new(wallets_client: crate::subclients::WalletsClient) -> Self {
Self { wallets_client }
}
pub async fn sign_message(
&self,
wallet_id: &str,
message: &str,
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let rpc_body = WalletRpcBody::SolanaSignMessageRpcInput(SolanaSignMessageRpcInput {
address: None,
chain_type: None,
method: SolanaSignMessageRpcInputMethod::SignMessage,
params: SolanaSignMessageRpcInputParams {
encoding: SolanaSignMessageRpcInputParamsEncoding::Base64,
message: message.to_string(),
},
});
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
pub async fn sign_transaction(
&self,
wallet_id: &str,
transaction: &str,
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let rpc_body =
WalletRpcBody::SolanaSignTransactionRpcInput(SolanaSignTransactionRpcInput {
address: None,
chain_type: None,
method: SolanaSignTransactionRpcInputMethod::SignTransaction,
params: SolanaSignTransactionRpcInputParams {
encoding: SolanaSignTransactionRpcInputParamsEncoding::Base64,
transaction: transaction.to_string(),
},
});
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
pub async fn sign_and_send_transaction(
&self,
wallet_id: &str,
caip2: &str,
transaction: &str,
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let caip2_parsed = SolanaSignAndSendTransactionRpcInputCaip2::from_str(caip2)
.map_err(|_| Error::InvalidRequest("Invalid CAIP-2 format".to_string()))?;
let rpc_body = WalletRpcBody::SolanaSignAndSendTransactionRpcInput(
SolanaSignAndSendTransactionRpcInput {
address: None,
caip2: caip2_parsed,
chain_type: None,
method: SolanaSignAndSendTransactionRpcInputMethod::SignAndSendTransaction,
params: SolanaSignAndSendTransactionRpcInputParams {
encoding: SolanaSignAndSendTransactionRpcInputParamsEncoding::Base64,
transaction: transaction.to_string(),
},
sponsor: Some(false),
},
);
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
}