Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect from Unisat wallet #9

Open
Spreeyka opened this issue Dec 19, 2023 · 3 comments
Open

Disconnect from Unisat wallet #9

Spreeyka opened this issue Dec 19, 2023 · 3 comments

Comments

@Spreeyka
Copy link

I need to implement disconnect fron Unisat wallet functionality. How to do that? I don't see this code neither in demo-example nor in API docs.

@hovanhoa
Copy link

hovanhoa commented May 2, 2024

Any update on this issue?

@redcomethk
Copy link

I'm afraid currently we still need to instruct the users to disconnect it from the unisat extension when needed.
But it seems that Disconnect is also available in unisat.io
Maybe it would be nice if someone could share how to do that, thanks!

@SmartDev-0205
Copy link

On unisat.io , when user click disconnect, they changed only status.
This is my unisatprovider

export const UnisatProvider = ({ children }: any) => {
  const [unisatInstalled, setUnisatInstalled] = useState(false);
  const [connected, setConnected] = useState(false);
  const [accounts, setAccounts] = useState<string[]>([]);
  const [publicKey, setPublicKey] = useState("");
  const [address, setAddress] = useState("");
  const [network, setNetwork] = useState("testnet");

  const getBasicInfo = async () => {
    const unisat = (window as any).unisat;
    const [address] = await unisat.getAccounts();
    setAddress(address);

    const publicKey = await unisat.getPublicKey();
    setPublicKey(publicKey);

    const network = await unisat.getNetwork();
    setNetwork(network);
  };

  const selfRef = useRef<{ accounts: string[] }>({
    accounts: [],
  });
  const self = selfRef.current;

  const connectWallet = async () => {
    console.log("unisat------>", unisat);
    if (unisat) {
      const result = await unisat.requestAccounts();
      getBasicInfo();
    } else {
      window.location.href = "https://unisat.io";
    }
  };

  const disconnectWallet = async () => {
    setConnected(false);
    setAccounts([]);
    setPublicKey("");
    setAddress("");
    setNetwork("");
  };
  const handleAccountsChanged = (_accounts: string[]) => {
    if (self.accounts[0] === _accounts[0]) {
      // prevent from triggering twice
      return;
    }
    self.accounts = _accounts;
    if (_accounts.length > 0) {
      setAccounts(_accounts);
      setConnected(true);

      setAddress(_accounts[0]);

      getBasicInfo();
    } else {
      setConnected(false);
    }
  };

  const handleNetworkChanged = (network: string) => {
    setNetwork(network);
    getBasicInfo();
  };

  useEffect(() => {
    async function checkUnisat() {
      window.Buffer = Buffer;
      if (typeof process === "undefined") {
        const process = require("process");
        window.process = process;
      }
      let unisat = (window as any).unisat;

      for (let i = 1; i < 10 && !unisat; i += 1) {
        await new Promise((resolve) => setTimeout(resolve, 100 * i));
        unisat = (window as any).unisat;
      }

      if (unisat) {
        setUnisatInstalled(true);
      } else if (!unisat) return;

      unisat.getAccounts().then((accounts: string[]) => {
        handleAccountsChanged(accounts);
      });

      unisat.on("accountsChanged", handleAccountsChanged);
      unisat.on("networkChanged", handleNetworkChanged);

      return () => {
        unisat.removeListener("accountsChanged", handleAccountsChanged);
        unisat.removeListener("networkChanged", handleNetworkChanged);
      };
    }

    checkUnisat().then();
  }, []);

  const getUnisatInscription = async () => {
    let inscriptions: any[] = [];
    try {
      let unisat = (window as any).unisat;
      if (unisat) {
        let index = 0;
        while (true) {
          let res = await unisat.getInscriptions(index, 10);
          if (res.list && res.list.length == 0) break;
          let pageInscriptions = res.list.map((inscription: any) => ({
            id: inscription.inscriptionId,
            contentURI: inscription.content,
            contentType: inscription.contentType,
            contentPreviewURI: inscription.preview,
            sat: 1360552204244140,
            satName: "Bitcoin Sat",
            genesisTransaction: inscription.genesisTransaction,
            genesisTransactionBlockTime: getUtcTime(
              parseInt(inscription.timestamp)
            ),
            inscriptionNumber: inscription.inscriptionNumber,
            chain: "btc-testnet",
            location: inscription.location,
            output: inscription.output,
            outputValue: inscription.outputValue,
            owner: inscription.address,
            listed: false,
            postage: inscription.outputValue,
            offset: inscription.offset,
          }));
          inscriptions = inscriptions.concat(pageInscriptions);
          index += 1;
        }
      }
    } catch (e) {
      console.log(e);
    } finally {
      const uniqueInscriptions = inscriptions.filter(
        (inscription, index, self) =>
          index === self.findIndex((t) => t.id === inscription.id)
      );
      return uniqueInscriptions;
    }
  };

  let unisat: any = undefined;
  if (unisatInstalled) {
    unisat = (window as any).unisat;
  }

  let contextData = {
    connected: connected,
    connectWallet: connectWallet,
    disconnectWallet: disconnectWallet,
    address: address,
    network: network,
    publicKey: publicKey,
    updateWallet: getBasicInfo,
    getUnisatInscription: getUnisatInscription,
  };

  return (
    <UnisatContext.Provider value={contextData}>
      {children}
    </UnisatContext.Provider>
  );
};

Not sure if this can be help for you.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants