nut10.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use std::str::FromStr;
  2. use serde::ser::SerializeTuple;
  3. use serde::{Deserialize, Serialize, Serializer};
  4. use crate::error::Error;
  5. #[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
  6. pub enum Kind {
  7. /// NUT-11 P2PK
  8. #[default]
  9. P2PK,
  10. }
  11. #[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq, Serialize)]
  12. pub struct SecretData {
  13. /// Unique random string
  14. pub nonce: String,
  15. /// Expresses the spending condition specific to each kind
  16. pub data: String,
  17. /// Additional data committed to and can be used for feature extensions
  18. #[serde(skip_serializing_if = "Vec::is_empty")]
  19. pub tags: Vec<Vec<String>>,
  20. }
  21. #[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
  22. pub struct Secret {
  23. /// Kind of the spending condition
  24. pub kind: Kind,
  25. pub secret_data: SecretData,
  26. }
  27. impl Secret {
  28. pub fn new<S>(kind: Kind, data: S, tags: Vec<Vec<String>>) -> Self
  29. where
  30. S: Into<String>,
  31. {
  32. let nonce = crate::secret::Secret::new().to_string();
  33. let secret_data = SecretData {
  34. nonce,
  35. data: data.into(),
  36. tags,
  37. };
  38. Self { kind, secret_data }
  39. }
  40. }
  41. impl Serialize for Secret {
  42. fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  43. where
  44. S: Serializer,
  45. {
  46. // Create a tuple representing the struct fields
  47. let secret_tuple = (&self.kind, &self.secret_data);
  48. // Serialize the tuple as a JSON array
  49. let mut s = serializer.serialize_tuple(2)?;
  50. s.serialize_element(&secret_tuple.0)?;
  51. s.serialize_element(&secret_tuple.1)?;
  52. s.end()
  53. }
  54. }
  55. impl TryFrom<Secret> for crate::secret::Secret {
  56. type Error = Error;
  57. fn try_from(secret: Secret) -> Result<crate::secret::Secret, Self::Error> {
  58. Ok(crate::secret::Secret::from_str(&serde_json::to_string(
  59. &secret,
  60. )?)?)
  61. }
  62. }
  63. #[cfg(test)]
  64. mod tests {
  65. use std::assert_eq;
  66. use super::*;
  67. #[test]
  68. fn test_secret_serialize() {
  69. let secret = Secret {
  70. kind: Kind::P2PK,
  71. secret_data: SecretData {
  72. nonce: "5d11913ee0f92fefdc82a6764fd2457a".to_string(),
  73. data: "026562efcfadc8e86d44da6a8adf80633d974302e62c850774db1fb36ff4cc7198"
  74. .to_string(),
  75. tags: vec![vec![
  76. "key".to_string(),
  77. "value1".to_string(),
  78. "value2".to_string(),
  79. ]],
  80. },
  81. };
  82. let secret_str = r#"["P2PK",{"nonce":"5d11913ee0f92fefdc82a6764fd2457a","data":"026562efcfadc8e86d44da6a8adf80633d974302e62c850774db1fb36ff4cc7198","tags":[["key","value1","value2"]]}]"#;
  83. assert_eq!(serde_json::to_string(&secret).unwrap(), secret_str);
  84. }
  85. }