1use std::fmt;
2
3use near_primitives::types::BlockHeight;
4use reqwest::header::InvalidHeaderValue;
5
6use crate::errors::CryptoHashError;
7
8pub mod contract;
9pub mod reference;
10pub mod signed_delegate_action;
11pub mod stake;
12pub mod storage;
13pub mod tokens;
14pub mod transactions;
15
16#[derive(
19 Debug,
20 Clone,
21 serde::Serialize,
22 serde::Deserialize,
23 borsh::BorshDeserialize,
24 borsh::BorshSerialize,
25)]
26pub struct Data<T> {
27 pub data: T,
29 pub block_height: BlockHeight,
31 pub block_hash: CryptoHash,
33}
34
35impl<T> Data<T> {
36 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Data<U> {
37 Data {
38 data: f(self.data),
39 block_height: self.block_height,
40 block_hash: self.block_hash,
41 }
42 }
43}
44
45#[derive(Eq, Hash, Clone, Debug, PartialEq)]
61pub struct ApiKey(near_jsonrpc_client::auth::ApiKey);
62
63impl From<ApiKey> for near_jsonrpc_client::auth::ApiKey {
64 fn from(api_key: ApiKey) -> Self {
65 api_key.0
66 }
67}
68
69impl std::fmt::Display for ApiKey {
70 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
71 write!(f, "{}", self.0.to_str().map_err(|_| std::fmt::Error)?)
72 }
73}
74
75impl std::str::FromStr for ApiKey {
76 type Err = InvalidHeaderValue;
77
78 fn from_str(api_key: &str) -> Result<Self, Self::Err> {
79 Ok(Self(near_jsonrpc_client::auth::ApiKey::new(api_key)?))
80 }
81}
82
83impl serde::ser::Serialize for ApiKey {
84 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
85 where
86 S: serde::ser::Serializer,
87 {
88 serializer.serialize_str(self.0.to_str().map_err(serde::ser::Error::custom)?)
89 }
90}
91
92impl<'de> serde::de::Deserialize<'de> for ApiKey {
93 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
94 where
95 D: serde::de::Deserializer<'de>,
96 {
97 String::deserialize(deserializer)?
98 .parse()
99 .map_err(|err: InvalidHeaderValue| serde::de::Error::custom(err.to_string()))
100 }
101}
102
103fn from_base58(s: &str) -> Result<Vec<u8>, bs58::decode::Error> {
104 bs58::decode(s).into_vec()
105}
106
107#[derive(
112 Copy,
113 Clone,
114 Default,
115 Hash,
116 Eq,
117 PartialEq,
118 Ord,
119 PartialOrd,
120 serde::Serialize,
121 serde::Deserialize,
122 borsh::BorshDeserialize,
123 borsh::BorshSerialize,
124)]
125pub struct CryptoHash(pub [u8; 32]);
126
127impl std::str::FromStr for CryptoHash {
128 type Err = CryptoHashError;
129
130 fn from_str(s: &str) -> Result<Self, Self::Err> {
131 let bytes = from_base58(s)?;
132 Self::try_from(bytes)
133 }
134}
135
136impl TryFrom<&[u8]> for CryptoHash {
137 type Error = CryptoHashError;
138
139 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
140 if bytes.len() != 32 {
141 return Err(CryptoHashError::IncorrectHashLength(bytes.len()));
142 }
143 let mut buf = [0; 32];
144 buf.copy_from_slice(bytes);
145 Ok(Self(buf))
146 }
147}
148
149impl TryFrom<Vec<u8>> for CryptoHash {
150 type Error = CryptoHashError;
151
152 fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
153 <Self as TryFrom<&[u8]>>::try_from(v.as_ref())
154 }
155}
156
157impl std::fmt::Debug for CryptoHash {
158 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159 write!(f, "{self}")
160 }
161}
162
163impl std::fmt::Display for CryptoHash {
164 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
165 std::fmt::Display::fmt(&bs58::encode(self.0).into_string(), f)
166 }
167}
168
169impl From<near_primitives::hash::CryptoHash> for CryptoHash {
170 fn from(hash: near_primitives::hash::CryptoHash) -> Self {
171 Self(hash.0)
172 }
173}
174
175impl From<CryptoHash> for near_primitives::hash::CryptoHash {
176 fn from(hash: CryptoHash) -> Self {
177 Self(hash.0)
178 }
179}