use near_sdk::near; use near_sdk::serde::{ self, de::{self, Visitor}, Deserialize, Serialize, }; use std::{fmt, str::FromStr}; #[near(serializers = [borsh])] pub struct H256(pub [u8; 32]); impl FromStr for H256 { type Err = hex::FromHexError; fn from_str(s: &str) -> Result { let mut result = [0; 32]; hex::decode_to_slice(s, &mut result)?; result.reverse(); Ok(H256(result)) } } #[near(serializers = [borsh])] pub struct ProofArgs { pub tx_id: H256, pub tx_block_blockhash: H256, pub tx_index: u64, pub merkle_proof: Vec, pub confirmations: u64, } impl<'de> Deserialize<'de> for H256 { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, { struct HexVisitor; impl Visitor<'_> for HexVisitor { type Value = H256; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a hex string") } fn visit_str(self, s: &str) -> Result where E: de::Error, { let mut result = [0; 32]; hex::decode_to_slice(s, &mut result).map_err(de::Error::custom)?; result.reverse(); Ok(H256(result)) } } deserializer.deserialize_str(HexVisitor) } } impl Serialize for H256 { fn serialize( &self, serializer: S, ) -> Result<::Ok, ::Error> where S: serde::Serializer, { let reversed: Vec = self.0.into_iter().rev().collect(); serializer.serialize_str(&hex::encode(reversed)) } } #[derive(Default)] #[near(contract_state)] pub struct Contract {} #[near] impl Contract { #[allow(unused_variables)] pub fn verify_transaction_inclusion(&self, #[serializer(borsh)] args: ProofArgs) -> bool { true } }