nut06.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //! NUT-06: Mint Information
  2. //!
  3. //! <https://github.com/cashubtc/nuts/blob/main/06.md>
  4. #[cfg(feature = "auth")]
  5. use std::collections::HashMap;
  6. use std::collections::HashSet;
  7. use serde::{Deserialize, Deserializer, Serialize, Serializer};
  8. use super::nut01::PublicKey;
  9. use super::nut17::SupportedMethods;
  10. use super::nut19::CachedEndpoint;
  11. use super::{nut04, nut05, nut15, nut19, MppMethodSettings};
  12. #[cfg(feature = "auth")]
  13. use super::{AuthRequired, BlindAuthSettings, ClearAuthSettings, ProtectedEndpoint};
  14. use crate::CurrencyUnit;
  15. /// Mint Version
  16. #[derive(Debug, Clone, PartialEq, Eq, Hash)]
  17. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  18. pub struct MintVersion {
  19. /// Mint Software name
  20. pub name: String,
  21. /// Mint Version
  22. pub version: String,
  23. }
  24. impl MintVersion {
  25. /// Create new [`MintVersion`]
  26. pub fn new(name: String, version: String) -> Self {
  27. Self { name, version }
  28. }
  29. }
  30. impl std::fmt::Display for MintVersion {
  31. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  32. write!(f, "{}/{}", self.name, self.version)
  33. }
  34. }
  35. impl Serialize for MintVersion {
  36. fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  37. where
  38. S: Serializer,
  39. {
  40. let combined = format!("{}/{}", self.name, self.version);
  41. serializer.serialize_str(&combined)
  42. }
  43. }
  44. impl<'de> Deserialize<'de> for MintVersion {
  45. fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  46. where
  47. D: Deserializer<'de>,
  48. {
  49. let combined = String::deserialize(deserializer)?;
  50. let parts: Vec<&str> = combined.split('/').collect();
  51. if parts.len() != 2 {
  52. return Err(serde::de::Error::custom("Invalid input string"));
  53. }
  54. Ok(MintVersion {
  55. name: parts[0].to_string(),
  56. version: parts[1].to_string(),
  57. })
  58. }
  59. }
  60. /// Mint Info [NUT-06]
  61. #[derive(Default, Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
  62. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  63. pub struct MintInfo {
  64. /// name of the mint and should be recognizable
  65. #[serde(skip_serializing_if = "Option::is_none")]
  66. pub name: Option<String>,
  67. /// hex pubkey of the mint
  68. #[serde(skip_serializing_if = "Option::is_none")]
  69. pub pubkey: Option<PublicKey>,
  70. /// implementation name and the version running
  71. #[serde(skip_serializing_if = "Option::is_none")]
  72. pub version: Option<MintVersion>,
  73. /// short description of the mint
  74. #[serde(skip_serializing_if = "Option::is_none")]
  75. pub description: Option<String>,
  76. /// long description
  77. #[serde(skip_serializing_if = "Option::is_none")]
  78. pub description_long: Option<String>,
  79. /// Contact info
  80. #[serde(skip_serializing_if = "Option::is_none")]
  81. pub contact: Option<Vec<ContactInfo>>,
  82. /// shows which NUTs the mint supports
  83. pub nuts: Nuts,
  84. /// Mint's icon URL
  85. #[serde(skip_serializing_if = "Option::is_none")]
  86. pub icon_url: Option<String>,
  87. /// Mint's endpoint URLs
  88. #[serde(skip_serializing_if = "Option::is_none")]
  89. pub urls: Option<Vec<String>>,
  90. /// message of the day that the wallet must display to the user
  91. #[serde(skip_serializing_if = "Option::is_none")]
  92. pub motd: Option<String>,
  93. /// server unix timestamp
  94. #[serde(skip_serializing_if = "Option::is_none")]
  95. pub time: Option<u64>,
  96. /// terms of url service of the mint
  97. #[serde(skip_serializing_if = "Option::is_none")]
  98. pub tos_url: Option<String>,
  99. }
  100. impl MintInfo {
  101. /// Create new [`MintInfo`]
  102. pub fn new() -> Self {
  103. Self::default()
  104. }
  105. /// Set name
  106. pub fn name<S>(self, name: S) -> Self
  107. where
  108. S: Into<String>,
  109. {
  110. Self {
  111. name: Some(name.into()),
  112. ..self
  113. }
  114. }
  115. /// Set pubkey
  116. pub fn pubkey(self, pubkey: PublicKey) -> Self {
  117. Self {
  118. pubkey: Some(pubkey),
  119. ..self
  120. }
  121. }
  122. /// Set [`MintVersion`]
  123. pub fn version(self, mint_version: MintVersion) -> Self {
  124. Self {
  125. version: Some(mint_version),
  126. ..self
  127. }
  128. }
  129. /// Set description
  130. pub fn description<S>(self, description: S) -> Self
  131. where
  132. S: Into<String>,
  133. {
  134. Self {
  135. description: Some(description.into()),
  136. ..self
  137. }
  138. }
  139. /// Set long description
  140. pub fn long_description<S>(self, description_long: S) -> Self
  141. where
  142. S: Into<String>,
  143. {
  144. Self {
  145. description_long: Some(description_long.into()),
  146. ..self
  147. }
  148. }
  149. /// Set contact info
  150. pub fn contact_info(self, contact_info: Vec<ContactInfo>) -> Self {
  151. Self {
  152. contact: Some(contact_info),
  153. ..self
  154. }
  155. }
  156. /// Set nuts
  157. pub fn nuts(self, nuts: Nuts) -> Self {
  158. Self { nuts, ..self }
  159. }
  160. /// Set mint icon url
  161. pub fn icon_url<S>(self, icon_url: S) -> Self
  162. where
  163. S: Into<String>,
  164. {
  165. Self {
  166. icon_url: Some(icon_url.into()),
  167. ..self
  168. }
  169. }
  170. /// Set motd
  171. pub fn motd<S>(self, motd: S) -> Self
  172. where
  173. S: Into<String>,
  174. {
  175. Self {
  176. motd: Some(motd.into()),
  177. ..self
  178. }
  179. }
  180. /// Set time
  181. pub fn time<S>(self, time: S) -> Self
  182. where
  183. S: Into<u64>,
  184. {
  185. Self {
  186. time: Some(time.into()),
  187. ..self
  188. }
  189. }
  190. /// Set tos_url
  191. pub fn tos_url<S>(self, tos_url: S) -> Self
  192. where
  193. S: Into<String>,
  194. {
  195. Self {
  196. tos_url: Some(tos_url.into()),
  197. ..self
  198. }
  199. }
  200. /// Get protected endpoints
  201. #[cfg(feature = "auth")]
  202. pub fn protected_endpoints(&self) -> HashMap<ProtectedEndpoint, AuthRequired> {
  203. let mut protected_endpoints = HashMap::new();
  204. if let Some(nut21_settings) = &self.nuts.nut21 {
  205. for endpoint in nut21_settings.protected_endpoints.iter() {
  206. protected_endpoints.insert(*endpoint, AuthRequired::Clear);
  207. }
  208. }
  209. if let Some(nut22_settings) = &self.nuts.nut22 {
  210. for endpoint in nut22_settings.protected_endpoints.iter() {
  211. protected_endpoints.insert(*endpoint, AuthRequired::Blind);
  212. }
  213. }
  214. protected_endpoints
  215. }
  216. /// Get Openid discovery of the mint if it is set
  217. #[cfg(feature = "auth")]
  218. pub fn openid_discovery(&self) -> Option<String> {
  219. self.nuts
  220. .nut21
  221. .as_ref()
  222. .map(|s| s.openid_discovery.to_string())
  223. }
  224. /// Get Openid discovery of the mint if it is set
  225. #[cfg(feature = "auth")]
  226. pub fn client_id(&self) -> Option<String> {
  227. self.nuts.nut21.as_ref().map(|s| s.client_id.clone())
  228. }
  229. /// Max bat mint
  230. #[cfg(feature = "auth")]
  231. pub fn bat_max_mint(&self) -> Option<u64> {
  232. self.nuts.nut22.as_ref().map(|s| s.bat_max_mint)
  233. }
  234. /// Get all supported currency units for this mint (both mint and melt)
  235. pub fn supported_units(&self) -> Vec<&CurrencyUnit> {
  236. let mut units = HashSet::new();
  237. units.extend(self.nuts.supported_mint_units());
  238. units.extend(self.nuts.supported_melt_units());
  239. units.into_iter().collect()
  240. }
  241. }
  242. /// Supported nuts and settings
  243. #[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
  244. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  245. pub struct Nuts {
  246. /// NUT04 Settings
  247. #[serde(default)]
  248. #[serde(rename = "4")]
  249. pub nut04: nut04::Settings,
  250. /// NUT05 Settings
  251. #[serde(default)]
  252. #[serde(rename = "5")]
  253. pub nut05: nut05::Settings,
  254. /// NUT07 Settings
  255. #[serde(default)]
  256. #[serde(rename = "7")]
  257. pub nut07: SupportedSettings,
  258. /// NUT08 Settings
  259. #[serde(default)]
  260. #[serde(rename = "8")]
  261. pub nut08: SupportedSettings,
  262. /// NUT09 Settings
  263. #[serde(default)]
  264. #[serde(rename = "9")]
  265. pub nut09: SupportedSettings,
  266. /// NUT10 Settings
  267. #[serde(rename = "10")]
  268. #[serde(default)]
  269. pub nut10: SupportedSettings,
  270. /// NUT11 Settings
  271. #[serde(rename = "11")]
  272. #[serde(default)]
  273. pub nut11: SupportedSettings,
  274. /// NUT12 Settings
  275. #[serde(default)]
  276. #[serde(rename = "12")]
  277. pub nut12: SupportedSettings,
  278. /// NUT14 Settings
  279. #[serde(default)]
  280. #[serde(rename = "14")]
  281. pub nut14: SupportedSettings,
  282. /// NUT15 Settings
  283. #[serde(default)]
  284. #[serde(rename = "15")]
  285. pub nut15: nut15::Settings,
  286. /// NUT17 Settings
  287. #[serde(default)]
  288. #[serde(rename = "17")]
  289. pub nut17: super::nut17::SupportedSettings,
  290. /// NUT19 Settings
  291. #[serde(default)]
  292. #[serde(rename = "19")]
  293. pub nut19: nut19::Settings,
  294. /// NUT20 Settings
  295. #[serde(default)]
  296. #[serde(rename = "20")]
  297. pub nut20: SupportedSettings,
  298. /// NUT21 Settings
  299. #[serde(rename = "21")]
  300. #[serde(skip_serializing_if = "Option::is_none")]
  301. #[cfg(feature = "auth")]
  302. pub nut21: Option<ClearAuthSettings>,
  303. /// NUT22 Settings
  304. #[serde(rename = "22")]
  305. #[serde(skip_serializing_if = "Option::is_none")]
  306. #[cfg(feature = "auth")]
  307. pub nut22: Option<BlindAuthSettings>,
  308. }
  309. impl Nuts {
  310. /// Create new [`Nuts`]
  311. pub fn new() -> Self {
  312. Self::default()
  313. }
  314. /// Nut04 settings
  315. pub fn nut04(self, nut04_settings: nut04::Settings) -> Self {
  316. Self {
  317. nut04: nut04_settings,
  318. ..self
  319. }
  320. }
  321. /// Nut05 settings
  322. pub fn nut05(self, nut05_settings: nut05::Settings) -> Self {
  323. Self {
  324. nut05: nut05_settings,
  325. ..self
  326. }
  327. }
  328. /// Nut07 settings
  329. pub fn nut07(self, supported: bool) -> Self {
  330. Self {
  331. nut07: SupportedSettings { supported },
  332. ..self
  333. }
  334. }
  335. /// Nut08 settings
  336. pub fn nut08(self, supported: bool) -> Self {
  337. Self {
  338. nut08: SupportedSettings { supported },
  339. ..self
  340. }
  341. }
  342. /// Nut09 settings
  343. pub fn nut09(self, supported: bool) -> Self {
  344. Self {
  345. nut09: SupportedSettings { supported },
  346. ..self
  347. }
  348. }
  349. /// Nut10 settings
  350. pub fn nut10(self, supported: bool) -> Self {
  351. Self {
  352. nut10: SupportedSettings { supported },
  353. ..self
  354. }
  355. }
  356. /// Nut11 settings
  357. pub fn nut11(self, supported: bool) -> Self {
  358. Self {
  359. nut11: SupportedSettings { supported },
  360. ..self
  361. }
  362. }
  363. /// Nut12 settings
  364. pub fn nut12(self, supported: bool) -> Self {
  365. Self {
  366. nut12: SupportedSettings { supported },
  367. ..self
  368. }
  369. }
  370. /// Nut14 settings
  371. pub fn nut14(self, supported: bool) -> Self {
  372. Self {
  373. nut14: SupportedSettings { supported },
  374. ..self
  375. }
  376. }
  377. /// Nut15 settings
  378. pub fn nut15(self, mpp_settings: Vec<MppMethodSettings>) -> Self {
  379. Self {
  380. nut15: nut15::Settings {
  381. methods: mpp_settings,
  382. },
  383. ..self
  384. }
  385. }
  386. /// Nut17 settings
  387. pub fn nut17(self, supported: Vec<SupportedMethods>) -> Self {
  388. Self {
  389. nut17: super::nut17::SupportedSettings { supported },
  390. ..self
  391. }
  392. }
  393. /// Nut19 settings
  394. pub fn nut19(self, ttl: Option<u64>, cached_endpoints: Vec<CachedEndpoint>) -> Self {
  395. Self {
  396. nut19: nut19::Settings {
  397. ttl,
  398. cached_endpoints,
  399. },
  400. ..self
  401. }
  402. }
  403. /// Nut20 settings
  404. pub fn nut20(self, supported: bool) -> Self {
  405. Self {
  406. nut20: SupportedSettings { supported },
  407. ..self
  408. }
  409. }
  410. /// Units where minting is supported
  411. pub fn supported_mint_units(&self) -> Vec<&CurrencyUnit> {
  412. self.nut04
  413. .methods
  414. .iter()
  415. .map(|s| &s.unit)
  416. .collect::<HashSet<_>>()
  417. .into_iter()
  418. .collect()
  419. }
  420. /// Units where melting is supported
  421. pub fn supported_melt_units(&self) -> Vec<&CurrencyUnit> {
  422. self.nut05
  423. .methods
  424. .iter()
  425. .map(|s| &s.unit)
  426. .collect::<HashSet<_>>()
  427. .into_iter()
  428. .collect()
  429. }
  430. }
  431. /// Check state Settings
  432. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash, Serialize, Deserialize)]
  433. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  434. pub struct SupportedSettings {
  435. /// Setting supported
  436. pub supported: bool,
  437. }
  438. /// Contact Info
  439. #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
  440. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  441. pub struct ContactInfo {
  442. /// Contact Method i.e. nostr
  443. pub method: String,
  444. /// Contact info i.e. npub...
  445. pub info: String,
  446. }
  447. impl ContactInfo {
  448. /// Create new [`ContactInfo`]
  449. pub fn new(method: String, info: String) -> Self {
  450. Self { method, info }
  451. }
  452. }
  453. #[cfg(test)]
  454. mod tests {
  455. use super::*;
  456. use crate::nut04::MintMethodOptions;
  457. #[test]
  458. fn test_des_mint_into() {
  459. let mint_info_str = r#"{
  460. "name": "Cashu mint",
  461. "pubkey": "0296d0aa13b6a31cf0cd974249f28c7b7176d7274712c95a41c7d8066d3f29d679",
  462. "version": "Nutshell/0.15.3",
  463. "contact": [
  464. ["", ""],
  465. ["", ""]
  466. ],
  467. "nuts": {
  468. "4": {
  469. "methods": [
  470. {"method": "bolt11", "unit": "sat", "description": true},
  471. {"method": "bolt11", "unit": "usd", "description": true}
  472. ],
  473. "disabled": false
  474. },
  475. "5": {
  476. "methods": [
  477. {"method": "bolt11", "unit": "sat"},
  478. {"method": "bolt11", "unit": "usd"}
  479. ],
  480. "disabled": false
  481. },
  482. "7": {"supported": true},
  483. "8": {"supported": true},
  484. "9": {"supported": true},
  485. "10": {"supported": true},
  486. "11": {"supported": true}
  487. },
  488. "tos_url": "https://cashu.mint/tos"
  489. }"#;
  490. let _mint_info: MintInfo = serde_json::from_str(mint_info_str).unwrap();
  491. }
  492. #[test]
  493. fn test_ser_mint_info() {
  494. /*
  495. let mint_info = serde_json::to_string(&MintInfo {
  496. name: Some("Cashu-crab".to_string()),
  497. pubkey: None,
  498. version: None,
  499. description: Some("A mint".to_string()),
  500. description_long: Some("Some longer test".to_string()),
  501. contact: None,
  502. nuts: Nuts::default(),
  503. motd: None,
  504. })
  505. .unwrap();
  506. println!("{}", mint_info);
  507. */
  508. let mint_info_str = r#"
  509. {
  510. "name": "Bob's Cashu mint",
  511. "pubkey": "0283bf290884eed3a7ca2663fc0260de2e2064d6b355ea13f98dec004b7a7ead99",
  512. "version": "Nutshell/0.15.0",
  513. "description": "The short mint description",
  514. "description_long": "A description that can be a long piece of text.",
  515. "contact": [
  516. {
  517. "method": "nostr",
  518. "info": "xxxxx"
  519. },
  520. {
  521. "method": "email",
  522. "info": "contact@me.com"
  523. }
  524. ],
  525. "motd": "Message to display to users.",
  526. "icon_url": "https://this-is-a-mint-icon-url.com/icon.png",
  527. "nuts": {
  528. "4": {
  529. "methods": [
  530. {
  531. "method": "bolt11",
  532. "unit": "sat",
  533. "min_amount": 0,
  534. "max_amount": 10000,
  535. "options": {
  536. "description": true
  537. }
  538. }
  539. ],
  540. "disabled": false
  541. },
  542. "5": {
  543. "methods": [
  544. {
  545. "method": "bolt11",
  546. "unit": "sat",
  547. "min_amount": 0,
  548. "max_amount": 10000
  549. }
  550. ],
  551. "disabled": false
  552. },
  553. "7": {"supported": true},
  554. "8": {"supported": true},
  555. "9": {"supported": true},
  556. "10": {"supported": true},
  557. "12": {"supported": true}
  558. },
  559. "tos_url": "https://cashu.mint/tos"
  560. }"#;
  561. let info: MintInfo = serde_json::from_str(mint_info_str).unwrap();
  562. let mint_info_str = r#"
  563. {
  564. "name": "Bob's Cashu mint",
  565. "pubkey": "0283bf290884eed3a7ca2663fc0260de2e2064d6b355ea13f98dec004b7a7ead99",
  566. "version": "Nutshell/0.15.0",
  567. "description": "The short mint description",
  568. "description_long": "A description that can be a long piece of text.",
  569. "contact": [
  570. ["nostr", "xxxxx"],
  571. ["email", "contact@me.com"]
  572. ],
  573. "motd": "Message to display to users.",
  574. "icon_url": "https://this-is-a-mint-icon-url.com/icon.png",
  575. "nuts": {
  576. "4": {
  577. "methods": [
  578. {
  579. "method": "bolt11",
  580. "unit": "sat",
  581. "min_amount": 0,
  582. "max_amount": 10000,
  583. "options": {
  584. "description": true
  585. }
  586. }
  587. ],
  588. "disabled": false
  589. },
  590. "5": {
  591. "methods": [
  592. {
  593. "method": "bolt11",
  594. "unit": "sat",
  595. "min_amount": 0,
  596. "max_amount": 10000
  597. }
  598. ],
  599. "disabled": false
  600. },
  601. "7": {"supported": true},
  602. "8": {"supported": true},
  603. "9": {"supported": true},
  604. "10": {"supported": true},
  605. "12": {"supported": true}
  606. },
  607. "tos_url": "https://cashu.mint/tos"
  608. }"#;
  609. let mint_info: MintInfo = serde_json::from_str(mint_info_str).unwrap();
  610. let t = mint_info
  611. .nuts
  612. .nut04
  613. .get_settings(&crate::CurrencyUnit::Sat, &crate::PaymentMethod::Bolt11)
  614. .unwrap();
  615. let t = t.options.unwrap();
  616. matches!(t, MintMethodOptions::Bolt11 { description: true });
  617. assert_eq!(info, mint_info);
  618. }
  619. }