diff --git a/src/network.rs b/src/network.rs index 8efa290..c989730 100644 --- a/src/network.rs +++ b/src/network.rs @@ -3,139 +3,12 @@ use js_sys::Date; use rand::{thread_rng, RngCore}; use serde::{Deserialize, Serialize}; use sp_client::bitcoin::hex::{DisplayHex, FromHex}; -use sp_client::bitcoin::key::constants::ZERO; use sp_client::bitcoin::OutPoint; -use sp_client::silentpayments::bitcoin_hashes::{sha256t_hash_newtype, Hash, HashEngine}; use tsify::Tsify; -use crate::crypto::{Aes256Decryption, CipherText, Purpose}; +use crate::crypto::{Aes256Decryption, Purpose}; use crate::error::AnkError; -const RAWTXTOPIC: &'static str = "rawtx"; -const RAWBLOCKTOPIC: &'static str = "rawblock"; - -#[derive(Debug, Serialize, Deserialize)] -pub enum BitcoinTopic { - RawTx, - RawBlock, -} - -impl BitcoinTopic { - pub fn as_str(&self) -> &str { - match self { - Self::RawTx => RAWTXTOPIC, - Self::RawBlock => RAWBLOCKTOPIC, - } - } -} - -#[derive(Debug, Serialize, Deserialize, Tsify)] -#[tsify(from_wasm_abi, into_wasm_abi)] -pub struct BitcoinNetworkMsg<'a> { - pub topic: BitcoinTopic, - pub data: &'a [u8], - pub sequence: &'a [u8], - pub addon: &'a [u8], -} - -impl<'a> BitcoinNetworkMsg<'a> { - pub fn new(raw_msg: &'a [u8]) -> Result { - let topic: BitcoinTopic; - let data: &[u8]; - let sequence: &[u8]; - let addon: &[u8]; - let addon_len: usize; - let raw_msg_len = raw_msg.len(); - - if raw_msg.starts_with(RAWTXTOPIC.as_bytes()) { - topic = BitcoinTopic::RawTx; - addon_len = 33; - } else if raw_msg.starts_with(RAWBLOCKTOPIC.as_bytes()) { - topic = BitcoinTopic::RawBlock; - addon_len = 0; - } else { - return Err(Error::msg("Unknown prefix")); - } - - data = &raw_msg[topic.as_str().as_bytes().len()..raw_msg_len - 4 - addon_len]; - sequence = &raw_msg[raw_msg_len - 4 - addon_len..]; - addon = &raw_msg[raw_msg_len - addon_len..]; - - Ok(Self { - topic, - data, - sequence, - addon, - }) - } -} - -sha256t_hash_newtype! { - pub struct PcdTag = hash_str("4nk/PCD"); - - #[hash_newtype(forward)] - pub struct PcdHash(_); - - pub struct PrdTag = hash_str("4nk/PRD"); - - #[hash_newtype(forward)] - pub struct PrdHash(_); -} - -impl PcdHash { - pub fn from_pcd(pcd: Pcd) -> Result { - let mut eng = PcdHash::engine(); - eng.input(&serde_json::to_string(&pcd)?.into_bytes()); - Ok(PcdHash::from_engine(eng)) - } -} - -impl PrdHash { - pub fn from_prd(prd: Prd) -> Result { - let mut eng = PrdHash::engine(); - eng.input(&serde_json::to_string(&prd)?.into_bytes()); - Ok(PrdHash::from_engine(eng)) - } -} - -type Item = String; // Is either a stringified json or the hex representation of its cipher - -#[derive(Debug, Serialize, Deserialize)] -pub struct Pcd { - name: String, - items: Item, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct Prd { - name: String, - seal: Option, - key: Option>, // encrypted key used to decrypt the linked pcd - pcd: PcdHash, -} - -impl Default for Prd { - fn default() -> Self { - Self { - name: "".to_owned(), - seal: None, - key: None, - pcd: PcdHash::from_byte_array(ZERO), - } - } -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct Envelope { - encrypted_prd: CipherText, -} - -impl Envelope { - pub fn new(encrypted_prd: Vec) -> Result { - Ok(Self { encrypted_prd }) - } -} - #[derive(Debug, Serialize, Deserialize, Tsify)] #[tsify(into_wasm_abi, from_wasm_abi)] pub enum AnkFlag {