wallet.rs 10 KB

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