[Python] Web3.py and Python: Unable to generate valid signatures for Four.meme wallet...

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    Problem Description: I'm trying to authenticate with Four.meme using their wallet-based authentication system, but I keep receiving a "Signature for this request is not valid" error. I've tried multiple approaches to sign messages with my wallet's private key, but none of them produce a signature that the Four.meme API accepts.

    from web3 import Web3, Account
    from eth_account.messages import encode_defunct

    # Setup
    PRIVATE_KEY = "YOUR_PRIVATE_KEY" # Private key redacted for security
    WALLET_ADDRESS = Web3.to_checksum_address("0x8358494c81a35db01AfB1EEb93cBfe995B0cF057")
    web3 = Web3(Web3.HTTPProvider("https://bsc-dataseed.binance.org/"))

    # Create session
    session = requests.Session()
    session.headers.update({
    "Origin": "https://four.meme",
    "Referer": "https://four.meme/",
    "User-Agent": "Mozilla/5.0",
    "Content-Type": "application/json"
    })

    # Get nonce
    nonce_url = "https://four.meme/meme-api/v1/private/user/nonce/generate"
    nonce_payload = {
    "accountAddress": WALLET_ADDRESS,
    "verifyType": "LOGIN"
    }
    resp = session.post(nonce_url, json=nonce_payload)
    nonce = resp.json()["data"]

    # Sign message
    message = str(nonce) # Just the nonce as a string, without formatting
    message_encoded = encode_defunct(text=message)
    signed_msg = Account.from_key(PRIVATE_KEY).sign_message(message_encoded)
    signature = signed_msg.signature.hex()
    if not signature.startswith("0x"):
    signature = "0x" + signature

    # Login attempt
    login_url = "https://four.meme/meme-api/v1/private/user/login/dex"
    login_payload = {
    "region": "WEB",
    "langType": "EN",
    "loginIp": "",
    "inviteCode": "",
    "verifyInfo": {
    "address": WALLET_ADDRESS,
    "networkCode": "BSC",
    "signature": signature,
    "verifyType": "LOGIN"
    },
    "walletName": "MetaMask"
    }

    login_resp = session.post(login_url, json=login_payload)
    print(login_resp.json()) # {"code": -1006, "msg": "Signature for this request is not valid"} ```

    What I've Tried

    Different message formats:

    Just the nonce as a string: "123456"
    Formatted message: "Login Four.Meme:\n\n123456"
    Hex-encoded nonce
    UTF-8 encoded bytes


    Different signature methods:

    Using encode_defunct() with various parameters
    Different recovery ID formats (0/1 vs 27/28)
    Personal sign format with Ethereum-specific prefixes


    Manual signature verification:

    Signed a message manually with MetaMask in the browser
    Used the same nonce and signature in my script
    Still received "Signature for this request is not valid"


    Recovery validation:

    Verified that the correct address is recovered from the signatures
    Confirmed that the signature format appears correct (65 bytes with 0x prefix)



    I've even tried directly copying a successful signature from a browser connection, but it's still rejected when coming from my script.
    Environment

    Python 3.10
    web3.py 6.0.0
    Binance Smart Chain
    MetaMask for browser comparison

    Question
    What could be causing the "Signature for this request is not valid" error when my signatures appear to be correctly formatted and recover to my wallet address? Are there specific requirements for Four.meme's signature validation that I'm missing?

    Continue reading...

Compartilhe esta Página