Create a new account id on the ledger for an EVM address

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 EVM address. If you want to generate an account id for a new hedera account based on its public key, 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 an EVM address.

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
  newAccountEvmAddress: "0x5Ce55759b0D4049ED015a657726C1aaFC77F7bEC"
}

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 EVM address.

  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 EVM 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 an EVM address: 0x5Ce55759b0D4049ED015a657726C1aaFC77F7bEC

{
    'account': '0.0.3988440',
    'evmAddress': '0x5Ce55759b0D4049ED015a657726C1aaFC77F7bEC',
    'newlyCreated': true
}

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

{
  "account": "0.0.3988440",
  "evmAddress": "0x5ce55759b0d4049ed015a657726c1aafc77f7bec",
  "alias": "LTSVOWNQ2QCJ5UAVUZLXE3A2V7DX667M",
  "balance": 1,
  "createdDate": "Tue Apr 04 2023 14:31:49 GMT-0400 (Eastern Daylight Time)",
  "expiryDate": "Mon Jul 03 2023 14:31:49 GMT-0400 (Eastern Daylight Time)",
  "memo": "lazy-created account",
  "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