use secp256k1::ThirtyTwoByteHash;
use sha2::Digest;
use std::fmt;
pub mod account;
pub mod contract;
pub mod crypto;
pub mod errors;
pub mod ft;
pub mod json;
pub mod nft;
pub mod reference;
pub mod signable_message;
pub mod stake;
pub mod storage;
pub mod tokens;
pub mod transaction;
pub mod utils;
pub use near_abi as abi;
pub use near_account_id::AccountId;
pub use near_gas::NearGas;
pub use near_openapi_types::{
AccountView, ContractCodeView, FunctionArgs, RpcBlockResponse,
RpcLightClientExecutionProofResponse, RpcReceiptResponse, RpcTransactionResponse,
RpcValidatorResponse, StoreKey, StoreValue, TxExecutionStatus, ViewStateResult,
};
pub use near_token::NearToken;
pub use reference::{EpochReference, Reference};
pub use storage::{StorageBalance, StorageBalanceInternal};
pub use account::Account;
pub use crypto::public_key::PublicKey;
pub use crypto::secret_key::SecretKey;
pub use crypto::signature::Signature;
pub use transaction::actions::{AccessKey, AccessKeyPermission, Action};
use crate::errors::DataConversionError;
pub type BlockHeight = u64;
pub type Nonce = u64;
pub type StorageUsage = u64;
#[derive(
Debug,
Clone,
serde::Serialize,
serde::Deserialize,
borsh::BorshDeserialize,
borsh::BorshSerialize,
)]
pub struct Data<T> {
pub data: T,
pub block_height: BlockHeight,
pub block_hash: CryptoHash,
}
impl<T> Data<T> {
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Data<U> {
Data {
data: f(self.data),
block_height: self.block_height,
block_hash: self.block_hash,
}
}
}
#[derive(
Copy,
Clone,
Default,
Hash,
Eq,
PartialEq,
Ord,
PartialOrd,
borsh::BorshDeserialize,
borsh::BorshSerialize,
)]
pub struct CryptoHash(pub [u8; 32]);
impl ThirtyTwoByteHash for CryptoHash {
fn into_32(self) -> [u8; 32] {
self.0
}
}
impl serde::Serialize for CryptoHash {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> serde::Deserialize<'de> for CryptoHash {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
<Self as std::str::FromStr>::from_str(&s).map_err(serde::de::Error::custom)
}
}
impl CryptoHash {
pub fn hash(bytes: &[u8]) -> Self {
Self(sha2::Sha256::digest(bytes).into())
}
}
impl std::str::FromStr for CryptoHash {
type Err = DataConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = bs58::decode(s).into_vec()?;
Self::try_from(bytes)
}
}
impl TryFrom<&[u8]> for CryptoHash {
type Error = DataConversionError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
if bytes.len() != 32 {
return Err(DataConversionError::IncorrectLength(bytes.len()));
}
let mut buf = [0; 32];
buf.copy_from_slice(bytes);
Ok(Self(buf))
}
}
impl TryFrom<Vec<u8>> for CryptoHash {
type Error = DataConversionError;
fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
<Self as TryFrom<&[u8]>>::try_from(v.as_ref())
}
}
impl From<near_openapi_types::CryptoHash> for CryptoHash {
fn from(value: near_openapi_types::CryptoHash) -> Self {
Self(value.0)
}
}
impl std::fmt::Debug for CryptoHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}
impl std::fmt::Display for CryptoHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
std::fmt::Display::fmt(&bs58::encode(self.0).into_string(), f)
}
}
impl From<CryptoHash> for near_openapi_types::CryptoHash {
fn from(hash: CryptoHash) -> Self {
Self(hash.0)
}
}