secret.rs 3.4 KB

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