wallet.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /// Quote ID if this is a mint or melt transaction
  185. pub quote_id: Option<String>,
  186. }
  187. impl Transaction {
  188. /// Transaction ID
  189. pub fn id(&self) -> TransactionId {
  190. TransactionId::new(self.ys.clone())
  191. }
  192. /// Check if transaction matches conditions
  193. pub fn matches_conditions(
  194. &self,
  195. mint_url: &Option<MintUrl>,
  196. direction: &Option<TransactionDirection>,
  197. unit: &Option<CurrencyUnit>,
  198. ) -> bool {
  199. if let Some(mint_url) = mint_url {
  200. if &self.mint_url != mint_url {
  201. return false;
  202. }
  203. }
  204. if let Some(direction) = direction {
  205. if &self.direction != direction {
  206. return false;
  207. }
  208. }
  209. if let Some(unit) = unit {
  210. if &self.unit != unit {
  211. return false;
  212. }
  213. }
  214. true
  215. }
  216. }
  217. impl PartialOrd for Transaction {
  218. fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
  219. Some(self.cmp(other))
  220. }
  221. }
  222. impl Ord for Transaction {
  223. fn cmp(&self, other: &Self) -> std::cmp::Ordering {
  224. self.timestamp.cmp(&other.timestamp).reverse()
  225. }
  226. }
  227. /// Transaction Direction
  228. #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
  229. pub enum TransactionDirection {
  230. /// Incoming transaction (i.e., receive or mint)
  231. Incoming,
  232. /// Outgoing transaction (i.e., send or melt)
  233. Outgoing,
  234. }
  235. impl std::fmt::Display for TransactionDirection {
  236. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  237. match self {
  238. TransactionDirection::Incoming => write!(f, "Incoming"),
  239. TransactionDirection::Outgoing => write!(f, "Outgoing"),
  240. }
  241. }
  242. }
  243. impl FromStr for TransactionDirection {
  244. type Err = Error;
  245. fn from_str(value: &str) -> Result<Self, Self::Err> {
  246. match value {
  247. "Incoming" => Ok(Self::Incoming),
  248. "Outgoing" => Ok(Self::Outgoing),
  249. _ => Err(Error::InvalidTransactionDirection),
  250. }
  251. }
  252. }
  253. /// Transaction ID
  254. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
  255. #[serde(transparent)]
  256. pub struct TransactionId([u8; 32]);
  257. impl TransactionId {
  258. /// Create new [`TransactionId`]
  259. pub fn new(ys: Vec<PublicKey>) -> Self {
  260. let mut ys = ys;
  261. ys.sort();
  262. let mut hasher = sha256::Hash::engine();
  263. for y in ys {
  264. hasher.input(&y.to_bytes());
  265. }
  266. let hash = sha256::Hash::from_engine(hasher);
  267. Self(hash.to_byte_array())
  268. }
  269. /// From proofs
  270. pub fn from_proofs(proofs: Proofs) -> Result<Self, nut00::Error> {
  271. let ys = proofs
  272. .iter()
  273. .map(|proof| proof.y())
  274. .collect::<Result<Vec<PublicKey>, nut00::Error>>()?;
  275. Ok(Self::new(ys))
  276. }
  277. /// From bytes
  278. pub fn from_bytes(bytes: [u8; 32]) -> Self {
  279. Self(bytes)
  280. }
  281. /// From hex string
  282. pub fn from_hex(value: &str) -> Result<Self, Error> {
  283. let bytes = hex::decode(value)?;
  284. if bytes.len() != 32 {
  285. return Err(Error::InvalidTransactionId);
  286. }
  287. let mut array = [0u8; 32];
  288. array.copy_from_slice(&bytes);
  289. Ok(Self(array))
  290. }
  291. /// From slice
  292. pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
  293. if slice.len() != 32 {
  294. return Err(Error::InvalidTransactionId);
  295. }
  296. let mut array = [0u8; 32];
  297. array.copy_from_slice(slice);
  298. Ok(Self(array))
  299. }
  300. /// Get inner value
  301. pub fn as_bytes(&self) -> &[u8; 32] {
  302. &self.0
  303. }
  304. /// Get inner value as slice
  305. pub fn as_slice(&self) -> &[u8] {
  306. &self.0
  307. }
  308. }
  309. impl std::fmt::Display for TransactionId {
  310. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  311. write!(f, "{}", hex::encode(self.0))
  312. }
  313. }
  314. impl FromStr for TransactionId {
  315. type Err = Error;
  316. fn from_str(value: &str) -> Result<Self, Self::Err> {
  317. Self::from_hex(value)
  318. }
  319. }
  320. impl TryFrom<Proofs> for TransactionId {
  321. type Error = nut00::Error;
  322. fn try_from(proofs: Proofs) -> Result<Self, Self::Error> {
  323. Self::from_proofs(proofs)
  324. }
  325. }
  326. #[cfg(test)]
  327. mod tests {
  328. use super::*;
  329. #[test]
  330. fn test_transaction_id_from_hex() {
  331. let hex_str = "a1b2c3d4e5f60718293a0b1c2d3e4f506172839a0b1c2d3e4f506172839a0b1c";
  332. let transaction_id = TransactionId::from_hex(hex_str).unwrap();
  333. assert_eq!(transaction_id.to_string(), hex_str);
  334. }
  335. #[test]
  336. fn test_transaction_id_from_hex_empty_string() {
  337. let hex_str = "";
  338. let res = TransactionId::from_hex(hex_str);
  339. assert!(matches!(res, Err(Error::InvalidTransactionId)));
  340. }
  341. #[test]
  342. fn test_transaction_id_from_hex_longer_string() {
  343. let hex_str = "a1b2c3d4e5f60718293a0b1c2d3e4f506172839a0b1c2d3e4f506172839a0b1ca1b2";
  344. let res = TransactionId::from_hex(hex_str);
  345. assert!(matches!(res, Err(Error::InvalidTransactionId)));
  346. }
  347. }