Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
How to check if a web3 wallet is unlocked?

What is the best practice to see if metamask or other web3 wallets are unlocked with ethers.js?

I currently use this:

window.ethereum._metamask.isUnlocked()

But it is reported as experimental methods by the metamask documentation and I would like to find something better.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

What my experimentation found was that listAccounts returns an array of strings and if the wallet is not unlocked, it returns an empty array.

For MetaMask specifically, I know you can't have an account without having an address.

Therefore the function I came up with was:

async function isUnlocked() {
    const provider = new ethers.providers.Web3Provider(window.ethereum);

    let unlocked;

    try {
        const accounts = await provider.listAccounts();

        unlocked = accounts.length > 0;
    } catch (e) {
        unlocked = false;
    }

    return unlocked;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

That's my own resolution if anyone need it :

isUnlocked$ = new BehaviorSubject<boolean>(false);

async isWalletUnlocked() {
      const web3Provider = new ethers.providers.Web3Provider(window.ethereum, 'any');
      const signer = await this.web3Provider.getSigner();
    
      signer
      .getAddress()
      .then((address: string) => {
        this.isUnlocked$.next(true);
      })
      .catch((err) => this.isUnlocked$.next(false));
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda