Browse Source

refactor: consolidate to one CDK crate

Having the two crates adds complexity without
benefit since features can be used instead
thesimplekid 11 months ago
parent
commit
5cd03b0027

+ 0 - 9
Cargo.toml

@@ -1,6 +1,5 @@
 [workspace]
 members = [
-    "crates/cashu",
     "crates/cdk",
 ]
 resolver = "2"
@@ -21,15 +20,7 @@ license-file = "LICENSE"
 keywords = ["bitcoin", "e-cash", "cashu"]
 
 [workspace.dependencies]
-serde = { version = "1.0.160", features = ["derive"]}
-serde_json = "1.0.96"
-url = "2.3.1"
 tokio = { version = "1.32", default-features = false }
-tracing = { version = "0.1", default-features = false }
-tracing-subscriber = "0.3"
-uniffi = "0.24"
-thiserror = "1.0.50"
-getrandom = { version = "0.2", features = ["js"] }
 
 [profile]
 

+ 0 - 37
crates/cashu/Cargo.toml

@@ -1,37 +0,0 @@
-[package]
-name = "cashu"
-version = "0.4.1-ALPHA"
-edition = "2021"
-authors = ["thesimplekid"]
-readme = "README.md"
-homepage.workspace = true
-repository.workspace = true
-license.workspace = true
-rust-version.workspace = true # MSRV
-description = "Cashu rust wallet and mint library"
-
-
-[features]
-default = ["mint", "wallet", "all-nuts"]
-mint = []
-wallet = []
-all-nuts = ["nut13"]
-nut13 = ["dep:bip39"]
-
-[dependencies]
-base64 = "0.21" # bitcoin uses v0.21 (optional dep)
-bip39 = { version = "2.0", optional = true }
-bitcoin = { version = "0.30", features = ["serde", "rand", "rand-std"] } # lightning-invoice uses v0.30
-lightning-invoice = { version = "0.29", features = ["serde"] }
-once_cell = "1.19"
-serde.workspace = true
-serde_json.workspace = true
-serde_with = "3.4"
-url.workspace = true
-thiserror.workspace = true
-tracing.workspace = true
-uuid = { version = "1.6", features = ["v4"] }
-
-[target.'cfg(target_arch = "wasm32")'.dependencies]
-getrandom.workspace = true
-instant = { version = "0.1", features = [ "wasm-bindgen", "inaccurate" ] }

+ 0 - 184
crates/cashu/src/error.rs

@@ -1,184 +0,0 @@
-use std::string::FromUtf8Error;
-
-use serde::{Deserialize, Serialize};
-use thiserror::Error;
-
-use crate::util::hex;
-
-#[derive(Debug, Error)]
-pub enum Error {
-    /// Parse Url Error
-    #[error("`{0}`")]
-    UrlParseError(#[from] url::ParseError),
-    /// Utf8 parse error
-    #[error("`{0}`")]
-    Utf8ParseError(#[from] FromUtf8Error),
-    /// Serde Json error
-    #[error("`{0}`")]
-    SerdeJsonError(#[from] serde_json::Error),
-    /// Base64 error
-    #[error("`{0}`")]
-    Base64Error(#[from] base64::DecodeError),
-    /// From hex error
-    #[error("`{0}`")]
-    HexError(#[from] hex::Error),
-    /// Secp256k1 error
-    #[error("`{0}`")]
-    Secp256k1(#[from] bitcoin::secp256k1::Error),
-    #[error("No Key for Amoun")]
-    AmountKey,
-    #[error("Amount miss match")]
-    Amount,
-    #[error("Token already spent")]
-    TokenSpent,
-    #[error("Token not verified")]
-    TokenNotVerifed,
-    #[error("Invoice Amount undefined")]
-    InvoiceAmountUndefined,
-    #[error("Proof missing required field")]
-    MissingProofField,
-    #[error("No valid point found")]
-    NoValidPoint,
-    #[error("Kind not found")]
-    KindNotFound,
-    #[error("Unknown Tag")]
-    UnknownTag,
-    #[error("Incorrect Secret Kind")]
-    IncorrectSecretKind,
-    #[error("Spending conditions not met")]
-    SpendConditionsNotMet,
-    #[error("Could not convert key")]
-    Key,
-    #[error("Invalid signature")]
-    InvalidSignature,
-    #[error("Locktime in past")]
-    LocktimeInPast,
-    #[error("`{0}`")]
-    Secret(#[from] super::secret::Error),
-    #[error("`{0}`")]
-    NUT01(#[from] crate::nuts::nut01::Error),
-    #[error("`{0}`")]
-    NUT02(#[from] crate::nuts::nut02::Error),
-    #[cfg(feature = "nut13")]
-    #[error("`{0}`")]
-    Bip32(#[from] bitcoin::bip32::Error),
-    #[error("`{0}`")]
-    ParseInt(#[from] std::num::ParseIntError),
-    /// Custom error
-    #[error("`{0}`")]
-    CustomError(String),
-}
-
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
-pub struct ErrorResponse {
-    pub code: u32,
-    pub error: Option<String>,
-    pub detail: Option<String>,
-}
-
-impl ErrorResponse {
-    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
-        if let Ok(res) = serde_json::from_str::<ErrorResponse>(json) {
-            Ok(res)
-        } else {
-            Ok(Self {
-                code: 999,
-                error: Some(json.to_string()),
-                detail: None,
-            })
-        }
-    }
-}
-
-pub mod wallet {
-    use std::string::FromUtf8Error;
-
-    use thiserror::Error;
-
-    use crate::nuts::nut01;
-
-    #[derive(Debug, Error)]
-    pub enum Error {
-        /// Serde Json error
-        #[error("`{0}`")]
-        SerdeJsonError(#[from] serde_json::Error),
-        /// Secp256k1 error
-        #[error("`{0}`")]
-        Secp256k1(#[from] bitcoin::secp256k1::Error),
-        /// NUT01 error
-        #[error("`{0}`")]
-        NUT01(#[from] nut01::Error),
-        /// Insufficient Funds
-        #[error("Insufficient funds")]
-        InsufficientFunds,
-        /// Utf8 parse error
-        #[error("`{0}`")]
-        Utf8ParseError(#[from] FromUtf8Error),
-        /// Base64 error
-        #[error("`{0}`")]
-        Base64Error(#[from] base64::DecodeError),
-        /// Unsupported Token
-        #[error("Token unsupported")]
-        UnsupportedToken,
-        /// Token Requires proofs
-        #[error("Proofs Required")]
-        ProofsRequired,
-        /// Url Parse error
-        #[error("Url Parse")]
-        UrlParse,
-        #[error("`{0}`")]
-        Secret(#[from] crate::secret::Error),
-        #[error("`{0}`")]
-        Cashu(#[from] super::Error),
-        /// Custom Error message
-        #[error("`{0}`")]
-        CustomError(String),
-    }
-
-    impl From<crate::url::Error> for Error {
-        fn from(_err: crate::url::Error) -> Error {
-            Error::UrlParse
-        }
-    }
-}
-
-pub mod mint {
-    use thiserror::Error;
-
-    use crate::nuts::nut01;
-
-    #[derive(Debug, Error)]
-    pub enum Error {
-        #[error("No key for amount")]
-        AmountKey,
-        #[error("Amount miss match")]
-        Amount,
-        #[error("Token Already Spent")]
-        TokenSpent,
-        /// Secp256k1 error
-        #[error("`{0}`")]
-        Secp256k1(#[from] bitcoin::secp256k1::Error),
-        /// NUT01 error
-        #[error("`{0}`")]
-        NUT01(#[from] nut01::Error),
-        #[error("`Token not verified`")]
-        TokenNotVerifed,
-        #[error("Invoice amount undefined")]
-        InvoiceAmountUndefined,
-        /// Duplicate Proofs sent in request
-        #[error("Duplicate proofs")]
-        DuplicateProofs,
-        /// Keyset id not active
-        #[error("Keyset id is not active")]
-        InactiveKeyset,
-        /// Keyset is not known
-        #[error("Unknown Keyset")]
-        UnknownKeySet,
-        #[error("`{0}`")]
-        Secret(#[from] crate::secret::Error),
-        #[error("`{0}`")]
-        Cashu(#[from] super::Error),
-        #[error("`{0}`")]
-        CustomError(String),
-    }
-}

+ 0 - 20
crates/cashu/src/lib.rs

@@ -1,20 +0,0 @@
-extern crate core;
-
-pub use bitcoin::hashes::sha256::Hash as Sha256;
-pub use bitcoin::secp256k1;
-pub use lightning_invoice::{self, Bolt11Invoice};
-
-pub mod amount;
-pub mod dhke;
-pub mod error;
-pub mod nuts;
-pub mod secret;
-pub mod serde_utils;
-pub mod types;
-pub mod url;
-pub mod util;
-
-pub use self::amount::Amount;
-pub use self::util::SECP256K1;
-
-pub type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;

+ 17 - 11
crates/cdk/Cargo.toml

@@ -11,25 +11,30 @@ license.workspace = true
 
 [features]
 default = ["mint", "wallet", "all-nuts", "redb"]
-mint = ["cashu/mint"]
-wallet = ["cashu/wallet", "dep:minreq"]
+mint = []
+wallet = ["dep:minreq"]
 gloo = ["dep:gloo"]
 all-nuts = ["nut13"]
-nut13 = ["cashu/nut13"]
+nut13 = ["dep:bip39"]
 redb = ["dep:redb"]
 
 
 [dependencies]
-bip39 = "2.0.0"
-cashu = { path = "../cashu" }
-serde = { workspace = true }
-serde_json = { workspace = true }
-url = { workspace = true }
-tracing = { workspace = true }
-thiserror = { workspace = true }
+base64 = "0.21" # bitcoin uses v0.21 (optional dep)
+bip39 = { version = "2.0", optional = true }
+bitcoin = { version = "0.30", features = ["serde", "rand", "rand-std"] } # lightning-invoice uses v0.30
+lightning-invoice = { version = "0.29", features = ["serde"] }
+once_cell = "1.19"
+serde = { version = "1.0.160", default-features = false, features = ["derive"]}
+serde_json = { version = "1.0.96", default-features = false }
+serde_with = "3.4"
+url = "2.3.1"
+tracing = { version = "0.1", default-features = false }
+thiserror = "1.0.50"
 async-trait = "0.1.74"
 gloo = { version = "0.11.0", optional = true, features = ["net"] }
 http = "1.0.0"
+uuid = { version = "1.6", features = ["v4"] }
 
 [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
 tokio = { workspace = true, features = ["rt-multi-thread", "time", "macros", "sync"] }
@@ -38,6 +43,7 @@ redb = { version = "2.0.0", optional = true }
 
 [target.'cfg(target_arch = "wasm32")'.dependencies]
 tokio = { workspace = true, features = ["rt", "macros", "sync", "time"] }
-getrandom = { workspace = true }
+getrandom = { version = "0.2.14" }
+instant = { version = "0.1", features = [ "wasm-bindgen", "inaccurate" ] }
 
 

+ 0 - 0
crates/cashu/src/amount.rs → crates/cdk/src/amount.rs


+ 0 - 0
crates/cashu/src/dhke.rs → crates/cdk/src/dhke.rs


+ 183 - 0
crates/cdk/src/error.rs

@@ -1 +1,184 @@
+use std::string::FromUtf8Error;
 
+use serde::{Deserialize, Serialize};
+use thiserror::Error;
+
+use crate::util::hex;
+
+#[derive(Debug, Error)]
+pub enum Error {
+    /// Parse Url Error
+    #[error("`{0}`")]
+    UrlParseError(#[from] url::ParseError),
+    /// Utf8 parse error
+    #[error("`{0}`")]
+    Utf8ParseError(#[from] FromUtf8Error),
+    /// Serde Json error
+    #[error("`{0}`")]
+    SerdeJsonError(#[from] serde_json::Error),
+    /// Base64 error
+    #[error("`{0}`")]
+    Base64Error(#[from] base64::DecodeError),
+    /// From hex error
+    #[error("`{0}`")]
+    HexError(#[from] hex::Error),
+    /// Secp256k1 error
+    #[error("`{0}`")]
+    Secp256k1(#[from] bitcoin::secp256k1::Error),
+    #[error("No Key for Amoun")]
+    AmountKey,
+    #[error("Amount miss match")]
+    Amount,
+    #[error("Token already spent")]
+    TokenSpent,
+    #[error("Token not verified")]
+    TokenNotVerifed,
+    #[error("Invoice Amount undefined")]
+    InvoiceAmountUndefined,
+    #[error("Proof missing required field")]
+    MissingProofField,
+    #[error("No valid point found")]
+    NoValidPoint,
+    #[error("Kind not found")]
+    KindNotFound,
+    #[error("Unknown Tag")]
+    UnknownTag,
+    #[error("Incorrect Secret Kind")]
+    IncorrectSecretKind,
+    #[error("Spending conditions not met")]
+    SpendConditionsNotMet,
+    #[error("Could not convert key")]
+    Key,
+    #[error("Invalid signature")]
+    InvalidSignature,
+    #[error("Locktime in past")]
+    LocktimeInPast,
+    #[error("`{0}`")]
+    Secret(#[from] super::secret::Error),
+    #[error("`{0}`")]
+    NUT01(#[from] crate::nuts::nut01::Error),
+    #[error("`{0}`")]
+    NUT02(#[from] crate::nuts::nut02::Error),
+    #[cfg(feature = "nut13")]
+    #[error("`{0}`")]
+    Bip32(#[from] bitcoin::bip32::Error),
+    #[error("`{0}`")]
+    ParseInt(#[from] std::num::ParseIntError),
+    /// Custom error
+    #[error("`{0}`")]
+    CustomError(String),
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ErrorResponse {
+    pub code: u32,
+    pub error: Option<String>,
+    pub detail: Option<String>,
+}
+
+impl ErrorResponse {
+    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
+        if let Ok(res) = serde_json::from_str::<ErrorResponse>(json) {
+            Ok(res)
+        } else {
+            Ok(Self {
+                code: 999,
+                error: Some(json.to_string()),
+                detail: None,
+            })
+        }
+    }
+}
+
+pub mod wallet {
+    use std::string::FromUtf8Error;
+
+    use thiserror::Error;
+
+    use crate::nuts::nut01;
+
+    #[derive(Debug, Error)]
+    pub enum Error {
+        /// Serde Json error
+        #[error("`{0}`")]
+        SerdeJsonError(#[from] serde_json::Error),
+        /// Secp256k1 error
+        #[error("`{0}`")]
+        Secp256k1(#[from] bitcoin::secp256k1::Error),
+        /// NUT01 error
+        #[error("`{0}`")]
+        NUT01(#[from] nut01::Error),
+        /// Insufficient Funds
+        #[error("Insufficient funds")]
+        InsufficientFunds,
+        /// Utf8 parse error
+        #[error("`{0}`")]
+        Utf8ParseError(#[from] FromUtf8Error),
+        /// Base64 error
+        #[error("`{0}`")]
+        Base64Error(#[from] base64::DecodeError),
+        /// Unsupported Token
+        #[error("Token unsupported")]
+        UnsupportedToken,
+        /// Token Requires proofs
+        #[error("Proofs Required")]
+        ProofsRequired,
+        /// Url Parse error
+        #[error("Url Parse")]
+        UrlParse,
+        #[error("`{0}`")]
+        Secret(#[from] crate::secret::Error),
+        #[error("`{0}`")]
+        Cashu(#[from] super::Error),
+        /// Custom Error message
+        #[error("`{0}`")]
+        CustomError(String),
+    }
+
+    impl From<crate::url::Error> for Error {
+        fn from(_err: crate::url::Error) -> Error {
+            Error::UrlParse
+        }
+    }
+}
+
+pub mod mint {
+    use thiserror::Error;
+
+    use crate::nuts::nut01;
+
+    #[derive(Debug, Error)]
+    pub enum Error {
+        #[error("No key for amount")]
+        AmountKey,
+        #[error("Amount miss match")]
+        Amount,
+        #[error("Token Already Spent")]
+        TokenSpent,
+        /// Secp256k1 error
+        #[error("`{0}`")]
+        Secp256k1(#[from] bitcoin::secp256k1::Error),
+        /// NUT01 error
+        #[error("`{0}`")]
+        NUT01(#[from] nut01::Error),
+        #[error("`Token not verified`")]
+        TokenNotVerifed,
+        #[error("Invoice amount undefined")]
+        InvoiceAmountUndefined,
+        /// Duplicate Proofs sent in request
+        #[error("Duplicate proofs")]
+        DuplicateProofs,
+        /// Keyset id not active
+        #[error("Keyset id is not active")]
+        InactiveKeyset,
+        /// Keyset is not known
+        #[error("Unknown Keyset")]
+        UnknownKeySet,
+        #[error("`{0}`")]
+        Secret(#[from] crate::secret::Error),
+        #[error("`{0}`")]
+        Cashu(#[from] super::Error),
+        #[error("`{0}`")]
+        CustomError(String),
+    }
+}

+ 20 - 9
crates/cdk/src/lib.rs

@@ -1,9 +1,20 @@
-pub use bip39::Mnemonic;
-pub use cashu::{self, *};
-
-#[cfg(feature = "wallet")]
-pub mod client;
-#[cfg(feature = "mint")]
-pub mod mint;
-#[cfg(feature = "wallet")]
-pub mod wallet;
+extern crate core;
+
+pub use bitcoin::hashes::sha256::Hash as Sha256;
+pub use bitcoin::secp256k1;
+pub use lightning_invoice::{self, Bolt11Invoice};
+
+pub mod amount;
+pub mod dhke;
+pub mod error;
+pub mod nuts;
+pub mod secret;
+pub mod serde_utils;
+pub mod types;
+pub mod url;
+pub mod util;
+
+pub use self::amount::Amount;
+pub use self::util::SECP256K1;
+
+pub type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;

+ 0 - 0
crates/cashu/src/nuts/mod.rs → crates/cdk/src/nuts/mod.rs


+ 0 - 0
crates/cashu/src/nuts/nut00.rs → crates/cdk/src/nuts/nut00.rs


+ 0 - 0
crates/cashu/src/nuts/nut01/mod.rs → crates/cdk/src/nuts/nut01/mod.rs


+ 0 - 0
crates/cashu/src/nuts/nut01/public_key.rs → crates/cdk/src/nuts/nut01/public_key.rs


+ 0 - 0
crates/cashu/src/nuts/nut01/secret_key.rs → crates/cdk/src/nuts/nut01/secret_key.rs


+ 0 - 0
crates/cashu/src/nuts/nut02.rs → crates/cdk/src/nuts/nut02.rs


+ 0 - 0
crates/cashu/src/nuts/nut03.rs → crates/cdk/src/nuts/nut03.rs


+ 0 - 0
crates/cashu/src/nuts/nut04.rs → crates/cdk/src/nuts/nut04.rs


+ 0 - 0
crates/cashu/src/nuts/nut05.rs → crates/cdk/src/nuts/nut05.rs


+ 0 - 0
crates/cashu/src/nuts/nut06.rs → crates/cdk/src/nuts/nut06.rs


+ 0 - 0
crates/cashu/src/nuts/nut07.rs → crates/cdk/src/nuts/nut07.rs


+ 0 - 0
crates/cashu/src/nuts/nut08.rs → crates/cdk/src/nuts/nut08.rs


+ 0 - 0
crates/cashu/src/nuts/nut09.rs → crates/cdk/src/nuts/nut09.rs


+ 0 - 0
crates/cashu/src/nuts/nut10.rs → crates/cdk/src/nuts/nut10.rs


+ 0 - 0
crates/cashu/src/nuts/nut11.rs → crates/cdk/src/nuts/nut11.rs


+ 0 - 0
crates/cashu/src/nuts/nut12.rs → crates/cdk/src/nuts/nut12.rs


+ 0 - 0
crates/cashu/src/nuts/nut13.rs → crates/cdk/src/nuts/nut13.rs


+ 0 - 0
crates/cashu/src/secret.rs → crates/cdk/src/secret.rs


+ 0 - 0
crates/cashu/src/serde_utils.rs → crates/cdk/src/serde_utils.rs


+ 0 - 0
crates/cashu/src/types.rs → crates/cdk/src/types.rs


+ 0 - 0
crates/cashu/src/url.rs → crates/cdk/src/url.rs


+ 0 - 0
crates/cashu/src/util/hex.rs → crates/cdk/src/util/hex.rs


+ 0 - 0
crates/cashu/src/util/mod.rs → crates/cdk/src/util/mod.rs