52 lines
1.2 KiB
Rust
52 lines
1.2 KiB
Rust
#![allow(dead_code)]
|
|
use std::collections::HashMap;
|
|
use std::sync::Mutex;
|
|
use ed25519_dalek::{VerifyingKey, SigningKey};
|
|
use once_cell::sync::Lazy;
|
|
use std::time::SystemTime;
|
|
|
|
#[derive(Clone)]
|
|
pub struct NodeInfo {
|
|
pubkey: VerifyingKey,
|
|
updated_at: SystemTime,
|
|
}
|
|
|
|
static NODES: Lazy<Mutex<HashMap<String, NodeInfo>>> = Lazy::new(|| {
|
|
Mutex::new(HashMap::new())
|
|
});
|
|
|
|
static KEYS: Lazy<Mutex<HashMap<VerifyingKey, SigningKey>>> = Lazy::new(|| {
|
|
Mutex::new(HashMap::new())
|
|
});
|
|
|
|
pub fn add_key(pubkey: VerifyingKey, privkey: SigningKey) {
|
|
let mut map = KEYS.lock().unwrap();
|
|
map.insert(pubkey, privkey);
|
|
}
|
|
|
|
pub fn remove_key(pubkey: &VerifyingKey) {
|
|
let mut map = KEYS.lock().unwrap();
|
|
map.remove(pubkey);
|
|
}
|
|
|
|
pub fn get_privkey(pubkey: &VerifyingKey) -> Option<SigningKey> {
|
|
let map = KEYS.lock().unwrap();
|
|
map.get(pubkey).cloned()
|
|
}
|
|
|
|
pub fn add_node(ip: String, info: NodeInfo) {
|
|
let mut map = NODES.lock().unwrap();
|
|
map.insert(ip, info);
|
|
}
|
|
|
|
pub fn remove_node(ip: &str) {
|
|
let mut map = NODES.lock().unwrap();
|
|
map.remove(ip);
|
|
}
|
|
|
|
pub fn get_pubkey(ip: &str) -> Option<NodeInfo> {
|
|
let map = NODES.lock().unwrap();
|
|
map.get(ip).cloned()
|
|
}
|
|
|