Skip to content

Commit

Permalink
improve wallet handling startup
Browse files Browse the repository at this point in the history
  • Loading branch information
capt-nemo429 committed Jun 6, 2024
1 parent 5c684b6 commit 3440462
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/stories/walletStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import {
import { ErgoAddress } from "@fleet-sdk/core";
import { EIP12ErgoAPI, SignedTransaction } from "@nautilus-js/eip12-types";
import { acceptHMRUpdate, defineStore } from "pinia";
import { computed, onMounted, ref, watch } from "vue";
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
import { useChainStore } from "./chainStore";
import { ERG_TOKEN_ID } from "@/constants";
import { AssetInfo } from "@/types";
import { getNetworkType, showToast } from "@/utils";

console.log("listening");
addEventListener("ergo-wallet:injected", (ev) => console.log(ev));
const WALLET_INJECTED_EVENT = "ergo-wallet:injected";

export const useWalletStore = defineStore("wallet", () => {
const chain = useChainStore();
Expand Down Expand Up @@ -67,15 +66,21 @@ export const useWalletStore = defineStore("wallet", () => {

// hooks
onMounted(async () => {
if (typeof ergoConnector !== "undefined") {
Object.keys(ergoConnector).map((key) => (_wallets.value[key] = true));
}
addEventListener(WALLET_INJECTED_EVENT, lookupWallets);
lookupWallets();
});

onUnmounted(() => removeEventListener(WALLET_INJECTED_EVENT, lookupWallets));

const connectedWallet = localStorage.getItem("connectedWallet");
if (connectedWallet) {
await connect(connectedWallet);
async function lookupWallets() {
if (typeof ergoConnector === "undefined") return;

Object.keys(ergoConnector).map((key) => (_wallets.value[key] = true));
const walletId = localStorage.getItem("connectedWallet");
if (walletId) {
await connect(walletId);
}
});
}

// actions
async function connect(walletName: "nautilus" | "safew" | string) {
Expand Down

0 comments on commit 3440462

Please sign in to comment.