Create a new account id on the ledger for a public key

Overview

To learn about how accounts works within Identify Snap, refer to the Snap Account documentation.

Identify Snap connects to your currently connected Metamask account by default. To learn how apps can connect to Identify Snap using a non-metamask(external) account, refer to this documentation.

By default, accounts on the Hedera ledger do not have account ids. Whenever a hedera account first interacts with the Hedera ledger, an account id is automatically generated. As the Identify Snap requires each hedera account to have an account id in addition to an EVM address, we have provided the createNewHederaAccount snap API that will let a user help generate a new account id for a new hedera account based on its public key. If you want to generate an account id for a new hedera account based on its EVM address, refer to the tutorial for that here.

In this tutorial, we are going to create a new hedera account id on the Hedera ledger by sending some HBAR to a public key.

Prerequisites

Make sure that you have added Hedera Network to your Metamask settings.

How to call the API from an app

const snapId = `npm:@tuum-tech/identify`;

const metamaskAddress = '0x2e5fF0267b678A0FAF9A9f5b0FBf7Ac9638B5b57'
const params = {
  metamaskAddress,
  hbarAmountToSend: 1, // This is the amount in TINYBAR
  newAccountPublickey: "032edfa618284f77c5e2092862964c1d233c74cc309c893c53a06ad71b8a8ccac0"
}

const createNewHederaAccountForEVMAddress = async () => {
  await window.ethereum.request({
    method: `wallet_snap_${snapId}`,
    params: {
      method: 'createNewHederaAccount',
      params: params,
    },
  });
};

Notes: Identify Snap will ensure that you're first connected to Hedera network before you can call any hedera specific APIs.

How the API is handled between the app and snap

What the API does

  1. Retrieves the currently connected account and the blockchain network the user has selected on Metamask. If it's the first time, the account info is also saved in snap state.

  2. Queries the Hedera Mirror Node to check whether an account id already exists for a given public key.

  3. If an account id already exists on the ledger, the snap returns it along with other information such as the current balance of the hedera account.

  4. If an account id doesn't exist on the ledger, the snap uses hedera sdk to send the appropriate amount of tinybars(smallest unit of HBARs on the hedera network) to the given public key address.

  5. The account id is retrieved from the transaction receipt and returned as the result.

Some example responses:

Creating a new hedera account id for a public key: 032edfa618284f77c5e2092862964c1d233c74cc309c893c53a06ad71b8a8ccac0

{
  "account": "0.0.3999983",
  "publicKey": "032edfa618284f77c5e2092862964c1d233c74cc309c893c53a06ad71b8a8ccac0",
  "newlyCreated": true
}

Calling the API to create a hedera account id for a public key address whereby accountId already exists on the hedera ledger:

{
  "account": "0.0.3999983",
  "evmAddress": "0x00000000000000000000000000000000003d08ef",
  "publicKey": "032edfa618284f77c5e2092862964c1d233c74cc309c893c53a06ad71b8a8ccac0",
  "alias": null,
  "balance": 1e-8,
  "createdDate": "Thu Apr 06 2023 15:17:11 GMT-0400 (Eastern Daylight Time)",
  "expiryDate": "Wed Jul 05 2023 15:17:11 GMT-0400 (Eastern Daylight Time)",
  "memo": "",
  "newlyCreated": false
}

Live Demo on CodePen

Make sure you're connected to the Hedera network before trying out the following demo.

Some things to keep in mind while interacting with the above demo
  • If you're getting any errors with the live demo, make sure you go through the FAQs section to learn about what you may be missing. You need to install Metamask in your browser for the live demo to work

  • Whenever there is a new version of the IdentifySnap, always make sure to first uninstall the old version of the snap from Metamask and only then try the above demo so it can install the latest version

To ease the integration of Identify Snap on an application, we have created a template web application that you can run locally and check out the code in its entirety to learn how you can integrate and interact with various APIs exposed by Identify Snap. Check out the full source code at template application github repository.

You can also check out the API reference to learn how each API works.

Last updated