wallet.rs 11 KB

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