2

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 2

0

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'))
-1

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)

1
  • 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...
    – CherryDT
    Oct 28, 2022 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.