From 69d0af24d2d650552408b390bbac7058a563de0b Mon Sep 17 00:00:00 2001
From: Niko PLP <niko@nextgraph.org>
Date: Sun, 6 Aug 2023 14:41:50 +0300
Subject: [PATCH] fix android tauri window doesnt have js injected

---
 ng-app/package.json                    |  7 +--
 ng-app/src-tauri/Cargo.toml            |  2 +-
 ng-app/src-tauri/src/lib.rs            |  4 +-
 ng-app/src/App.svelte                  |  2 +-
 ng-app/src/api.ts                      |  4 +-
 ng-app/src/lib/Login.svelte            |  4 +-
 ng-app/src/routes/WalletCreate.svelte  | 61 ++++++++++++++++----------
 ngaccount/src/main.rs                  |  2 +-
 ngaccount/web/package.json             |  2 +-
 ngaccount/web/pnpm-lock.yaml           |  9 +++-
 ngaccount/web/src/routes/Create.svelte |  2 +-
 ngaccount/web/src/routes/Delete.svelte |  2 +-
 ngaccount/web/src/routes/Home.svelte   | 10 ++---
 ngone/src/store/dynpeer.rs             |  2 +-
 ngone/src/store/wallet_record.rs       |  2 +-
 p2p-broker/src/broker_store/wallet.rs  |  2 +-
 pnpm-lock.yaml                         |  9 +++-
 17 files changed, 77 insertions(+), 49 deletions(-)

diff --git a/ng-app/package.json b/ng-app/package.json
index 61a2d7b..7b1b8eb 100644
--- a/ng-app/package.json
+++ b/ng-app/package.json
@@ -6,17 +6,18 @@
   "scripts": {
     "dev": "vite",
     "webdev": "cross-env NG_APP_WEB=1 TAURI_DEBUG=1 vite",
-    "webbuild": "cross-env NG_APP_WEB=1 vite build",
+    "webbuild": "cross-env NG_APP_WEB=1 vite build && rm ./dist/assets/*.svg",
     "filebuild": "cross-env NG_APP_WEB=1 NG_APP_FILE=1 vite build && node prepare-app-file.cjs",
     "filebuilddebug": "cross-env NG_APP_WEB=1 NG_APP_FILE=1 TAURI_DEBUG=1 vite build -m debug",
-    "build": "vite build",
+    "build": "vite build && rm ./dist/assets/*.svg",
+    "buildfile": "cross-env NG_APP_FILE=1 vite build && rm -r ./dist/assets",
     "preview": "vite preview",
     "check": "svelte-check --tsconfig ./tsconfig.json",
     "tauri": "tauri"
   },
   "dependencies": {
     "@popperjs/core": "^2.11.8",
-    "@tauri-apps/api": "2.0.0-alpha.4",
+    "@tauri-apps/api": "2.0.0-alpha.5",
     "@tauri-apps/plugin-window": "2.0.0-alpha.0",
     "async-proxy": "^0.4.1",
     "classnames": "^2.3.2",
diff --git a/ng-app/src-tauri/Cargo.toml b/ng-app/src-tauri/Cargo.toml
index b2f7c6a..9d75607 100644
--- a/ng-app/src-tauri/Cargo.toml
+++ b/ng-app/src-tauri/Cargo.toml
@@ -17,7 +17,7 @@ crate-type = ["staticlib", "cdylib", "rlib"]
 tauri-build = { version = "2.0.0-alpha.5", features = [] }
 
 [dependencies]
-tauri = { version = "2.0.0-alpha.9", features = [] }
+tauri = { version = "2.0.0-alpha.10", features = [] }
 serde = { version = "1.0", features = ["derive"] }
 serde_json = "1.0"
 p2p-repo = { path = "../../p2p-repo" }
diff --git a/ng-app/src-tauri/src/lib.rs b/ng-app/src-tauri/src/lib.rs
index d2b33dc..f5f277b 100644
--- a/ng-app/src-tauri/src/lib.rs
+++ b/ng-app/src-tauri/src/lib.rs
@@ -208,7 +208,8 @@ async fn open_window(
 ) -> Result<(), ()> {
     log_debug!("open window url {:?}", url);
     let already_exists = app.get_window(&label);
-    if (already_exists.is_some()) {
+    #[cfg(desktop)]
+    if already_exists.is_some() {
         let _ = already_exists.unwrap().close();
         std::thread::sleep(std::time::Duration::from_secs(1));
     }
@@ -332,6 +333,7 @@ impl AppBuilder {
                     app.ipc_scope().configure_remote_access(
                         RemoteDomainAccessScope::new(domain)
                             .add_window("registration")
+                            .add_window("main")
                             .add_plugins(["window", "event"]),
                     );
                 }
diff --git a/ng-app/src/App.svelte b/ng-app/src/App.svelte
index ebd63c6..56ae93b 100644
--- a/ng-app/src/App.svelte
+++ b/ng-app/src/App.svelte
@@ -11,7 +11,7 @@
 
 <script lang="ts">
   // this line is needed to have the SDK working when compiling for a single file bundle (pnpm filebuild)
-  import * as api from "ng-sdk-js";
+  //import * as api from "ng-sdk-js";
   import { push, default as Router } from "svelte-spa-router";
   import { onMount, tick, onDestroy } from "svelte";
   import {
diff --git a/ng-app/src/api.ts b/ng-app/src/api.ts
index 35db3e5..ad814d4 100644
--- a/ng-app/src/api.ts
+++ b/ng-app/src/api.ts
@@ -112,10 +112,10 @@ const handler = {
                 return res;
             } else if (path[0] === "get_wallets_from_localstorage") {
                 let res = await tauri.invoke(path[0],{});
-                for (let e of Object.entries(res)) {
+                if (res) for (let e of Object.entries(res)) {
                     e[1].wallet.V0.content.security_img = Uint8Array.from(e[1].wallet.V0.content.security_img);
                 }
-                return res;
+                return res || {};
 
             } else if (path[0] === "wallet_create_wallet") {
                 let params = args[0];
diff --git a/ng-app/src/lib/Login.svelte b/ng-app/src/lib/Login.svelte
index 3ad467e..7208027 100644
--- a/ng-app/src/lib/Login.svelte
+++ b/ng-app/src/lib/Login.svelte
@@ -191,7 +191,7 @@
     </svg>
   </div>
 {:else if step == "pazzle"}
-  <div class="h-screen aspect-[3/5] pazzleline max-w-[600px] min-w-[500px]">
+  <div class="h-screen aspect-[3/5] pazzleline max-w-[600px] min-w-[350px]">
     {#each [0, 1, 2, 3, 4] as row}
       <div class="columns-3 gap-0">
         {#each emojis2[display]?.slice(0 + row * 3, 3 + row * 3) || [] as emoji, i}
@@ -210,7 +210,7 @@
   </div>
 {:else if step == "order"}
   <!-- console.log(cat_idx, emoji_cat[cat_idx], idx, cat[idx].code); -->
-  <div class="h-screen aspect-[3/3] pazzleline max-w-[600px] min-w-[500px]">
+  <div class="h-screen aspect-[3/3] pazzleline max-w-[600px] min-w-[350px]">
     {#each [0, 1, 2] as row}
       <div class="columns-3 gap-0">
         {#each selection.slice(0 + row * 3, 3 + row * 3) || [] as emoji, i}
diff --git a/ng-app/src/routes/WalletCreate.svelte b/ng-app/src/routes/WalletCreate.svelte
index 61ee36c..cdd81af 100644
--- a/ng-app/src/routes/WalletCreate.svelte
+++ b/ng-app/src/routes/WalletCreate.svelte
@@ -125,10 +125,14 @@
     }
   }
 
-  function confirm_pin(val) {
+  async function confirm_pin(val) {
     if (pin_confirm.length < 4) {
       pin_confirm.push(val);
       pin_confirm = pin_confirm;
+      if (pin_confirm.length == 4) {
+        await tick();
+        scrollToTop();
+      }
     }
   }
 
@@ -138,7 +142,7 @@
 
   async function bootstrap() {
     //console.log(await ng.client_info());
-    if (!tauri_platform) {
+    if (!tauri_platform || tauri_platform == "android") {
       if (param.get("skipintro") || param.get("rs")) {
         intro = false;
       }
@@ -148,21 +152,22 @@
       if (param.get("rs")) {
         registration_success = param.get("rs");
         invitation = await ng.decode_invitation(param.get("i"));
-      } else {
+        window.location.replace(window.location.href.split("?")[0]);
+      } else if (param.get("i")) {
         invitation = await ng.get_local_bootstrap_with_public(
           location.href,
           param.get("i")
         );
-      }
-      console.log(invitation);
-      if (!invitation && param.get("i")) {
-        let redirect = await ng.get_ngone_url_of_invitation(param.get("i"));
-        if (redirect) {
-          console.error("got an invitation for another broker. redirecting");
-          window.location.href = redirect;
-        } else {
-          //let i = await ng.decode_invitation(param.get("i"));
-          console.error("invalid invitation. ignoring it");
+        console.log(invitation);
+        if (!invitation) {
+          let redirect = await ng.get_ngone_url_of_invitation(param.get("i"));
+          if (redirect) {
+            console.error("got an invitation for another broker. redirecting");
+            window.location.href = redirect;
+          } else {
+            //let i = await ng.decode_invitation(param.get("i"));
+            console.error("invalid invitation. ignoring it");
+          }
         }
       }
     }
@@ -277,14 +282,23 @@
   });
 
   const select_bsp = async (bsp_url, bsp_name) => {
-    if (!tauri_platform) {
-      let local_url = await ng.get_local_url(location.href);
-      if (!import.meta.env.PROD) {
-        local_url = "http://localhost:1421";
+    if (!tauri_platform || tauri_platform == "android") {
+      let redirect_url;
+      if (tauri_platform) {
+        redirect_url = window.location.href;
+      } else {
+        let local_url;
+        if (!import.meta.env.PROD) {
+          local_url = "http://localhost:1421";
+        } else {
+          local_url = await ng.get_local_url(location.href);
+        }
+        redirect_url = local_url + APP_WALLET_CREATE_SUFFIX;
       }
+
       let create = {
         V0: {
-          redirect_url: local_url + APP_WALLET_CREATE_SUFFIX,
+          redirect_url,
         },
       };
       let ca = await ng.encode_create_account(create);
@@ -338,14 +352,14 @@
   const enterINVITE = (event) => {};
   const enterQRcode = (event) => {};
   const displayNGbox = async (event) => {
-    if (!tauri_platform) {
+    if (!tauri_platform || tauri_platform == "android") {
       window.open(LINK_NG_BOX, "_blank").focus();
     } else {
       await ng.open_window(LINK_NG_BOX, "viewer", "Own your NG-Box");
     }
   };
   const displaySelfHost = async (event) => {
-    if (!tauri_platform) {
+    if (!tauri_platform || tauri_platform == "android") {
       window.open(LINK_SELF_HOST, "_blank").focus();
     } else {
       await ng.open_window(LINK_SELF_HOST, "viewer", "Self-host a broker");
@@ -1030,7 +1044,8 @@
   {:else if pin_confirm.length < 4}
     <div class=" max-w-6xl lg:px-8 mx-auto px-4">
       <p class="max-w-xl md:mx-auto lg:max-w-2xl">
-        <span class="text-xl">Please confirm your PIN code.</span>
+        <span class="animate-bounce text-xl">Please confirm your PIN code.</span
+        >
         Enter the same PIN again
       </p>
       <Alert color="blue" class="mt-5">
@@ -1045,7 +1060,7 @@
               <button
                 tabindex="0"
                 class="m-1 select-none align-bottom text-7xl w-[100px] h-[100px] p-0"
-                on:click={async () => confirm_pin(num + row * 3)}
+                on:click={async () => await confirm_pin(num + row * 3)}
               >
                 <span>{num + row * 3}</span>
               </button>
@@ -1055,7 +1070,7 @@
         <button
           tabindex="0"
           class="m-1 select-none mx-auto align-bottom text-7xl w-[100px] h-[100px] p-0"
-          on:click={async () => confirm_pin(0)}
+          on:click={async () => await confirm_pin(0)}
         >
           <span>0</span>
         </button>
diff --git a/ngaccount/src/main.rs b/ngaccount/src/main.rs
index 459065b..7f57f2d 100644
--- a/ngaccount/src/main.rs
+++ b/ngaccount/src/main.rs
@@ -272,7 +272,7 @@ async fn main() -> anyhow::Result<()> {
     }
     log::info!("Starting server on http://localhost:3031");
     warp::serve(api_v1.or(static_files).with(cors))
-        .run(([127, 0, 0, 1], 3031))
+        .run(([192, 168, 192, 2], 3031))
         .await;
 
     Ok(())
diff --git a/ngaccount/web/package.json b/ngaccount/web/package.json
index 3a3834a..88f524c 100644
--- a/ngaccount/web/package.json
+++ b/ngaccount/web/package.json
@@ -10,7 +10,7 @@
     "preview": "vite preview"
   },
   "dependencies": {
-    "@tauri-apps/api": "2.0.0-alpha.4",
+    "@tauri-apps/api": "2.0.0-alpha.5",
     "@tauri-apps/plugin-window": "2.0.0-alpha.0",
     "flowbite": "^1.6.5",
     "flowbite-svelte": "^0.37.1",
diff --git a/ngaccount/web/pnpm-lock.yaml b/ngaccount/web/pnpm-lock.yaml
index 5cffc75..fcc1f14 100644
--- a/ngaccount/web/pnpm-lock.yaml
+++ b/ngaccount/web/pnpm-lock.yaml
@@ -2,7 +2,7 @@ lockfileVersion: 5.4
 
 specifiers:
   '@sveltejs/vite-plugin-svelte': ^2.0.4
-  '@tauri-apps/api': 2.0.0-alpha.4
+  '@tauri-apps/api': 2.0.0-alpha.5
   '@tauri-apps/plugin-window': 2.0.0-alpha.0
   autoprefixer: ^10.4.14
   cross-env: ^7.0.3
@@ -18,7 +18,7 @@ specifiers:
   vite-plugin-svelte-svg: ^2.2.1
 
 dependencies:
-  '@tauri-apps/api': 2.0.0-alpha.4
+  '@tauri-apps/api': 2.0.0-alpha.5
   '@tauri-apps/plugin-window': 2.0.0-alpha.0
   flowbite: 1.6.6
   flowbite-svelte: 0.37.5_svelte@3.59.1
@@ -341,6 +341,11 @@ packages:
     engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'}
     dev: false
 
+  /@tauri-apps/api/2.0.0-alpha.5:
+    resolution: {integrity: sha512-OqysC4c819itGxic50RoDMrmd+ofX+MMNkXKeRS0BV2rkKqrnuV17o3TrQXFI1xs/kXRmmPC+3Y42P9Y5uNvRg==}
+    engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'}
+    dev: false
+
   /@tauri-apps/plugin-window/2.0.0-alpha.0:
     resolution: {integrity: sha512-ZXFXOu9m8QiDB8d8LFFgwcfxIAbr0bhzj06YvmZDB3isuVtlFP9EyU4D+zmumWEWvNN2XP7xgpn68ivOVhmNNQ==}
     dependencies:
diff --git a/ngaccount/web/src/routes/Create.svelte b/ngaccount/web/src/routes/Create.svelte
index 8feedec..c3e391f 100644
--- a/ngaccount/web/src/routes/Create.svelte
+++ b/ngaccount/web/src/routes/Create.svelte
@@ -27,7 +27,7 @@
   let top;
   const api_url = import.meta.env.PROD
     ? "api/v1/"
-    : "http://localhost:3031/api/v1/";
+    : "http://192.168.192.2:3031/api/v1/";
 
   async function register() {
     const opts = {
diff --git a/ngaccount/web/src/routes/Delete.svelte b/ngaccount/web/src/routes/Delete.svelte
index e631f34..58c26ed 100644
--- a/ngaccount/web/src/routes/Delete.svelte
+++ b/ngaccount/web/src/routes/Delete.svelte
@@ -27,7 +27,7 @@
   let top;
   const api_url = import.meta.env.PROD
     ? "api/v1/"
-    : "http://localhost:3031/api/v1/";
+    : "http://192.168.192.2:3031/api/v1/";
 
   async function bootstrap() {}
   let error;
diff --git a/ngaccount/web/src/routes/Home.svelte b/ngaccount/web/src/routes/Home.svelte
index c40f102..6d9f492 100644
--- a/ngaccount/web/src/routes/Home.svelte
+++ b/ngaccount/web/src/routes/Home.svelte
@@ -15,13 +15,13 @@
 
   import { onMount } from "svelte";
 
-  const api_url = import.meta.env.PROD
-    ? "api/v1/"
-    : "http://localhost:3031/api/v1/";
+  // const api_url = import.meta.env.PROD
+  //   ? "api/v1/"
+  //   : "http://localhost:3031/api/v1/";
 
-  async function bootstrap() {}
+  // async function bootstrap() {}
 
-  onMount(() => bootstrap());
+  // onMount(() => bootstrap());
 </script>
 
 <div />
diff --git a/ngone/src/store/dynpeer.rs b/ngone/src/store/dynpeer.rs
index 1b7dc4b..3991455 100644
--- a/ngone/src/store/dynpeer.rs
+++ b/ngone/src/store/dynpeer.rs
@@ -55,7 +55,7 @@ impl<'a> DynPeer<'a> {
         if acc.exists() {
             return Err(StorageError::BackendError);
         }
-        store.write_transaction(&|tx| {
+        store.write_transaction(&mut |tx| {
             tx.put(
                 Self::PREFIX,
                 &to_vec(&id)?,
diff --git a/ngone/src/store/wallet_record.rs b/ngone/src/store/wallet_record.rs
index 905e0e7..38425a2 100644
--- a/ngone/src/store/wallet_record.rs
+++ b/ngone/src/store/wallet_record.rs
@@ -55,7 +55,7 @@ impl<'a> WalletRecord<'a> {
         if wallet.exists() {
             return Err(StorageError::BackendError);
         }
-        store.write_transaction(&|tx| {
+        store.write_transaction(&mut |tx| {
             tx.put(
                 Self::PREFIX,
                 &to_vec(&id)?,
diff --git a/p2p-broker/src/broker_store/wallet.rs b/p2p-broker/src/broker_store/wallet.rs
index fc12933..170ea41 100644
--- a/p2p-broker/src/broker_store/wallet.rs
+++ b/p2p-broker/src/broker_store/wallet.rs
@@ -76,7 +76,7 @@ impl<'a> Wallet<'a> {
     }
 
     pub fn get_or_create_overlay_key(&self, overlay: &OverlayId) -> Result<SymKey, StorageError> {
-        self.get_or_create_single_key(Self::PREFIX_USER, &to_vec(overlay)?)
+        self.get_or_create_single_key(Self::PREFIX_OVERLAY, &to_vec(overlay)?)
     }
 
     pub fn create_single_key(
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 439d64b..21a6b48 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -9,7 +9,7 @@ importers:
     specifiers:
       '@popperjs/core': ^2.11.8
       '@sveltejs/vite-plugin-svelte': ^2.0.0
-      '@tauri-apps/api': 2.0.0-alpha.4
+      '@tauri-apps/api': 2.0.0-alpha.5
       '@tauri-apps/cli': 2.0.0-alpha.9
       '@tauri-apps/plugin-window': 2.0.0-alpha.0
       '@tsconfig/svelte': ^3.0.0
@@ -39,7 +39,7 @@ importers:
       vite-plugin-wasm: ^3.2.2
     dependencies:
       '@popperjs/core': 2.11.8
-      '@tauri-apps/api': 2.0.0-alpha.4
+      '@tauri-apps/api': 2.0.0-alpha.5
       '@tauri-apps/plugin-window': 2.0.0-alpha.0
       async-proxy: 0.4.1
       classnames: 2.3.2
@@ -507,6 +507,11 @@ packages:
     engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'}
     dev: false
 
+  /@tauri-apps/api/2.0.0-alpha.5:
+    resolution: {integrity: sha512-OqysC4c819itGxic50RoDMrmd+ofX+MMNkXKeRS0BV2rkKqrnuV17o3TrQXFI1xs/kXRmmPC+3Y42P9Y5uNvRg==}
+    engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'}
+    dev: false
+
   /@tauri-apps/cli-darwin-arm64/2.0.0-alpha.9:
     resolution: {integrity: sha512-h7AQY4MbHyy7CLANfoegJyaypKQ3fYPlBA3mnhtF+zntrMxVTv+HO9sgn98jGnwlOak8y0NPPKJ6gZFu5GYiNg==}
     engines: {node: '>= 10'}