1

I want to use openSea's api, but I don't know how to get the hash and signature of this json object using java.

https://docs.opensea.io/reference/create-an-order

{
"parameters": {
    "offerer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
    "zone": "0x004C00500000aD104D7DBd00e3ae0A5C00560C00",
    "zoneHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "startTime": "0",
    "endTime": "1656044994000",
    "orderType": 0,
    "offer": [
        {
            "itemType": 2,
            "token": "0x0165878A594ca255338adfa4d48449f69242Eb8F",
            "identifierOrCriteria": "1",
            "startAmount": "1",
            "endAmount": "1",
        },
    ],
    "consideration": [
        {
            "itemType": 0,
            "token": "0x0000000000000000000000000000000000000000",
            "identifierOrCriteria": "0",
            "startAmount": "9750000000000000000",
            "endAmount": "9750000000000000000",
            "recipient": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
        },
        {
            "itemType": 0, 
            "token": "0x0000000000000000000000000000000000000000",
            "identifierOrCriteria": "0",
            "startAmount": "250000000000000000",
            "endAmount": "250000000000000000",
            "recipient": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
        },
        {
            "itemType": 0, 
            "token": "0x0000000000000000000000000000000000000000",
            "identifierOrCriteria": "0",
            "startAmount": "500000000000000000",
            "endAmount": "500000000000000000",
            "recipient": "0x8a90cab2b38dba80c64b7734e58ee1db38b8992e",
        },
    ],
    "totalOriginalConsiderationItems": 2,
    "salt": 12686911856931635052326433555881236148,
    "conduitKey": "0x0000007b02230091a7ed01230072f7006a004d60a8d4e71d599b8104250f0000",
    "nonce": 0,
},
"signature": "0x",

"protocol_address": "0x00000000000001ad428e4906aE43D8F9852d0dD6", }

1 Answer 1

0

OpenSeaOrder.json

demo

{
  "domain": {
    "chainId": 5,
    "name": "Seaport",
    "verifyingContract": "0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC",
    "version": "1.5"
  },
  "message": {
    "offerer": "0x6278A1E803A76796a3A1f7F6344fE874ebfe94B2",
    "zone": "0x004C00500000aD104D7DBd00e3ae0A5C00560C00",
    "offer": [
      {
        "itemType": "2",
        "token": "0x97f236E644db7Be9B8308525e6506E4B3304dA7B",
        "identifierOrCriteria": "111",
        "startAmount": "1",
        "endAmount": "1"
      }
    ],
    "consideration": [
      {
        "itemType": "0",
        "token": "0x0000000000000000000000000000000000000000",
        "identifierOrCriteria": "0",
        "startAmount": "1082250000000000000",
        "endAmount": "1082250000000000000",
        "recipient": "0x6278A1E803A76796a3A1f7F6344fE874ebfe94B2"
      },
      {
        "itemType": "0",
        "token": "0x0000000000000000000000000000000000000000",
        "identifierOrCriteria": "0",
        "startAmount": "27750000000000000",
        "endAmount": "27750000000000000",
        "recipient": "0x0000a26b00c1F0DF003000390027140000fAa719"
      }
    ],
    "orderType": "0",
    "startTime": "1686193412",
    "endTime": "1688785412",
    "zoneHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "salt": "24446860302761739304752683030156737591518664810215442929818227897836383814680",
    "conduitKey": "0x0000007b02230091a7ed01230072f7006a004d60a8d4e71d599b8104250f0000",
    "counter": "0"
  },
  "primaryType": "OrderComponents",
  "types": {
    "EIP712Domain": [
      {
        "name": "name",
        "type": "string"
      },
      {
        "name": "version",
        "type": "string"
      },
      {
        "name": "chainId",
        "type": "uint256"
      },
      {
        "name": "verifyingContract",
        "type": "address"
      }
    ],
    "OfferItem": [
      {
        "name": "itemType",
        "type": "uint8"
      },
      {
        "name": "token",
        "type": "address"
      },
      {
        "name": "identifierOrCriteria",
        "type": "uint256"
      },
      {
        "name": "startAmount",
        "type": "uint256"
      },
      {
        "name": "endAmount",
        "type": "uint256"
      }
    ],
    "ConsiderationItem": [
      {
        "name": "itemType",
        "type": "uint8"
      },
      {
        "name": "token",
        "type": "address"
      },
      {
        "name": "identifierOrCriteria",
        "type": "uint256"
      },
      {
        "name": "startAmount",
        "type": "uint256"
      },
      {
        "name": "endAmount",
        "type": "uint256"
      },
      {
        "name": "recipient",
        "type": "address"
      }
    ],
    "OrderComponents": [
      {
        "name": "offerer",
        "type": "address"
      },
      {
        "name": "zone",
        "type": "address"
      },
      {
        "name": "offer",
        "type": "OfferItem[]"
      },
      {
        "name": "consideration",
        "type": "ConsiderationItem[]"
      },
      {
        "name": "orderType",
        "type": "uint8"
      },
      {
        "name": "startTime",
        "type": "uint256"
      },
      {
        "name": "endTime",
        "type": "uint256"
      },
      {
        "name": "zoneHash",
        "type": "bytes32"
      },
      {
        "name": "salt",
        "type": "uint256"
      },
      {
        "name": "conduitKey",
        "type": "bytes32"
      },
      {
        "name": "counter",
        "type": "uint256"
      }
    ]
  }
}

Get order hash

CalcOpenseaOrderHash

package com.utils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SignatureException;
import java.util.HashMap;
import org.web3j.crypto.StructuredDataEncoder;
import org.web3j.utils.Numeric;

public class CalcOpenseaOrderHash {
    private static String jsonMessageString;

    public static void main(String[] args) throws SignatureException, Exception {

        String JSONFilePath = "./OpenSeaOrder.json";
        jsonMessageString = new String(
                Files.readAllBytes(Paths.get(JSONFilePath).toAbsolutePath()), StandardCharsets.UTF_8);
        System.out.println(jsonMessageString);
        StructuredDataEncoder dataEncoder = new StructuredDataEncoder(jsonMessageString);
        //
        byte[] structHashDomain = dataEncoder.hashDomain();

        String structHashDomainString = Numeric.toHexString(structHashDomain);
        System.out.println("keccak256(abi.encode(typeHash, nameHash, versionHash, block.chainid, address(this))):");
        System.out.println(structHashDomainString);

        // TODO: orderHash
        byte[] dataHash = dataEncoder.hashMessage(
                dataEncoder.jsonMessageObject.getPrimaryType(),
                (HashMap<String, Object>) dataEncoder.jsonMessageObject.getMessage());
        String dataHashString = Numeric.toHexString(dataHash);
        System.out.println("keccak256(abi.encode(TYPE_HASH, parameters)):");
        System.out.println("orderHash: " + dataHashString);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.