'How can I get the private key of the address of the underlying contract through HardHat?
I have smartcontract from HardHat tutorial https://hardhat.org/tutorial/writing-and-compiling-contracts.html
and I successfully deployed it.
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
console.log("Account balance:", (await deployer.getBalance()).toString());
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy();
console.log("Token address:", token.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
But only the address of the contact is returned to me and its private key is not.
console.log("Deploying contracts with the account:", deployer.address);
How can I get the private key in this way? I need it for the method
web3.eth.accounts.wallet.add('0x<private_key>');
Because otherwise I can't call the transfer method on the smart contract.
Solution 1:[1]
Let's assume you know the private key, and you make a lending smart contract, everybody can pool their money on the contract and lend money from the contract. One day you feel greedy, and you just use your private key transfer all the pool to your private wallet. Does this sound good? By design even the deployer should never have access to the private key of the contract, that's the point of the ethereum and the point of the dapp and dao, without it basically the deployer still is the "center" who controls everything.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Boyce Cecil |
