wallet.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //! Wallet Types
  2. use std::collections::HashMap;
  3. use std::fmt;
  4. use std::str::FromStr;
  5. use bitcoin::hashes::{sha256, Hash, HashEngine};
  6. use cashu::util::hex;
  7. use cashu::{nut00, PaymentMethod, Proofs, PublicKey};
  8. use serde::{Deserialize, Serialize};
  9. use crate::mint_url::MintUrl;
  10. use crate::nuts::{CurrencyUnit, MeltQuoteState, MintQuoteState, SecretKey};
  11. use crate::{Amount, Error};
  12. /// Wallet Key
  13. #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
  14. pub struct WalletKey {
  15. /// Mint Url
  16. pub mint_url: MintUrl,
  17. /// Currency Unit
  18. pub unit: CurrencyUnit,
  19. }
  20. impl fmt::Display for WalletKey {
  21. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  22. write!(f, "mint_url: {}, unit: {}", self.mint_url, self.unit,)
  23. }
  24. }
  25. impl WalletKey {
  26. /// Create new [`WalletKey`]
  27. pub fn new(mint_url: MintUrl, unit: CurrencyUnit) -> Self {
  28. Self { mint_url, unit }
  29. }
  30. }
  31. /// Mint Quote Info
  32. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  33. pub struct MintQuote {
  34. /// Quote id
  35. pub id: String,
  36. /// Mint Url
  37. pub mint_url: MintUrl,
  38. /// Payment method
  39. #[serde(default)]
  40. pub payment_method: PaymentMethod,
  41. /// Amount of quote
  42. pub amount: Option<Amount>,
  43. /// Unit of quote
  44. pub unit: CurrencyUnit,
  45. /// Quote payment request e.g. bolt11
  46. pub request: String,
  47. /// Quote state
  48. pub state: MintQuoteState,
  49. /// Expiration time of quote
  50. pub expiry: u64,
  51. /// Secretkey for signing mint quotes [NUT-20]
  52. pub secret_key: Option<SecretKey>,
  53. /// Amount minted
  54. #[serde(default)]
  55. pub amount_issued: Amount,
  56. /// Amount paid to the mint for the quote
  57. #[serde(default)]
  58. pub amount_paid: Amount,
  59. }
  60. /// Melt Quote Info
  61. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  62. pub struct MeltQuote {
  63. /// Quote id
  64. pub id: String,
  65. /// Quote unit
  66. pub unit: CurrencyUnit,
  67. /// Quote amount
  68. pub amount: Amount,
  69. /// Quote Payment request e.g. bolt11
  70. pub request: String,
  71. /// Quote fee reserve
  72. pub fee_reserve: Amount,
  73. /// Quote state
  74. pub state: MeltQuoteState,
  75. /// Expiration time of quote
  76. pub expiry: u64,
  77. /// Payment preimage
  78. pub payment_preimage: Option<String>,
  79. /// Payment method
  80. #[serde(default)]
  81. pub payment_method: PaymentMethod,
  82. }
  83. impl MintQuote {
  84. /// Create a new MintQuote
  85. #[allow(clippy::too_many_arguments)]
  86. pub fn new(
  87. id: String,
  88. mint_url: MintUrl,
  89. payment_method: PaymentMethod,
  90. amount: Option<Amount>,
  91. unit: CurrencyUnit,
  92. request: String,
  93. expiry: u64,
  94. secret_key: Option<SecretKey>,
  95. ) -> Self {
  96. Self {
  97. id,
  98. mint_url,
  99. payment_method,
  100. amount,
  101. unit,
  102. request,
  103. state: MintQuoteState::Unpaid,
  104. expiry,
  105. secret_key,
  106. amount_issued: Amount::ZERO,
  107. amount_paid: Amount::ZERO,
  108. }
  109. }
  110. /// Calculate the total amount including any fees
  111. pub fn total_amount(&self) -> Amount {
  112. self.amount_paid
  113. }
  114. /// Check if the quote has expired
  115. pub fn is_expired(&self, current_time: u64) -> bool {
  116. current_time > self.expiry
  117. }
  118. /// Amount that can be minted
  119. pub fn amount_mintable(&self) -> Amount {
  120. if self.amount_issued > self.amount_paid {
  121. return Amount::ZERO;
  122. }
  123. let difference = self.amount_paid - self.amount_issued;
  124. if difference == Amount::ZERO && self.state != MintQuoteState::Issued {
  125. if let Some(amount) = self.amount {
  126. return amount;
  127. }
  128. }
  129. difference
  130. }
  131. }
  132. /// Send Kind
  133. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default, Serialize, Deserialize)]
  134. pub enum SendKind {
  135. #[default]
  136. /// Allow online swap before send if wallet does not have exact amount
  137. OnlineExact,
  138. /// Prefer offline send if difference is less then tolerance
  139. OnlineTolerance(Amount),
  140. /// Wallet cannot do an online swap and selected proof must be exactly send amount
  141. OfflineExact,
  142. /// Wallet must remain offline but can over pay if below tolerance
  143. OfflineTolerance(Amount),
  144. }
  145. impl SendKind {
  146. /// Check if send kind is online
  147. pub fn is_online(&self) -> bool {
  148. matches!(self, Self::OnlineExact | Self::OnlineTolerance(_))
  149. }
  150. /// Check if send kind is offline
  151. pub fn is_offline(&self) -> bool {
  152. matches!(self, Self::OfflineExact | Self::OfflineTolerance(_))
  153. }
  154. /// Check if send kind is exact
  155. pub fn is_exact(&self) -> bool {
  156. matches!(self, Self::OnlineExact | Self::OfflineExact)
  157. }
  158. /// Check if send kind has tolerance
  159. pub fn has_tolerance(&self) -> bool {
  160. matches!(self, Self::OnlineTolerance(_) | Self::OfflineTolerance(_))
  161. }
  162. }
  163. /// Wallet Transaction
  164. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
  165. pub struct Transaction {
  166. /// Mint Url
  167. pub mint_url: MintUrl,
  168. /// Transaction direction
  169. pub direction: TransactionDirection,
  170. /// Amount
  171. pub amount: Amount,
  172. /// Fee
  173. pub fee: Amount,
  174. /// Currency Unit
  175. pub unit: CurrencyUnit,
  176. /// Proof Ys
  177. pub ys: Vec<PublicKey>,
  178. /// Unix timestamp
  179. pub timestamp: u64,
  180. /// Memo
  181. pub memo: Option<String>,
  182. /// User-defined metadata
  183. pub metadata: HashMap<String, String>,
  184. }
  185. impl Transaction {
  186. /// Transaction ID
  187. pub fn id(&self) -> TransactionId {
  188. TransactionId::new(self.ys.clone())
  189. }
  190. /// Check if transaction matches conditions
  191. pub fn matches_conditions(
  192. &self,
  193. mint_url: &Option<MintUrl>,
  194. direction: &Option<TransactionDirection>,
  195. unit: &Option<CurrencyUnit>,
  196. ) -> bool {
  197. if let Some(mint_url) = mint_url {
  198. if &self.mint_url != mint_url {
  199. return false;
  200. }
  201. }
  202. if let Some(direction) = direction {
  203. if &self.direction != direction {
  204. return false;
  205. }
  206. }
  207. if let Some(unit) = unit {
  208. if &self.unit != unit {
  209. return false;
  210. }
  211. }
  212. true
  213. }
  214. }
  215. impl PartialOrd for Transaction {
  216. fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
  217. Some(self.cmp(other))
  218. }
  219. }
  220. impl Ord for Transaction {
  221. fn cmp(&self, other: &Self) -> std::cmp::Ordering {
  222. self.timestamp.cmp(&other.timestamp).reverse()
  223. }
  224. }
  225. /// Transaction Direction
  226. #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
  227. pub enum TransactionDirection {
  228. /// Incoming transaction (i.e., receive or mint)
  229. Incoming,
  230. /// Outgoing transaction (i.e., send or melt)
  231. Outgoing,
  232. }
  233. impl std::fmt::Display for TransactionDirection {
  234. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  235. match self {
  236. TransactionDirection::Incoming => write!(f, "Incoming"),
  237. TransactionDirection::Outgoing => write!(f, "Outgoing"),
  238. }
  239. }
  240. }
  241. impl FromStr for TransactionDirection {
  242. type Err = Error;
  243. fn from_str(value: &str) -> Result<Self, Self::Err> {
  244. match value {
  245. "Incoming" => Ok(Self::Incoming),
  246. "Outgoing" => Ok(Self::Outgoing),
  247. _ => Err(Error::InvalidTransactionDirection),
  248. }
  249. }
  250. }
  251. /// Transaction ID
  252. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
  253. #[serde(transparent)]
  254. pub struct TransactionId([u8; 32]);
  255. impl TransactionId {
  256. /// Create new [`TransactionId`]
  257. pub fn new(ys: Vec<PublicKey>) -> Self {
  258. let mut ys = ys;
  259. ys.sort();
  260. let mut hasher = sha256::Hash::engine();
  261. for y in ys {
  262. hasher.input(&y.to_bytes());
  263. }
  264. let hash = sha256::Hash::from_engine(hasher);
  265. Self(hash.to_byte_array())
  266. }
  267. /// From proofs
  268. pub fn from_proofs(proofs: Proofs) -> Result<Self, nut00::Error> {
  269. let ys = proofs
  270. .iter()
  271. .map(|proof| proof.y())
  272. .collect::<Result<Vec<PublicKey>, nut00::Error>>()?;
  273. Ok(Self::new(ys))
  274. }
  275. /// From bytes
  276. pub fn from_bytes(bytes: [u8; 32]) -> Self {
  277. Self(bytes)
  278. }
  279. /// From hex string
  280. pub fn from_hex(value: &str) -> Result<Self, Error> {
  281. let bytes = hex::decode(value)?;
  282. if bytes.len() != 32 {
  283. return Err(Error::InvalidTransactionId);
  284. }
  285. let mut array = [0u8; 32];
  286. array.copy_from_slice(&bytes);
  287. Ok(Self(array))
  288. }
  289. /// From slice
  290. pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
  291. if slice.len() != 32 {
  292. return Err(Error::InvalidTransactionId);
  293. }
  294. let mut array = [0u8; 32];
  295. array.copy_from_slice(slice);
  296. Ok(Self(array))
  297. }
  298. /// Get inner value
  299. pub fn as_bytes(&self) -> &[u8; 32] {
  300. &self.0
  301. }
  302. /// Get inner value as slice
  303. pub fn as_slice(&self) -> &[u8] {
  304. &self.0
  305. }
  306. }
  307. impl std::fmt::Display for TransactionId {
  308. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  309. write!(f, "{}", hex::encode(self.0))
  310. }
  311. }
  312. impl FromStr for TransactionId {
  313. type Err = Error;
  314. fn from_str(value: &str) -> Result<Self, Self::Err> {
  315. Self::from_hex(value)
  316. }
  317. }
  318. impl TryFrom<Proofs> for TransactionId {
  319. type Error = nut00::Error;
  320. fn try_from(proofs: Proofs) -> Result<Self, Self::Error> {
  321. Self::from_proofs(proofs)
  322. }
  323. }
  324. #[cfg(test)]
  325. mod tests {
  326. use super::*;
  327. #[test]
  328. fn test_transaction_id_from_hex() {
  329. let hex_str = "a1b2c3d4e5f60718293a0b1c2d3e4f506172839a0b1c2d3e4f506172839a0b1c";
  330. let transaction_id = TransactionId::from_hex(hex_str).unwrap();
  331. assert_eq!(transaction_id.to_string(), hex_str);
  332. }
  333. #[test]
  334. fn test_transaction_id_from_hex_empty_string() {
  335. let hex_str = "";
  336. let res = TransactionId::from_hex(hex_str);
  337. assert!(matches!(res, Err(Error::InvalidTransactionId)));
  338. }
  339. #[test]
  340. fn test_transaction_id_from_hex_longer_string() {
  341. let hex_str = "a1b2c3d4e5f60718293a0b1c2d3e4f506172839a0b1c2d3e4f506172839a0b1ca1b2";
  342. let res = TransactionId::from_hex(hex_str);
  343. assert!(matches!(res, Err(Error::InvalidTransactionId)));
  344. }
  345. }