From 3b27eabf730bae992570bc98a11511d051964c9f Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 12 Apr 2024 13:57:58 +0100 Subject: [PATCH] refactor: include wallet and mint in cdk --- crates/cdk/Cargo.toml | 2 +- crates/cdk/src/client/gloo_client.rs | 13 +++--- crates/cdk/src/client/minreq_client.rs | 19 ++++----- crates/cdk/src/client/mod.rs | 19 +++++---- crates/cdk/src/lib.rs | 6 +++ crates/cdk/src/mint/localstore/memory.rs | 10 ++--- crates/cdk/src/mint/localstore/mod.rs | 15 +++---- crates/cdk/src/mint/localstore/redb_store.rs | 12 +++--- crates/cdk/src/mint/mod.rs | 30 ++++++------- crates/cdk/src/nuts/mod.rs | 3 +- crates/cdk/src/wallet/localstore/memory.rs | 6 +-- crates/cdk/src/wallet/localstore/mod.rs | 7 ++-- .../cdk/src/wallet/localstore/redb_store.rs | 6 +-- crates/cdk/src/wallet/mod.rs | 42 +++++++++---------- 14 files changed, 96 insertions(+), 94 deletions(-) diff --git a/crates/cdk/Cargo.toml b/crates/cdk/Cargo.toml index 983928bd..42e803b2 100644 --- a/crates/cdk/Cargo.toml +++ b/crates/cdk/Cargo.toml @@ -11,7 +11,7 @@ license.workspace = true [features] default = ["mint", "wallet", "all-nuts", "redb"] -mint = [] +mint = ["dep:bip39"] wallet = ["dep:minreq"] gloo = ["dep:gloo"] all-nuts = ["nut13"] diff --git a/crates/cdk/src/client/gloo_client.rs b/crates/cdk/src/client/gloo_client.rs index 7a785a14..3d226ad8 100644 --- a/crates/cdk/src/client/gloo_client.rs +++ b/crates/cdk/src/client/gloo_client.rs @@ -1,19 +1,18 @@ //! gloo wasm http Client use async_trait::async_trait; -use cashu::nuts::nut09::{RestoreRequest, RestoreResponse}; -use cashu::nuts::{ - BlindedMessage, CheckSpendableRequest, CheckSpendableResponse, MeltBolt11Request, - MeltBolt11Response, MintBolt11Request, MintBolt11Response, MintInfo, PreMintSecrets, Proof, - PublicKey, SwapRequest, SwapResponse, *, -}; -use cashu::{Amount, Bolt11Invoice}; use gloo::net::http::Request; use serde_json::Value; use url::Url; use super::join_url; use crate::client::{Client, Error}; +use crate::nuts::{ + BlindedMessage, CheckSpendableRequest, CheckSpendableResponse, MeltBolt11Request, + MeltBolt11Response, MintBolt11Request, MintBolt11Response, MintInfo, PreMintSecrets, Proof, + PublicKey, RestoreRequest, RestoreResponse, SwapRequest, SwapResponse, *, +}; +use crate::{Amount, Bolt11Invoice}; #[derive(Debug, Clone)] pub struct HttpClient {} diff --git a/crates/cdk/src/client/minreq_client.rs b/crates/cdk/src/client/minreq_client.rs index 78c40809..2d21b928 100644 --- a/crates/cdk/src/client/minreq_client.rs +++ b/crates/cdk/src/client/minreq_client.rs @@ -1,22 +1,21 @@ //! Minreq http Client use async_trait::async_trait; -use cashu::error::ErrorResponse; -use cashu::nuts::nut09::{RestoreRequest, RestoreResponse}; -use cashu::nuts::{ - BlindedMessage, CheckStateRequest, CheckStateResponse, CurrencyUnit, Id, KeySet, KeysResponse, - KeysetResponse, MeltBolt11Request, MeltBolt11Response, MeltQuoteBolt11Request, - MeltQuoteBolt11Response, MintBolt11Request, MintBolt11Response, MintInfo, - MintQuoteBolt11Request, MintQuoteBolt11Response, PreMintSecrets, Proof, PublicKey, SwapRequest, - SwapResponse, -}; -use cashu::{Amount, Bolt11Invoice}; use serde_json::Value; use tracing::warn; use url::Url; use super::join_url; use crate::client::{Client, Error}; +use crate::error::ErrorResponse; +use crate::nuts::{ + BlindedMessage, CheckStateRequest, CheckStateResponse, CurrencyUnit, Id, KeySet, KeysResponse, + KeysetResponse, MeltBolt11Request, MeltBolt11Response, MeltQuoteBolt11Request, + MeltQuoteBolt11Response, MintBolt11Request, MintBolt11Response, MintInfo, + MintQuoteBolt11Request, MintQuoteBolt11Response, PreMintSecrets, Proof, PublicKey, + RestoreRequest, RestoreResponse, SwapRequest, SwapResponse, +}; +use crate::{Amount, Bolt11Invoice}; #[derive(Debug, Clone)] pub struct HttpClient {} diff --git a/crates/cdk/src/client/mod.rs b/crates/cdk/src/client/mod.rs index 7445e4e9..8ba896c1 100644 --- a/crates/cdk/src/client/mod.rs +++ b/crates/cdk/src/client/mod.rs @@ -1,23 +1,24 @@ //! Client to connet to mint use async_trait::async_trait; -use cashu::error::ErrorResponse; -use cashu::nuts::nut09::{RestoreRequest, RestoreResponse}; -use cashu::nuts::{ +use thiserror::Error; +use url::Url; + +use crate::error::ErrorResponse; +use crate::nuts::nut09::{RestoreRequest, RestoreResponse}; +use crate::nuts::{ BlindedMessage, CheckStateResponse, CurrencyUnit, Id, KeySet, KeysetResponse, MeltBolt11Response, MeltQuoteBolt11Response, MintBolt11Response, MintInfo, MintQuoteBolt11Response, PreMintSecrets, Proof, PublicKey, SwapRequest, SwapResponse, }; -use cashu::Amount; -use thiserror::Error; -use url::Url; +use crate::Amount; #[cfg(feature = "gloo")] pub mod gloo_client; #[cfg(not(target_arch = "wasm32"))] pub mod minreq_client; -pub use cashu::Bolt11Invoice; +pub use crate::Bolt11Invoice; #[derive(Debug, Error)] pub enum Error { @@ -33,7 +34,7 @@ pub enum Error { SerdeJson(#[from] serde_json::Error), /// Cashu Url Error #[error("`{0}`")] - CashuUrl(#[from] cashu::url::Error), + CashuUrl(#[from] crate::url::Error), /// Min req error #[cfg(not(target_arch = "wasm32"))] #[error("`{0}`")] @@ -42,7 +43,7 @@ pub enum Error { #[error("`{0}`")] Gloo(String), #[error("Unknown Error response")] - UnknownErrorResponse(cashu::error::ErrorResponse), + UnknownErrorResponse(crate::error::ErrorResponse), /// Custom Error #[error("`{0}`")] Custom(String), diff --git a/crates/cdk/src/lib.rs b/crates/cdk/src/lib.rs index 6e55d7ff..bbbcd235 100644 --- a/crates/cdk/src/lib.rs +++ b/crates/cdk/src/lib.rs @@ -5,14 +5,20 @@ pub use bitcoin::secp256k1; pub use lightning_invoice::{self, Bolt11Invoice}; pub mod amount; +#[cfg(feature = "wallet")] +pub mod client; pub mod dhke; pub mod error; +#[cfg(feature = "mint")] +pub mod mint; pub mod nuts; pub mod secret; pub mod serde_utils; pub mod types; pub mod url; pub mod util; +#[cfg(feature = "wallet")] +pub mod wallet; pub use self::amount::Amount; pub use self::util::SECP256K1; diff --git a/crates/cdk/src/mint/localstore/memory.rs b/crates/cdk/src/mint/localstore/memory.rs index b9ec9258..1c43e047 100644 --- a/crates/cdk/src/mint/localstore/memory.rs +++ b/crates/cdk/src/mint/localstore/memory.rs @@ -2,14 +2,14 @@ use std::collections::HashMap; use std::sync::Arc; use async_trait::async_trait; -use cashu::dhke::hash_to_curve; -use cashu::nuts::nut02::mint::KeySet; -use cashu::nuts::{BlindSignature, CurrencyUnit, Id, MintInfo, Proof, Proofs, PublicKey}; -use cashu::secret::Secret; -use cashu::types::{MeltQuote, MintQuote}; use tokio::sync::Mutex; use super::{Error, LocalStore}; +use crate::dhke::hash_to_curve; +use crate::nuts::nut02::mint::KeySet; +use crate::nuts::{BlindSignature, CurrencyUnit, Id, MintInfo, Proof, Proofs, PublicKey}; +use crate::secret::Secret; +use crate::types::{MeltQuote, MintQuote}; #[derive(Debug, Clone)] pub struct MemoryLocalStore { diff --git a/crates/cdk/src/mint/localstore/mod.rs b/crates/cdk/src/mint/localstore/mod.rs index 94ff2281..cbc20dce 100644 --- a/crates/cdk/src/mint/localstore/mod.rs +++ b/crates/cdk/src/mint/localstore/mod.rs @@ -6,15 +6,16 @@ use std::collections::HashMap; use std::num::ParseIntError; use async_trait::async_trait; -use cashu::nuts::nut02::mint::KeySet; -use cashu::nuts::{BlindSignature, CurrencyUnit, Id, MintInfo, Proof, PublicKey}; -use cashu::secret::Secret; -use cashu::types::{MeltQuote, MintQuote}; pub use memory::MemoryLocalStore; #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))] pub use redb_store::RedbLocalStore; use thiserror::Error; +use crate::nuts::nut02::mint::KeySet; +use crate::nuts::{BlindSignature, CurrencyUnit, Id, MintInfo, Proof, PublicKey}; +use crate::secret::Secret; +use crate::types::{MeltQuote, MintQuote}; + #[derive(Debug, Error)] pub enum Error { #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))] @@ -43,11 +44,11 @@ pub enum Error { #[error("Unknown Mint Info")] UnknownMintInfo, #[error("`{0}`")] - Cashu(#[from] cashu::error::Error), + Cashu(#[from] crate::error::Error), #[error("`{0}`")] - CashuNut02(#[from] cashu::nuts::nut02::Error), + CashuNut02(#[from] crate::nuts::nut02::Error), #[error("`{0}`")] - Secret(#[from] cashu::secret::Error), + Secret(#[from] crate::secret::Error), } #[async_trait] diff --git a/crates/cdk/src/mint/localstore/redb_store.rs b/crates/cdk/src/mint/localstore/redb_store.rs index 5e901115..a09faf73 100644 --- a/crates/cdk/src/mint/localstore/redb_store.rs +++ b/crates/cdk/src/mint/localstore/redb_store.rs @@ -3,17 +3,17 @@ use std::str::FromStr; use std::sync::Arc; use async_trait::async_trait; -use cashu::dhke::hash_to_curve; -use cashu::nuts::{ - BlindSignature, CurrencyUnit, Id, MintInfo, MintKeySet as KeySet, Proof, PublicKey, -}; -use cashu::secret::Secret; -use cashu::types::{MeltQuote, MintQuote}; use redb::{Database, ReadableTable, TableDefinition}; use tokio::sync::Mutex; use tracing::debug; use super::{Error, LocalStore}; +use crate::dhke::hash_to_curve; +use crate::nuts::{ + BlindSignature, CurrencyUnit, Id, MintInfo, MintKeySet as KeySet, Proof, PublicKey, +}; +use crate::secret::Secret; +use crate::types::{MeltQuote, MintQuote}; const ACTIVE_KEYSETS_TABLE: TableDefinition<&str, &str> = TableDefinition::new("active_keysets"); const KEYSETS_TABLE: TableDefinition<&str, &str> = TableDefinition::new("keysets"); diff --git a/crates/cdk/src/mint/mod.rs b/crates/cdk/src/mint/mod.rs index b96a11a6..1083730e 100644 --- a/crates/cdk/src/mint/mod.rs +++ b/crates/cdk/src/mint/mod.rs @@ -1,21 +1,21 @@ use std::collections::HashSet; use std::sync::Arc; -use cashu::dhke::{hash_to_curve, sign_message, verify_message}; -use cashu::error::ErrorResponse; -use cashu::nuts::nut07::{ProofState, State}; -use cashu::nuts::{ - BlindSignature, BlindedMessage, CheckStateRequest, CheckStateResponse, MeltBolt11Request, - MeltBolt11Response, Proof, RestoreRequest, RestoreResponse, SwapRequest, SwapResponse, *, -}; -use cashu::types::{MeltQuote, MintQuote}; -use cashu::Amount; +use bip39::Mnemonic; use http::StatusCode; use serde::{Deserialize, Serialize}; use thiserror::Error; use tracing::{debug, error, info}; -use crate::Mnemonic; +use crate::dhke::{hash_to_curve, sign_message, verify_message}; +use crate::error::ErrorResponse; +use crate::nuts::nut07::{ProofState, State}; +use crate::nuts::{ + BlindSignature, BlindedMessage, CheckStateRequest, CheckStateResponse, MeltBolt11Request, + MeltBolt11Response, Proof, RestoreRequest, RestoreResponse, SwapRequest, SwapResponse, *, +}; +use crate::types::{MeltQuote, MintQuote}; +use crate::Amount; mod localstore; #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))] @@ -45,15 +45,15 @@ pub enum Error { #[error("`{0}`")] Custom(String), #[error("`{0}`")] - CashuMint(#[from] cashu::error::mint::Error), + CashuMint(#[from] crate::error::mint::Error), #[error("`{0}`")] - Cashu(#[from] cashu::error::Error), + Cashu(#[from] crate::error::Error), #[error("`{0}`")] Localstore(#[from] localstore::Error), #[error("`{0}`")] - Secret(#[from] cashu::secret::Error), + Secret(#[from] crate::secret::Error), #[error("`{0}`")] - Nut12(#[from] cashu::nuts::nut12::Error), + Nut12(#[from] crate::nuts::nut12::Error), #[error("Unknown quote")] UnknownQuote, #[error("Unknown secret kind")] @@ -460,7 +460,7 @@ impl Mint { async fn verify_proof(&self, proof: &Proof) -> Result<(), Error> { // Check if secret is a nut10 secret with conditions if let Ok(secret) = - <&cashu::secret::Secret as TryInto>::try_into(&proof.secret) + <&crate::secret::Secret as TryInto>::try_into(&proof.secret) { // Verify if p2pk if secret.kind.eq(&Kind::P2PK) { diff --git a/crates/cdk/src/nuts/mod.rs b/crates/cdk/src/nuts/mod.rs index 8e510787..7e59e7c9 100644 --- a/crates/cdk/src/nuts/mod.rs +++ b/crates/cdk/src/nuts/mod.rs @@ -30,8 +30,7 @@ pub use nut05::{ MeltBolt11Request, MeltBolt11Response, MeltQuoteBolt11Request, MeltQuoteBolt11Response, }; pub use nut06::{MintInfo, MintVersion, Nuts}; -#[cfg(feature = "wallet")] -pub use nut07::{CheckStateRequest, CheckStateResponse}; +pub use nut07::{CheckStateRequest, CheckStateResponse, ProofState, State}; pub use nut09::{RestoreRequest, RestoreResponse}; pub use nut10::{Kind, Secret as Nut10Secret, SecretData}; pub use nut11::{P2PKConditions, SigFlag, Signatures, SigningKey, VerifyingKey}; diff --git a/crates/cdk/src/wallet/localstore/memory.rs b/crates/cdk/src/wallet/localstore/memory.rs index 496f4267..93e48ea6 100644 --- a/crates/cdk/src/wallet/localstore/memory.rs +++ b/crates/cdk/src/wallet/localstore/memory.rs @@ -2,12 +2,12 @@ use std::collections::{HashMap, HashSet}; use std::sync::Arc; use async_trait::async_trait; -use cashu::nuts::{Id, KeySetInfo, Keys, MintInfo, Proof, Proofs}; -use cashu::types::{MeltQuote, MintQuote}; -use cashu::url::UncheckedUrl; use tokio::sync::Mutex; use super::{Error, LocalStore}; +use crate::nuts::{Id, KeySetInfo, Keys, MintInfo, Proof, Proofs}; +use crate::types::{MeltQuote, MintQuote}; +use crate::url::UncheckedUrl; #[derive(Default, Debug, Clone)] pub struct MemoryLocalStore { diff --git a/crates/cdk/src/wallet/localstore/mod.rs b/crates/cdk/src/wallet/localstore/mod.rs index 898ed04f..f073177a 100644 --- a/crates/cdk/src/wallet/localstore/mod.rs +++ b/crates/cdk/src/wallet/localstore/mod.rs @@ -7,14 +7,15 @@ use std::collections::HashMap; use std::num::ParseIntError; use async_trait::async_trait; -use cashu::nuts::{Id, KeySetInfo, Keys, MintInfo, Proofs}; -use cashu::types::{MeltQuote, MintQuote}; -use cashu::url::UncheckedUrl; pub use memory::MemoryLocalStore; #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))] pub use redb_store::RedbLocalStore; use thiserror::Error; +use crate::nuts::{Id, KeySetInfo, Keys, MintInfo, Proofs}; +use crate::types::{MeltQuote, MintQuote}; +use crate::url::UncheckedUrl; + #[derive(Debug, Error)] pub enum Error { #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))] diff --git a/crates/cdk/src/wallet/localstore/redb_store.rs b/crates/cdk/src/wallet/localstore/redb_store.rs index 377bdc5d..d1ea2780 100644 --- a/crates/cdk/src/wallet/localstore/redb_store.rs +++ b/crates/cdk/src/wallet/localstore/redb_store.rs @@ -3,13 +3,13 @@ use std::str::FromStr; use std::sync::Arc; use async_trait::async_trait; -use cashu::nuts::{Id, KeySetInfo, Keys, MintInfo, Proofs}; -use cashu::types::{MeltQuote, MintQuote}; -use cashu::url::UncheckedUrl; use redb::{Database, MultimapTableDefinition, ReadableTable, TableDefinition}; use tokio::sync::Mutex; use super::{Error, LocalStore}; +use crate::nuts::{Id, KeySetInfo, Keys, MintInfo, Proofs}; +use crate::types::{MeltQuote, MintQuote}; +use crate::url::UncheckedUrl; const MINTS_TABLE: TableDefinition<&str, &str> = TableDefinition::new("mints_table"); const MINT_KEYSETS_TABLE: MultimapTableDefinition<&str, &str> = diff --git a/crates/cdk/src/wallet/mod.rs b/crates/cdk/src/wallet/mod.rs index 9d4d0431..310e8799 100644 --- a/crates/cdk/src/wallet/mod.rs +++ b/crates/cdk/src/wallet/mod.rs @@ -5,23 +5,21 @@ use std::str::FromStr; use std::sync::Arc; use bip39::Mnemonic; -use cashu::dhke::{construct_proofs, unblind_message}; -use cashu::nuts::nut07::{ProofState, State}; -use cashu::nuts::nut09::RestoreRequest; -use cashu::nuts::nut11::SigningKey; -use cashu::nuts::{ - BlindSignature, CurrencyUnit, Id, KeySet, KeySetInfo, Keys, MintInfo, P2PKConditions, - PreMintSecrets, PreSwap, Proof, Proofs, PublicKey, SigFlag, SwapRequest, Token, -}; -use cashu::types::{MeltQuote, Melted, MintQuote}; -use cashu::url::UncheckedUrl; -use cashu::util::unix_time; -use cashu::{Amount, Bolt11Invoice}; use localstore::LocalStore; use thiserror::Error; use tracing::{debug, warn}; use crate::client::Client; +use crate::dhke::{construct_proofs, hash_to_curve, unblind_message}; +use crate::nuts::{ + BlindSignature, CurrencyUnit, Id, KeySet, KeySetInfo, Keys, MintInfo, P2PKConditions, + PreMintSecrets, PreSwap, Proof, ProofState, Proofs, PublicKey, RestoreRequest, SigFlag, + SigningKey, State, SwapRequest, Token, +}; +use crate::types::{MeltQuote, Melted, MintQuote}; +use crate::url::UncheckedUrl; +use crate::util::unix_time; +use crate::{Amount, Bolt11Invoice}; pub mod localstore; @@ -31,12 +29,12 @@ pub enum Error { #[error("Insufficient Funds")] InsufficientFunds, #[error("`{0}`")] - CashuWallet(#[from] cashu::error::wallet::Error), + CashuWallet(#[from] crate::error::wallet::Error), #[error("`{0}`")] Client(#[from] crate::client::Error), /// Cashu Url Error #[error("`{0}`")] - CashuUrl(#[from] cashu::url::Error), + CashuUrl(#[from] crate::url::Error), #[error("Quote Expired")] QuoteExpired, #[error("Quote Unknown")] @@ -46,7 +44,7 @@ pub enum Error { #[error("`{0}`")] LocalStore(#[from] localstore::Error), #[error("`{0}`")] - Cashu(#[from] cashu::error::Error), + Cashu(#[from] crate::error::Error), #[error("Could not verify Dleq")] CouldNotVerifyDleq, #[error("P2PK Condition Not met `{0}`")] @@ -187,8 +185,6 @@ impl Wallet { mint_url: UncheckedUrl, proofs: Proofs, ) -> Result, Error> { - use cashu::dhke::hash_to_curve; - let spendable = self .client .post_check_state( @@ -361,7 +357,7 @@ impl Wallet { let key = keys.amount_key(sig.amount).ok_or(Error::UnknownKey)?; match sig.verify_dleq(key, premint.blinded_message.b) { Ok(_) => (), - Err(cashu::nuts::nut12::Error::MissingDleqProof) => (), + Err(crate::nuts::nut12::Error::MissingDleqProof) => (), Err(_) => return Err(Error::CouldNotVerifyDleq), } } @@ -411,7 +407,7 @@ impl Wallet { let key = keys.amount_key(proof.amount).ok_or(Error::UnknownKey)?; match proof.verify_dleq(key) { Ok(_) => continue, - Err(cashu::nuts::nut12::Error::MissingDleqProof) => continue, + Err(crate::nuts::nut12::Error::MissingDleqProof) => continue, Err(_) => return Err(Error::CouldNotVerifyDleq), } } @@ -571,7 +567,7 @@ impl Wallet { let key = keys.amount_key(promise.amount).ok_or(Error::UnknownKey)?; match promise.verify_dleq(key, premint.blinded_message.b) { Ok(_) => (), - Err(cashu::nuts::nut12::Error::MissingDleqProof) => (), + Err(crate::nuts::nut12::Error::MissingDleqProof) => (), Err(_) => return Err(Error::CouldNotVerifyDleq), } } @@ -932,7 +928,7 @@ impl Wallet { let mut change_proofs = vec![]; for proof in post_swap_proofs { - let conditions: Result = (&proof.secret).try_into(); + let conditions: Result = (&proof.secret).try_into(); if conditions.is_ok() { send_proofs.push(proof); } else { @@ -1001,7 +997,7 @@ impl Wallet { } if let Ok(secret) = - >::try_into( + >::try_into( proof.secret.clone(), ) { @@ -1183,7 +1179,7 @@ impl Wallet { token: &Token, spending_conditions: P2PKConditions, ) -> Result<(), Error> { - use cashu::nuts::nut10; + use crate::nuts::nut10; if spending_conditions.refund_keys.is_some() && spending_conditions.locktime.is_none() { warn!(