From 481c4a96fd2f002a86bd438c07e0da4b11190d84 Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Fri, 10 May 2024 17:42:59 +0300 Subject: [PATCH] getting dressed for first release --- nextgraph/examples/in_memory.rs | 15 +- nextgraph/examples/open.rs | 13 +- nextgraph/examples/persistent.rs | 19 +- nextgraph/src/local_broker.rs | 65 ++-- ng-app/src-tauri/src/lib.rs | 56 ++-- ng-app/src/App.svelte | 14 +- ng-app/src/lib/Install.svelte | 4 +- ng-app/src/lib/Login.svelte | 5 - ng-app/src/lib/Test.svelte | 6 +- ng-app/src/routes/Test.svelte | 2 +- ng-app/src/routes/User.svelte | 3 + ng-app/src/routes/WalletCreate.svelte | 7 +- ng-app/src/routes/WalletLogin.svelte | 7 +- ng-broker/src/rocksdb_server_storage.rs | 58 ++-- ng-broker/src/server_broker.rs | 28 +- ng-broker/src/server_storage/admin/account.rs | 9 +- .../src/server_storage/admin/invitation.rs | 18 +- ng-broker/src/server_storage/admin/wallet.rs | 9 +- ng-broker/src/server_storage/config.rs | 105 ------- ng-broker/src/server_storage/core/commit.rs | 12 +- ng-broker/src/server_storage/core/overlay.rs | 9 +- ng-broker/src/server_storage/core/peer.rs | 9 +- ng-broker/src/server_storage/core/repo.rs | 10 +- ng-broker/src/server_storage/core/topic.rs | 5 +- ng-broker/src/server_storage/mod.rs | 2 - ng-broker/src/server_ws.rs | 69 ++--- ng-broker/src/types.rs | 7 +- ng-client-ws/src/remote_ws.rs | 47 ++- ng-client-ws/src/remote_ws_wasm.rs | 38 ++- ng-net/src/actor.rs | 18 +- ng-net/src/actors/admin/add_invitation.rs | 19 +- ng-net/src/actors/admin/add_user.rs | 18 +- ng-net/src/actors/admin/del_user.rs | 16 +- ng-net/src/actors/admin/list_invitations.rs | 20 +- ng-net/src/actors/admin/list_users.rs | 20 +- ng-net/src/actors/client/blocks_exist.rs | 16 +- ng-net/src/actors/client/blocks_get.rs | 19 +- ng-net/src/actors/client/blocks_put.rs | 16 +- ng-net/src/actors/client/commit_get.rs | 19 +- ng-net/src/actors/client/event.rs | 28 +- ng-net/src/actors/client/pin_repo.rs | 17 +- ng-net/src/actors/client/repo_pin_status.rs | 16 +- ng-net/src/actors/client/topic_sub.rs | 15 +- ng-net/src/actors/client/topic_sync_req.rs | 19 +- ng-net/src/actors/connecting.rs | 20 +- ng-net/src/actors/noise.rs | 11 +- ng-net/src/actors/probe.rs | 18 +- ng-net/src/actors/start.rs | 31 +- ng-net/src/broker.rs | 74 +++-- ng-net/src/connection.rs | 37 ++- ng-net/src/errors.rs | 84 ------ ng-net/src/lib.rs | 4 - ng-net/src/server_broker.rs | 3 +- ng-net/src/types.rs | 33 +- ng-net/src/utils.rs | 23 +- ng-repo/src/block.rs | 10 +- ng-repo/src/block_storage.rs | 13 +- ng-repo/src/branch.rs | 128 +++++--- ng-repo/src/commit.rs | 20 +- ng-repo/src/errors.rs | 11 +- ng-repo/src/event.rs | 16 +- ng-repo/src/file.rs | 9 +- ng-repo/src/kcv_storage.rs | 67 ++++- ng-repo/src/object.rs | 35 ++- ng-repo/src/os_info.rs | 11 +- ng-repo/src/repo.rs | 33 +- ng-repo/src/store.rs | 17 +- ng-repo/src/types.rs | 23 +- ng-repo/src/utils.rs | 10 +- ng-sdk-js/README.md | 2 +- ng-sdk-js/src/lib.rs | 191 +++--------- ng-storage-rocksdb/src/block_storage.rs | 25 +- ng-storage-rocksdb/src/kcv_storage.rs | 33 +- ng-verifier/src/commits/mod.rs | 76 +++-- ng-verifier/src/request_processor.rs | 26 +- ng-verifier/src/rocksdb_user_storage.rs | 26 +- ng-verifier/src/site.rs | 21 +- ng-verifier/src/types.rs | 32 +- ng-verifier/src/user_storage/branch.rs | 25 +- ng-verifier/src/user_storage/repo.rs | 27 +- ng-verifier/src/user_storage/storage.rs | 35 ++- ng-verifier/src/verifier.rs | 282 ++++++++---------- ng-wallet/src/bip39.rs | 1 + ng-wallet/src/emojis.rs | 4 +- ng-wallet/src/lib.rs | 61 ++-- ng-wallet/src/types.rs | 20 +- ngaccount/src/main.rs | 37 ++- ngaccount/src/types.rs | 3 +- ngcli/src/main.rs | 42 +-- ngd/src/cli.rs | 1 - ngd/src/main.rs | 67 ++--- 91 files changed, 1260 insertions(+), 1445 deletions(-) delete mode 100644 ng-broker/src/server_storage/config.rs delete mode 100644 ng-net/src/errors.rs diff --git a/nextgraph/examples/in_memory.rs b/nextgraph/examples/in_memory.rs index c5c74d0..03d451c 100644 --- a/nextgraph/examples/in_memory.rs +++ b/nextgraph/examples/in_memory.rs @@ -7,6 +7,9 @@ // notice may not be copied, modified, or distributed except // according to those terms. +use std::fs::read; + +#[allow(unused_imports)] use nextgraph::local_broker::{ app_request, app_request_stream, init_local_broker, session_start, session_stop, user_connect, user_disconnect, wallet_close, wallet_create_v0, wallet_get, wallet_get_file, wallet_import, @@ -19,9 +22,6 @@ use nextgraph::repo::types::PubKey; use nextgraph::wallet::types::CreateWalletV0; use nextgraph::wallet::{display_mnemonic, emojis::display_pazzle}; -use std::env::current_dir; -use std::fs::read; - #[async_std::main] async fn main() -> std::io::Result<()> { // initialize the local_broker with in-memory config. @@ -64,7 +64,12 @@ async fn main() -> std::io::Result<()> { let mut pazzle_words = vec![]; println!("Your pazzle is: {:?}", wallet_result.pazzle); for emoji in pazzle { - println!(" {}:\t{}", emoji.0, emoji.1); + println!( + "\t{}:\t{}{}", + emoji.0, + if emoji.0.len() > 12 { "" } else { "\t" }, + emoji.1 + ); pazzle_words.push(emoji.1.to_string()); } println!("Your mnemonic is:"); @@ -104,7 +109,7 @@ async fn main() -> std::io::Result<()> { // if you have saved the wallet locally (which we haven't done in the example above, see `local_save: false`), next time you want to connect, // you can retrieve the wallet, display the security phrase and image to the user, ask for the pazzle or mnemonic, and then open the wallet // if you haven't saved the wallet, the next line will not work once you restart the LocalBroker. - let wallet = wallet_get(&wallet_result.wallet_name).await?; + let _wallet = wallet_get(&wallet_result.wallet_name).await?; // at this point, the wallet is kept in the internal memory of the LocalBroker // and it hasn't been opened yet, so it is not usable right away. diff --git a/nextgraph/examples/open.rs b/nextgraph/examples/open.rs index 0930f5c..56c698c 100644 --- a/nextgraph/examples/open.rs +++ b/nextgraph/examples/open.rs @@ -7,21 +7,16 @@ // notice may not be copied, modified, or distributed except // according to those terms. +use std::env::current_dir; +use std::fs::create_dir_all; + +#[allow(unused_imports)] use nextgraph::local_broker::{ app_request, app_request_stream, init_local_broker, session_start, session_stop, user_connect, user_disconnect, wallet_close, wallet_create_v0, wallet_get, wallet_get_file, wallet_import, wallet_open_with_pazzle, wallet_open_with_pazzle_words, wallet_read_file, wallet_was_opened, LocalBrokerConfig, SessionConfig, }; -use nextgraph::net::types::BootstrapContentV0; -use nextgraph::repo::errors::NgError; -use nextgraph::repo::types::PubKey; -use nextgraph::wallet::types::CreateWalletV0; -use nextgraph::wallet::{display_mnemonic, emojis::display_pazzle}; - -use std::env::current_dir; -use std::fs::create_dir_all; -use std::fs::read; #[async_std::main] async fn main() -> std::io::Result<()> { diff --git a/nextgraph/examples/persistent.rs b/nextgraph/examples/persistent.rs index 6123d2f..2c3af54 100644 --- a/nextgraph/examples/persistent.rs +++ b/nextgraph/examples/persistent.rs @@ -7,6 +7,11 @@ // notice may not be copied, modified, or distributed except // according to those terms. +use std::env::current_dir; +use std::fs::create_dir_all; +use std::fs::read; + +#[allow(unused_imports)] use nextgraph::local_broker::{ app_request, app_request_stream, init_local_broker, session_start, session_stop, user_connect, user_disconnect, wallet_close, wallet_create_v0, wallet_get, wallet_get_file, wallet_import, @@ -14,15 +19,10 @@ use nextgraph::local_broker::{ SessionConfig, }; use nextgraph::net::types::BootstrapContentV0; -use nextgraph::repo::errors::NgError; use nextgraph::repo::types::PubKey; use nextgraph::wallet::types::CreateWalletV0; use nextgraph::wallet::{display_mnemonic, emojis::display_pazzle}; -use std::env::current_dir; -use std::fs::create_dir_all; -use std::fs::read; - #[async_std::main] async fn main() -> std::io::Result<()> { // get the current working directory @@ -72,7 +72,12 @@ async fn main() -> std::io::Result<()> { let mut pazzle_words = vec![]; println!("Your pazzle is: {:?}", wallet_result.pazzle); for emoji in pazzle { - println!(" {}:\t{}", emoji.0, emoji.1); + println!( + "\t{}:\t{}{}", + emoji.0, + if emoji.0.len() > 12 { "" } else { "\t" }, + emoji.1 + ); pazzle_words.push(emoji.1.to_string()); } println!("Your mnemonic is:"); @@ -111,7 +116,7 @@ async fn main() -> std::io::Result<()> { // as we have saved the wallet, the next time we want to connect, // we can retrieve the wallet, display the security phrase and image to the user, ask for the pazzle or mnemonic, and then open the wallet - let wallet = wallet_get(&wallet_result.wallet_name).await?; + let _wallet = wallet_get(&wallet_result.wallet_name).await?; // at this point, the wallet is kept in the internal memory of the LocalBroker // and it hasn't been opened yet, so it is not usable right away. diff --git a/nextgraph/src/local_broker.rs b/nextgraph/src/local_broker.rs index 90df58f..5e6744f 100644 --- a/nextgraph/src/local_broker.rs +++ b/nextgraph/src/local_broker.rs @@ -7,34 +7,38 @@ // notice may not be copied, modified, or distributed except // according to those terms. -use async_once_cell::OnceCell; -use async_std::sync::{Arc, Mutex, RwLock, RwLockReadGuard}; use core::fmt; +use std::collections::HashMap; +use std::fs::{read, remove_file, write}; +use std::path::PathBuf; + +use async_once_cell::OnceCell; +use async_std::sync::{Arc, Mutex, RwLock}; use futures::channel::mpsc; use futures::SinkExt; -use ng_net::actor::EActor; -use ng_net::connection::{ClientConfig, IConnect, NoiseFSM, StartConfig}; -use ng_net::types::{ClientInfo, ClientType, ProtocolMessage}; -use ng_net::utils::{Receiver, Sender}; -use ng_repo::block_storage::HashMapBlockStorage; -use ng_repo::os_info::get_os_info; -use ng_verifier::types::*; -use ng_verifier::verifier::Verifier; -use ng_wallet::emojis::encode_pazzle; use once_cell::sync::Lazy; use serde_bare::to_vec; use serde_json::json; -use std::collections::HashMap; -use std::fs::{read, remove_file, write, File, OpenOptions}; -use std::path::PathBuf; -use zeroize::{Zeroize, ZeroizeOnDrop}; +use zeroize::Zeroize; -use ng_net::broker::*; use ng_repo::block_storage::BlockStorage; +use ng_repo::block_storage::HashMapBlockStorage; use ng_repo::errors::{NgError, ProtocolError}; use ng_repo::log::*; +use ng_repo::os_info::get_os_info; use ng_repo::types::*; use ng_repo::utils::derive_key; + +use ng_net::actor::EActor; +use ng_net::broker::*; +use ng_net::connection::{ClientConfig, IConnect, NoiseFSM, StartConfig}; +use ng_net::types::{ClientInfo, ClientType, ProtocolMessage}; +use ng_net::utils::{Receiver, Sender}; + +use ng_verifier::types::*; +use ng_verifier::verifier::Verifier; + +use ng_wallet::emojis::encode_pazzle; use ng_wallet::{create_wallet_first_step_v0, create_wallet_second_step_v0, types::*}; #[cfg(not(target_arch = "wasm32"))] @@ -192,6 +196,7 @@ impl LocalBrokerConfig { _ => None, } } + #[cfg(not(target_family = "wasm"))] fn compute_path(&self, dir: &String) -> Result { match self { Self::BasePath(path) => { @@ -221,6 +226,7 @@ pub enum SessionConfig { struct Session { config: SessionConfig, peer_key: PrivKey, + #[allow(dead_code)] last_wallet_nonce: u64, verifier: Verifier, } @@ -536,6 +542,8 @@ impl LocalBroker { ) -> Result { let broker = self; + //log_info!("wallet_was_opened {}", wallet.id()); + match broker.opened_wallets.get(&wallet.id()) { Some(opened_wallet) => { return Ok(opened_wallet.wallet.client().to_owned().unwrap()); @@ -591,7 +599,7 @@ impl LocalBroker { wallet, block_storage, }; - + //log_info!("inserted wallet_was_opened {}", wallet_id); broker.opened_wallets.insert(wallet_id, opened_wallet); Ok(client) } @@ -845,7 +853,7 @@ impl LocalBroker { return Err(NgError::IoError); } } - _ => panic!("wrong LocalBrokerConfig"), + _ => return Err(NgError::CannotSaveWhenInMemoryConfig), } Ok(()) } @@ -1064,6 +1072,7 @@ pub async fn wallets_reload() -> Result<(), NgError> { base64_url::decode(&wallets_string).map_err(|_| NgError::SerializationError)?; let wallets: LocalWalletStorage = serde_bare::from_slice(&map_ser)?; let LocalWalletStorage::V0(v0) = wallets; + //log_info!("adding wallet {:?}", v0); broker.wallets.extend(v0); } _ => {} @@ -1074,7 +1083,7 @@ pub async fn wallets_reload() -> Result<(), NgError> { #[doc(hidden)] /// This should not be used by programmers. Only here because the JS SDK needs it. /// -/// It will throw and error if you use it. +/// It will throw an error if you use it. pub async fn wallet_add(lws: LocalWalletStorageV0) -> Result<(), NgError> { let mut broker = match LOCAL_BROKER.get() { None | Some(Err(_)) => return Err(NgError::LocalBrokerNotInitialized), @@ -1210,7 +1219,7 @@ pub async fn wallet_import( /// this is a separate step because in JS webapp, the opening of a wallet takes time and freezes the GUI. /// We need to run it in the background in a WebWorker. but there, the LocalBroker cannot access localStorage... /// So a separate function must be called, once the WebWorker is done. -pub async fn wallet_was_opened(mut wallet: SensitiveWallet) -> Result { +pub async fn wallet_was_opened(wallet: SensitiveWallet) -> Result { let mut broker = match LOCAL_BROKER.get() { None | Some(Err(_)) => return Err(NgError::LocalBrokerNotInitialized), Some(Ok(broker)) => broker.write().await, @@ -1224,7 +1233,7 @@ pub async fn wallet_was_opened(mut wallet: SensitiveWallet) -> Result Result { +pub async fn session_start(config: SessionConfig) -> Result { let mut broker = match LOCAL_BROKER.get() { None | Some(Err(_)) => return Err(NgError::LocalBrokerNotInitialized), Some(Ok(broker)) => broker.write().await, @@ -1324,7 +1333,11 @@ pub async fn user_connect_with_device_info( let user_id = user.to_string(); let peer_key = &session.peer_key; let peer_id = peer_key.to_pub(); - log_info!("local peer_id {}", peer_id); + log_info!( + "connecting with local peer_id {} for user {}", + peer_id, + user_id + ); let site = wallet.sites.get(&user_id); if site.is_none() { result.push(( @@ -1363,7 +1376,7 @@ pub async fn user_connect_with_device_info( //Option<(String, Vec)> if url.is_some() { let url = url.unwrap(); - if url.1.len() == 0 { + if url.1.is_empty() { // TODO deal with Box(Dyn)Public -> tunnel, and on tauri/forward/CLIs, deal with all Box -> direct connections (when url.1.len is > 0) let res = BROKER .write() @@ -1488,16 +1501,14 @@ pub async fn wallet_close(wallet_name: &String) -> Result<(), NgError> { } /// (not implemented yet) -pub async fn wallet_remove(wallet_name: String) -> Result<(), NgError> { - let mut broker = match LOCAL_BROKER.get() { +pub async fn wallet_remove(_wallet_name: String) -> Result<(), NgError> { + let _broker = match LOCAL_BROKER.get() { None | Some(Err(_)) => return Err(NgError::LocalBrokerNotInitialized), Some(Ok(broker)) => broker.write().await, }; todo!(); // should close the wallet, then remove all the saved sessions and remove the wallet - - Ok(()) } // /// fetches a document's content. diff --git a/ng-app/src-tauri/src/lib.rs b/ng-app/src-tauri/src/lib.rs index cc4fce7..941ada8 100644 --- a/ng-app/src-tauri/src/lib.rs +++ b/ng-app/src-tauri/src/lib.rs @@ -6,25 +6,29 @@ // at your option. All files in the project carrying such // notice may not be copied, modified, or distributed except // according to those terms. + +use std::collections::HashMap; +use std::fs::write; + use async_std::stream::StreamExt; -use nextgraph::local_broker::*; -use nextgraph::verifier::types::*; -use ng_net::broker::*; -use ng_net::types::{ClientInfo, CreateAccountBSP, Invitation}; -use ng_net::utils::{decode_invitation_string, spawn_and_log_error, Receiver, ResultSend}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use tauri::scope::ipc::RemoteDomainAccessScope; +use tauri::utils::config::WindowConfig; +use tauri::{path::BaseDirectory, App, Manager}; + use ng_repo::errors::NgError; use ng_repo::log::*; use ng_repo::types::*; + +use ng_net::types::{ClientInfo, CreateAccountBSP, Invitation}; +use ng_net::utils::{decode_invitation_string, spawn_and_log_error, Receiver, ResultSend}; + use ng_wallet::types::*; use ng_wallet::*; -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use std::collections::HashMap; -use std::fs::{read, write, File, OpenOptions}; -use std::io::Write; -use tauri::scope::ipc::RemoteDomainAccessScope; -use tauri::utils::config::WindowConfig; -use tauri::{path::BaseDirectory, App, Manager, Window}; + +use nextgraph::local_broker::*; +use nextgraph::verifier::types::*; #[cfg(mobile)] mod mobile; @@ -43,10 +47,10 @@ async fn test(app: tauri::AppHandle) -> Result<(), ()> { init_local_broker(Box::new(move || LocalBrokerConfig::BasePath(path.clone()))).await; //log_debug!("test is {}", BROKER.read().await.test()); - let path = app - .path() - .resolve("storage", BaseDirectory::AppLocalData) - .map_err(|_| ())?; + // let path = app + // .path() + // .resolve("storage", BaseDirectory::AppLocalData) + // .map_err(|_| ())?; //BROKER.read().await.test_storage(path); @@ -73,7 +77,7 @@ async fn wallet_open_with_pazzle( wallet: Wallet, pazzle: Vec, pin: [u8; 4], - app: tauri::AppHandle, + _app: tauri::AppHandle, ) -> Result { //log_debug!("wallet_open_with_pazzle from rust {:?}", pazzle); let wallet = nextgraph::local_broker::wallet_open_with_pazzle(&wallet, pazzle, pin) @@ -126,7 +130,7 @@ async fn wallet_create( } #[tauri::command(rename_all = "snake_case")] -async fn wallet_read_file(file: Vec, app: tauri::AppHandle) -> Result { +async fn wallet_read_file(file: Vec, _app: tauri::AppHandle) -> Result { nextgraph::local_broker::wallet_read_file(file) .await .map_err(|e: NgError| e.to_string()) @@ -135,7 +139,7 @@ async fn wallet_read_file(file: Vec, app: tauri::AppHandle) -> Result Result { nextgraph::local_broker::wallet_was_opened(opened_wallet) .await @@ -147,7 +151,7 @@ async fn wallet_import( encrypted_wallet: Wallet, opened_wallet: SensitiveWallet, in_memory: bool, - app: tauri::AppHandle, + _app: tauri::AppHandle, ) -> Result { nextgraph::local_broker::wallet_import(encrypted_wallet, opened_wallet, in_memory) .await @@ -178,7 +182,7 @@ async fn get_wallets( async fn session_start( wallet_name: String, user: PubKey, - app: tauri::AppHandle, + _app: tauri::AppHandle, ) -> Result { let config = SessionConfig::new_save(&user, &wallet_name); nextgraph::local_broker::session_start(config) @@ -191,7 +195,7 @@ async fn session_start_remote( wallet_name: String, user: PubKey, peer_id: Option, - app: tauri::AppHandle, + _app: tauri::AppHandle, ) -> Result { let config = SessionConfig::new_remote(&user, &wallet_name, peer_id); nextgraph::local_broker::session_start(config) @@ -292,7 +296,7 @@ async fn doc_fetch_private_subscribe() -> Result { async fn app_request( session_id: u64, request: AppRequest, - app: tauri::AppHandle, + _app: tauri::AppHandle, ) -> Result { log_debug!("app request {:?}", request); @@ -307,7 +311,7 @@ async fn upload_chunk( upload_id: u32, chunk: serde_bytes::ByteBuf, nuri: NuriV0, - app: tauri::AppHandle, + _app: tauri::AppHandle, ) -> Result { //log_debug!("upload_chunk {:?}", chunk); @@ -399,7 +403,7 @@ struct ConnectionInfo { async fn user_connect( info: ClientInfo, user_id: UserId, - location: Option, + _location: Option, ) -> Result, String> { let mut opened_connections: HashMap = HashMap::new(); diff --git a/ng-app/src/App.svelte b/ng-app/src/App.svelte index 4c6711f..465d6a3 100644 --- a/ng-app/src/App.svelte +++ b/ng-app/src/App.svelte @@ -161,12 +161,18 @@ break; case "opened": if (!$opened_wallets[event.data.wallet.id]) { + await tick(); // console.log( // "ADDING TO OPENED", // event.data.wallet.id, // JSON.stringify($opened_wallets), // event.data.wallet.wallet // ); + if (event.data.ng_wallets) { + localStorage.setItem("ng_wallets", event.data.ng_wallets); + await ng.wallets_reload(); + wallets.set(await ng.get_wallets()); + } try { await ng.wallet_was_opened(event.data.wallet.wallet); } catch (e) { @@ -220,8 +226,14 @@ w[value.id] = value.wallet; return w; }); + await tick(); + //console.log("posting opened"); wallet_channel.postMessage( - { cmd: "opened", wallet: value }, + { + cmd: "opened", + wallet: value, + ng_wallets: localStorage.getItem("ng_wallets"), + }, location.href ); } else { diff --git a/ng-app/src/lib/Install.svelte b/ng-app/src/lib/Install.svelte index c527066..1b1af5a 100644 --- a/ng-app/src/lib/Install.svelte +++ b/ng-app/src/lib/Install.svelte @@ -52,7 +52,7 @@ {/if}
- +