id.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. use serde::{Deserialize, Deserializer, Serialize};
  2. use sha2::{Digest, Sha256};
  3. use std::fmt::Display;
  4. use std::num::ParseIntError;
  5. use std::str::FromStr;
  6. use crate::PaymentId;
  7. /// Error
  8. #[derive(thiserror::Error, Debug)]
  9. pub enum Error {
  10. /// The number of bytes is invalid
  11. #[error("Invalid length for {0}: {1} (expected: {2})")]
  12. InvalidLength(String, usize, usize),
  13. /// The provided prefix on the string ID is unknown
  14. #[error("Unknown prefix {0}")]
  15. UnknownPrefix(String),
  16. #[error("Invalid PaymentId: {0}")]
  17. InvalidPaymentId(#[from] ParseIntError),
  18. }
  19. macro_rules! Id {
  20. ($id:ident, $suffix:expr) => {
  21. #[derive(Clone, Debug, Eq, PartialOrd, Ord, Hash, PartialEq)]
  22. /// A unique identifier for $id
  23. pub struct $id {
  24. bytes: [u8; 32],
  25. }
  26. impl $id {
  27. /// Creates a new instance of $id from the raw bytes
  28. pub fn new(bytes: [u8; 32]) -> Self {
  29. Self { bytes }
  30. }
  31. }
  32. impl FromStr for $id {
  33. type Err = Error;
  34. fn from_str(value: &str) -> Result<Self, Self::Err> {
  35. Ok(Self::try_from(value).unwrap_or_else(|_| {
  36. let mut hasher = Sha256::new();
  37. hasher.update(&value);
  38. Self {
  39. bytes: hasher.finalize().into(),
  40. }
  41. }))
  42. }
  43. }
  44. impl Serialize for $id {
  45. fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  46. where
  47. S: serde::Serializer,
  48. {
  49. serializer.collect_str(&self)
  50. }
  51. }
  52. impl<'de> Deserialize<'de> for $id {
  53. fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  54. where
  55. D: Deserializer<'de>,
  56. {
  57. let s = String::deserialize(deserializer)?;
  58. // Use FromStr to parse the string and construct the struct
  59. $id::from_str(&s).map_err(serde::de::Error::custom)
  60. }
  61. }
  62. impl TryFrom<&str> for $id {
  63. type Error = Error;
  64. fn try_from(value: &str) -> Result<Self, Self::Error> {
  65. if $suffix.len() + 64 != value.len() {
  66. return Err(Error::InvalidLength(
  67. stringify!($id).to_owned(),
  68. value.len(),
  69. $suffix.len() + 64,
  70. ));
  71. }
  72. if !value.starts_with($suffix) {
  73. return Err(Error::InvalidLength(
  74. stringify!($id).to_owned(),
  75. value.len(),
  76. $suffix.len() + 64,
  77. ));
  78. }
  79. let bytes = hex::decode(&value[$suffix.len()..]).map_err(|_| {
  80. Error::InvalidLength(stringify!($id).to_owned(), value.len(), 32)
  81. })?;
  82. bytes.try_into()
  83. }
  84. }
  85. impl TryFrom<&[u8]> for $id {
  86. type Error = Error;
  87. fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
  88. if value.len() != 32 {
  89. return Err(Error::InvalidLength(
  90. stringify!($id).to_owned(),
  91. value.len(),
  92. 32,
  93. ));
  94. }
  95. let mut bytes = [0u8; 32];
  96. bytes.copy_from_slice(&value);
  97. Ok(Self { bytes })
  98. }
  99. }
  100. impl TryFrom<Vec<u8>> for $id {
  101. type Error = Error;
  102. fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
  103. if value.len() != 32 {
  104. return Err(Error::InvalidLength(
  105. stringify!($id).to_owned(),
  106. value.len(),
  107. 32,
  108. ));
  109. }
  110. let mut bytes = [0u8; 32];
  111. bytes.copy_from_slice(&value);
  112. Ok(Self { bytes })
  113. }
  114. }
  115. impl Display for $id {
  116. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  117. write!(f, "{}{}", $suffix, hex::encode(self.bytes))
  118. }
  119. }
  120. impl AsRef<[u8]> for $id {
  121. fn as_ref(&self) -> &[u8] {
  122. &self.bytes
  123. }
  124. }
  125. impl AsRef<[u8; 32]> for $id {
  126. fn as_ref(&self) -> &[u8; 32] {
  127. &self.bytes
  128. }
  129. }
  130. impl std::ops::Deref for $id {
  131. type Target = [u8; 32];
  132. fn deref(&self) -> &Self::Target {
  133. &self.bytes
  134. }
  135. }
  136. };
  137. }
  138. Id!(AccountId, "account");
  139. Id!(TransactionId, "tx");
  140. /// A generic ID wrapper
  141. ///
  142. /// This enum can be used whenever a human ID is passed and needs to be parsed and validated.
  143. #[derive(Debug)]
  144. pub enum AnyId {
  145. /// AccountId
  146. Account(AccountId),
  147. /// TransactionId
  148. Transaction(TransactionId),
  149. /// Payment
  150. Payment(PaymentId),
  151. }
  152. impl FromStr for AnyId {
  153. type Err = Error;
  154. fn from_str(value: &str) -> Result<Self, Self::Err> {
  155. if value.starts_with("account") {
  156. Ok(Self::Account(value.parse()?))
  157. } else if value.starts_with("tx") {
  158. if let Some(at) = value.find(':') {
  159. let (tx, pos) = value.split_at(at);
  160. Ok(Self::Payment(PaymentId {
  161. transaction: tx.parse()?,
  162. position: (pos[1..]).parse()?,
  163. }))
  164. } else {
  165. Ok(Self::Transaction(value.parse()?))
  166. }
  167. } else {
  168. Err(Error::UnknownPrefix(value.to_owned()))
  169. }
  170. }
  171. }
  172. impl<'de> Deserialize<'de> for AnyId {
  173. fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  174. where
  175. D: Deserializer<'de>,
  176. {
  177. let s = String::deserialize(deserializer)?;
  178. AnyId::from_str(&s).map_err(serde::de::Error::custom)
  179. }
  180. }
  181. #[cfg(test)]
  182. mod test {
  183. use super::*;
  184. #[test]
  185. fn from_random_data() {
  186. let id = "something".parse::<AccountId>().expect("hashed value");
  187. let id_str = id.to_string();
  188. let id_bin: &[u8] = id.as_ref();
  189. assert_eq!(71, id_str.len());
  190. assert_eq!(
  191. <&str as TryInto<AccountId>>::try_into(id_str.as_str()).expect("valid"),
  192. id
  193. );
  194. assert_eq!(
  195. <Vec<u8> as TryInto<AccountId>>::try_into(id_bin.to_owned()).expect("valid"),
  196. id
  197. );
  198. assert_eq!(
  199. <&[u8] as TryInto<AccountId>>::try_into(id_bin).expect("valid"),
  200. id
  201. );
  202. }
  203. }