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;
17
18pub use near_abi as abi;
19pub use near_account_id::AccountId;
20pub use near_gas::NearGas;
21pub use near_openapi_types::{
22 AccountView, ContractCodeView, FunctionArgs, RpcBlockResponse, RpcTransactionResponse,
23 RpcValidatorResponse, StoreKey, TxExecutionStatus, ViewStateResult,
24};
25pub use near_token::NearToken;
26pub use reference::{EpochReference, Reference};
27pub use storage::{StorageBalance, StorageBalanceInternal};
28
29pub use account::Account;
30pub use crypto::public_key::PublicKey;
31pub use crypto::secret_key::SecretKey;
32pub use crypto::signature::Signature;
33pub use transaction::actions::{AccessKey, AccessKeyPermission, Action};
34
35use crate::errors::DataConversionError;
36
37pub type BlockHeight = u64;
38pub type Nonce = u64;
39pub type StorageUsage = u64;
40
41#[derive(
44 Debug,
45 Clone,
46 serde::Serialize,
47 serde::Deserialize,
48 borsh::BorshDeserialize,
49 borsh::BorshSerialize,
50)]
51pub struct Data<T> {
52 pub data: T,
54 pub block_height: BlockHeight,
56 pub block_hash: CryptoHash,
58}
59
60impl<T> Data<T> {
61 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Data<U> {
62 Data {
63 data: f(self.data),
64 block_height: self.block_height,
65 block_hash: self.block_hash,
66 }
67 }
68}
69
70#[derive(
75 Copy,
76 Clone,
77 Default,
78 Hash,
79 Eq,
80 PartialEq,
81 Ord,
82 PartialOrd,
83 serde::Serialize,
84 serde::Deserialize,
85 borsh::BorshDeserialize,
86 borsh::BorshSerialize,
87)]
88pub struct CryptoHash(pub [u8; 32]);
89
90impl CryptoHash {
91 pub fn hash(bytes: &[u8]) -> Self {
92 Self(sha2::Sha256::digest(bytes).into())
93 }
94}
95
96impl std::str::FromStr for CryptoHash {
97 type Err = DataConversionError;
98
99 fn from_str(s: &str) -> Result<Self, Self::Err> {
100 let bytes = bs58::decode(s).into_vec()?;
101 Self::try_from(bytes)
102 }
103}
104
105impl TryFrom<&[u8]> for CryptoHash {
106 type Error = DataConversionError;
107
108 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
109 if bytes.len() != 32 {
110 return Err(DataConversionError::IncorrectLength(bytes.len()));
111 }
112 let mut buf = [0; 32];
113 buf.copy_from_slice(bytes);
114 Ok(Self(buf))
115 }
116}
117
118impl TryFrom<Vec<u8>> for CryptoHash {
119 type Error = DataConversionError;
120
121 fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
122 <Self as TryFrom<&[u8]>>::try_from(v.as_ref())
123 }
124}
125
126impl TryFrom<near_openapi_types::CryptoHash> for CryptoHash {
127 type Error = DataConversionError;
128
129 fn try_from(value: near_openapi_types::CryptoHash) -> Result<Self, Self::Error> {
130 let near_openapi_types::CryptoHash(hash) = value;
131 let bytes = bs58::decode(hash).into_vec()?;
132 Self::try_from(bytes)
133 }
134}
135
136impl std::fmt::Debug for CryptoHash {
137 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138 write!(f, "{self}")
139 }
140}
141
142impl std::fmt::Display for CryptoHash {
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 std::fmt::Display::fmt(&bs58::encode(self.0).into_string(), f)
145 }
146}
147
148impl From<CryptoHash> for near_openapi_types::CryptoHash {
149 fn from(hash: CryptoHash) -> Self {
150 Self(hash.to_string())
151 }
152}