Creating Accounts from Private Keys⚓︎
This tutorial shows how to create accounts for the NEM blockchain, either by using an existing private key or by generating a new random account.
Prerequisites⚓︎
If you have not done so already, start with Setting Up a Development Environment.
Full Code⚓︎
Code Explanation⚓︎
Initializing the Facade⚓︎
The provides access to NEM's cryptographic operations and network utilities.
It is initialized with a network name (testnet or mainnet) to ensure that network-specific values,
such as addresses, are generated correctly.
Defining a Private Key⚓︎
The example starts by retrieving a private key from the environment variable PRIVATE_KEY as a hexadecimal string.
If the variable is set, the value is converted into a object.
Otherwise, a new random private key is generated using instead.
Store your private key securely
The private key gives full control over the account and any assets it holds. If you lose the private key, you lose access to the account permanently. If someone else obtains the private key, they can control the account.
Never share your private key with anyone, and always store it in a secure location.
Creating the Account⚓︎
After defining the private key, an account is created by deriving its public key and address.
-
Key pair creation: The constructor takes the private key and mathematically derives the corresponding public key. While the private key must remain secret, the public key can be safely shared with anyone.
-
Address derivation: The method converts the public key into an address, a shorter, human-readable, network-specific identifier for the account.
Output⚓︎
The output shown below corresponds to a typical run of the program.
Loading account from environment variable...
Address: TBONKWCOWBZYZB2I5JD3LSDBQVBYHB757VN3SKPP
Public key: 462EE976890916E54FA825D26BDD0235F5EB5B6A143C199AB0AE5EE9328E08CE
Private key: 0000000000000000000000000000000000000000000000000000000000000000
Each time the program runs without the environment variable, it generates a different random account. If a private key is provided, the same public key and address are always derived.
Conclusion⚓︎
This tutorial showed how to:
| Step | Related documentation |
|---|---|
| Load a private key | |
| Create a random private key | |
| Get the public key | |
| Get the address |
Next Steps⚓︎
Now that you have an account, you can: