ソースを参照

TokenV4: unit is mandatory

ok300 4 ヶ月 前
コミット
419b1a360d

+ 13 - 16
crates/cdk/src/nuts/nut00/token.rs

@@ -43,7 +43,7 @@ impl Token {
         mint_url: MintUrl,
         proofs: Proofs,
         memo: Option<String>,
-        unit: Option<CurrencyUnit>,
+        unit: CurrencyUnit,
     ) -> Self {
         let proofs = proofs
             .into_iter()
@@ -90,10 +90,10 @@ impl Token {
     }
 
     /// Unit
-    pub fn unit(&self) -> &Option<CurrencyUnit> {
+    pub fn unit(&self) -> Option<CurrencyUnit> {
         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<CurrencyUnit> {
-        &self.unit
+    fn unit(&self) -> Option<CurrencyUnit> {
+        self.unit
     }
 }
 
@@ -257,7 +257,7 @@ impl From<TokenV4> 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<CurrencyUnit>,
+    #[serde(rename = "u")]
+    pub unit: CurrencyUnit,
     /// Memo for token
     #[serde(rename = "d", skip_serializing_if = "Option::is_none")]
     pub memo: Option<String>,
-    /// Proofs
-    ///
-    /// Proofs separated by keyset_id
+    /// Proofs grouped by keyset_id
     #[serde(rename = "t")]
     pub token: Vec<TokenV4Token>,
 }
@@ -319,8 +317,8 @@ impl TokenV4 {
     }
 
     #[inline]
-    fn unit(&self) -> &Option<CurrencyUnit> {
-        &self.unit
+    fn unit(&self) -> CurrencyUnit {
+        self.unit
     }
 }
 
@@ -374,7 +372,7 @@ impl TryFrom<TokenV3> 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 {

+ 0 - 1
crates/cdk/src/wallet/mint.rs

@@ -48,7 +48,6 @@ impl Wallet {
         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
                 .localstore

+ 1 - 6
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

+ 1 - 2
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),