ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.89k stars 1.67k forks source link

Help for ERC20Permit: invalid signature #3416

Closed ylasgamers closed 3 weeks ago

ylasgamers commented 3 weeks ago

What happened?

i trying to make erc20 permit with python but got ERC20Permit: invalid signature can some one help me ? my code below, i using python 3.10.13 , and web3 py 6.18.0

from web3 import Web3, HTTPProvider
from eth_abi import encode
import json
import time
import config
from eth_account import Account
from eth_account.messages import encode_structured_data

account = Account.from_key(pvkey)
nonce = web3.eth.get_transaction_count(account.address)
deadline = int(time.time()) + 1000000
fdi = bytes(0x0f)
salt = bytes(0x0000000000000000000000000000000000000000000000000000000000000000)
data = {
    "types": {
        "EIP712Domain": [
            {"name": "fields", "type": "bytes"},
            {"name": "name", "type": "string"},
            {"name": "version", "type": "string"},
            {"name": "chainId", "type": "uint256"},
            {"name": "verifyingContract", "type": "address"},
            {"name": "salt", "type": "bytes32"},
            {"name": "extensions", "type": "uint256[]"},
        ],
        "Permit": [
            {"name": "owner", "type": "address"},
            {"name": "spender", "type": "address"},
            {"name": "value", "type": "uint256"},
            {"name": "nonce", "type": "uint256"},
            {"name": "deadline", "type": "uint256"},
        ],
    },
    "domain": {
        "fields": fdi,
        "name": "Taiko Token", 
        "version": "1",
        "chainId": 167000,
        "verifyingContract": tokenaddr,
        "salt": salt,
        "extensions": [],
    },
    "primaryType": "Permit",
    "message": {
        "owner": account.address,
        "spender": sender,
        "value": config.apprv,
        "nonce": nonce,
        "deadline": deadline,
    },
}

def test_permit():
    structured_msg = encode_structured_data(data)
    signed = Account.sign_message(structured_msg, account.key)
    recovered_address = Account.recover_message(structured_msg, signature=signed.signature)
    assert recovered_address == account.address
    gasAmount = 250000
    tx_permit = token_contract.functions.permit(account.address, sender, config.apprv, deadline, signed.v, web3.to_bytes(signed.r), web3.to_bytes(signed.s)).build_transaction({
        'chainId': chainId,
        'from': sender,
        'gas': gasAmount,
        'gasPrice': web3.eth.gas_price,
        'nonce': web3.eth.get_transaction_count(sender)
    })

    #sign the transaction
    sign_txn = web3.eth.account.sign_transaction(tx_permit, senderkey)
    #send transaction
    tx_hash = web3.eth.send_raw_transaction(sign_txn.rawTransaction)

    #get transaction hash
    txid = str(web3.to_hex(tx_hash))
    print('')
    print('Transaction Success TX-ID Result...')
    print(txid)

test_permit()

Code that produced the error

No response

Full error output

No response

Fill this section in if you know how this could or should be fixed

No response

web3 Version

6.18.0

Python Version

3.10.13

Operating System

windows

Output from pip freeze

No response