description |
---|
Setup tutorial - HSCS workshop. Learn how to enable custom logic & processing on Hedera through smart contracts. |
{% embed url="https://www.youtube.com/watch?v=25y5zslOUj8" %} Hedera Smart Contract Service Workshop Part 2/6 | Setup {% endembed %}
The setup has already been (mostly) done. All that's left for you to do is clone the accompanying tutorial GitHub repository and install the dependencies:
git clone -b main [email protected]:hedera-dev/hedera-smart-contracts-workshop.git
cd hedera-smart-contracts-workshop
{% hint style="info" %} If you do not have SSH available on your system, or are unable to configure it for GitHub, you may wish to try this git command instead:
git clone -b main https://github.com/hedera-dev/hedera-smart-contracts-workshop.git
{% endhint %}
In the root directory of the repo, you will find a file named .env.example
. Make a copy of this file, and name it .env
.
cp .env.example .env
An operator account is used to obtain an initial sum of HBAR on Hedera Testnet, and then use that to pay for various Hedera network operations. This includes everything from basic transactions, to gas fees for HSCS interactions.
Visit the Hedera Portal to get started.
(1) Create a Testnet account.
Hedera Portal - Create Testnet Account
(2) Copy-paste the confirmation code sent to your email.
Hedera Portal - Email Verification
(3) Fill out this form with details for your profile.
Hedera Portal - Profile Details
(4) In the top-left there is a drop down menu, select between Hedera Testnet (default) and Previewnet:
Hedera Portal - Select Network
(5) From the next screen that shows your accounts, copy the value of the "DER-encoded private key" and replace OPERATOR_KEY
in the .env
file with it.
Hedera Portal - Account Details
(6) From the same screen, copy the value of "Account ID" and replace the value of the OPERATOR_ID
variable in the .env
file with it.
{% hint style="info" %} Note that private keys should be stored and managed securely. For the purposes of a tutorial, secure key management has been skipped, and you are storing your private keys in plain text on disk. Do not do this in production applications. {% endhint %}
When developing smart contracts, you often need more than 1 account to do so. Thankfully we do not need to go through the somewhat cumbersome process of creating multiple accounts via the Hedera Portal - you only really need to do that once for the operator account.
Any subsequent accounts that you wish to create can be generated programmatically, and funded with HBAR from your operator account.
To do so, we will utilise something called a seed phrase, which is a sequence of selected dictionary words chosen at random. This process is defined in BIP-39.
Subsequently, we will use that seed phrase as an input and generate multiple accounts; each of which consists of a private key, a public key, and an address. This process is defined in BIP-44.
BIP, EIP, and HIP
- "BIP" stands for Bitcoin Improvement Proposal.
- "EIP" stands for Ethereum Improvement Proposal, and was preceded by "ERC" which stands for Ethereum Request for Comments.
- "HIP" stands for Hedera Improvement Proposal.
Interestingly these 2 BIPs were never adopted by the Bitcoin community, but are almost de-facto used by everyone in the Ethereum community. On Hedera, you can use these 2 BIPs to generate Hedera EVM accounts, but this is not possible for Hedera-native accounts (as they use a different type of public key algorithm).
ECDSA and EdDSA
ECDSA (Elliptic Curve Digital Signing Algorithm) is a public key algorithm, and secp256k1 is a particular configuration that may be used by the ECDSA algorithm.
EdDSA (Edwards Digital Signing Algorithm) is another public key algorithm, and Ed25519 is a particular configuration that may be used by the EdDSA algorithm.
Both Bitcoin and Ethereum use ECDSA with secp256k1 for their accounts.
Hedera native accounts use EdDSA with Ed25519, and Hedera EVM accounts use ECDSA with secp256k1.
Enough theory - let's generate a seed phrase!
Visit iancoleman.io/bip39
, and you can generate a BIP39 seed phrase there: `
`
- Locate the line that is labelled "Generate a random mnemonic"
- Select any number from the dropdown that is more than or equal to
12
- Press "GENERATE"
- Locate the section that is labelled "BIP39 Mnemonic"
- Copy these words from the text box - this will be your BIP39 seed phrase
Replace the value of the BIP39_SEED_PHRASE
variable in the .env
file with this phrase.
{% hint style="info" %}
- Ref: BIP-39 Mnemonic code for generating deterministic keys
- Ref: BIP-44: Multi-Account Hierarchy for Deterministic Wallets {% endhint %}
At this point, you have an operator account, which is already funded with HBAR, and you have a seed phrase. Let's generate more accounts based on the seed phrase , and then transfer HBAR to them from the operator account.
First switch to the intro
directory, and install dependencies using npm.
cd ./intro
npm install
Next, let's use a script already prepared for you. We want this script to generate 2 Hedera EVM accounts, and transfer 100 HBAR to each of them, so let's set those values in generate-evm-accounts.js
.
const NUM_ACCOUNTS = 2;
const AMOUNT_PER_ACCOUNT = 100;
const HD_PATH = "m/44'/60'/0'/0";
Choice of derivation path
Above, we're using m/44'/60'/0'/0
as the derivation path. This value is the Ethereum derivation path, and we need to use this because Metamask does not allow it to be configured.
Run this script.
node ./generate-evm-accounts.js
This should output something similar to the following:
EVM account #0 generated.
#0 HD path: m/44'/60'/0'/0/0
#0 Private key: 3030020100300706052b8104000a04220420fb11afc5d508036ac7a9df9f1eb7cea551e4a7b738c2c70da099fe5f379f3364
#0 Public key: 302d300706052b8104000a032200027a753c29cc9f0ea0b6ccf0614676daeba3da0dbd5f54ef9850ad3878ded4e077
#0 EVM address: 07ffaadfe3a598b91ee08c88e5924be3eff35796
EVM account #1 generated.
#1 HD path: m/44'/60'/0'/0/1
#1 Private key: 3030020100300706052b8104000a042204206e3ff9f1f1ae58248a5838ec877acc55d103009586224d76ab74a652d408cf12
#1 Public key: 302d300706052b8104000a03220002c4c2ed7a682a601c9c61dec42e87442b63893a6e5efdf6dc327a4b3bcc62aba9
#1 EVM address: 1c29e31d241f0d06f3763221f5224a6b82f09cce
Transfer transaction ID: [email protected]
HashScan URL: https://hashscan.io/testnet/transaction/[email protected]
Copy the HashScan URL, paste it into a browser, and you will see a "Transaction" page on HashScan.
Screenshot showing a single transaction with multiple recipients transferring HBAR (on hashscan.io).
Scroll down to the "Transfers" section, which should show the flow of HBAR between various accounts. In this case -200
(and a fractional amount of -0.00185217
) from the operator account, +100.00000000
to each of the 2 EVM accounts, and fractional amounts to a couple of other accounts to pay for transaction processing. (Note that the fractional amounts may vary, they won't necessarily be 0.00185217
as above.)
Now you should have 1 Hedera-native account (previously funded), plus 2 new EVM accounts (freshly funded).
Hedera networks have a native account address format, called the Account ID. An example of this would be: 0.0.3996280
.
Hedera also supports EVM account address formats. This has 2 variants:
The EVM Address Alias. An example of this would be: 0x7394111093687e9710b7a7aeba3ba0f417c54474
. This is sometimes referred to as the non-long-zero address.
The Account Num Alias. An example of this would be: 0x00000000000000000000000000000000003cfa78
. This is sometimes referred to as the long-zero address.
Finally Hedera also supports a Key Alias, and this is something that you're unlikely to encounter in most situations.
While you may choose to interact with the Hedera network using any of the address formats, when interacting with smart contracts, the EVM Address Alias is the most useful, as that is what is visible and understood by smart contracts when they are invoked.
{% hint style="info" %}
- Ref: Hedera - Account Properties
- Ref: HIP-583 - Expand alias support in CryptoCreate & CryptoTransfer Transactions
- Ref: hedera-code-snippets - Convert address from Hedera-native (
S.R.N
) format to EVM (0x...
) format - Ref: Stackoverflow - How to convert a Hedera native address into a non-long-zero EVM address? {% endhint %}
For this step, you have a choice:
- Run your own Hedera RPC Relay server: Configuring Hedera JSON-RPC Relay endpoints
- Use an RPC service provider, Arkhia: Configuring Arkhia RPC endpoints
Whichever method you choose, obtain the JSON-RPC URL for Hedera Testnet, and replace the value of the RPC_URL_HEDERATESTNET
variable in the .env
file with this.