Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: add logic to moksha-wallet dependencies #247

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions moksha-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ thiserror = "1.0.56"
async-trait = "0.1.77"
lightning-invoice = "0.29.0"
url = "2.5.0"

[target.'cfg(not(target_os="espidf"))'.dependencies]
dirs = "5.0.1"

[target.'cfg(target_family = "wasm")'.dependencies]
[target.'cfg(any(target_family = "wasm", target_os = "espidf"))'.dependencies]
gloo-net = { version = "0.5.0" }
serde-wasm-bindgen = "0.6.3"
wasm-bindgen = "0.2.90"
rexie = "0.5.0"
tokio = { version = "1.35.1", features = ["rt", "sync"] }

[target.'cfg(not(target_family="wasm"))'.dependencies]
[target.'cfg(all(not(target_family="wasm"), not(target_os="espidf")))'.dependencies]
reqwest = { version = "0.11.23", features = ["serde_json", "json", "rustls-tls"], default-features = false }
tokio = { version = "1.35.1", features = ["rt", "rt-multi-thread", "macros"] }
sqlx = { version = "0.7.3", default-features = false, features = ["sqlite", "runtime-tokio", "tls-rustls", "migrate", "macros"] }
Expand Down
3 changes: 2 additions & 1 deletion moksha-wallet/src/btcprice.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::error::MokshaWalletError;

async fn execute_request(url: &str) -> Result<String, MokshaWalletError> {
#[cfg(not(target_os = "espidf"))]
#[cfg(not(target_arch = "wasm32"))]
{
let request_client = reqwest::Client::new();
let response = request_client.get(url).send().await?;
Ok(response.text().await?)
}

#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", target_os = "espidf"))]
{
let resp = gloo_net::http::Request::get(url).send().await.unwrap();
Ok(resp.text().await?)
Expand Down
113 changes: 113 additions & 0 deletions moksha-wallet/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod crossplatform;
#[cfg(test)]
use mockall::automock;

#[cfg(not(target_os = "espidf"))]
#[cfg_attr(test, automock)]
#[async_trait(?Send)]
pub trait CashuClient {
Expand Down Expand Up @@ -132,3 +133,115 @@ pub trait CashuClient {
txid: String,
) -> Result<GetMeltOnchainResponse, MokshaWalletError>;
}

#[cfg(target_os = "espidf")]
pub trait CashuClient {
fn get_keys(&self, mint_url: &Url) -> Result<KeysResponse, MokshaWalletError>;

fn get_keys_by_id(
&self,
mint_url: &Url,
keyset_id: String,
) -> Result<KeysResponse, MokshaWalletError>;

fn get_keysets(&self, mint_url: &Url) -> Result<V1Keysets, MokshaWalletError>;

fn post_swap(
&self,
mint_url: &Url,
proofs: Proofs,
output: Vec<BlindedMessage>,
) -> Result<PostSwapResponse, MokshaWalletError>;

fn post_melt_bolt11(
&self,
mint_url: &Url,
proofs: Proofs,
quote: String,
outputs: Vec<BlindedMessage>,
) -> Result<PostMeltBolt11Response, MokshaWalletError>;

fn post_melt_quote_bolt11(
&self,
mint_url: &Url,
payment_request: String,
unit: CurrencyUnit,
) -> Result<PostMeltQuoteBolt11Response, MokshaWalletError>;

fn get_melt_quote_bolt11(
&self,
mint_url: &Url,
quote: String,
) -> Result<PostMeltQuoteBolt11Response, MokshaWalletError>;

fn post_mint_bolt11(
&self,
mint_url: &Url,
quote: String,
blinded_messages: Vec<BlindedMessage>,
) -> Result<PostMintBolt11Response, MokshaWalletError>;

fn post_mint_quote_bolt11(
&self,
mint_url: &Url,
amount: u64,
unit: CurrencyUnit,
) -> Result<PostMintQuoteBolt11Response, MokshaWalletError>;

fn get_mint_quote_bolt11(
&self,
mint_url: &Url,
quote: String,
) -> Result<PostMintQuoteBolt11Response, MokshaWalletError>;

fn get_info(&self, mint_url: &Url) -> Result<MintInfoResponse, MokshaWalletError>;

fn is_v1_supported(&self, mint_url: &Url) -> Result<bool, MokshaWalletError>;

fn post_mint_onchain(
&self,
mint_url: &Url,
quote: String,
blinded_messages: Vec<BlindedMessage>,
) -> Result<PostMintOnchainResponse, MokshaWalletError>;

fn post_mint_quote_onchain(
&self,
mint_url: &Url,
amount: u64,
unit: CurrencyUnit,
) -> Result<PostMintQuoteOnchainResponse, MokshaWalletError>;

fn get_mint_quote_onchain(
&self,
mint_url: &Url,
quote: String,
) -> Result<PostMintQuoteOnchainResponse, MokshaWalletError>;

fn post_melt_onchain(
&self,
mint_url: &Url,
proofs: Proofs,
quote: String,
) -> Result<PostMeltOnchainResponse, MokshaWalletError>;

fn post_melt_quote_onchain(
&self,
mint_url: &Url,
address: String,
amount: u64,
unit: CurrencyUnit,
) -> Result<Vec<PostMeltQuoteOnchainResponse>, MokshaWalletError>;

fn get_melt_quote_onchain(
&self,
mint_url: &Url,
quote: String,
) -> Result<PostMeltQuoteOnchainResponse, MokshaWalletError>;

fn get_melt_onchain(
&self,
mint_url: &Url,
txid: String,
) -> Result<GetMeltOnchainResponse, MokshaWalletError>;
}
3 changes: 3 additions & 0 deletions moksha-wallet/src/config_path.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(not(target_os = "espidf"))]
use dirs::home_dir;
use std::{fs::create_dir, path::PathBuf};

Expand All @@ -15,6 +16,7 @@ pub const ENV_DB_PATH: &str = "WALLET_DB_PATH";
/// let db_path = moksha_wallet::config_path::db_path();
/// println!("Database path: {}", db_path);
/// ```
#[cfg(not(target_os = "espidf"))]
pub fn db_path() -> String {
std::env::var(ENV_DB_PATH).map_or_else(
|_| {
Expand Down Expand Up @@ -43,6 +45,7 @@ pub fn db_path() -> String {
)
}

#[cfg(not(target_os = "espidf"))]
pub fn config_dir() -> PathBuf {
let home = home_dir()
.expect("home dir not found")
Expand Down
6 changes: 5 additions & 1 deletion moksha-wallet/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum MokshaWalletError {
#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", target_os = "espidf"))]
#[error("GlooNetError - {0}")]
GlooNet(#[from] gloo_net::Error),

#[error("SerdeJsonError - {0}")]
Json(#[from] serde_json::Error),

#[cfg(not(target_os = "espidf"))]
#[cfg(not(target_arch = "wasm32"))]
#[error("ReqwestError - {0}")]
Reqwest(#[from] reqwest::Error),

#[cfg(not(target_os = "espidf"))]
#[cfg(not(target_arch = "wasm32"))]
#[error("InvalidHeaderValueError - {0}")]
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
Expand All @@ -31,10 +33,12 @@ pub enum MokshaWalletError {
#[error("MokshaCoreError - {0}")]
MokshaCore(#[from] moksha_core::error::MokshaCoreError),

#[cfg(not(target_os = "espidf"))]
#[cfg(not(target_arch = "wasm32"))]
#[error("DB Error {0}")]
Db(#[from] sqlx::Error),

#[cfg(not(target_os = "espidf"))]
#[cfg(not(target_arch = "wasm32"))]
#[error("Migrate Error {0}")]
Migrate(#[from] sqlx::migrate::MigrateError),
Expand Down
4 changes: 3 additions & 1 deletion moksha-wallet/src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#[cfg(not(target_os = "espidf"))]
#[cfg(not(target_arch = "wasm32"))]
pub mod reqwest;

#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", target_os = "espidf"))]
pub mod wasm;

#[derive(Debug, Clone)]
pub struct CrossPlatformHttpClient {
#[cfg(not(target_os = "espidf"))]
#[cfg(not(target_arch = "wasm32"))]
client: ::reqwest::Client,
}
Expand Down
36 changes: 32 additions & 4 deletions moksha-wallet/src/localstore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use moksha_core::proof::Proofs;

use crate::error::MokshaWalletError;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", target_os = "espidf")))]
pub mod sqlite;

#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", target_os = "espidf"))]
pub mod rexie;

#[derive(Debug, Clone)]
Expand All @@ -15,7 +15,7 @@ pub struct WalletKeyset {
pub mint_url: String,
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(any(target_arch = "wasm32", target_os = "espidf")))]
#[async_trait(?Send)]
pub trait LocalStore {
type DB: sqlx::Database;
Expand All @@ -39,7 +39,7 @@ pub trait LocalStore {
async fn add_keyset(&self, keyset: &WalletKeyset) -> Result<(), MokshaWalletError>;
}

#[cfg(target_arch = "wasm32")]
#[cfg(any(target_arch = "wasm32", target_os = "espidf"))]
pub struct RexieTransaction {}

#[cfg(target_arch = "wasm32")]
Expand All @@ -48,6 +48,12 @@ impl RexieTransaction {
Ok(())
}
}
#[cfg(target_os = "espidf")]
impl RexieTransaction {
pub fn commit(&self) -> Result<(), MokshaWalletError> {
Ok(())
}
}

#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
Expand All @@ -71,3 +77,25 @@ pub trait LocalStore {
async fn get_keysets(&self) -> Result<Vec<WalletKeyset>, MokshaWalletError>;
async fn add_keyset(&self, keyset: &WalletKeyset) -> Result<(), MokshaWalletError>;
}

#[cfg(target_os = "espidf")]
pub trait LocalStore {
fn begin_tx(&self) -> Result<RexieTransaction, MokshaWalletError> {
Ok(RexieTransaction {})
}

fn delete_proofs(
&self,
tx: &mut RexieTransaction,
proofs: &Proofs,
) -> Result<(), MokshaWalletError>;
fn add_proofs(
&self,
tx: &mut RexieTransaction,
proofs: &Proofs,
) -> Result<(), MokshaWalletError>;
fn get_proofs(&self, tx: &mut RexieTransaction) -> Result<Proofs, MokshaWalletError>;

fn get_keysets(&self) -> Result<Vec<WalletKeyset>, MokshaWalletError>;
fn add_keyset(&self, keyset: &WalletKeyset) -> Result<(), MokshaWalletError>;
}
Loading