Python signing EIP-712 messages for blur.io

520 Views Asked by At

The goal is to place a collection bid on blur.io in python. To access the Blur API I use https://rapidapi.com/openseatools/api/blur/.

To summarise the issue: I cannot manage to successfully sign a message (or get the same signature in python as in Metamask, using the same data).

The attempt:

values = message["signatures"][0]["signData"]["value"]

# 'message' types that differs from 'types' schema
values["tokenId"] = int(values["tokenId"])
values["amount"] = int(values["amount"])
values["price"] = int(values["price"])
values["expirationTime"] = int(values["expirationTime"], 16)
values["salt"] = int(values["salt"], 16)
values["extraParams"] = bytes(int(values["extraParams"], 16))
values["nonce"] = int(values["nonce"])

data = {
    "domain": message["signatures"][0]["signData"]["domain"],
    "message": values,
    "types": { 
    "EIP712Domain": [
        {"name": "name", "type": "string"},
        {"name": "version", "type": "string"},
        {"name": "chainId", "type": "uint256"},
        {"name": "verifyingContract", "type": "address"},
     ], 
     **message["signatures"][0]["signData"]["types"]
  },
  "primaryType": 'Order',
}

signature = Web3().eth.account.sign_message(
    encode_structured_data(data), 
private_key=self.wallet_credentials.private_key) 

With message being the response from a POST /v1/collection-bids/format.

You can test the process by visiting blur.io and posting a collection bid (and watching the format and submit requests).


The developer from rapidapi.com provided the following code to sign the message in JS. This code works fine, so the question could be: how to implement this function in python?

const signature = await wallet.signer._signTypedData(
      message.signData.domain,
      message.signData.types,
      message.signData.value,
    );
2

There are 2 best solutions below

0
Joseph On

The error was here:

values["extraParams"] = bytes.fromhex(values["extraParams"][2:])
1
Alex Stuflesser On

Have got some troubles when trying to run this code

 values = message["signatures"][0]["signData"]["value"]
             ~~~~~~~^^^^^^^^^^^^^^
TypeError: string indices must be integers, not 'str'

encode_structured_data is not defined

private_key=self.wallet_credentials.private_key):
self is not defined, wallet_credentials.private_key = Any