From 25bc15d5bff77feb91f2c8d404445778d9bd4cee Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Thu, 8 Feb 2024 17:29:51 +0100 Subject: [PATCH] chore: cleanup --- moksha-core/src/token.rs | 10 ++++++++++ moksha-wallet/examples/receive_tokens.rs | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/moksha-core/src/token.rs b/moksha-core/src/token.rs index b48fc930..76c0fb84 100644 --- a/moksha-core/src/token.rs +++ b/moksha-core/src/token.rs @@ -2,6 +2,8 @@ //! //! The `Token` struct represents a token, with an optional `mint` field for the URL of the Mint and a `proofs` field for the proofs associated with the token. +use std::str::FromStr; + use base64::{engine::general_purpose, Engine as _}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde_with::skip_serializing_none; @@ -138,6 +140,14 @@ impl TryFrom for TokenV3 { } } +impl FromStr for TokenV3 { + type Err = MokshaCoreError; + + fn from_str(s: &str) -> Result { + Self::deserialize(s) + } +} + impl From<(Url, Proofs)> for TokenV3 { fn from(from: (Url, Proofs)) -> Self { Self { diff --git a/moksha-wallet/examples/receive_tokens.rs b/moksha-wallet/examples/receive_tokens.rs index 470f2ab0..63f97c65 100644 --- a/moksha-wallet/examples/receive_tokens.rs +++ b/moksha-wallet/examples/receive_tokens.rs @@ -6,6 +6,7 @@ use moksha_wallet::{ localstore::sqlite::SqliteLocalStore, wallet::{Wallet, WalletBuilder}, }; +use std::str::FromStr; use url::Url; #[tokio::main] @@ -18,7 +19,7 @@ async fn main() -> anyhow::Result<()> { .with_mint_url(Url::parse("https://mint.mutinynet.moksha.cash")?) .build() .await?; - let tokens = TokenV3::deserialize("cashuAeyJ0b2tlbiI6IFt7InByb29mcyI6IFt7ImlkIjogIjAwOTkxZjRmMjc3MzMzOGMiLCAiYW1vdW50IjogMiwgInNlY3JldCI6ICI5ZmFjZWE0Y2QzN2I3ZWRlOGE4NmQzYWY1ZWIxZTczNzIxMDNmZDE2YTQ1M2E5NDQ5YjE0MDFkZDhhMzAzMWJiIiwgIkMiOiAiMDM2ZTVhOWJhOWE1ZjYxZmQ5MTk3YzM2OTgzZjc1YzAzYTUyYzc0YTJmZmM2NTBmNzg5MjJlMDcyZWY1MTI0YjZlIn1dLCAibWludCI6ICJodHRwczovL21pbnQubXV0aW55bmV0Lm1va3NoYS5jYXNoOjMzMzgifV19")?; + let tokens = TokenV3::from_str("cashuAeyJ0b2tlbiI6IFt7InByb29mcyI6IFt7ImlkIjogIjAwOTkxZjRmMjc3MzMzOGMiLCAiYW1vdW50IjogMiwgInNlY3JldCI6ICI5ZmFjZWE0Y2QzN2I3ZWRlOGE4NmQzYWY1ZWIxZTczNzIxMDNmZDE2YTQ1M2E5NDQ5YjE0MDFkZDhhMzAzMWJiIiwgIkMiOiAiMDM2ZTVhOWJhOWE1ZjYxZmQ5MTk3YzM2OTgzZjc1YzAzYTUyYzc0YTJmZmM2NTBmNzg5MjJlMDcyZWY1MTI0YjZlIn1dLCAibWludCI6ICJodHRwczovL21pbnQubXV0aW55bmV0Lm1va3NoYS5jYXNoOjMzMzgifV19")?; wallet.receive_tokens(&tokens).await?; let balance = wallet.get_balance().await?; println!("New balance: {} sats", balance);