Skip to content

Instantly share code, notes, and snippets.

@simonvc
Created April 23, 2021 22:22
Show Gist options
  • Save simonvc/81984ca2981625193c249caa7c1df159 to your computer and use it in GitHub Desktop.
Save simonvc/81984ca2981625193c249caa7c1df159 to your computer and use it in GitHub Desktop.
Signing and verifying a message to enable single sign-on using web3py, but where the message was created by web3js 2021
from web3 import Web3
import web3
from random import randrange
from eth_account.messages import encode_defunct
# This is a server module. It runs on the Anvil server,
# rather than in the user's browser.
#
# To allow anvil.server.call() to call functions here, we mark
# them with @anvil.server.callable.
# Here is an example - you can replace it with your own:
#
base_message="""Signing this message confirms to this website that you're the real holder of some TreeDAO tree token or NFT.
This does not cost any ETH or Tokens. [%s]"""
@anvil.server.callable
def get_info_signing_request(account):
ar = tables.app_tables.nonces.get(account=account)
if not ar:
ar=tables.app_tables.nonces.add_row(account=account, nonce=str(randrange(100000000, 999990000)))
#todo remove the nonce from DB to stop replay attacks.
return base_message % ar['nonce']
def check_sig(account, message, signature):
#w3 = Web3(Web3.HTTPProvider(anvil.secrets.get_secret("infura_url")))
return account == web3.Account.recover_message(encode_defunct(text=message), signature=signature)
@anvil.server.callable
def get_info(account, signature):
return f"Here's some info {signature}, {check_sig(account, get_info_signing_request(account), signature)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment