Python – How to Recover Public Key Using ecRecover in Python?

ecrecovergo-ethereummetamaskpythonweb3.py

I am able to use ecRecover to verify the signer in Javascript, for example:

const ecRecoverAddr = await window.ethereum.request({
    method: "personal_ecRecover",
    params: [message, signedMessage],
});

However, when I try the following Python code:

from web3 import Web3, HTTPProvider

def verify(request, message, signedMessage):
    response = web3.geth.personal.ecRecover(message, signature)
    return HttpResponse(response, content_type='text/json')

I get this error:
name 'web3' is not defined

Does this mean that I need to instantiate some sort of provider for web3? What should be done?

Is there a way to retrieve the public key using ecRecover without the use of a provider?

Shouldn't it just be a method that can run without any connections?

Thanks!

Best Answer

You will need to instantiate the web3 object and cannot refer to a package here.

from web3 import Web3, HTTPProvider

w3 = Web3(Web3.HTTPProvider(<infura or alchemy URL>))

def verify(request, message, signedMessage):
    response = w3.geth.personal.ecRecover(message, signature)
    return HttpResponse(response, content_type='text/json')
Related Topic