init temp

This commit is contained in:
NicolasCantu 2025-05-28 15:18:17 +02:00
parent 985e6dcdc9
commit 091f0008cc
6 changed files with 592 additions and 506 deletions

View File

@ -9,6 +9,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
aes-gcm = "0.10.3"
anyhow = "1.0"
ciborium = "0.2.2"
js-sys = "0.3.69"
env_logger = "0.9"
log = "0.4.6"

View File

@ -3,11 +3,16 @@ use std::str::FromStr;
use std::sync::{Mutex, MutexGuard};
pub use aes_gcm;
pub use ciborium;
pub use env_logger;
pub use log;
pub use rand;
pub use sp_client;
pub use serde;
pub use serde_json;
pub use serde_wasm_bindgen;
pub use tsify;
pub use wasm_bindgen;
pub mod crypto;
pub mod device;

View File

@ -18,6 +18,7 @@ pub enum AnkFlag {
Cipher,
Commit,
Handshake,
Sync,
Unknown,
}
@ -29,6 +30,7 @@ impl From<&str> for AnkFlag {
"Cipher" => Self::Cipher,
"Commit" => Self::Commit,
"Handshake" => Self::Handshake,
"Sync" => Self::Sync,
_ => Self::Unknown,
}
}
@ -48,6 +50,7 @@ impl AnkFlag {
2 => Self::Cipher,
3 => Self::Commit,
4 => Self::Handshake,
5 => Self::Sync,
_ => Self::Unknown,
}
}
@ -59,6 +62,7 @@ impl AnkFlag {
Self::Cipher => "Cipher",
Self::Commit => "Commit",
Self::Handshake => "Handshake",
Self::Sync => "Sync",
Self::Unknown => "Unknown",
}
}
@ -164,14 +168,45 @@ pub struct HandshakeMessage {
pub sp_address: String,
pub peers_list: OutPointMemberMap,
pub processes_list: OutPointProcessMap,
pub chain_tip: u64,
}
impl HandshakeMessage {
pub fn new(sp_address: String, peers_list: OutPointMemberMap, processes_list: OutPointProcessMap) -> Self {
pub fn new(sp_address: String, peers_list: OutPointMemberMap, processes_list: OutPointProcessMap, chain_tip: u64) -> Self {
Self {
sp_address,
peers_list,
processes_list,
chain_tip,
}
}
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
#[derive(Debug, Serialize, Deserialize, Tsify)]
pub enum SyncMessageContent {
Tweaks,
WholeBlocks,
Headers,
}
#[derive(Debug, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct SyncMessage {
start: u32,
end: u32,
content: SyncMessageContent,
}
impl SyncMessage {
pub fn new(start: u32, end: u32, content: SyncMessageContent) -> Self {
Self {
start,
end,
content,
}
}

View File

@ -1,8 +1,6 @@
use anyhow::{Error, Result};
use rs_merkle::{algorithms::Sha256, MerkleTree};
use serde::de::{DeserializeOwned, Error as DeError};
use serde::ser::SerializeStruct;
use wasm_bindgen::JsValue;
use std::collections::btree_map::Keys;
use std::collections::{BTreeMap, HashSet};
use std::hash::{Hash as StdHash, Hasher};
@ -132,24 +130,38 @@ impl AnkPcdHash {
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Pcd(BTreeMap<String, Value>);
pub struct Pcd(BTreeMap<String, Vec<u8>>);
impl IntoIterator for Pcd {
type Item = (String, Value);
type IntoIter = std::collections::btree_map::IntoIter<String, Value>;
type Item = (String, Vec<u8>);
type IntoIter = std::collections::btree_map::IntoIter<String, Vec<u8>>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl TryFrom<Value> for Pcd {
type Error = Error;
fn try_from(value: Value) -> Result<Self> {
let as_object = value.as_object().ok_or_else(|| Error::msg("value is not an object"))?;
let map: Result<BTreeMap<String, Vec<u8>>> = as_object.iter().map(|(key, value)| {
let mut writer = vec![];
ciborium::into_writer(value, &mut writer)?;
Ok((key.clone(), writer))
}).collect();
Ok(Pcd(map?))
}
}
impl Pcd {
pub fn new(map: BTreeMap<String, Value>) -> Self {
pub fn new(map: BTreeMap<String, Vec<u8>>) -> Self {
Self(map)
}
pub fn get(&self, key: &str) -> Option<&Value> {
pub fn get(&self, key: &str) -> Option<&Vec<u8>> {
self.0.get(key)
}
@ -157,15 +169,15 @@ impl Pcd {
self.0.len()
}
pub fn iter(&self) -> std::collections::btree_map::Iter<'_, String, Value> {
pub fn iter(&self) -> std::collections::btree_map::Iter<'_, String, Vec<u8>> {
self.0.iter()
}
pub fn iter_mut(&mut self) -> std::collections::btree_map::IterMut<'_, String, Value> {
pub fn iter_mut(&mut self) -> std::collections::btree_map::IterMut<'_, String, Vec<u8>> {
self.0.iter_mut()
}
pub fn insert(&mut self, key: String, value: Value) -> Option<Value> {
pub fn insert(&mut self, key: String, value: Vec<u8>) -> Option<Vec<u8>> {
self.0.insert(key, value)
}
}
@ -187,7 +199,6 @@ impl PcdCommitments {
}
if roles.len() > 0 {
// This should be very rare, but just in case
let serialized_roles = roles.to_bytes()?;
let roles_hash = AnkPcdHash::from_value_with_outpoint(&serialized_roles, &serialized_outpoint);
@ -206,7 +217,9 @@ impl PcdCommitments {
self.0.is_empty()
}
pub fn update_with_value(&mut self, outpoint: &OutPoint, field: &str, new_value: &Value) -> Result<()> {
// TODO here we may end up with commitments with different outpoints in the same map, which is probably something we don't want?
// Probably worth not making it an empty struct and set the outpoint as an immutable paramater or something like that
pub fn update_with_value(&mut self, outpoint: &OutPoint, field: &str, new_value: &Vec<u8>) -> Result<()> {
let serialized_outpoint = serialize(outpoint);
if let Some(old_hash) = self.get_mut(field) {
// We hash the new_value
@ -444,6 +457,22 @@ impl IntoIterator for Roles {
}
}
impl TryFrom<Value> for Roles {
type Error = Error;
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
let as_object = value.as_object().ok_or_else(|| Error::msg("value is not an object"))?;
let map: Result<BTreeMap<String, RoleDefinition>> = as_object.iter().map(|(key, value)| {
let mut writer = vec![];
ciborium::into_writer(value, &mut writer)?;
let role_def: RoleDefinition = ciborium::from_reader(&writer[..])?;
Ok((key.clone(), role_def))
}).collect();
Ok(Self(map?))
}
}
impl Roles {
pub fn new(roles: BTreeMap<String, RoleDefinition>) -> Self {
@ -580,7 +609,7 @@ mod tests {
let validation_rule = ValidationRule::new(0.5, fields.clone(), 0.5).unwrap();
let clear_state_value = json!({"field1": "value1", "field2": "value2"});
let pcd: BTreeMap<String, Value> = serde_json::from_value(clear_state_value).unwrap();
let pcd: Pcd = clear_state_value.try_into().unwrap();
let public_data = BTreeMap::new();
let roles = BTreeMap::new();
let attributes = BTreeMap::from_iter(pcd.into_iter().chain(public_data));
@ -641,7 +670,7 @@ mod tests {
let validation_rule = ValidationRule::new(0.5, fields.clone(), 0.5).unwrap();
let clear_state_value = json!({"field1": "value1", "field2": "value2"});
let pcd: BTreeMap<String, Value> = serde_json::from_value(clear_state_value).unwrap();
let pcd: Pcd = clear_state_value.try_into().unwrap();
let public_data = BTreeMap::new();
let roles = BTreeMap::new();
let attributes = BTreeMap::from_iter(pcd.into_iter().chain(public_data));
@ -689,7 +718,7 @@ mod tests {
let validation_rule = ValidationRule::new(1.0, fields.clone(), 0.5).unwrap();
let clear_state_value = json!({"field1": "value1", "field2": "value2"});
let pcd: BTreeMap<String, Value> = serde_json::from_value(clear_state_value).unwrap();
let pcd: Pcd = clear_state_value.try_into().unwrap();
let public_data = BTreeMap::new();
let roles = BTreeMap::new();
let attributes = BTreeMap::from_iter(pcd.into_iter().chain(public_data));
@ -731,7 +760,7 @@ mod tests {
let validation_rule = ValidationRule::new(0.5, fields.clone(), 0.5).unwrap();
let clear_state_value = json!({"field1": "value1", "field2": "value2"});
let pcd: BTreeMap<String, Value> = serde_json::from_value(clear_state_value).unwrap();
let pcd: Pcd = clear_state_value.try_into().unwrap();
let public_data = BTreeMap::new();
let roles = BTreeMap::new();
let attributes = BTreeMap::from_iter(pcd.into_iter().chain(public_data));
@ -776,7 +805,7 @@ mod tests {
)
.unwrap()]);
let clear_state_value = json!({"field1": "value1", "field2": "value2"});
let pcd: BTreeMap<String, Value> = serde_json::from_value(clear_state_value).unwrap();
let pcd: Pcd = clear_state_value.try_into().unwrap();
let public_data = BTreeMap::new();
let roles = BTreeMap::new();
let attributes = BTreeMap::from_iter(pcd.into_iter().chain(public_data));
@ -824,7 +853,7 @@ mod tests {
let new_state = json!({ "field1": "new_value1", "field2": "new_value2" });
let clear_state_value = json!({"field1": "value1", "field2": "value2"});
let pcd: BTreeMap<String, Value> = serde_json::from_value(clear_state_value).unwrap();
let pcd: Pcd = clear_state_value.try_into().unwrap();
let public_data = BTreeMap::new();
let roles = BTreeMap::new();
let attributes = BTreeMap::from_iter(pcd.into_iter().chain(public_data));
@ -876,7 +905,7 @@ mod tests {
let new_state = json!({ "field1": "new_value1", "field2": "new_value2" });
let clear_state_value = json!({"field1": "value1", "field2": "value2"});
let pcd: BTreeMap<String, Value> = serde_json::from_value(clear_state_value).unwrap();
let pcd: Pcd = clear_state_value.try_into().unwrap();
let public_data = BTreeMap::new();
let roles = BTreeMap::new();
let attributes = BTreeMap::from_iter(pcd.into_iter().chain(public_data));
@ -929,7 +958,7 @@ mod tests {
let new_state = json!({ "field1": "old_value1", "field2": "new_value2" });
let clear_state_value = json!({"field1": "value1", "field2": "value2"});
let pcd: BTreeMap<String, Value> = serde_json::from_value(clear_state_value).unwrap();
let pcd: Pcd = clear_state_value.try_into().unwrap();
let public_data = BTreeMap::new();
let roles = BTreeMap::new();
let attributes = BTreeMap::from_iter(pcd.into_iter().chain(public_data));
@ -982,7 +1011,7 @@ mod tests {
let new_state = json!({ "field1": "old_value1", "field2": "new_value2" });
let clear_state_value = json!({"field1": "value1", "field2": "value2"});
let pcd: BTreeMap<String, Value> = serde_json::from_value(clear_state_value).unwrap();
let pcd: Pcd = clear_state_value.try_into().unwrap();
let public_data = BTreeMap::new();
let roles = BTreeMap::new();
let attributes = BTreeMap::from_iter(pcd.into_iter().chain(public_data));

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@ use sp_client::bitcoin::hex::{DisplayHex, FromHex};
use sp_client::bitcoin::OutPoint;
use tsify::Tsify;
use std::collections::{BTreeMap, HashMap};
// use crate::crypto::{encrypt_with_key, decrypt_with_key};
use crate::{pcd::Member, process::Process};
#[derive(Debug, Serialize, Deserialize, Tsify)]
@ -157,3 +158,31 @@ pub mod hex_array_btree {
Ok(map)
}
}
// #[derive(Debug, Serialize, Deserialize, Tsify, Encode)]
// pub struct Payload {
// name: Option<String>,
// data: Vec<u8>,
// encrypted: bool,
// mime: Option<String>,
// }
// impl Payload {
// pub fn new_data(data: Vec<u8>) -> Self {
// Self { data, encrypted: false, name: None, mime: None }
// }
// pub fn new_file(name: String, mime: String, data: Vec<u8>) -> Self {
// Self { name: Some(name), data, mime: Some(mime), encrypted: false }
// }
// pub fn encrypt(&mut self, key: &[u8; 32]) {
// self.data = encrypt_with_key(key, &self.data).unwrap();
// self.encrypted = true;
// }
// pub fn decrypt(&mut self, key: &[u8; 32]) {
// self.data = decrypt_with_key(key, &self.data).unwrap();
// self.encrypted = false;
// }
// }