use base64::{engine::general_purpose, Engine}; use borsh::BorshSerialize; use near_sdk::near; use sha2::{Digest, Sha256}; use crate::helpers::chainsig::ChainSig; pub struct Near {} impl ChainSig for Near { fn prepare_transaction_for_signing(encoded_tx: String) -> Vec { let raw_bytes = general_purpose::STANDARD .decode(encoded_tx.as_bytes()) .expect("Failed to decode message"); let mut hasher = Sha256::new(); hasher.update(raw_bytes); let result = hasher.finalize(); vec![hex::encode(result)].into() } } #[near(serializers = [json, borsh])] pub struct SignMessageParams { pub message: String, pub nonce: [u8; 32], pub recipient: String, pub callback_url: Option, } const NEP413_PREFIX: u32 = 2147484061; impl Near { pub fn prepare_nep413_message_for_signing( message: &String, recipient: &String, nonce: &[u8; 32], callback_url: &Option, ) -> Vec { let mut out = Vec::new(); NEP413_PREFIX .serialize(&mut out) .expect("Failed to serialize nep 413 prefix"); SignMessageParams { callback_url: callback_url.to_owned(), message: message.to_owned(), nonce: *nonce, recipient: recipient.to_owned(), } .serialize(&mut out) .expect("Failed to serialize nep 413 payload"); let mut hasher = Sha256::new(); hasher.update(out); let result = hasher.finalize(); vec![hex::encode(result)].into() } }