Most tutorials I googled need wallet's private key to send ETH, such as this. How can we send ETH without knowing the private key using ethersjs? So, that user can just call their MetaMask and send.
2 Answers
You can use the Signer#sendTransaction
method after getting the signer of the MetaMask provider:
async function sendEth (receiver, weiAmount) {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
const tx = await signer.sendTransaction({
to: receiver,
value: weiAmount
})
return tx.hash
}
await sendEth('0x12345...', ethers.utils.parseEther('0.12345'))
I am not sure I understand what you mean by user can just call their MetaMask and send.
However, in any case, it is not possible at all to send a transaction without having the private key of the sender.
Compare it to real life scenario: Would you think it is secured to be able to use a credit card without knowing its four digit code ? (yes, I am looking at you contactless payment, heresy)
-
Well, the user can just sign the transaction in MetaMask (or whatever wallet provider they use) when prompted, which is what was asked. You don't need to know their private key, only their wallet needs to know it...– CherryDTOct 28, 2022 at 13:51