Bladeren bron

refactor: add unit to token

thesimplekid 1 jaar geleden
bovenliggende
commit
834834fd46

+ 1 - 1
bindings/cashu-ffi/src/cashu.udl

@@ -88,7 +88,7 @@ interface MintProofs {
 
 interface Token {
     [Throws=CashuError]
-	constructor(string mint, sequence<Proof> token, string? memo);
+	constructor(string mint, sequence<Proof> token, string? unit, string? memo);
 	sequence<MintProofs> token();
 	string? memo();
     [Throws=CashuError]

+ 1 - 1
bindings/cashu-ffi/src/nuts/nut00/proof.rs

@@ -113,7 +113,7 @@ pub mod mint {
         }
 
         pub fn id(&self) -> Option<Arc<Id>> {
-            self.inner.id.clone().map(|id| Arc::new(id.into()))
+            self.inner.id.map(|id| Arc::new(id.into()))
         }
     }
 

+ 7 - 2
bindings/cashu-ffi/src/nuts/nut00/token.rs

@@ -12,11 +12,16 @@ pub struct Token {
 }
 
 impl Token {
-    pub fn new(mint: String, proofs: Vec<Arc<Proof>>, memo: Option<String>) -> Result<Self> {
+    pub fn new(
+        mint: String,
+        proofs: Vec<Arc<Proof>>,
+        unit: Option<String>,
+        memo: Option<String>,
+    ) -> Result<Self> {
         let mint = UncheckedUrl::from_str(&mint)?;
         let proofs = proofs.into_iter().map(|p| p.as_ref().into()).collect();
         Ok(Self {
-            inner: TokenSdk::new(mint, proofs, memo)?,
+            inner: TokenSdk::new(mint, proofs, unit, memo)?,
         })
     }
 

+ 7 - 2
bindings/cashu-js/src/nuts/nut00/token.rs

@@ -28,11 +28,16 @@ impl From<Token> for JsToken {
 #[wasm_bindgen(js_class = Token)]
 impl JsToken {
     #[wasm_bindgen(constructor)]
-    pub fn new(mint: String, proofs: JsValue, memo: Option<String>) -> Result<JsToken> {
+    pub fn new(
+        mint: String,
+        proofs: JsValue,
+        unit: Option<String>,
+        memo: Option<String>,
+    ) -> Result<JsToken> {
         let mint = UncheckedUrl::from_str(&mint).map_err(into_err)?;
         let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?;
         Ok(Self {
-            inner: Token::new(mint, proofs, memo).map_err(into_err)?,
+            inner: Token::new(mint, proofs, unit, memo).map_err(into_err)?,
         })
     }
 

+ 1 - 1
bindings/cashu-sdk-ffi/src/cashu_sdk.udl

@@ -91,7 +91,7 @@ interface MintProofs {
 
 interface Token {
     [Throws=CashuError]
-	constructor(string mint, sequence<Proof> token, string? memo);
+	constructor(string mint, sequence<Proof> token, string? unit, string? memo);
 	sequence<MintProofs> token();
 	string? memo();
     [Throws=CashuError]

+ 17 - 4
bindings/cashu-sdk-js/src/wallet.rs

@@ -68,10 +68,16 @@ impl JsWallet {
 
     /// Mint Token
     #[wasm_bindgen(js_name = mintToken)]
-    pub async fn mint_token(&self, amount: JsAmount, hash: String) -> Result<JsToken> {
+    pub async fn mint_token(
+        &self,
+        amount: JsAmount,
+        hash: String,
+        unit: Option<String>,
+        memo: Option<String>,
+    ) -> Result<JsToken> {
         Ok(self
             .inner
-            .mint_token(*amount.deref(), &hash)
+            .mint_token(*amount.deref(), &hash, unit, memo)
             .await
             .map_err(into_err)?
             .into())
@@ -159,9 +165,16 @@ impl JsWallet {
 
     /// Proofs to token
     #[wasm_bindgen(js_name = proofsToToken)]
-    pub fn proofs_to_token(&self, proofs: JsValue, memo: Option<String>) -> Result<String> {
+    pub fn proofs_to_token(
+        &self,
+        proofs: JsValue,
+        unit: Option<String>,
+        memo: Option<String>,
+    ) -> Result<String> {
         let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?;
 
-        self.inner.proofs_to_token(proofs, memo).map_err(into_err)
+        self.inner
+            .proofs_to_token(proofs, unit, memo)
+            .map_err(into_err)
     }
 }

+ 17 - 4
crates/cashu-sdk/src/wallet.rs

@@ -89,10 +89,18 @@ impl<C: Client> Wallet<C> {
             .await?)
     }
 
-    pub async fn mint_token(&self, amount: Amount, hash: &str) -> Result<Token, Error> {
+    // TODO: Need to use the unit, check keyset is of the same unit of attempting to
+    // mint
+    pub async fn mint_token(
+        &self,
+        amount: Amount,
+        hash: &str,
+        unit: Option<String>,
+        memo: Option<String>,
+    ) -> Result<Token, Error> {
         let proofs = self.mint(amount, hash).await?;
 
-        let token = Token::new(self.mint_url.clone(), proofs, None);
+        let token = Token::new(self.mint_url.clone(), proofs, unit, memo);
         Ok(token?)
     }
 
@@ -338,8 +346,13 @@ impl<C: Client> Wallet<C> {
         Ok(melted)
     }
 
-    pub fn proofs_to_token(&self, proofs: Proofs, memo: Option<String>) -> Result<String, Error> {
-        Ok(Token::new(self.mint_url.clone(), proofs, memo)?.convert_to_string()?)
+    pub fn proofs_to_token(
+        &self,
+        proofs: Proofs,
+        unit: Option<String>,
+        memo: Option<String>,
+    ) -> Result<String, Error> {
+        Ok(Token::new(self.mint_url.clone(), proofs, unit, memo)?.convert_to_string()?)
     }
 }
 

+ 8 - 0
crates/cashu/src/nuts/nut00.rs

@@ -116,13 +116,19 @@ pub mod wallet {
     #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
     pub struct Token {
         pub token: Vec<MintProofs>,
+        /// Memo for token
+        #[serde(skip_serializing_if = "Option::is_none")]
         pub memo: Option<String>,
+        #[serde(skip_serializing_if = "Option::is_none")]
+        /// Unit of the token eg: sats, usd
+        pub unit: Option<String>,
     }
 
     impl Token {
         pub fn new(
             mint_url: UncheckedUrl,
             proofs: Proofs,
+            unit: Option<String>,
             memo: Option<String>,
         ) -> Result<Self, wallet::Error> {
             if proofs.is_empty() {
@@ -135,6 +141,7 @@ pub mod wallet {
             Ok(Self {
                 token: vec![MintProofs::new(mint_url, proofs)],
                 memo,
+                unit,
             })
         }
 
@@ -316,6 +323,7 @@ mod tests {
             UncheckedUrl::from_str("https://localhost:5000/cashu").unwrap(),
             proof,
             None,
+            None,
         )
         .unwrap();