token.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //! FFI token bindings
  2. use std::collections::BTreeSet;
  3. use std::str::FromStr;
  4. use crate::error::FfiError;
  5. use crate::{Amount, CurrencyUnit, MintUrl, Proofs};
  6. /// FFI-compatible Token
  7. #[derive(Debug, uniffi::Object)]
  8. pub struct Token {
  9. pub(crate) inner: cdk::nuts::Token,
  10. }
  11. impl std::fmt::Display for Token {
  12. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  13. write!(f, "{}", self.inner)
  14. }
  15. }
  16. impl FromStr for Token {
  17. type Err = FfiError;
  18. fn from_str(s: &str) -> Result<Self, Self::Err> {
  19. let token = cdk::nuts::Token::from_str(s)
  20. .map_err(|e| FfiError::InvalidToken { msg: e.to_string() })?;
  21. Ok(Token { inner: token })
  22. }
  23. }
  24. impl From<cdk::nuts::Token> for Token {
  25. fn from(token: cdk::nuts::Token) -> Self {
  26. Self { inner: token }
  27. }
  28. }
  29. impl From<Token> for cdk::nuts::Token {
  30. fn from(token: Token) -> Self {
  31. token.inner
  32. }
  33. }
  34. #[uniffi::export]
  35. impl Token {
  36. /// Create a new Token from string
  37. #[uniffi::constructor]
  38. pub fn from_string(encoded_token: String) -> Result<Token, FfiError> {
  39. let token = cdk::nuts::Token::from_str(&encoded_token)
  40. .map_err(|e| FfiError::InvalidToken { msg: e.to_string() })?;
  41. Ok(Token { inner: token })
  42. }
  43. /// Get the total value of the token
  44. pub fn value(&self) -> Result<Amount, FfiError> {
  45. Ok(self.inner.value()?.into())
  46. }
  47. /// Get the memo from the token
  48. pub fn memo(&self) -> Option<String> {
  49. self.inner.memo().clone()
  50. }
  51. /// Get the currency unit
  52. pub fn unit(&self) -> Option<CurrencyUnit> {
  53. self.inner.unit().map(Into::into)
  54. }
  55. /// Get the mint URL
  56. pub fn mint_url(&self) -> Result<MintUrl, FfiError> {
  57. Ok(self.inner.mint_url()?.into())
  58. }
  59. /// Get proofs from the token (simplified - no keyset filtering for now)
  60. pub fn proofs_simple(&self) -> Result<Proofs, FfiError> {
  61. // For now, return empty keysets to get all proofs
  62. let empty_keysets = vec![];
  63. let proofs = self.inner.proofs(&empty_keysets)?;
  64. Ok(proofs.into_iter().map(|p| p.into()).collect())
  65. }
  66. /// Convert token to raw bytes
  67. pub fn to_raw_bytes(&self) -> Result<Vec<u8>, FfiError> {
  68. Ok(self.inner.to_raw_bytes()?)
  69. }
  70. /// Encode token to string representation
  71. pub fn encode(&self) -> String {
  72. self.to_string()
  73. }
  74. /// Decode token from string representation
  75. #[uniffi::constructor]
  76. pub fn decode(encoded_token: String) -> Result<Token, FfiError> {
  77. encoded_token.parse()
  78. }
  79. /// Return unique spending conditions across all proofs in this token
  80. pub fn spending_conditions(&self) -> Vec<crate::types::SpendingConditions> {
  81. self.inner
  82. .spending_conditions()
  83. .map(|set| set.into_iter().map(Into::into).collect())
  84. .unwrap_or_default()
  85. }
  86. /// Return all P2PK pubkeys referenced by this token's spending conditions
  87. pub fn p2pk_pubkeys(&self) -> Vec<String> {
  88. let set = self
  89. .inner
  90. .p2pk_pubkeys()
  91. .map(|keys| {
  92. keys.into_iter()
  93. .map(|k| k.to_string())
  94. .collect::<BTreeSet<_>>()
  95. })
  96. .unwrap_or_default();
  97. set.into_iter().collect()
  98. }
  99. /// Return all refund pubkeys from P2PK spending conditions
  100. pub fn p2pk_refund_pubkeys(&self) -> Vec<String> {
  101. let set = self
  102. .inner
  103. .p2pk_refund_pubkeys()
  104. .map(|keys| {
  105. keys.into_iter()
  106. .map(|k| k.to_string())
  107. .collect::<BTreeSet<_>>()
  108. })
  109. .unwrap_or_default();
  110. set.into_iter().collect()
  111. }
  112. /// Return all HTLC hashes from spending conditions
  113. pub fn htlc_hashes(&self) -> Vec<String> {
  114. let set = self
  115. .inner
  116. .htlc_hashes()
  117. .map(|hashes| {
  118. hashes
  119. .into_iter()
  120. .map(|h| h.to_string())
  121. .collect::<BTreeSet<_>>()
  122. })
  123. .unwrap_or_default();
  124. set.into_iter().collect()
  125. }
  126. /// Return all locktimes from spending conditions (sorted ascending)
  127. pub fn locktimes(&self) -> Vec<u64> {
  128. self.inner
  129. .locktimes()
  130. .map(|s| s.into_iter().collect())
  131. .unwrap_or_default()
  132. }
  133. }