defend against multiple mutiny wallet instances

This commit is contained in:
Paul Miller
2023-06-20 13:38:23 -05:00
parent 22ca57c93e
commit 5a7e67ea05
2 changed files with 27 additions and 6 deletions

View File

@@ -1,10 +1,8 @@
/* @refresh reload */
import initMutinyWallet, { MutinyWallet } from "@mutinywallet/mutiny-wasm";
import initWaila from "@mutinywallet/waila-wasm";
// export type MutinyWalletSettingStrings = {
// network?: string, proxy?: string, esplora?: string, rgs?: string, lsp?: string,
// }
export type Network = "bitcoin" | "testnet" | "regtest" | "signet";
export type MutinyWalletSettingStrings = {
network?: Network;
@@ -92,11 +90,23 @@ export async function checkForWasm() {
export async function setupMutinyWallet(
settings?: MutinyWalletSettingStrings
): Promise<MutinyWallet> {
// Ultimate defense against getting multiple instances of the wallet running.
// If we detect that the wallet has already been initialized in this session, we'll reload the page.
// A successful stop of the wallet in onCleanup will clear this flag
if (sessionStorage.getItem("MUTINY_WALLET_INITIALIZED")) {
console.error(
`Mutiny Wallet already initialized at ${sessionStorage.getItem(
"MUTINY_WALLET_INITIALIZED"
)}. Reloading page.`
);
sessionStorage.removeItem("MUTINY_WALLET_INITIALIZED");
window.location.reload();
}
await initMutinyWallet();
// Might as well init waila while we're at it
await initWaila();
console.time("Setup");
console.log("Starting setup...");
const { network, proxy, esplora, rgs, lsp } = await setAndGetMutinySettings(
settings
@@ -125,5 +135,7 @@ export async function setupMutinyWallet(
await mutinyWallet?.new_node();
}
sessionStorage.setItem("MUTINY_WALLET_INITIALIZED", Date.now().toString());
return mutinyWallet;
}

View File

@@ -225,7 +225,16 @@ export const Provider: ParentComponent = (props) => {
};
onCleanup(() => {
console.warn('Parent Component is being unmounted!!!');
console.warn("Parent Component is being unmounted!!!");
state.mutiny_wallet
?.stop()
.then(() => {
console.warn("Successfully stopped mutiny wallet");
sessionStorage.removeItem("MUTINY_WALLET_INITIALIZED");
})
.catch((e) => {
console.error("Error stopping mutiny wallet", e);
});
});
// Fetch status from remote on load