Sign EOS Transaction

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 metakeep@2.2.8
    require(metakeep)
    -->
    <script src="https://cdn.jsdelivr.net/npm/metakeep@2.2.8/lib/index.js" integrity="sha256-dVJ6hf8zqdtHxHJCDJnLAepAyCCbu6lCXzZS3lqMIto=" crossorigin="anonymous">
    </script>
  </head>

</html>

JavaScript

/* Init SDK */
const sdk = new MetaKeep({
  /* EOS App id to configure UI */
  appId: "e2dddfde-2c75-40f8-b697-6f47738bc5ed",
});

/* Call signTransaction. It returns a promise which returns when the operation succeeds.
Otherwise throws an error.
*/
async function sign() {
  try {
    var currentTime = new Date();
    currentTime.setMinutes(currentTime.getMinutes() + 2); // 2 minutes ahead
    const signResponse = await sdk.signTransaction(
      // Transaction object
      {
        // Raw EOS transaction
        "rawTransaction": {
          "expiration": currentTime.toISOString().slice(0, 19), // current time + 2 minutes
          "ref_block_num": 12345,
          "ref_block_prefix": 67890,
          "max_net_usage_words": 0,
          "max_cpu_usage_ms": 0,
          "delay_sec": 0,
          "context_free_actions": [],
          "actions": [{
            "account": "eosio.token",
            "name": "transfer",
            "authorization": [{
              "actor": "myaccount",
              "permission": "active"
            }],
            // Send action data as a serialized hex string.
            "data": "0000000000ea305500000000487a2b000000000000000004454f530000000000"
          }],
          "transaction_extensions": []
        },
        "extraSigningData": {
          // If chainId is part of the signature generation,
          // send it inside extraSigningData field.
          "chainId": "b20901380af44ef59c5918439a1f9a41d83669020319a80574b804a5f95cbd7e"
        }
      },
      // 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();