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.
git clone https://github.com/hedera-dev/hello-future-world.gitIn the terminal, from the hello-future-world directory, enter the subdirectory for this sequence.
cd 00-create-fund-account/Install the dependencies using npm.
npm installCreate 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.
cp .env.sample .envCheckpoint
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:
artefact gasp crop double silk grid visual gather argue glow melody netCopy the seed phrase. Replace SEED_PHRASE in the .env file with it. The file contents should now look similar to this:
SEED_PHRASE="artefact gasp crop double silk grid visual gather argue glow melody net"
ACCOUNT_PRIVATE_KEY=YOUR_HEX_ENCODED_PRIVATE_KEY
ACCOUNT_ID=YOUR_ACCOUNT_ID
RPC_URL=YOUR_JSON_RPC_URLYou 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.
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:
const hdNodeRoot = ethersHdNode.fromMnemonic(process.env.SEED_PHRASE);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:
const evmAddress = `0x${privateKey.publicKey.toEvmAddress()}`;Run the script
In the terminal, run the script using the following command:
node script-create-fund-account.jsThis should produce output similar to the following:
privateKeyHex: 0x0ac20a3c1573ba9a5c6c69349fa51f40bd502cf250e226a7100869338f15aae2
evmAddress: 0x61b47b6aa6595a6546873fc831331f36639c906f
accountExplorerUrl: https://hashscan.io/testnet/account/0x61b47b6aa6595a6546873fc831331f36639c906f
accountId: undefined
accountBalanceHbar: undefinedNote 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:
SEED_PHRASE="artefact gasp crop double silk grid visual gather argue glow melody net"
ACCOUNT_PRIVATE_KEY=0x0ac20a3c1573ba9a5c6c69349fa51f40bd502cf250e226a7100869338f15aae2
ACCOUNT_ID=YOUR_ACCOUNT_ID
RPC_URL=YOUR_JSON_RPC_URLCopy 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
evmAddressoutput 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.
Paste the value of
evmAddressoutput 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:
SEED_PHRASE="artefact gasp crop double silk grid visual gather argue glow melody net"
ACCOUNT_PRIVATE_KEY=0x0ac20a3c1573ba9a5c6c69349fa51f40bd502cf250e226a7100869338f15aae2
ACCOUNT_ID=0.0.2667268
RPC_URL=YOUR_JSON_RPC_URLRefresh 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_IDabove (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:
node script-create-fund-account.jsThis should produce output similar to the following:
privateKeyHex: 0x0ac20a3c1573ba9a5c6c69349fa51f40bd502cf250e226a7100869338f15aae2
evmAddress: 0x61b47b6aa6595a6546873fc831331f36639c906f
accountExplorerUrl: https://hashscan.io/testnet/account/0x61b47b6aa6595a6546873fc831331f36639c906f
accountId: 0.0.2667268
accountBalanceHbar: 100.00000000Note 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
Was this helpful?