2021-01-18 03:57:10 +03:00
|
|
|
// store list of trusts
|
|
|
|
// check if trust is already in list before adding
|
2021-01-20 00:17:24 +03:00
|
|
|
// if there is an older trust - don't add received trust
|
|
|
|
|
2021-01-20 16:14:01 +03:00
|
|
|
use fce_sqlite_connector;
|
|
|
|
use fce_sqlite_connector::Value;
|
|
|
|
use fce_sqlite_connector::{Connection, State};
|
2021-01-20 00:17:24 +03:00
|
|
|
use fluence_identity::public_key::PublicKey;
|
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
use std::time::Duration;
|
2021-01-20 16:14:01 +03:00
|
|
|
use trust_graph::{Auth, PublicKeyHashable, Revoke, Storage, TrustGraph, TrustNode, Weight};
|
2021-01-20 00:17:24 +03:00
|
|
|
|
|
|
|
static INSTANCE: OnceCell<Mutex<TrustGraph>> = OnceCell::new();
|
|
|
|
|
|
|
|
fn get_data() -> &'static Mutex<TrustGraph> {
|
|
|
|
INSTANCE.get_or_init(|| {
|
|
|
|
let db_path = "/var/folders/ww/v__xg0cj17x7h7sf3bgwpx8h0000gn/T/4589ab6f-5440-4933-ace5-a62714784142/tmp/users.sqlite";
|
|
|
|
let connection = fce_sqlite_connector::open(db_path).unwrap();
|
|
|
|
Mutex::new(TrustGraph::new(Box::new(SqliteStorage {connection})))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SqliteStorage {
|
|
|
|
connection: Connection,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SqliteStorage {
|
|
|
|
pub fn init(&self) {
|
2021-01-20 16:14:01 +03:00
|
|
|
let init_sql = "CREATE TABLE IF NOT EXISTS trustnodes(\
|
|
|
|
public_key TEXT PRIMARY KEY,\
|
|
|
|
trustnode TEXT NOT NULL,\
|
2021-01-20 00:17:24 +03:00
|
|
|
);";
|
2021-01-20 16:14:01 +03:00
|
|
|
|
|
|
|
self.connection.execute(init_sql).unwrap();
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Storage for SqliteStorage {
|
|
|
|
fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode> {
|
|
|
|
None
|
|
|
|
}
|
2021-01-20 16:14:01 +03:00
|
|
|
|
2021-01-20 00:17:24 +03:00
|
|
|
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) {
|
2021-01-20 16:14:01 +03:00
|
|
|
let mut cursor = self
|
|
|
|
.connection
|
|
|
|
.prepare("INSERT INTO trustnodes VALUES (?, ?)")
|
|
|
|
.unwrap()
|
|
|
|
.cursor();
|
|
|
|
|
|
|
|
let tn_str = serde_json::to_string(&node).unwrap();
|
|
|
|
|
|
|
|
cursor.bind(&[Value::String(format!("{}", pk))]).unwrap();
|
|
|
|
cursor
|
|
|
|
.bind(&[Value::String(format!("{}", tn_str))])
|
|
|
|
.unwrap();
|
2021-01-20 00:17:24 +03:00
|
|
|
|
2021-01-20 16:14:01 +03:00
|
|
|
cursor.next().unwrap();
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-01-20 16:14:01 +03:00
|
|
|
fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {}
|
|
|
|
|
2021-01-20 00:17:24 +03:00
|
|
|
fn root_keys(&self) -> Vec<PublicKeyHashable> {
|
|
|
|
vec![]
|
|
|
|
}
|
2021-01-20 16:14:01 +03:00
|
|
|
|
2021-01-20 00:17:24 +03:00
|
|
|
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String> {
|
|
|
|
Err("not implemented".to_string())
|
|
|
|
}
|
2021-01-20 16:14:01 +03:00
|
|
|
|
2021-01-20 00:17:24 +03:00
|
|
|
fn update_auth(
|
|
|
|
&mut self,
|
|
|
|
pk: &PublicKeyHashable,
|
|
|
|
auth: Auth,
|
|
|
|
issued_for: &PublicKey,
|
|
|
|
cur_time: Duration,
|
|
|
|
) {
|
|
|
|
}
|
2021-01-20 16:14:01 +03:00
|
|
|
}
|