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();
|
|
|
|
|
2021-01-20 17:21:02 +03:00
|
|
|
pub fn get_data() -> &'static Mutex<TrustGraph> {
|
2021-01-20 00:17:24 +03:00
|
|
|
INSTANCE.get_or_init(|| {
|
2021-01-20 17:21:02 +03:00
|
|
|
let db_path = "/tmp/users.sqlite";
|
2021-01-20 00:17:24 +03:00
|
|
|
let connection = fce_sqlite_connector::open(db_path).unwrap();
|
|
|
|
|
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
|
|
|
|
2021-01-20 17:21:02 +03:00
|
|
|
connection.execute(init_sql).expect("cannot connect to db");
|
|
|
|
|
|
|
|
Mutex::new(TrustGraph::new(Box::new(SqliteStorage { connection })))
|
|
|
|
})
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
|
|
|
|
2021-01-20 17:21:02 +03:00
|
|
|
struct SqliteStorage {
|
|
|
|
connection: Connection,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SqliteStorage {}
|
|
|
|
|
2021-01-20 00:17:24 +03:00
|
|
|
impl Storage for SqliteStorage {
|
2021-01-20 17:21:02 +03:00
|
|
|
fn get(&self, pk: &PublicKeyHashable) -> Option<TrustNode> {
|
|
|
|
let mut cursor = self
|
|
|
|
.connection
|
|
|
|
.prepare("SELECT trustnode FROM trustnodes WHERE public_key = ?")
|
|
|
|
.expect("unexpected: 'get' request should be correct")
|
|
|
|
.cursor();
|
|
|
|
|
|
|
|
cursor
|
|
|
|
.bind(&[Value::String(format!("{}", pk))])
|
|
|
|
.expect("unexpected: 'public_key' field should be string");
|
|
|
|
|
|
|
|
match cursor.next().unwrap() {
|
|
|
|
Some(r) => {
|
|
|
|
let tn_str = r[0]
|
|
|
|
.as_string()
|
|
|
|
.expect("unexpected: 'trustnode' in a table should be as string");
|
|
|
|
let trust_node: TrustNode = serde_json::from_str(tn_str)
|
|
|
|
.expect("unexpected: 'trustnode' should be as correct json");
|
|
|
|
Some(trust_node)
|
|
|
|
}
|
|
|
|
|
|
|
|
None => None,
|
|
|
|
}
|
2021-01-20 00:17:24 +03:00
|
|
|
}
|
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
|
|
|
}
|