Sign Typed Data

by passbird

HTML

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8">

    <!-- 
    Import metakeep sdk.
    You can also import npm package if you are using node for building website
    e.g. 
    npm install [email protected]
    require(metakeep)
    -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.js" integrity="sha256-XwEVfJ47JrjxB4a8Hyym7/kwX23rmtA1ZW6hbdCW090=" crossorigin="anonymous"></script>
  </head>

</html>

JavaScript

/* Init SDK */
const sdk = new MetaKeep({
  /* App id to configure UI */
  appId: "9cc98bca-da35-4da8-8f10-655b3e51cb9e",
});

/* Call signTypedData. It returns a promise which returns when the operation succeeds.
Otherwise throws an error.
*/
async function sign() {
  try {
    const signResponse = await sdk.signTypedData(
      // Typed data
      {
        types: {
          EIP712Domain: [{
              name: "name",
              type: "string"
            },
            {
              name: "version",
              type: "string"
            },
            {
              name: "chainId",
              type: "uint256"
            },
            {
              name: "verifyingContract",
              type: "address"
            },
          ],
          MetaTransaction: [{
              name: "nonce",
              type: "uint256",
            },
            {
              name: "from",
              type: "address",
            },
            {
              name: "functionSignature",
              type: "bytes",
            },
          ],
        },
        primaryType: "MetaTransaction",
        domain: {
          name: "Ether Mail",
          version: "1",
          chainId: 1,
          verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
        },
        message: {
          nonce: 200,
          from: "0x97706Df14A769E28EC897dAc5Ba7bCfa5AA9C444",
          functionSignature: "0xd1a1beb40000",
        },
      },
      // signing reason
      "reason",
    );
    console.log("sign successful");
    console.log(signResponse);
  } catch (err) {
    console.log("Error when trying to sign");
    console.log(err.message || err);
  }
}


sign();