secret.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // MIT License
  2. // Copyright (c) 2023 Clark Moody
  3. // https://github.com/clarkmoody/cashu-rs/blob/master/src/secret.rs
  4. use std::str::FromStr;
  5. use serde::{Deserialize, Serialize};
  6. use thiserror::Error;
  7. /// The secret data that allows spending ecash
  8. #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
  9. #[serde(transparent)]
  10. pub struct Secret(String);
  11. #[derive(Debug, Error)]
  12. pub enum Error {
  13. #[error("Invalid secret length: `{0}`")]
  14. InvalidLength(u64),
  15. }
  16. impl Default for Secret {
  17. fn default() -> Self {
  18. Self::new()
  19. }
  20. }
  21. impl Secret {
  22. const BIT_LENGTH: usize = 128;
  23. /// Create secret value
  24. pub fn new() -> Self {
  25. use base64::engine::general_purpose::URL_SAFE;
  26. use base64::Engine as _;
  27. use rand::RngCore;
  28. let mut rng = rand::thread_rng();
  29. let mut random_bytes = [0u8; Self::BIT_LENGTH / 8];
  30. // Generate random bytes
  31. rng.fill_bytes(&mut random_bytes);
  32. // The secret string is Base64-encoded
  33. let secret = URL_SAFE.encode(random_bytes);
  34. Self(secret)
  35. }
  36. pub fn as_bytes(&self) -> &[u8] {
  37. self.0.as_bytes()
  38. }
  39. }
  40. impl FromStr for Secret {
  41. type Err = Error;
  42. fn from_str(s: &str) -> Result<Self, Self::Err> {
  43. if s.len().ne(&24) {
  44. return Err(Error::InvalidLength(s.as_bytes().len() as u64));
  45. }
  46. Ok(Secret(s.to_string()))
  47. }
  48. }
  49. impl ToString for Secret {
  50. fn to_string(&self) -> String {
  51. self.0.clone()
  52. }
  53. }
  54. #[cfg(test)]
  55. mod tests {
  56. use std::str::FromStr;
  57. use super::*;
  58. #[test]
  59. fn test_secret_from_str() {
  60. let secret = Secret::new();
  61. let secret_str = secret.to_string();
  62. let secret_n = Secret::from_str(&secret_str).unwrap();
  63. assert_eq!(secret_n, secret)
  64. }
  65. }