use super::{Error, ResponseValue, types};
use crate::{
AuthorizationContext, PrivyApiError, PrivyExportError, PrivyHpke, PrivySignedApiError,
ethereum::EthereumService,
generate_authorization_signatures,
generated::types::{
HpkeEncryption, PrivateKeyInitInput, Wallet, WalletExportRequestBody,
WalletImportSubmissionRequestAdditionalSignersItem, WalletImportSubmissionRequestOwner,
WalletImportSupportedChains,
},
import::WalletImport,
solana::SolanaService,
subclients::WalletsClient,
};
impl WalletsClient {
pub async fn rpc<'a>(
&'a self,
wallet_id: &'a str,
ctx: &'a AuthorizationContext,
privy_idempotency_key: Option<&'a str>,
body: &'a crate::generated::types::WalletRpcBody,
) -> Result<ResponseValue<crate::generated::types::WalletRpcResponse>, PrivySignedApiError>
{
let sig = generate_authorization_signatures(
ctx,
&self.app_id,
crate::Method::POST,
format!("{}/v1/wallets/{}/rpc", self.base_url, wallet_id),
body,
privy_idempotency_key.map(|k| k.to_owned()),
)
.await?;
Ok(self
._rpc(wallet_id, Some(&sig), privy_idempotency_key, body)
.await?)
}
pub async fn raw_sign<'a>(
&'a self,
wallet_id: &'a str,
ctx: &'a AuthorizationContext,
privy_idempotency_key: Option<&'a str>,
body: &'a crate::generated::types::RawSign,
) -> Result<ResponseValue<crate::generated::types::RawSignResponse>, PrivySignedApiError> {
let sig = generate_authorization_signatures(
ctx,
&self.app_id,
crate::Method::POST,
format!("{}/v1/wallets/{}/raw_sign", self.base_url, wallet_id),
body,
privy_idempotency_key.map(|k| k.to_owned()),
)
.await?;
Ok(self
._raw_sign(wallet_id, Some(&sig), privy_idempotency_key, body)
.await?)
}
pub async fn update<'a>(
&'a self,
wallet_id: &'a str,
ctx: &'a AuthorizationContext,
body: &'a crate::generated::types::UpdateWalletBody,
) -> Result<ResponseValue<crate::generated::types::Wallet>, PrivySignedApiError> {
let sig = generate_authorization_signatures(
ctx,
&self.app_id,
crate::Method::PATCH,
format!("{}/v1/wallets/{}", self.base_url, wallet_id),
body,
None,
)
.await?;
Ok(self._update(wallet_id, Some(&sig), body).await?)
}
pub async fn export<'a>(
&'a self,
wallet_id: &'a str,
ctx: &'a AuthorizationContext,
) -> Result<zeroize::Zeroizing<Vec<u8>>, PrivyExportError> {
let privy_hpke = PrivyHpke::new();
let body = WalletExportRequestBody {
encryption_type: HpkeEncryption::Hpke,
recipient_public_key: privy_hpke.public_key()?,
};
let sig = generate_authorization_signatures(
ctx,
&self.app_id,
crate::Method::POST,
format!("{}/v1/wallets/{}/export", self.base_url, wallet_id),
&body,
None,
)
.await?;
let resp = self._export(wallet_id, Some(&sig), &body).await?;
tracing::debug!("Encapsulated key: {:?}", resp);
Ok(privy_hpke.decrypt_raw(&resp.encapsulated_key, &resp.ciphertext)?)
}
pub async fn import(
&self,
address: String,
private_key_hex: &str,
chain_type: WalletImportSupportedChains,
owner: Option<WalletImportSubmissionRequestOwner>,
policy_ids: Vec<String>,
additional_signers: Vec<WalletImportSubmissionRequestAdditionalSignersItem>,
) -> Result<ResponseValue<Wallet>, PrivyApiError> {
WalletImport::new(
self.clone(),
crate::generated::types::WalletImportInitializationRequest::PrivateKeyInitInput(
PrivateKeyInitInput {
address: address.clone(),
chain_type,
encryption_type: HpkeEncryption::Hpke,
entropy_type:
crate::generated::types::PrivateKeyInitInputEntropyType::PrivateKey,
},
),
)
.await?
.submit(private_key_hex, owner, policy_ids, additional_signers)
.await
}
pub(crate) async fn submit_import<'a>(
&'a self,
body: &'a types::WalletImportSubmissionRequest,
) -> Result<ResponseValue<types::Wallet>, Error<()>> {
self._submit_import(body).await
}
pub fn ethereum(&self) -> EthereumService {
EthereumService::new(self.clone())
}
pub fn solana(&self) -> SolanaService {
SolanaService::new(self.clone())
}
}