Skip to content

Commit

Permalink
improve: export nut structs, add nut08
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Nov 16, 2023
1 parent e1264d3 commit c8e3c66
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 53 deletions.
2 changes: 1 addition & 1 deletion bindings/cashu-ffi/src/nuts/nut00/blinded_message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::Deref;
use std::sync::Arc;

use cashu::nuts::nut00::BlindedMessage as BlindedMessageSdk;
use cashu::nuts::BlindedMessage as BlindedMessageSdk;

use crate::nuts::nut01::public_key::PublicKey;
use crate::Amount;
Expand Down
2 changes: 1 addition & 1 deletion bindings/cashu-ffi/src/nuts/nut01/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::ops::Deref;
use std::sync::Arc;

use cashu::nuts::nut01::{Keys as KeysSdk, Response as KeysResponseSdk};
use cashu::nuts::nut01::{Keys as KeysSdk, KeysResponse as KeysResponseSdk};
use cashu::Amount as AmountSdk;

use crate::{Amount, PublicKey};
Expand Down
10 changes: 5 additions & 5 deletions bindings/cashu-ffi/src/nuts/nut02/key_set.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::Deref;
use std::sync::Arc;

use cashu::nuts::nut02::{Id as IdSdk, KeySet as KeySetSdk, Response};
use cashu::nuts::nut02::{Id as IdSdk, KeySet as KeySetSdk, KeysetResponse as KeysetResponseSdk};

use crate::error::Result;
use crate::nuts::nut01::keys::Keys;
Expand Down Expand Up @@ -73,14 +73,14 @@ impl From<cashu::nuts::nut02::KeySet> for KeySet {
}

pub struct KeySetResponse {
inner: Response,
inner: KeysetResponseSdk,
}

impl KeySetResponse {
pub fn new(keyset_ids: Vec<Arc<Id>>) -> Self {
let keysets = keyset_ids.into_iter().map(|id| id.inner).collect();
Self {
inner: Response { keysets },
inner: KeysetResponseSdk { keysets },
}
}

Expand All @@ -94,8 +94,8 @@ impl KeySetResponse {
}
}

impl From<cashu::nuts::nut02::Response> for KeySetResponse {
fn from(inner: Response) -> KeySetResponse {
impl From<KeysetResponseSdk> for KeySetResponse {
fn from(inner: KeysetResponseSdk) -> KeySetResponse {
KeySetResponse { inner }
}
}
11 changes: 5 additions & 6 deletions bindings/cashu-js/src/nuts/nut02/keyset.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ops::Deref;

use cashu::nuts::nut01::Response as KeysResponse;
use cashu::nuts::nut02::{Id, KeySet, Response as KeySetsResponse};
use cashu::nuts::{Id, KeySet, KeysResponse, KeysetResponse};
use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};
Expand Down Expand Up @@ -86,18 +85,18 @@ impl JsKeySet {

#[wasm_bindgen(js_name = KeySetsResponse)]
pub struct JsKeySetsResponse {
inner: KeySetsResponse,
inner: KeysetResponse,
}

impl Deref for JsKeySetsResponse {
type Target = KeySetsResponse;
type Target = KeysetResponse;
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<KeySetsResponse> for JsKeySetsResponse {
fn from(inner: KeySetsResponse) -> JsKeySetsResponse {
impl From<KeysetResponse> for JsKeySetsResponse {
fn from(inner: KeysetResponse) -> JsKeySetsResponse {
JsKeySetsResponse { inner }
}
}
Expand Down
5 changes: 2 additions & 3 deletions bindings/cashu-sdk-js/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use cashu_js::nuts::nut07::{JsCheckSpendableRequest, JsCheckSpendableResponse};
use cashu_js::nuts::nut08::{JsMeltRequest, JsMeltResponse};
use cashu_js::JsAmount;
use cashu_sdk::mint::Mint;
use cashu_sdk::nuts::nut01;
use cashu_sdk::nuts::nut02::KeySet;
use cashu_sdk::nuts::{KeySet, KeysResponse};
use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};
Expand Down Expand Up @@ -64,7 +63,7 @@ impl JsMint {
pub fn active_keyset_pubkeys(&self) -> Result<JsKeysResponse> {
let keyset: KeySet = self.inner.active_keyset.clone().into();

Ok(nut01::Response { keys: keyset.keys }.into())
Ok(KeysResponse { keys: keyset.keys }.into())
}

/// Get Keysets
Expand Down
10 changes: 5 additions & 5 deletions crates/cashu-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ license.workspace = true


[features]
default = ["mint", "wallet"]
default = ["mint", "wallet", "all-nuts"]
mint = ["cashu/mint"]
wallet = ["cashu/wallet", "dep:minreq", "dep:once_cell"]
gloo = ["dep:gloo"]
all-nuts = ["nut07", "nut09"]
all-nuts = ["nut07", "nut08", "nut09"]
nut07 = ["cashu/nut07"]
# nut08 = ["cashu/nut08"]
nut08 = ["cashu/nut08"]
nut09 = ["cashu/nut09"]


[dependencies]
cashu = { path = "../cashu", features = ["nut08"] }
cashu = { path = "../cashu" }
serde = { workspace = true }
serde_json = { workspace = true }
url = { workspace = true }
Expand All @@ -30,7 +30,7 @@ futures-util = { version = "0.3", default-features = false, features = ["sink",
once_cell = { version = "1.17", optional = true }
thiserror = { workspace = true }
async-trait = "0.1.74"
gloo = { version = "0.10.0", optional = true, features = ["net"]}
gloo = { version = "0.10.0", optional = true, features = ["net"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { workspace = true, features = ["rt-multi-thread", "time", "macros", "sync"] }
Expand Down
4 changes: 2 additions & 2 deletions crates/cashu-sdk/src/client/gloo_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Client for HttpClient {
}

/// Get Keysets [NUT-02]
async fn get_mint_keysets(&self, mint_url: Url) -> Result<nut02::Response, Error> {
async fn get_mint_keysets(&self, mint_url: Url) -> Result<KeysetResponse, Error> {
let url = join_url(mint_url, "keysets")?;
let res = Request::get(url.as_str())
.send()
Expand All @@ -53,7 +53,7 @@ impl Client for HttpClient {
.await
.map_err(|err| Error::Gloo(err.to_string()))?;

let response: Result<nut02::Response, serde_json::Error> =
let response: Result<KeysetResponse, serde_json::Error> =
serde_json::from_value(res.clone());

match response {
Expand Down
4 changes: 2 additions & 2 deletions crates/cashu-sdk/src/client/minreq_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ impl Client for HttpClient {
}

/// Get Keysets [NUT-02]
async fn get_mint_keysets(&self, mint_url: Url) -> Result<nut02::Response, Error> {
async fn get_mint_keysets(&self, mint_url: Url) -> Result<KeysetResponse, Error> {
let url = join_url(mint_url, "keysets")?;
let res = minreq::get(url).send()?.json::<Value>()?;

let response: Result<nut02::Response, serde_json::Error> =
let response: Result<KeysetResponse, serde_json::Error> =
serde_json::from_value(res.clone());

match response {
Expand Down
2 changes: 1 addition & 1 deletion crates/cashu-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct MintErrorResponse {
pub trait Client {
async fn get_mint_keys(&self, mint_url: Url) -> Result<Keys, Error>;

async fn get_mint_keysets(&self, mint_url: Url) -> Result<nut02::Response, Error>;
async fn get_mint_keysets(&self, mint_url: Url) -> Result<KeysetResponse, Error>;

async fn get_request_mint(
&self,
Expand Down
28 changes: 13 additions & 15 deletions crates/cashu-sdk/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ use std::collections::{HashMap, HashSet};

use cashu::dhke::{sign_message, verify_message};
pub use cashu::error::mint::Error;
use cashu::nuts::nut00::{BlindedMessage, BlindedSignature, Proof};
use cashu::nuts::nut02::mint::KeySet;
use cashu::nuts::nut02::Id;
use cashu::nuts::nut06::{SplitRequest, SplitResponse};
use cashu::nuts::{
BlindedMessage, BlindedSignature, MeltRequest, MeltResponse, Proof, SplitRequest,
SplitResponse, *,
};
#[cfg(feature = "nut07")]
use cashu::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse};
use cashu::nuts::nut08::{MeltRequest, MeltResponse};
use cashu::nuts::*;
use cashu::nuts::{CheckSpendableRequest, CheckSpendableResponse};
use cashu::secret::Secret;
use cashu::types::KeysetInfo;
use cashu::Amount;
Expand Down Expand Up @@ -60,24 +58,24 @@ impl Mint {

/// Retrieve the public keys of the active keyset for distribution to
/// wallet clients
pub fn active_keyset_pubkeys(&self) -> nut01::Response {
nut01::Response {
keys: nut02::KeySet::from(self.active_keyset.clone()).keys,
pub fn active_keyset_pubkeys(&self) -> KeysResponse {
KeysResponse {
keys: KeySet::from(self.active_keyset.clone()).keys,
}
}

/// Return a list of all supported keysets
pub fn keysets(&self) -> nut02::Response {
pub fn keysets(&self) -> KeysetResponse {
let mut keysets: HashSet<_> = self.inactive_keysets.keys().cloned().collect();
keysets.insert(self.active_keyset.id);
nut02::Response { keysets }
KeysetResponse { keysets }
}

pub fn active_keyset(&self) -> nut02::mint::KeySet {
pub fn active_keyset(&self) -> MintKeySet {
self.active_keyset.clone()
}

pub fn keyset(&self, id: &Id) -> Option<nut02::KeySet> {
pub fn keyset(&self, id: &Id) -> Option<KeySet> {
if self.active_keyset.id.eq(id) {
return Some(self.active_keyset.clone().into());
}
Expand All @@ -99,7 +97,7 @@ impl Mint {
self.inactive_keysets
.insert(self.active_keyset.id, self.active_keyset_info.clone());

self.active_keyset = KeySet::generate(secret, derivation_path, max_order);
self.active_keyset = MintKeySet::generate(secret, derivation_path, max_order);
}

pub fn process_mint_request(
Expand Down
9 changes: 4 additions & 5 deletions crates/cashu-sdk/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
use std::str::FromStr;

use cashu::dhke::{construct_proofs, unblind_message};
use cashu::nuts::nut00::wallet::{BlindedMessages, Token};
use cashu::nuts::nut00::{BlindedSignature, Proof, Proofs};
use cashu::nuts::nut01::Keys;
use cashu::nuts::nut03::RequestMintResponse;
use cashu::nuts::nut06::{SplitPayload, SplitRequest};
use cashu::nuts::{
BlindedMessages, BlindedSignature, Keys, Proof, Proofs, RequestMintResponse, SplitPayload,
SplitRequest, Token,
};
#[cfg(feature = "nut07")]
use cashu::types::ProofsStatus;
use cashu::types::{Melted, SendProofs};
Expand Down
2 changes: 1 addition & 1 deletion crates/cashu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = "Cashu rust wallet and mint library"


[features]
default = ["mint", "wallet"]
default = ["mint", "wallet", "all-nuts"]
mint = []
wallet = []
all-nuts = ["nut07", "nut08", "nut09"]
Expand Down
18 changes: 18 additions & 0 deletions crates/cashu/src/nuts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,23 @@ pub mod nut08;
#[cfg(feature = "nut09")]
pub mod nut09;

#[cfg(feature = "wallet")]
pub use nut00::wallet::{BlindedMessages, Token};
#[cfg(feature = "mint")]
pub use nut00::BlindedMessage;
pub use nut00::{BlindedSignature, Proof, Proofs};
pub use nut01::{Keys, KeysResponse, PublicKey, SecretKey};
pub use nut02::mint::KeySet as MintKeySet;
pub use nut02::{Id, KeySet, KeysetResponse};
pub use nut03::RequestMintResponse;
pub use nut04::{MintRequest, PostMintResponse};
pub use nut05::{CheckFeesRequest, CheckFeesResponse};
#[cfg(not(feature = "nut08"))]
pub use nut05::{MeltRequest, MeltResponse};
pub use nut06::{SplitPayload, SplitRequest, SplitResponse};
#[cfg(feature = "nut07")]
pub use nut07::{CheckSpendableRequest, CheckSpendableResponse};
#[cfg(feature = "nut08")]
pub use nut08::{MeltRequest, MeltResponse};
#[cfg(feature = "nut09")]
pub use nut09::MintInfo;
10 changes: 5 additions & 5 deletions crates/cashu/src/nuts/nut01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ impl Keys {

/// Mint Public Keys [NUT-01]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Response {
pub struct KeysResponse {
/// set of public keys that the mint generates
#[serde(flatten)]
pub keys: Keys,
}

impl<'de> serde::de::Deserialize<'de> for Response {
impl<'de> serde::de::Deserialize<'de> for KeysResponse {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct KeysVisitor;

impl<'de> serde::de::Visitor<'de> for KeysVisitor {
type Value = Response;
type Value = KeysResponse;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("")
Expand All @@ -159,7 +159,7 @@ impl<'de> serde::de::Deserialize<'de> for Response {
// invalid and not continue
}

Ok(Response { keys: Keys(keys) })
Ok(KeysResponse { keys: Keys(keys) })
}
}

Expand Down Expand Up @@ -211,7 +211,7 @@ mod tests {
fn key_response() {
let res: String = r#"{"1":"02f71e2d93aa95fc52b938735a24774ad926406c81e9dc9d2aa699fb89281548fd","2":"03b28dd9c19aaf1ec847be31b60c6a5e1a6cb6f87434afcdb0d9348ba0e2bdb150","4":"03ede0e704e223e764a82f73984b0fec0fdbde15ef57b4de95b527f7182af7487e","8":"020fd24fbd552445df70c244be2af77da2b2f634ccfda9e9620b347b5cd50dbdd8","16":"03ef9ef2515df5c0d0851ed9419a24a571ef5e03206d9d2fc6572ac050c5afe1aa","32":"02dbd455474176b30234c178573e874cc79d0c2fc1920cf0e9f133204cf43299c1","64":"0237c1eb11b8a214cca3e0104684227952188039a05cd55c1ad3896a572c70a7c3","128":"02655041771766b94a269f9f1ec1860f2eade55bb472c4db74ac1257ef54aac52b","256":"02b9e0be7b423bded7d60ff7114549de8d2d5b9c099edf8887aff474037e4bc0cf","512":"0320454cc41e646f49e1ac0a62b9667c80dee45545b045575f2a26f01770dc2521","1024":"0267fc1dabac016f46b3d1a650d97b56f3e56540106720f3d24ff7a6e9cd7183e9","2048":"035a9a25251a4da56f49667ca50677470fc6d8e186a875ab7b32aa064eb9e9e948","4096":"02f607a9eed310825c2d2e66d6e64fb237fe21b640b9a66cc7646b2a6480d91457","8192":"033346f7dce2ef71a80c5d657a8930bdd19c7c1708d03829daf43f20eaeda76768","16384":"024fad3b0b60c6b71d848deac173183fae8ddde31bbde531f18ab23473ddff541d","32768":"020d195466819d96d8c7eee9150565b7bd37196c7d12d0e96e389f56be8aebb44b","65536":"038c9bf295a745726c38d14988851d68d201296a802c296faa838000c2f44d25e0","131072":"032ff6491cdeff0bf9b34acd5deceef3cca7682b5f94dbe3068af8bb3b5aa34b81","262144":"02570090f5b6900955fd794d8f22c23fb35fc87fa03069b9b16bea63ea7cda419a","524288":"029d3c751c7d1c3e1d3e4b7791e1e809f6dedf2c28e172a82967d49a14b7c26ce2","1048576":"03b4a41d39cc6f2a8925f694c514e107b87d7ddb8f5ac55c9e4b7895139d0decd8","2097152":"02d4abbce491f87656eb0d2e66ef18eb009f6320169ef12e66703298d5395f2b91","4194304":"0359023fb85f6e6ede0141ab8f4a1277c19ed62b49b8ef5c5e2c8ca7fefe9b2f91","8388608":"0353d3ae1dad05e1b46ab85a366bfcdb7a645e3457f7714003e0fb06f4d75f4d87","16777216":"032d0847606465b97f15aca30c69f5baeeb43bf6188b4679f723119ce6fb9708c5","33554432":"028a673a53e78aa8c992128e21efb3b33fbd54de20afcf81a67e69eaf2bab7e0e9","67108864":"0278b66e140559352bb5aeca854a6466bc439ee206a9f349ed7926aae4335269b7","134217728":"023834651da0737f484a77204c2d06543fb65ad2dd8d095a2be48ca12ebf2664ec","268435456":"032cba9068638965ccc3870c140c72a1b028a820851f36fe59639e7ab3093a8ffd","536870912":"03eae5e4b22dfa5ad77476c925717dc4e005da78142e75b47fb28569d745483af3","1073741824":"02d17d61027602432a8484b65e6d6063ed9157c51ce92099d61ac2820411c59f9f","2147483648":"0236870e39b3a739d5caa04988dce432e3d7988420f04d9b415125af22672e2726"}"#.to_string();

let response: Response = serde_json::from_str(&res).unwrap();
let response: Keys = serde_json::from_str(&res).unwrap();

assert_eq!(&serde_json::to_string(&response).unwrap(), &res)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cashu/src/nuts/nut02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl From<&Keys> for Id {
/// Mint Keysets [NUT-02]
/// Ids of mints keyset ids
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Response {
pub struct KeysetResponse {
/// set of public key ids that the mint generates
pub keysets: HashSet<Id>,
}
Expand Down

0 comments on commit c8e3c66

Please sign in to comment.