Search code examples
pythonblockchainethereummetamaskweb3py

Switch recipient account in Web3Py using Metamask


I want to dynamically change the recipient account web3.eth.accounts[0] in Web3py according to the currently selected Ethereum account in Metamask. How can I do that? Now, I set the sender and recipient on my Web3Py code like this but as soon as I change the current account, the address of the sender/receiver is still the same.

address = web3.eth.accounts[1] // recipient
address2 = web3.eth.accounts[0] // sender

Solution

  • How can I do that?

    You cannot.

    Web3.py and MetaMask cannot communicate. MetaMask cannot pass its private key to web3.py, as this is on purpose of the security (otherwise any software could steal your key). Please read here how to manage local private keys with Web3.py.