Create and fund account
Hello World sequence: Create a new account on Hedera Testnet, and fund it. Do this before any of the other Hello World sequences.
Why you need to create and fund an account
Hedera is a distributed ledger technology (DLT). To interact with it, you will need to send transactions to the network, which will then process them and add them to the ledger if they are deemed to be valid. On most web services (web2), you need to authenticate using usernames and passwords to operate your account. On DLTs such as Hedera, it is similar, except that you will need to use cryptographic keys instead of passwords to operate your account. One key difference is that unlike web2, each interaction needs to be paid for using the native currency of the DLT, which is similar to micro-transactions. On Hedera, this currency is HBAR.
What you will accomplish
Prerequisites
Before you begin, you should be familiar with the following:
Get started
Set up project
To follow along, start with the main
branch, which is the default branch of this repo. This gives you the initial state from which you can follow along with the steps as described in the tutorial.
In the terminal, from the hello-future-world
directory, enter the subdirectory for this sequence.
Install the dependencies using npm
.
Create your .env file
Make a .env
file by copying the provided .env.sample
file. Then open the .env
file in a code editor, such as VS Code.
Checkpoint
Generate seed phrase
In the terminal, run the following command:
This should output a seed phrase, a list of 12 randomly selected dictionary words, for example:
Note that mnemonics
is a tool that generates BIP-39 seed phrases, and your seed phrase will be different from above.
If this is your first time running this command, you need to enter y
to agree to do so:
This seed phrase will be used to generate the cryptographic keys for the accounts that you are about to create.
Copy the seed phrase. Replace SEED_PHRASE
in the .env
file with it. The file contents should now look similar to this:
You do not need to modify the other values in the .env
file yet.
Be sure to save your files before moving on to the next step.
Write the script
An almost-complete script has already been prepared for you, and you will only need to make a few modifications (outlined below) for it to run successfully.
Open the script file, script-create-fund-account.js
, in a code editor.
To follow along in the tutorial, when asked to modify code, look for a comment to locate the specific lines of code which you will need to edit.
For example, the comment for Step 1 looks like this:
Step 1: Derive private key
The ethersHdNode
module has been imported from EthersJs. This takes a seed phrase as input, and outputs a private key. To do so, invoke the fromMnemonic()
method and pass in process.env.SEED_PHRASE
as the parameter:
You will need to delete the inline comment that looks like this: /* ... */
. Replace it with the correct code. For example, in this step, the change looks like this:
The hdNodeRoot
instance is subsequently used to generate the private keys.
Step 2: Derive EVM address
A privateKey
instance has been initialized. This requires no further input to derive an EVM address - read its publicKey
property, and then invoke its toEvmAddress
method:
Run the script
In the terminal, run the script using the following command:
This should produce output similar to the following:
Note that accountId
and accountBalanceHbar
are both undefined
, because generating cryptographic keys alone is not enough to create an account - that only happens upon the first transaction.
Copy the value of privateKeyHex
. Replace YOUR_HEX_ENCODED_PRIVATE_KEY
in the .env
file with it. The file contents should now look similar to this:
Copy the value of accountExplorerUrl
and visit this in your browser.
You should see a page with:
The title "Inactive EVM Address" (1)
"Account ID: Assigned upon activation" (2)
"EVM Address:" matching the value of
evmAddress
output earlier (3)A helpful hint saying "Own this account? Activate it by transferring any amount of ℏ or tokens to ..." (4)
This is precisely the next step!
Transfer and activate account
Visit portal.hedera.com/faucet
.
The faucet dispenses Testnet HBAR to any account on Hedera Testnet. When it is asked to dispense to an EVM address that does not yet have an account, the account gets created as part of the HBAR transfer transaction.
Paste the value of
evmAddress
output earlier into the "enter wallet address" field (1)Press the "receive testnet HBAR" button (2)
A confirmation dialog will pop up.
Complete the ReCaptcha (1)
Press the "confirm transaction" button. (2)
A success dialog will pop up.
The account ID is displayed (1)
This indicates that the Testnet HBAR has been transferred, and in the process a new account has been created.
Note that the EVM address is not the same as the account ID - instead the EVM address is an alias of the account ID.
Press the icon to copy the account ID (2)
Replace YOUR_ACCOUNT_ID
in the .env
file with it. The file contents should now look similar to this:
You may have noticed that RPC_URL
is unused throughout. This is intentional - as it will be used in some of the other Hello World sequences, so be sure to check them out after completing this one!
Refresh the Hashscan page in your browser. Note that this is the accountExplorerUrl
that was output from the previous run of the script. This time you should see:
The title is "Account" (1)
instead of "Inactive EVM Address"
The "Account ID" field should matching the value of
ACCOUNT_ID
above (2)instead of "Assigned upon activation"
The "Create Transaction" field displays a transaction ID (3)
Verify account creation and funding
In the terminal, re-run the script using the following command:
This should produce output similar to the following:
Note that this is almost the same as when you first ran the same script. The difference is that previously both accountId
and accountBalanceHbar
were undefined
; and now accountId
should now show a value (in the format of 0.0.XYZ
), and accountBalanceHbar
should now show a number (with 8 decimal places). This is because the account has been created and funded.
🎉 Now you are ready to start using your Hedera Testnet account from the portal within script files on your computer! 🎉
Complete
Congratulations, you have completed the create and fund account Hello World sequence! 🎉🎉🎉
You have learned how to:
Next Steps
Now that you have an account on Hedera Testnet and it is funded, you can interact with the Hedera network. Continue by following along with the other Hello World sequences.
Cheat sheet
Last updated