Skip to main content

hyperqit/
wallet.rs

1use crate::{
2    errors::Result,
3    signing::{SignedMessage, Signer as WalletSigner},
4};
5use alloy::{
6    primitives::{Address, FixedBytes},
7    signers::{Signer, local::PrivateKeySigner},
8    sol_types::Eip712Domain,
9};
10use anyhow::Ok;
11
12pub trait HyperLiquidSigningHash {
13    fn hyperliquid_signing_hash(&self, domain: &Eip712Domain) -> FixedBytes<32>;
14}
15
16#[derive(Clone)]
17pub struct LocalWallet {
18    wallet_key: PrivateKeySigner,
19}
20
21impl LocalWallet {
22    pub fn signer(pk: String) -> Self {
23        Self {
24            wallet_key: pk.parse().unwrap(),
25        }
26    }
27    pub fn address(&self) -> Address {
28        self.wallet_key.address()
29    }
30
31    pub async fn sign_hash(&self, hash: FixedBytes<32>) -> Result<alloy::signers::Signature> {
32        Ok(self.wallet_key.sign_hash(&hash).await?)
33    }
34}
35
36#[async_trait::async_trait]
37impl WalletSigner for LocalWallet {
38    async fn sign_order(&self, to_sign: FixedBytes<32>) -> Result<SignedMessage> {
39        let signature = self.sign_hash(to_sign).await?;
40        Ok(SignedMessage {
41            r: signature.r(),
42            s: signature.s(),
43            v: signature.v() as u64 + 27,
44        })
45    }
46}