mod.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //! NUT-01: Mint public key exchange
  2. //!
  3. //! <https://github.com/cashubtc/nuts/blob/main/01.md>
  4. use std::collections::BTreeMap;
  5. use std::ops::{Deref, DerefMut};
  6. use bitcoin::secp256k1;
  7. use serde::{Deserialize, Serialize};
  8. use serde_with::{serde_as, VecSkipError};
  9. use thiserror::Error;
  10. mod public_key;
  11. mod secret_key;
  12. pub use self::public_key::PublicKey;
  13. pub use self::secret_key::SecretKey;
  14. use super::nut02::KeySet;
  15. use crate::amount::{Amount, AmountStr};
  16. /// Nut01 Error
  17. #[derive(Debug, Error)]
  18. pub enum Error {
  19. /// Secp256k1 Error
  20. #[error(transparent)]
  21. Secp256k1(#[from] secp256k1::Error),
  22. /// Json Error
  23. #[error(transparent)]
  24. Json(#[from] serde_json::Error),
  25. /// Invalid Pubkey size
  26. #[error("Invalid public key size: expected={expected}, found={found}")]
  27. InvalidPublicKeySize {
  28. /// Expected size
  29. expected: usize,
  30. /// Actual size
  31. found: usize,
  32. },
  33. }
  34. /// Mint public keys per amount.
  35. ///
  36. /// This is a variation of [MintKeys] that only exposes the public keys.
  37. ///
  38. /// See [NUT-01]
  39. #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
  40. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  41. pub struct Keys(BTreeMap<AmountStr, PublicKey>);
  42. impl Deref for Keys {
  43. type Target = BTreeMap<AmountStr, PublicKey>;
  44. fn deref(&self) -> &Self::Target {
  45. &self.0
  46. }
  47. }
  48. impl From<MintKeys> for Keys {
  49. fn from(keys: MintKeys) -> Self {
  50. Self(
  51. keys.0
  52. .into_iter()
  53. .map(|(amount, keypair)| (AmountStr::from(amount), keypair.public_key))
  54. .collect(),
  55. )
  56. }
  57. }
  58. impl Keys {
  59. /// Create new [`Keys`]
  60. #[inline]
  61. pub fn new(keys: BTreeMap<AmountStr, PublicKey>) -> Self {
  62. Self(keys)
  63. }
  64. /// Get [`Keys`]
  65. #[inline]
  66. pub fn keys(&self) -> &BTreeMap<AmountStr, PublicKey> {
  67. &self.0
  68. }
  69. /// Get [`PublicKey`] for [`Amount`]
  70. #[inline]
  71. pub fn amount_key(&self, amount: Amount) -> Option<PublicKey> {
  72. self.0.get(&AmountStr::from(amount)).copied()
  73. }
  74. /// Iterate through the (`Amount`, `PublicKey`) entries in the Map
  75. #[inline]
  76. pub fn iter(&self) -> impl Iterator<Item = (&AmountStr, &PublicKey)> {
  77. self.0.iter()
  78. }
  79. }
  80. /// Mint Public Keys [NUT-01]
  81. #[serde_as]
  82. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  83. #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
  84. pub struct KeysResponse {
  85. /// Keysets
  86. #[serde_as(as = "VecSkipError<_>")]
  87. pub keysets: Vec<KeySet>,
  88. }
  89. /// Mint key pairs per amount
  90. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  91. pub struct MintKeys(BTreeMap<Amount, MintKeyPair>);
  92. impl Deref for MintKeys {
  93. type Target = BTreeMap<Amount, MintKeyPair>;
  94. fn deref(&self) -> &Self::Target {
  95. &self.0
  96. }
  97. }
  98. impl DerefMut for MintKeys {
  99. fn deref_mut(&mut self) -> &mut Self::Target {
  100. &mut self.0
  101. }
  102. }
  103. impl MintKeys {
  104. /// Create new [`MintKeys`]
  105. #[inline]
  106. pub fn new(map: BTreeMap<Amount, MintKeyPair>) -> Self {
  107. Self(map)
  108. }
  109. }
  110. /// Mint Public Private key pair
  111. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  112. pub struct MintKeyPair {
  113. /// Publickey
  114. pub public_key: PublicKey,
  115. /// Secretkey
  116. pub secret_key: SecretKey,
  117. }
  118. impl MintKeyPair {
  119. /// [`MintKeyPair`] from secret key
  120. #[inline]
  121. pub fn from_secret_key(secret_key: SecretKey) -> Self {
  122. Self {
  123. public_key: secret_key.public_key(),
  124. secret_key,
  125. }
  126. }
  127. }
  128. #[cfg(test)]
  129. mod tests {
  130. use std::str::FromStr;
  131. use super::*;
  132. #[test]
  133. fn pubkey() {
  134. let pubkey_str = "02c020067db727d586bc3183aecf97fcb800c3f4cc4759f69c626c9db5d8f5b5d4";
  135. let pubkey = PublicKey::from_str(pubkey_str).unwrap();
  136. assert_eq!(pubkey_str, pubkey.to_string());
  137. /*
  138. let pubkey_str = "04918dfc36c93e7db6cc0d60f37e1522f1c36b64d3f4b424c532d7c595febbc5";
  139. let pubkey = PublicKey::from_hex(pubkey_str.to_string()).unwrap();
  140. assert_eq!(pubkey_str, pubkey.to_hex())*/
  141. }
  142. #[test]
  143. fn test_ser_der_secret() {
  144. let secret = SecretKey::generate();
  145. let json = serde_json::to_string(&secret).unwrap();
  146. let sec: SecretKey = serde_json::from_str(&json).unwrap();
  147. assert_eq!(sec, secret);
  148. }
  149. #[test]
  150. fn test_vectors_01() {
  151. let incorrect_1 = r#"{
  152. "1":"03a40f20667ed53513075dc51e715ff2046cad64eb68960632269ba7f0210e38","2":"03fd4ce5a16b65576145949e6f99f445f8249fee17c606b688b504a849cdc452de","4":"02648eccfa4c026960966276fa5a4cae46ce0fd432211a4f449bf84f13aa5f8303","8":"02fdfd6796bfeac490cbee12f778f867f0a2c68f6508d17c649759ea0dc3547528"
  153. }"#;
  154. let response: Result<Keys, serde_json::Error> = serde_json::from_str(incorrect_1);
  155. assert!(response.is_err());
  156. let incorrect_1 = r#"{
  157. "1":"03a40f20667ed53513075dc51e715ff2046cad64eb68960632269ba7f0210e38bc","2":"04fd4ce5a16b65576145949e6f99f445f8249fee17c606b688b504a849cdc452de3625246cb2c27dac965cb7200a5986467eee92eb7d496bbf1453b074e223e481","4":"02648eccfa4c026960966276fa5a4cae46ce0fd432211a4f449bf84f13aa5f8303","8":"02fdfd6796bfeac490cbee12f778f867f0a2c68f6508d17c649759ea0dc3547528"
  158. }"#;
  159. let response: Result<Keys, serde_json::Error> = serde_json::from_str(incorrect_1);
  160. assert!(response.is_err());
  161. let incorrect_1 = r#"{
  162. "1":"03a40f20667ed53513075dc51e715ff2046cad64eb68960632269ba7f0210e38bc","2":"03fd4ce5a16b65576145949e6f99f445f8249fee17c606b688b504a849cdc452de","4":"02648eccfa4c026960966276fa5a4cae46ce0fd432211a4f449bf84f13aa5f8303","8":"02fdfd6796bfeac490cbee12f778f867f0a2c68f6508d17c649759ea0dc3547528"
  163. }"#;
  164. let response: Result<Keys, serde_json::Error> = serde_json::from_str(incorrect_1);
  165. assert!(response.is_ok());
  166. let incorrect_1 = r#"{
  167. "1":"03ba786a2c0745f8c30e490288acd7a72dd53d65afd292ddefa326a4a3fa14c566","2":"03361cd8bd1329fea797a6add1cf1990ffcf2270ceb9fc81eeee0e8e9c1bd0cdf5","4":"036e378bcf78738ddf68859293c69778035740e41138ab183c94f8fee7572214c7","8":"03909d73beaf28edfb283dbeb8da321afd40651e8902fcf5454ecc7d69788626c0","16":"028a36f0e6638ea7466665fe174d958212723019ec08f9ce6898d897f88e68aa5d","32":"03a97a40e146adee2687ac60c2ba2586a90f970de92a9d0e6cae5a4b9965f54612","64":"03ce86f0c197aab181ddba0cfc5c5576e11dfd5164d9f3d4a3fc3ffbbf2e069664","128":"0284f2c06d938a6f78794814c687560a0aabab19fe5e6f30ede38e113b132a3cb9","256":"03b99f475b68e5b4c0ba809cdecaae64eade2d9787aa123206f91cd61f76c01459","512":"03d4db82ea19a44d35274de51f78af0a710925fe7d9e03620b84e3e9976e3ac2eb","1024":"031fbd4ba801870871d46cf62228a1b748905ebc07d3b210daf48de229e683f2dc","2048":"0276cedb9a3b160db6a158ad4e468d2437f021293204b3cd4bf6247970d8aff54b","4096":"02fc6b89b403ee9eb8a7ed457cd3973638080d6e04ca8af7307c965c166b555ea2","8192":"0320265583e916d3a305f0d2687fcf2cd4e3cd03a16ea8261fda309c3ec5721e21","16384":"036e41de58fdff3cb1d8d713f48c63bc61fa3b3e1631495a444d178363c0d2ed50","32768":"0365438f613f19696264300b069d1dad93f0c60a37536b72a8ab7c7366a5ee6c04","65536":"02408426cfb6fc86341bac79624ba8708a4376b2d92debdf4134813f866eb57a8d","131072":"031063e9f11c94dc778c473e968966eac0e70b7145213fbaff5f7a007e71c65f41","262144":"02f2a3e808f9cd168ec71b7f328258d0c1dda250659c1aced14c7f5cf05aab4328","524288":"038ac10de9f1ff9395903bb73077e94dbf91e9ef98fd77d9a2debc5f74c575bc86","1048576":"0203eaee4db749b0fc7c49870d082024b2c31d889f9bc3b32473d4f1dfa3625788","2097152":"033cdb9d36e1e82ae652b7b6a08e0204569ec7ff9ebf85d80a02786dc7fe00b04c","4194304":"02c8b73f4e3a470ae05e5f2fe39984d41e9f6ae7be9f3b09c9ac31292e403ac512","8388608":"025bbe0cfce8a1f4fbd7f3a0d4a09cb6badd73ef61829dc827aa8a98c270bc25b0","16777216":"037eec3d1651a30a90182d9287a5c51386fe35d4a96839cf7969c6e2a03db1fc21","33554432":"03280576b81a04e6abd7197f305506476f5751356b7643988495ca5c3e14e5c262","67108864":"03268bfb05be1dbb33ab6e7e00e438373ca2c9b9abc018fdb452d0e1a0935e10d3","134217728":"02573b68784ceba9617bbcc7c9487836d296aa7c628c3199173a841e7a19798020","268435456":"0234076b6e70f7fbf755d2227ecc8d8169d662518ee3a1401f729e2a12ccb2b276","536870912":"03015bd88961e2a466a2163bd4248d1d2b42c7c58a157e594785e7eb34d880efc9","1073741824":"02c9b076d08f9020ebee49ac8ba2610b404d4e553a4f800150ceb539e9421aaeee","2147483648":"034d592f4c366afddc919a509600af81b489a03caf4f7517c2b3f4f2b558f9a41a","4294967296":"037c09ecb66da082981e4cbdb1ac65c0eb631fc75d85bed13efb2c6364148879b5","8589934592":"02b4ebb0dda3b9ad83b39e2e31024b777cc0ac205a96b9a6cfab3edea2912ed1b3","17179869184":"026cc4dacdced45e63f6e4f62edbc5779ccd802e7fabb82d5123db879b636176e9","34359738368":"02b2cee01b7d8e90180254459b8f09bbea9aad34c3a2fd98c85517ecfc9805af75","68719476736":"037a0c0d564540fc574b8bfa0253cca987b75466e44b295ed59f6f8bd41aace754","137438953472":"021df6585cae9b9ca431318a713fd73dbb76b3ef5667957e8633bca8aaa7214fb6","274877906944":"02b8f53dde126f8c85fa5bb6061c0be5aca90984ce9b902966941caf963648d53a","549755813888":"029cc8af2840d59f1d8761779b2496623c82c64be8e15f9ab577c657c6dd453785","1099511627776":"03e446fdb84fad492ff3a25fc1046fb9a93a5b262ebcd0151caa442ea28959a38a","2199023255552":"02d6b25bd4ab599dd0818c55f75702fde603c93f259222001246569018842d3258","4398046511104":"03397b522bb4e156ec3952d3f048e5a986c20a00718e5e52cd5718466bf494156a","8796093022208":"02d1fb9e78262b5d7d74028073075b80bb5ab281edcfc3191061962c1346340f1e","17592186044416":"030d3f2ad7a4ca115712ff7f140434f802b19a4c9b2dd1c76f3e8e80c05c6a9310","35184372088832":"03e325b691f292e1dfb151c3fb7cad440b225795583c32e24e10635a80e4221c06","70368744177664":"03bee8f64d88de3dee21d61f89efa32933da51152ddbd67466bef815e9f93f8fd1","140737488355328":"0327244c9019a4892e1f04ba3bf95fe43b327479e2d57c25979446cc508cd379ed","281474976710656":"02fb58522cd662f2f8b042f8161caae6e45de98283f74d4e99f19b0ea85e08a56d","562949953421312":"02adde4b466a9d7e59386b6a701a39717c53f30c4810613c1b55e6b6da43b7bc9a","1125899906842624":"038eeda11f78ce05c774f30e393cda075192b890d68590813ff46362548528dca9","2251799813685248":"02ec13e0058b196db80f7079d329333b330dc30c000dbdd7397cbbc5a37a664c4f","4503599627370496":"02d2d162db63675bd04f7d56df04508840f41e2ad87312a3c93041b494efe80a73","9007199254740992":"0356969d6aef2bb40121dbd07c68b6102339f4ea8e674a9008bb69506795998f49","18014398509481984":"02f4e667567ebb9f4e6e180a4113bb071c48855f657766bb5e9c776a880335d1d6","36028797018963968":"0385b4fe35e41703d7a657d957c67bb536629de57b7e6ee6fe2130728ef0fc90b0","72057594037927936":"02b2bc1968a6fddbcc78fb9903940524824b5f5bed329c6ad48a19b56068c144fd","144115188075855872":"02e0dbb24f1d288a693e8a49bc14264d1276be16972131520cf9e055ae92fba19a","288230376151711744":"03efe75c106f931a525dc2d653ebedddc413a2c7d8cb9da410893ae7d2fa7d19cc","576460752303423488":"02c7ec2bd9508a7fc03f73c7565dc600b30fd86f3d305f8f139c45c404a52d958a","1152921504606846976":"035a6679c6b25e68ff4e29d1c7ef87f21e0a8fc574f6a08c1aa45ff352c1d59f06","2305843009213693952":"033cdc225962c052d485f7cfbf55a5b2367d200fe1fe4373a347deb4cc99e9a099","4611686018427387904":"024a4b806cf413d14b294719090a9da36ba75209c7657135ad09bc65328fba9e6f","9223372036854775808":"0377a6fe114e291a8d8e991627c38001c8305b23b9e98b1c7b1893f5cd0dda6cad"
  168. }"#;
  169. let response: Result<Keys, serde_json::Error> = serde_json::from_str(incorrect_1);
  170. assert!(response.is_ok());
  171. }
  172. }