πŸ”Get an address for a Signer

In this section we'll explain how to get a public EVM address from a wallet using different methods

With Private key:

const privateKey = 'your_private_key'; // Replace with the private key of your signer
const wallet = new ethers.Wallet(privateKey, provider);

const signerAddress = wallet.address;
console.log('Signer Address:', signerAddress);

With Mnemonic phrase:

const mnemonic = 'your_mnemonic_phrase'; // Replace with the mnemonic of your signer
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
wallet.connect(provider);

const signerAddress = wallet.address;
console.log('Signer Address:', signerAddress);

Important: you should never store your private key nor Mnemonic phrase in a file. This is only for development purposes

With a Browser provider:

let provider = await new ethers.providers.Web3Provider(window.chosenProvider); // Storing the provider on the client side
let signer = await provider.getSigner();

const signerAddres = await signer.getAddress();
console.log('Signer Address:', signerAddress);

To be capable to handle several wallets on the front-end, we are using a client storage method that'll remember the chosen provider inside the window.choseProvider object as described in the "Implement multiple wallet support on your dApp front-end" section.

Last updated