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