From 419b1a360d83a2b1a4a149e387956ff3cc19943f Mon Sep 17 00:00:00 2001 From: ok300 <106775972+ok300@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:07:47 +0200 Subject: [PATCH] TokenV4: unit is mandatory --- crates/cdk/src/nuts/nut00/token.rs | 29 +++++++++++++---------------- crates/cdk/src/wallet/mint.rs | 1 - crates/cdk/src/wallet/send.rs | 7 +------ crates/cdk/src/wallet/types.rs | 3 +-- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/crates/cdk/src/nuts/nut00/token.rs b/crates/cdk/src/nuts/nut00/token.rs index abaa1024..12ffbd5d 100644 --- a/crates/cdk/src/nuts/nut00/token.rs +++ b/crates/cdk/src/nuts/nut00/token.rs @@ -43,7 +43,7 @@ impl Token { mint_url: MintUrl, proofs: Proofs, memo: Option, - unit: Option, + unit: CurrencyUnit, ) -> Self { let proofs = proofs .into_iter() @@ -90,10 +90,10 @@ impl Token { } /// Unit - pub fn unit(&self) -> &Option { + pub fn unit(&self) -> Option { match self { Self::TokenV3(token) => token.unit(), - Self::TokenV4(token) => token.unit(), + Self::TokenV4(token) => Some(token.unit()), } } @@ -219,8 +219,8 @@ impl TokenV3 { } #[inline] - fn unit(&self) -> &Option { - &self.unit + fn unit(&self) -> Option { + self.unit } } @@ -257,7 +257,7 @@ impl From for TokenV3 { TokenV3 { token: vec![TokenV3Token::new(mint_url, proofs)], memo: token.memo, - unit: token.unit, + unit: Some(token.unit), } } } @@ -269,14 +269,12 @@ pub struct TokenV4 { #[serde(rename = "m")] pub mint_url: MintUrl, /// Token Unit - #[serde(rename = "u", skip_serializing_if = "Option::is_none")] - pub unit: Option, + #[serde(rename = "u")] + pub unit: CurrencyUnit, /// Memo for token #[serde(rename = "d", skip_serializing_if = "Option::is_none")] pub memo: Option, - /// Proofs - /// - /// Proofs separated by keyset_id + /// Proofs grouped by keyset_id #[serde(rename = "t")] pub token: Vec, } @@ -319,8 +317,8 @@ impl TokenV4 { } #[inline] - fn unit(&self) -> &Option { - &self.unit + fn unit(&self) -> CurrencyUnit { + self.unit } } @@ -374,7 +372,7 @@ impl TryFrom for TokenV4 { mint_url: mint_url.to_owned(), token: proofs, memo: token.memo, - unit: token.unit, + unit: token.unit.ok_or(Error::UnsupportedUnit)?, }) } } @@ -469,8 +467,7 @@ mod tests { assert_eq!(amount, Amount::from(4)); - let unit = (*token.unit()).unwrap(); - + let unit = token.unit().unwrap(); assert_eq!(CurrencyUnit::Sat, unit); match token { diff --git a/crates/cdk/src/wallet/mint.rs b/crates/cdk/src/wallet/mint.rs index 3717edad..b429daaf 100644 --- a/crates/cdk/src/wallet/mint.rs +++ b/crates/cdk/src/wallet/mint.rs @@ -47,7 +47,6 @@ impl Wallet { let mint_url = self.mint_url.clone(); let unit = self.unit; - // If we have a description, we check that the mint supports it. // If we have a description, we check that the mint supports it. if description.is_some() { let mint_method_settings = self diff --git a/crates/cdk/src/wallet/send.rs b/crates/cdk/src/wallet/send.rs index 1e8b99fc..f9a62b9a 100644 --- a/crates/cdk/src/wallet/send.rs +++ b/crates/cdk/src/wallet/send.rs @@ -16,12 +16,7 @@ impl Wallet { let ys = proofs.ys()?; self.localstore.reserve_proofs(ys).await?; - Ok(Token::new( - self.mint_url.clone(), - proofs, - memo, - Some(self.unit), - )) + Ok(Token::new(self.mint_url.clone(), proofs, memo, self.unit)) } /// Send diff --git a/crates/cdk/src/wallet/types.rs b/crates/cdk/src/wallet/types.rs index 301c63ee..309a4c1c 100644 --- a/crates/cdk/src/wallet/types.rs +++ b/crates/cdk/src/wallet/types.rs @@ -54,8 +54,7 @@ pub enum SendKind { OnlineExact, /// Prefer offline send if difference is less then tolerance OnlineTolerance(Amount), - /// Wallet cannot do an online swap and selectedp proof must be exactly send - /// amount + /// Wallet cannot do an online swap and selected proof must be exactly send amount OfflineExact, /// Wallet must remain offline but can over pay if below tolerance OfflineTolerance(Amount),