From ee385cbdfe9b573162e34ba62dff8b00d0c18fb0 Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Thu, 11 Jul 2024 03:51:33 +0300 Subject: [PATCH] get_device_name --- Cargo.lock | 27 +++++++++++++++++++++++ nextgraph/Cargo.toml | 1 + nextgraph/src/lib.rs | 31 +++++++++++++++++++++++++++ ng-app/src-tauri/src/lib.rs | 11 ++++++++-- ng-app/src/api.ts | 1 + ng-app/src/lib/Login.svelte | 13 ++++++----- ng-app/src/locales/en.json | 5 ++--- ng-app/src/routes/WalletCreate.svelte | 13 +++++------ ng-sdk-js/src/lib.rs | 5 +++++ 9 files changed, 87 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f550fea..f103341 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3274,6 +3274,7 @@ dependencies = [ "serde_bare", "serde_json", "web-time", + "whoami", "zeroize", ] @@ -4608,6 +4609,15 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "redox_users" version = "0.4.3" @@ -6325,6 +6335,12 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" + [[package]] name = "wasm-bindgen" version = "0.2.87" @@ -6540,6 +6556,17 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb" +[[package]] +name = "whoami" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44ab49fad634e88f55bf8f9bb3abd2f27d7204172a112c7c9987e01c1c94ea9" +dependencies = [ + "redox_syscall 0.4.1", + "wasite", + "web-sys", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/nextgraph/Cargo.toml b/nextgraph/Cargo.toml index a160823..b2b1a93 100644 --- a/nextgraph/Cargo.toml +++ b/nextgraph/Cargo.toml @@ -26,6 +26,7 @@ async-std = { version = "1.12.0", features = [ "attributes", "unstable" ] } async-trait = "0.1.64" async-once-cell = "0.5.3" web-time = "0.2.0" +whoami = "1.5.1" ng-repo = { path = "../ng-repo", version = "0.1.0-preview.1" } ng-net = { path = "../ng-net", version = "0.1.0-preview.1" } ng-wallet = { path = "../ng-wallet", version = "0.1.0-preview.5" } diff --git a/nextgraph/src/lib.rs b/nextgraph/src/lib.rs index 9dbf3a0..6cfc7a7 100644 --- a/nextgraph/src/lib.rs +++ b/nextgraph/src/lib.rs @@ -98,3 +98,34 @@ pub mod verifier { pub mod wallet { pub use ng_wallet::*; } + +pub fn get_device_name() -> String { + let mut list: Vec = Vec::with_capacity(3); + #[cfg(not(target_arch = "wasm32"))] + if let Ok(realname) = whoami::fallible::realname() { + list.push(realname); + } else { + #[cfg(not(target_arch = "wasm32"))] + if let Ok(username) = whoami::fallible::username() { + list.push(username); + } + } + if let Ok(devicename) = whoami::fallible::devicename() { + list.push(devicename); + } else { + #[cfg(not(target_arch = "wasm32"))] + if let Ok(hostname) = whoami::fallible::hostname() { + list.push(hostname); + } else { + if let Ok(distro) = whoami::fallible::distro() { + list.push(distro); + } + } + } + #[cfg(target_arch = "wasm32")] + if let Ok(distro) = whoami::fallible::distro() { + list.push(distro); + } + + list.join(" ") +} diff --git a/ng-app/src-tauri/src/lib.rs b/ng-app/src-tauri/src/lib.rs index e5684af..d01d501 100644 --- a/ng-app/src-tauri/src/lib.rs +++ b/ng-app/src-tauri/src/lib.rs @@ -127,8 +127,9 @@ async fn wallet_open_with_mnemonic_words( pin: [u8; 4], _app: tauri::AppHandle, ) -> Result { - let wallet = nextgraph::local_broker::wallet_open_with_mnemonic_words(&wallet, &mnemonic_words, pin) - .map_err(|e| e.to_string())?; + let wallet = + nextgraph::local_broker::wallet_open_with_mnemonic_words(&wallet, &mnemonic_words, pin) + .map_err(|e| e.to_string())?; Ok(wallet) } @@ -491,6 +492,11 @@ fn client_info_rust() -> Result { Ok(ng_repo::os_info::get_os_info()) } +#[tauri::command(rename_all = "snake_case")] +fn get_device_name() -> Result { + Ok(nextgraph::get_device_name()) +} + #[derive(Default)] pub struct AppBuilder { setup: Option, @@ -565,6 +571,7 @@ impl AppBuilder { app_request_stream, app_request, upload_chunk, + get_device_name, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/ng-app/src/api.ts b/ng-app/src/api.ts index 91732c0..9ab962a 100644 --- a/ng-app/src/api.ts +++ b/ng-app/src/api.ts @@ -34,6 +34,7 @@ const mapping = { "user_disconnect": ["user_id"], "app_request": ["request"], "test": [ ], + "get_device_name": [], "doc_fetch_private_subscribe": [], "doc_fetch_repo_subscribe": ["repo_id"], } diff --git a/ng-app/src/lib/Login.svelte b/ng-app/src/lib/Login.svelte index 0f7f7fa..4bcf7a4 100644 --- a/ng-app/src/lib/Login.svelte +++ b/ng-app/src/lib/Login.svelte @@ -47,9 +47,13 @@ onMount(async () => { loaded = false; + if (for_import) { + device_name = await ng.get_device_name(); + } load_svg(); //console.log(wallet); await init(); + }); async function init() { @@ -123,9 +127,6 @@ let device_name; - // TODO: @niko Implement API - // ng.get_device_name().then((name) => (device_name = name)); - function order() { step = "order"; ordered = []; @@ -412,12 +413,10 @@ {/if} @@ -449,7 +448,7 @@ on:keypress={start_with_mnemonic} role="link" tabindex="0" - class="mt-1 text-lg px-5 py-2.5 text-center inline-flex items-center mb-2 underline cursor-pointer" + class="mt-1 text-lg px-5 py-2.5 text-center inline-flex items-center mb-10 underline cursor-pointer" > {$t("pages.login.open_with_mnemonic")} diff --git a/ng-app/src/locales/en.json b/ng-app/src/locales/en.json index 630f122..7916ccb 100644 --- a/ng-app/src/locales/en.json +++ b/ng-app/src/locales/en.json @@ -84,8 +84,8 @@ }, "trust_device_allow_cookies": "By selecting this option, you agree to saving some cookies on your browser.", "trust_device_yes": "Yes, save my wallet on this device", - "device_name_label": "Name of device as seen on your other devices", - "device_name_placeholder": "Display name of this device", + "device_name_label": "Name of this device. You can edit it now", + "device_name_placeholder": "Enter name of this device", "loading_pazzle": "Loading Pazzle", "open_with_pazzle": "Open With Pazzle", "login_cancel": "Cancel Login", @@ -194,7 +194,6 @@ "trust_description": "If you do, if this device is yours, or it is used by a few trusted persons of your family or workplace, and you would like to login again from this device in the future, then you can save your wallet on this device. To the contrary, if this device is public and shared by strangers, do not save your wallet here.", "trust_toggle": "Save my wallet on this device?", "device_name_description": "To see which devices you are connected with, every device should have a name (e.g. Bob's laptop). Please enter it here.", - "device_name_placeholder": "Display name of this device", "cloud": "Keep a copy in the cloud?", "cloud_description": "Are you afraid that you will loose the file containing your wallet? If this would happen, your wallet would be lost forever, together with all your documents. We can keep an encrypted copy of your wallet in our cloud. Only you will be able to download it with a special link. You would have to keep this link safely though. By selecting this option, you agree to the", "cloud_toggle": "Save my wallet in the cloud?", diff --git a/ng-app/src/routes/WalletCreate.svelte b/ng-app/src/routes/WalletCreate.svelte index 47f392a..4ec7f7a 100644 --- a/ng-app/src/routes/WalletCreate.svelte +++ b/ng-app/src/routes/WalletCreate.svelte @@ -141,9 +141,6 @@ let confirm_modal_open = false; let device_name; - // TODO @niko add API - // ng.get_device_name().then((name) => (device_name = name)); - function scrollToTop() { top.scrollIntoView(); } @@ -226,6 +223,8 @@ } async function save_security() { + + device_name = await ng.get_device_name(); options = { trusted: true, cloud: false, @@ -259,7 +258,7 @@ core_bootstrap: invitation.V0.bootstrap, core_registration, additional_bootstrap, - device_name, + //TODO: device_name, }; //console.log("do wallet with params", params); try { @@ -1441,14 +1440,12 @@

{/if}

diff --git a/ng-sdk-js/src/lib.rs b/ng-sdk-js/src/lib.rs index 469e76c..871ecd8 100644 --- a/ng-sdk-js/src/lib.rs +++ b/ng-sdk-js/src/lib.rs @@ -60,6 +60,11 @@ pub async fn locales() -> Result { Ok(serde_wasm_bindgen::to_value(&get_locales().collect::>()).unwrap()) } +#[wasm_bindgen] +pub async fn get_device_name() -> Result { + Ok(serde_wasm_bindgen::to_value(&nextgraph::get_device_name()).unwrap()) +} + #[wasm_bindgen] pub async fn get_local_bootstrap(location: String, invite: JsValue) -> JsValue { let res = retrieve_local_bootstrap(location, invite.as_string(), false).await;