wallet.rs 10 KB

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