use std::future::Future;
use df_hyperliquid_core::account::{LocalKey, Signature};
use df_hyperliquid_core::utils::Address;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Purpose {
RegisterSessionKey,
Withdraw,
Stake,
Unstake,
Claim,
DepositToSpot,
Deposit,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignRequest {
pub purpose: Purpose,
pub json: String,
pub digest: [u8; 32],
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum WalletError {
#[error("declined in the wallet")]
Declined,
#[error("the wallet is on chain {got}, and this is signed on {want}")]
WrongChain { got: u64, want: u64 },
#[error("the wallet could not sign: {0}")]
Failed(String),
}
pub trait Wallet {
fn address(&self) -> Address;
fn sign(&self, request: &SignRequest) -> impl Future<Output = Result<Signature, WalletError>>;
}
pub struct LocalWallet(pub LocalKey);
impl std::fmt::Debug for LocalWallet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("LocalWallet").field(&self.0.address()).finish()
}
}
impl Wallet for LocalWallet {
fn address(&self) -> Address {
self.0.address()
}
fn sign(&self, request: &SignRequest) -> impl Future<Output = Result<Signature, WalletError>> {
std::future::ready(self.0.sign_digest(&request.digest).map_err(|e| WalletError::Failed(e.to_string())))
}
}
#[cfg(test)]
#[path = "tests/wallet.rs"]
mod tests;