1use sha2::Digest;
2use std::fmt;
3
4pub mod account;
5pub mod contract;
6pub mod crypto;
7pub mod errors;
8pub mod ft;
9pub mod json;
10pub mod nft;
11pub mod reference;
12pub mod signable_message;
13pub mod stake;
14pub mod storage;
15pub mod tokens;
16pub mod transaction;
17pub mod utils;
18
19pub use near_abi as abi;
20pub use near_account_id::AccountId;
21pub use near_gas::NearGas;
22pub use near_openapi_types::{
23 AccountView, ContractCodeView, FunctionArgs, RpcBlockResponse, RpcTransactionResponse,
24 RpcValidatorResponse, StoreKey, StoreValue, TxExecutionStatus, ViewStateResult,
25};
26pub use near_token::NearToken;
27pub use reference::{EpochReference, Reference};
28pub use storage::{StorageBalance, StorageBalanceInternal};
29
30pub use account::Account;
31pub use crypto::public_key::PublicKey;
32pub use crypto::secret_key::SecretKey;
33pub use crypto::signature::Signature;
34pub use transaction::actions::{AccessKey, AccessKeyPermission, Action};
35
36use crate::errors::DataConversionError;
37
38pub type BlockHeight = u64;
39pub type Nonce = u64;
40pub type StorageUsage = u64;
41
42#[derive(
45 Debug,
46 Clone,
47 serde::Serialize,
48 serde::Deserialize,
49 borsh::BorshDeserialize,
50 borsh::BorshSerialize,
51)]
52pub struct Data<T> {
53 pub data: T,
55 pub block_height: BlockHeight,
57 pub block_hash: CryptoHash,
59}
60
61impl<T> Data<T> {
62 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Data<U> {
63 Data {
64 data: f(self.data),
65 block_height: self.block_height,
66 block_hash: self.block_hash,
67 }
68 }
69}
70
71#[derive(
76 Copy,
77 Clone,
78 Default,
79 Hash,
80 Eq,
81 PartialEq,
82 Ord,
83 PartialOrd,
84 borsh::BorshDeserialize,
85 borsh::BorshSerialize,
86)]
87pub struct CryptoHash(pub [u8; 32]);
88
89impl serde::Serialize for CryptoHash {
90 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
91 where
92 S: serde::Serializer,
93 {
94 serializer.serialize_str(&self.to_string())
95 }
96}
97
98impl<'de> serde::Deserialize<'de> for CryptoHash {
99 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
100 where
101 D: serde::Deserializer<'de>,
102 {
103 let s = String::deserialize(deserializer)?;
104 <Self as std::str::FromStr>::from_str(&s).map_err(serde::de::Error::custom)
105 }
106}
107
108impl CryptoHash {
109 pub fn hash(bytes: &[u8]) -> Self {
110 Self(sha2::Sha256::digest(bytes).into())
111 }
112}
113
114impl std::str::FromStr for CryptoHash {
115 type Err = DataConversionError;
116
117 fn from_str(s: &str) -> Result<Self, Self::Err> {
118 let bytes = bs58::decode(s).into_vec()?;
119 Self::try_from(bytes)
120 }
121}
122
123impl TryFrom<&[u8]> for CryptoHash {
124 type Error = DataConversionError;
125
126 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
127 if bytes.len() != 32 {
128 return Err(DataConversionError::IncorrectLength(bytes.len()));
129 }
130 let mut buf = [0; 32];
131 buf.copy_from_slice(bytes);
132 Ok(Self(buf))
133 }
134}
135
136impl TryFrom<Vec<u8>> for CryptoHash {
137 type Error = DataConversionError;
138
139 fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
140 <Self as TryFrom<&[u8]>>::try_from(v.as_ref())
141 }
142}
143
144impl TryFrom<near_openapi_types::CryptoHash> for CryptoHash {
145 type Error = DataConversionError;
146
147 fn try_from(value: near_openapi_types::CryptoHash) -> Result<Self, Self::Error> {
148 let near_openapi_types::CryptoHash(hash) = value;
149 let bytes = bs58::decode(hash).into_vec()?;
150 Self::try_from(bytes)
151 }
152}
153
154impl std::fmt::Debug for CryptoHash {
155 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 write!(f, "{self}")
157 }
158}
159
160impl std::fmt::Display for CryptoHash {
161 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162 std::fmt::Display::fmt(&bs58::encode(self.0).into_string(), f)
163 }
164}
165
166impl From<CryptoHash> for near_openapi_types::CryptoHash {
167 fn from(hash: CryptoHash) -> Self {
168 Self(hash.to_string())
169 }
170}