1

I'm trying to use web3.py in my decentralized application to allow random users to sign transactions in my dApp with their Metamask wallet. I realize we need ethers.js to connect to the user's Metamask and get the signer's details.

const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
const signer_details = provider.getSigner();

But once we've captured the signers' details with ethers.js, is it possible to use it for signing transaction in web3.py? Instead of using this line of code in javascript: await signer_details.sendTransaction(unsignedTrx); I'd like to somehow write this in web3.py.

For example, to sign a transaction in web3.py, we would insert the transaction and private key, as such:

signed_tx = w3.eth.account.sign_transaction(unsignedTrx, private_key=private_key)

But getting others people's Metamask private key is not realistic.

Is there a way to use the information we got from ethers.js (signer_details ) in lieu of the private_key?

2 Answers 2

1

You can not use the signer provided by MetaMask to sign transactions without the approval of the user, unlike a signer created using a private key.

2
  • yes, I think I understand that. But the approval of the user is given to us through JS. I was wondering if there is a Python wrapper for that js code. Jul 18, 2023 at 17:02
  • 1
    I don't think that is possible, as MetaMask injects into the browser and you approve stuff on MetaMask itself, but afaik Python does not have a wrapper for MetaMask or similar wallets. If you still want to sign transactions without directly using the private key in the code, you can take a look at keystores, which store the private key inside and are unlockable with a password you decide: web3py.readthedocs.io/en/stable/… Jul 19, 2023 at 7:57
0

But once we've captured the signers' details with ethers.js, is it possible to use it for signing transaction in web3.py?

web3.py does not have an adapter for this yet. There is no theoretical reason why not - web3.py just needs to spin up a web server and open a web browser to perform signing there.

There is also one similar example. Daniel Schiavini wrote a plugin for Jupyter Notebooks that allows such flow.

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.