From 5fed085379518c5353ab7ab19bd537f8cae3a6e6 Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Sun, 21 Apr 2024 00:44:56 +0300 Subject: [PATCH] user_storage keeps repos and stores. events sent to broker on first connection --- Cargo.lock | 1 + nextgraph/.gitignore | 1 + nextgraph/examples/open.rs | 10 +- nextgraph/examples/persistent.rs | 1 - nextgraph/src/local_broker.rs | 52 ++- ng-app/src/routes/WalletCreate.svelte | 5 +- ng-broker/src/broker_storage/account.rs | 14 +- ng-broker/src/broker_storage/config.rs | 13 +- ng-broker/src/broker_storage/invitation.rs | 12 +- ng-broker/src/broker_storage/overlay.rs | 8 +- ng-broker/src/broker_storage/peer.rs | 13 +- ng-broker/src/broker_storage/topic.rs | 8 +- ng-broker/src/broker_storage/wallet.rs | 6 +- ng-broker/src/server_storage.rs | 72 +++- ng-broker/src/server_ws.rs | 2 +- ng-net/src/actors/client/mod.rs | 5 + ng-net/src/actors/client/pin_repo.rs | 124 ++++++ ng-net/src/actors/client/repo_pin_status.rs | 88 ++++ ng-net/src/actors/client/topic_sub.rs | 113 +++++ ng-net/src/actors/mod.rs | 2 + ng-net/src/broker.rs | 32 +- ng-net/src/errors.rs | 3 + ng-net/src/server_storage.rs | 24 +- ng-net/src/types.rs | 297 ++++++++++++- ng-repo/src/errors.rs | 7 + ng-repo/src/event.rs | 11 +- ng-repo/src/kcv_storage.rs | 12 +- ng-repo/src/repo.rs | 29 +- ng-repo/src/store.rs | 12 +- ng-repo/src/types.rs | 33 +- ng-storage-lmdb/src/kcv_storage.rs | 30 +- ng-storage-rocksdb/src/block_storage.rs | 8 +- ng-storage-rocksdb/src/kcv_storage.rs | 101 ++++- ng-verifier/Cargo.toml | 1 + ng-verifier/src/rocksdb_user_storage.rs | 37 +- ng-verifier/src/site.rs | 27 +- ng-verifier/src/types.rs | 13 + ng-verifier/src/user_storage/branch.rs | 173 ++++++++ ng-verifier/src/user_storage/mod.rs | 29 ++ ng-verifier/src/user_storage/repo.rs | 352 ++++++++++++++++ .../storage.rs} | 36 +- ng-verifier/src/verifier.rs | 390 ++++++++++++++---- ng-wallet/src/lib.rs | 19 +- ngcli/src/old.rs | 2 +- ngone/src/main.rs | 6 +- ngone/src/store/dynpeer.rs | 8 +- ngone/src/store/wallet_record.rs | 11 +- 47 files changed, 2015 insertions(+), 238 deletions(-) create mode 100644 nextgraph/.gitignore create mode 100644 ng-net/src/actors/client/mod.rs create mode 100644 ng-net/src/actors/client/pin_repo.rs create mode 100644 ng-net/src/actors/client/repo_pin_status.rs create mode 100644 ng-net/src/actors/client/topic_sub.rs create mode 100644 ng-verifier/src/user_storage/branch.rs create mode 100644 ng-verifier/src/user_storage/mod.rs create mode 100644 ng-verifier/src/user_storage/repo.rs rename ng-verifier/src/{user_storage.rs => user_storage/storage.rs} (54%) diff --git a/Cargo.lock b/Cargo.lock index 9a568ee..829a076 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3419,6 +3419,7 @@ dependencies = [ "automerge", "blake3", "chacha20", + "either", "getrandom 0.2.10", "ng-net", "ng-repo", diff --git a/nextgraph/.gitignore b/nextgraph/.gitignore new file mode 100644 index 0000000..3598c30 --- /dev/null +++ b/nextgraph/.gitignore @@ -0,0 +1 @@ +tests \ No newline at end of file diff --git a/nextgraph/examples/open.rs b/nextgraph/examples/open.rs index a733d03..6c3cefc 100644 --- a/nextgraph/examples/open.rs +++ b/nextgraph/examples/open.rs @@ -37,7 +37,7 @@ async fn main() -> std::io::Result<()> { })) .await; - let wallet_name = "EJdLRVx93o3iUXoB0wSTqxh1-zYac-84vHb3oBbZ_HY".to_string(); + let wallet_name = "hQK0RBKua5TUm2jqeSGPOMMzqplllAkbUgEh5P6Otf4".to_string(); // as we have previously saved the wallet, // we can retrieve it, display the security phrase and image to the user, ask for the pazzle or mnemonic, and then open the wallet @@ -48,8 +48,8 @@ async fn main() -> std::io::Result<()> { // now let's open the wallet, by providing the pazzle and PIN code let opened_wallet = wallet_open_with_pazzle( &wallet, - vec![117, 134, 59, 92, 98, 35, 70, 22, 9], - [1, 2, 1, 2], + vec![134, 54, 112, 46, 94, 65, 20, 2, 99], + [2, 3, 2, 3], )?; let user_id = opened_wallet.personal_identity(); @@ -65,8 +65,8 @@ async fn main() -> std::io::Result<()> { let status = user_connect(&user_id).await?; // The connection cannot succeed because we miss-configured the core_bootstrap of the wallet. its Peer ID is invalid. - let error_reason = status[0].3.as_ref().unwrap(); - assert!(error_reason == "NoiseHandshakeFailed" || error_reason == "ConnectionError"); + println!("Connection was : {:?}", status[0]); + //assert!(error_reason == "NoiseHandshakeFailed" || error_reason == "ConnectionError"); // Then we should disconnect user_disconnect(&user_id).await?; diff --git a/nextgraph/examples/persistent.rs b/nextgraph/examples/persistent.rs index 753427f..803e002 100644 --- a/nextgraph/examples/persistent.rs +++ b/nextgraph/examples/persistent.rs @@ -44,7 +44,6 @@ async fn main() -> std::io::Result<()> { // the peer_id should come from somewhere else. // this is just given for the sake of an example - #[allow(deprecated)] let peer_id_of_server_broker = PubKey::nil(); // Create your wallet diff --git a/nextgraph/src/local_broker.rs b/nextgraph/src/local_broker.rs index 4521761..e21c3c2 100644 --- a/nextgraph/src/local_broker.rs +++ b/nextgraph/src/local_broker.rs @@ -168,6 +168,12 @@ impl LocalBrokerConfig { _ => false, } } + pub fn is_persistent(&self) -> bool { + match self { + Self::BasePath(_) => true, + _ => false, + } + } #[doc(hidden)] pub fn is_js(&self) -> bool { match self { @@ -702,7 +708,7 @@ impl LocalBroker { key_material.as_slice(), ); key_material.zeroize(); - let verifier = Verifier::new( + let mut verifier = Verifier::new( VerifierConfig { config_type: broker.verifier_config_type_from_session_config(&config), user_master_key: key, @@ -714,6 +720,10 @@ impl LocalBroker { block_storage, )?; key.zeroize(); + + //load verifier from local_storage (if rocks_db) + let _ = verifier.load(); + broker.opened_sessions_list.push(Some(Session { config, peer_key: session.peer_key.clone(), @@ -722,6 +732,7 @@ impl LocalBroker { })); let idx = broker.opened_sessions_list.len() - 1; broker.opened_sessions.insert(user_id, idx as u8); + Ok(SessionInfo { session_id: idx as u8, user: user_id, @@ -892,7 +903,7 @@ pub async fn wallet_create_v0(params: CreateWalletV0) -> Result (UserId, String) { + #[async_std::test] + async fn gen_opened_wallet_file_for_test() { + let wallet_file = read("tests/wallet.ngw").expect("read wallet file"); + + init_local_broker(Box::new(|| LocalBrokerConfig::InMemory)).await; + + let wallet = wallet_read_file(wallet_file) + .await + .expect("wallet_read_file"); + + let pazzle_string = read_to_string("tests/wallet.pazzle").expect("read pazzle file"); + let pazzle_words = pazzle_string.split(' ').map(|s| s.to_string()).collect(); + + let opened_wallet = wallet_open_with_pazzle_words(&wallet, &pazzle_words, [2, 3, 2, 3]) + .expect("opening of wallet"); + + let mut file = + File::create("tests/opened_wallet.ngw").expect("open for write opened_wallet file"); + let ser = serde_bare::to_vec(&opened_wallet).expect("serialization of opened wallet"); + + file.write_all(&ser).expect("write of opened_wallet file"); + } + + async fn import_session_for_test() -> (UserId, String) { let wallet_file = read("tests/wallet.ngw").expect("read wallet file"); let opened_wallet_file = read("tests/opened_wallet.ngw").expect("read opened_wallet file"); let opened_wallet: SensitiveWallet = @@ -1523,7 +1565,7 @@ mod test { #[async_std::test] async fn import_wallet() { - let (user_id, wallet_name) = init_session_for_test().await; + let (user_id, wallet_name) = import_session_for_test().await; let status = user_connect(&user_id).await.expect("user_connect"); diff --git a/ng-app/src/routes/WalletCreate.svelte b/ng-app/src/routes/WalletCreate.svelte index 0641b93..480da94 100644 --- a/ng-app/src/routes/WalletCreate.svelte +++ b/ng-app/src/routes/WalletCreate.svelte @@ -1527,7 +1527,10 @@ /> then throw it away.
The order of each image is important.
- Now click on "Continue to Login"

+ Now click on "Continue to Login."

It is important that + you login with this wallet at least once from this device
+ (while connected to the internet), so that your personal site is + created on your broker.