token.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use std::str::FromStr;
  2. use std::sync::Arc;
  3. use cashu::nuts::nut00::wallet::Token as TokenSdk;
  4. use cashu::url::UncheckedUrl;
  5. use crate::error::Result;
  6. use crate::MintProofs;
  7. use crate::Proof;
  8. pub struct Token {
  9. inner: TokenSdk,
  10. }
  11. impl Token {
  12. pub fn new(mint: String, proofs: Vec<Arc<Proof>>, memo: Option<String>) -> Result<Self> {
  13. let mint = UncheckedUrl::from_str(&mint)?;
  14. let proofs = proofs.into_iter().map(|p| p.as_ref().into()).collect();
  15. Ok(Self {
  16. inner: TokenSdk::new(mint, proofs, memo)?,
  17. })
  18. }
  19. pub fn token(&self) -> Vec<Arc<MintProofs>> {
  20. self.inner
  21. .token
  22. .clone()
  23. .into_iter()
  24. .map(|p| Arc::new(p.into()))
  25. .collect()
  26. }
  27. pub fn memo(&self) -> Option<String> {
  28. self.inner.memo.clone()
  29. }
  30. pub fn from_string(token: String) -> Result<Self> {
  31. Ok(Self {
  32. inner: TokenSdk::from_str(&token)?,
  33. })
  34. }
  35. pub fn as_string(&self) -> Result<String> {
  36. Ok(self.inner.convert_to_string()?)
  37. }
  38. }
  39. impl From<cashu::nuts::nut00::wallet::Token> for Token {
  40. fn from(inner: cashu::nuts::nut00::wallet::Token) -> Token {
  41. Token { inner }
  42. }
  43. }