sdk_client/src/lib.rs
2024-09-23 12:44:20 +02:00

70 lines
2.0 KiB
Rust

#![allow(warnings)]
use anyhow::Error;
use sdk_common::crypto::AnkSharedSecret;
use sdk_common::network::CachedMessage;
use sdk_common::pcd::AnkPcdHash;
use sdk_common::prd::Prd;
use sdk_common::process::Process;
use sdk_common::signature::Proof;
use sdk_common::sp_client::bitcoin::OutPoint;
use sdk_common::uuid::Uuid;
use sdk_common::MutexExt;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::ops::Index;
use std::sync::{Mutex, MutexGuard, OnceLock};
use tsify::Tsify;
pub mod api;
mod peers;
mod user;
mod wallet;
pub static CACHEDMESSAGES: OnceLock<Mutex<Vec<CachedMessage>>> = OnceLock::new();
pub fn lock_messages() -> Result<MutexGuard<'static, Vec<CachedMessage>>, Error> {
CACHEDMESSAGES
.get_or_init(|| Mutex::new(vec![]))
.lock_anyhow()
}
// TODO move to sdk-common
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ProcessState {
pub commited_in: OutPoint,
pub encrypted_pcd: Value,
pub keys: Map<String, Value>, // We may not always have all the keys
pub validation_token: Vec<Proof>, // This signs the encrypted pcd
}
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct RelevantProcess {
states: Vec<ProcessState>,
shared_secrets: HashMap<String, String>,
impending_requests: Vec<Prd>,
}
impl RelevantProcess {
pub fn get_status_at(&self, index: usize) -> Option<ProcessState> {
self.states.get(index).cloned()
}
pub fn get_latest_state(&self) -> Option<ProcessState> {
self.states.last().cloned()
}
pub fn get_impending_requests(&self) -> Vec<Prd> {
self.impending_requests.clone()
}
}
pub static CACHEDPROCESSES: OnceLock<Mutex<HashMap<OutPoint, RelevantProcess>>> = OnceLock::new();
pub fn lock_processes() -> Result<MutexGuard<'static, HashMap<OutPoint, RelevantProcess>>, Error> {
CACHEDPROCESSES
.get_or_init(|| Mutex::new(HashMap::new()))
.lock_anyhow()
}