secret.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //! Secret
  2. use std::str::FromStr;
  3. use serde::{Deserialize, Serialize};
  4. use thiserror::Error;
  5. /// The secret data that allows spending ecash
  6. #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
  7. #[serde(transparent)]
  8. pub struct Secret(pub String);
  9. #[derive(Debug, Error)]
  10. pub enum Error {
  11. #[error("Invalid secret length: `{0}`")]
  12. InvalidLength(u64),
  13. #[error("Hex error: `{0}`")]
  14. Hex(#[from] hex::FromHexError),
  15. }
  16. impl Default for Secret {
  17. fn default() -> Self {
  18. Self::new()
  19. }
  20. }
  21. impl Secret {
  22. /// Create secret value
  23. /// Generate a new random secret as the recommended 32 byte hex
  24. pub fn new() -> Self {
  25. use rand::RngCore;
  26. let mut rng = rand::thread_rng();
  27. let mut random_bytes = [0u8; 32];
  28. // Generate random bytes
  29. rng.fill_bytes(&mut random_bytes);
  30. // The secret string is hex encoded
  31. let secret = hex::encode(random_bytes);
  32. Self(secret)
  33. }
  34. pub fn to_bytes(&self) -> Vec<u8> {
  35. self.0.clone().into_bytes()
  36. }
  37. #[cfg(feature = "nut11")]
  38. pub fn is_p2pk(&self) -> bool {
  39. use crate::nuts::Kind;
  40. let secret: Result<crate::nuts::nut10::Secret, serde_json::Error> =
  41. serde_json::from_str(&self.0);
  42. if let Ok(secret) = secret {
  43. if secret.kind.eq(&Kind::P2PK) {
  44. return true;
  45. }
  46. }
  47. false
  48. }
  49. }
  50. impl FromStr for Secret {
  51. type Err = Error;
  52. fn from_str(s: &str) -> Result<Self, Self::Err> {
  53. Ok(Secret(s.to_string()))
  54. }
  55. }
  56. impl ToString for Secret {
  57. fn to_string(&self) -> String {
  58. self.0.clone()
  59. }
  60. }
  61. impl From<Secret> for Vec<u8> {
  62. fn from(value: Secret) -> Vec<u8> {
  63. value.to_bytes()
  64. }
  65. }
  66. impl From<&Secret> for Vec<u8> {
  67. fn from(value: &Secret) -> Vec<u8> {
  68. value.to_bytes()
  69. }
  70. }
  71. #[cfg(feature = "nut10")]
  72. impl TryFrom<Secret> for crate::nuts::nut10::Secret {
  73. type Error = serde_json::Error;
  74. fn try_from(unchecked_secret: Secret) -> Result<crate::nuts::nut10::Secret, Self::Error> {
  75. serde_json::from_str(&unchecked_secret.0)
  76. }
  77. }
  78. #[cfg(feature = "nut10")]
  79. impl TryFrom<&Secret> for crate::nuts::nut10::Secret {
  80. type Error = serde_json::Error;
  81. fn try_from(unchecked_secret: &Secret) -> Result<crate::nuts::nut10::Secret, Self::Error> {
  82. serde_json::from_str(&unchecked_secret.0)
  83. }
  84. }
  85. #[cfg(test)]
  86. mod tests {
  87. use std::assert_eq;
  88. use std::str::FromStr;
  89. use super::*;
  90. #[test]
  91. fn test_secret_from_str() {
  92. let secret = Secret::new();
  93. let secret_str = secret.to_string();
  94. assert_eq!(hex::decode(secret_str.clone()).unwrap().len(), 32);
  95. let secret_n = Secret::from_str(&secret_str).unwrap();
  96. assert_eq!(secret_n, secret)
  97. }
  98. }