work
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use ed25519_dalek::SigningKey;
|
||||
use rand::{
|
||||
SeedableRng,
|
||||
rngs::{OsRng, StdRng},
|
||||
rngs::{StdRng, SysRng},
|
||||
};
|
||||
|
||||
pub struct Account {
|
||||
@@ -11,7 +11,7 @@ pub struct Account {
|
||||
|
||||
impl Account {
|
||||
pub fn new() -> Account {
|
||||
let mut csprng = StdRng::try_from_rng(&mut OsRng).unwrap();
|
||||
let mut csprng = StdRng::try_from_rng(&mut SysRng).unwrap();
|
||||
let device_key = SigningKey::generate(&mut csprng);
|
||||
let account_key = SigningKey::generate(&mut csprng);
|
||||
Account {
|
||||
|
||||
@@ -40,6 +40,7 @@ pub fn start(rsc: &mut Rsc) -> WeakWidget {
|
||||
|
||||
pub fn create_account(rsc: &mut Rsc) -> WeakWidget {
|
||||
let url = field("", "server", rsc);
|
||||
let token = field("", "account token", rsc);
|
||||
let username = field("", "username", rsc);
|
||||
let password = field("", "password", rsc);
|
||||
|
||||
@@ -47,6 +48,7 @@ pub fn create_account(rsc: &mut Rsc) -> WeakWidget {
|
||||
rsc.events.register(create, Submit, move |ctx, rsc| {
|
||||
create.disable(rsc);
|
||||
let url = rsc[url].content();
|
||||
let token = rsc[token].content();
|
||||
let username = rsc[username].content();
|
||||
let password = rsc[password].content();
|
||||
let login_key = ctx.state.data.login_key(&username);
|
||||
@@ -72,6 +74,7 @@ pub fn create_account(rsc: &mut Rsc) -> WeakWidget {
|
||||
.request(CreateAccount {
|
||||
username,
|
||||
password,
|
||||
token,
|
||||
login_key,
|
||||
})
|
||||
.await
|
||||
@@ -84,6 +87,7 @@ pub fn create_account(rsc: &mut Rsc) -> WeakWidget {
|
||||
(
|
||||
wtext("Create Account").text_align(Align::CENTER).size(30),
|
||||
field_box(url, rsc),
|
||||
field_box(token, rsc),
|
||||
field_box(username, rsc),
|
||||
field_box(password, rsc),
|
||||
create,
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
use std::{
|
||||
marker::PhantomData,
|
||||
ops::{Deref, DerefMut},
|
||||
path::Path,
|
||||
};
|
||||
use std::{marker::PhantomData, path::Path};
|
||||
|
||||
use bitcode::{Decode, DecodeOwned, Encode};
|
||||
use fjall::{Database, Keyspace, KeyspaceCreateOptions, UserValue};
|
||||
|
||||
pub const DB_VERSION: u64 = 0;
|
||||
pub struct DbU64(u64);
|
||||
pub const DB_VERSION: DbU64 = DbU64(0);
|
||||
|
||||
impl Into<UserValue> for DbU64 {
|
||||
fn into(self) -> UserValue {
|
||||
UserValue::new(&self.0.to_be_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode)]
|
||||
pub struct User {
|
||||
@@ -29,7 +33,7 @@ pub struct Db {
|
||||
}
|
||||
|
||||
pub struct DbMap<K, V> {
|
||||
tree: fjall::Tree,
|
||||
keyspace: Keyspace,
|
||||
_pd: PhantomData<(K, V)>,
|
||||
}
|
||||
|
||||
@@ -64,42 +68,43 @@ impl Key for u64 {
|
||||
|
||||
impl<K: Key, V: Encode + DecodeOwned> DbMap<K, V> {
|
||||
pub fn insert(&self, k: &K, v: &V) {
|
||||
self.tree.insert_(k, v);
|
||||
self.keyspace.insert_(k, v);
|
||||
}
|
||||
|
||||
pub fn get(&self, k: &K) -> Option<V> {
|
||||
self.tree.get_(k)
|
||||
self.keyspace.get_(k)
|
||||
}
|
||||
|
||||
pub fn init_unique(&self, k: &K) -> bool {
|
||||
self.tree
|
||||
self.keyspace
|
||||
.compare_and_swap(k.bytes(), None as Option<&[u8]>, Some(&[0]))
|
||||
.unwrap()
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn iter_all(&self) -> impl Iterator<Item = V> {
|
||||
self.tree.iter_all()
|
||||
self.keyspace.iter_all()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_db(path: impl AsRef<Path>) -> Db {
|
||||
let db = sled::open(path).expect("failed to open database");
|
||||
if !db.was_recovered() {
|
||||
let config = fjall::Config::new(path.as_ref());
|
||||
let db = Database::open(config).expect("failed to open database");
|
||||
let info = open_ks("info", &db);
|
||||
if !info.contains_key("version").unwrap() {
|
||||
println!("no previous db found, creating new");
|
||||
db.insert_("version", DB_VERSION);
|
||||
db.flush().unwrap();
|
||||
info.insert("version", DB_VERSION);
|
||||
} else {
|
||||
let version: u64 = db.get_("version").expect("failed to read db version");
|
||||
let version: u64 = info.get("version").expect("failed to read db version");
|
||||
println!("found existing db version {version}");
|
||||
if version != DB_VERSION {
|
||||
panic!("non matching db version! (auto update in the future)");
|
||||
}
|
||||
}
|
||||
Db {
|
||||
msgs: open_tree("msg", &db),
|
||||
users: open_tree("user", &db),
|
||||
usernames: open_tree("username", &db),
|
||||
msgs: open_ks("msg", &db),
|
||||
users: open_ks("user", &db),
|
||||
usernames: open_ks("username", &db),
|
||||
db,
|
||||
}
|
||||
}
|
||||
@@ -134,32 +139,25 @@ impl DbUtil for Tree {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_tree<K, V>(name: &str, db: &sled::Db) -> DbMap<K, V> {
|
||||
pub fn open_ks<K, V>(name: &str, db: &Database) -> DbMap<K, V> {
|
||||
DbMap {
|
||||
tree: db.open_tree(name).unwrap(),
|
||||
keyspace: db.keyspace(name, KeyspaceCreateOptions::default).unwrap(),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Db {
|
||||
type Target = sled::Db;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.db
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Db {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.db
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Clone for DbMap<K, V> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
tree: self.tree.clone(),
|
||||
keyspace: self.keyspace.clone(),
|
||||
_pd: self._pd,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Db {
|
||||
pub fn flush(&self) {
|
||||
// test to see if it gets dropped and just works
|
||||
// self.db.persist(fjall::PersistMode::SyncAll).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ pub async fn run_server(port: u16) {
|
||||
let _ = handle.await;
|
||||
endpoint.wait_idle().await;
|
||||
println!("saving...");
|
||||
db.flush_async().await.unwrap();
|
||||
db.flush();
|
||||
println!("saved");
|
||||
}
|
||||
|
||||
@@ -162,6 +162,7 @@ impl RecvHandler<ClientMsgInst> for ClientHandler {
|
||||
}
|
||||
ClientMsg::CreateAccount(info) => {
|
||||
let CreateAccount {
|
||||
token,
|
||||
username,
|
||||
password,
|
||||
login_key,
|
||||
|
||||
Reference in New Issue
Block a user