To obtain a signer object from a named account address in a Hardhat project with the
hardhat-deploy plugin, you can use the
ethers.getSigner method, which is designed to work with the addresses of the accounts that Hardhat manages. Here's how you can do it:
// First, retrieve the named accounts using the hardhat-deploy plugin
const namedAccounts = await getNamedAccounts();
// Now, use ethers to get the signer for the 'deployer' named account
// namedAccounts.deployer returns the address for the deployer named account
const deployer = await ethers.getSigner(namedAccounts.deployer);
// You can now use the signer to interact with your contracts
In this example,
namedAccounts.deployer provides the address of the deployer account, and
ethers.getSigner returns the signer object associated with that address. This works because Hardhat knows the private keys of the named accounts for the Hardhat network environment. Remember that this approach is suitable for local development and testing purposes where Hardhat manages the private keys of the accounts.
For live networks or addresses not managed by Hardhat, you would need the private keys to create a new
ethers.Wallet instance connected to the appropriate provider, or in a hardhat local or forked node you can use hardhat_impersonateAccount to get the signer for an account you don't control to mock control on the hardhat node.