فهرست منبع

dynamic change tokens

thesimplekid 1 سال پیش
والد
کامیت
97e22286b1
3فایلهای تغییر یافته به همراه30 افزوده شده و 7 حذف شده
  1. 10 3
      integration_test/src/main.rs
  2. 7 2
      src/cashu_wallet.rs
  3. 13 2
      src/types.rs

+ 10 - 3
integration_test/src/main.rs

@@ -40,7 +40,14 @@ async fn main() {
     let spendable = test_check_spendable(&client, &new_token).await;
     let proofs = test_send(&wallet, spendable).await;
 
-    test_melt(&wallet, Invoice::from_str(MELTINVOICE).unwrap(), proofs).await;
+    test_melt(
+        &wallet,
+        Invoice::from_str(MELTINVOICE).unwrap(),
+        proofs,
+        // TODO:
+        10,
+    )
+    .await;
 
     test_check_fees(&client).await;
 }
@@ -152,8 +159,8 @@ async fn test_send(wallet: &CashuWallet, proofs: Proofs) -> Proofs {
     send.send_proofs
 }
 
-async fn test_melt(wallet: &CashuWallet, invoice: Invoice, proofs: Proofs) {
-    let res = wallet.melt(invoice, proofs).await.unwrap();
+async fn test_melt(wallet: &CashuWallet, invoice: Invoice, proofs: Proofs, fee_reserve: u64) {
+    let res = wallet.melt(invoice, proofs, fee_reserve).await.unwrap();
 
     println!("{:?}", res);
 }

+ 7 - 2
src/cashu_wallet.rs

@@ -220,8 +220,13 @@ impl CashuWallet {
         })
     }
 
-    pub async fn melt(&self, invoice: Invoice, proofs: Proofs) -> Result<Melted, Error> {
-        let change = BlindedMessages::blank()?;
+    pub async fn melt(
+        &self,
+        invoice: Invoice,
+        proofs: Proofs,
+        fee_reserve: u64,
+    ) -> Result<Melted, Error> {
+        let change = BlindedMessages::blank(fee_reserve)?;
         let melt_response = self
             .client
             .melt(proofs, invoice, Some(change.blinded_messages))

+ 13 - 2
src/types.rs

@@ -60,10 +60,12 @@ impl BlindedMessages {
     }
 
     /// Blank Outputs used for NUT-08 change
-    pub fn blank() -> Result<Self, Error> {
+    pub fn blank(fee_reserve: u64) -> Result<Self, Error> {
         let mut blinded_messages = BlindedMessages::default();
 
-        for _i in 0..4 {
+        let count = ((fee_reserve as f64).log2().ceil() as u64).max(1);
+
+        for _i in 0..count {
             let secret = generate_secret();
             let (blinded, r) = blind_message(secret.as_bytes(), None)?;
 
@@ -385,4 +387,13 @@ mod tests {
 
         assert_eq!(token_data, token);
     }
+
+    #[test]
+    fn test_blank_blinded_messages() {
+        let b = BlindedMessages::blank(1000).unwrap();
+        assert_eq!(b.blinded_messages.len(), 10);
+
+        let b = BlindedMessages::blank(1).unwrap();
+        assert_eq!(b.blinded_messages.len(), 1);
+    }
 }