Account Generation
On this page, we'll cover how to generate a new account on the AVN.
The Aventus blockchain is a Substrate-based blockchain, and like all Substrate-based blockchains, all keys use the SS58 address encoding format.
There are multiple ways to generate an account, and we'll explore three below.
- Using this Gateway API
- Subkey
- Using Polkadot libraries
avnSdk.accountUtils.generateNewAccount();
This account generation process happens offline and thus does not confine the generated account to any particular network. Also, this operation is gasless and thus does not cost any AVT.
Example
const { AvnApi, SetupMode, SigningMode } = require("avn-api");
const AVN_GATEWAY_URL = "https://gateway.testnet.aventus.network/";
const options = {
suri: "0x5392ca60a61aea99fce14358798de93c1bc11c3696a905718738c71fae539c24", // this is from the generated example account, replace with your suri
setupMode: SetupMode.SingleUser,
signingMode: SigningMode.SuriBased,
};
const avnSdk = new AvnApi(AVN_GATEWAY_URL, options);
async function main() {
await avnSdk.init();
console.log(avnSdk.accountUtils.generateNewAccount());
}
(async () => {
await main();
})();
The below account details are just an example and are public. Please, DO NOT send anything to it or use it for anything.
Example Generated Account
{
mnemonic: 'flag dynamic laptop often park illegal equip curve game blame junior warm',
seed: '0x5392ca60a61aea99fce14358798de93c1bc11c3696a905718738c71fae539c24',
address: '5EYzWhGxbogEfwNKL52ZRDCgBxu4t8oWDFAsXXVYvH6dMQTo',
publicKey: '0x6e0b67f4590ac2b4e9daa9f229cc4a0c77b3109e8a622e5a1128518ad76e736b'
}
To begin, you would need to install Subkey. Subkey is a Substrate utility for generating accounts and keys and verifying them as well. The documentation provides an apt description on how to install Subkey, however, just to provide you with the briefs.
- Install cargo
- Run this command in your terminal:
# Use the `--fast` flag to get the dependencies without needing to install the Substrate and Subkey binary
curl https://getsubstrate.io -sSf | bash -s -- --fast
# Install only `subkey`, at a specific version
cargo install --force subkey --git https://github.com/paritytech/substrate --version 2.0.1 --locked
You can verify you have cargo installed by running
cargo --version
. You can also verify you have subkey installed by runningsubkey --version
.
Now, to generating your account on the Aventus network. Run this command in your terminal:
subkey generate
If it generated something similar to the example below, Congratulations! You now have an account that can be used on the Aventus Network.
Example Generated Account
Secret phrase: flag dynamic laptop often park illegal equip curve game blame junior warm
Secret seed: 0x5392ca60a61aea99fce14358798de93c1bc11c3696a905718738c71fae539c24
Public Key (hex): 0x6e0b67f4590ac2b4e9daa9f229cc4a0c77b3109e8a622e5a1128518ad76e736b
Account ID: 0x6e0b67f4590ac2b4e9daa9f229cc4a0c77b3109e8a622e5a1128518ad76e736b
Public key (SS58): 5EYzWhGxbogEfwNKL52ZRDCgBxu4t8oWDFAsXXVYvH6dMQTo
SS58 Address: 5EYzWhGxbogEfwNKL52ZRDCgBxu4t8oWDFAsXXVYvH6dMQTo
The above account details are just an example and are public. Please, DO NOT send anything to it or use it for anything.
Install yarn by running npm i --global yarn
on your terminal. Verify it has been installed by running yarn --version
.
yarn add @polkadot/util @polkadot/api
Running the above command in your terminal will install the Polkadot npm libraries required to generate your account keys. These keys can later be imported into your javascript script.
If you're getting a duplication error, try clearing your cache with npm cache clean --force
. If that doesn't work, you could try reinstalling your node packages with yarn
You can use this code to generate an account
const { Keyring } = require("@polkadot/api");
const { u8aToHex } = require("@polkadot/util");
const { cryptoWaitReady, mnemonicGenerate } = require("@polkadot/util-crypto");
(async () => {
//To initialise WASM
await cryptoWaitReady();
// To generate a new keyring
const keyring = new Keyring({ type: "sr25519" });
// Generate a mnemonic seed phrase with 12 words
const mnemonic = mnemonicGenerate();
const keyPair = keyring.addFromUri(mnemonic, "sr25519");
// It is important to store the values returned here:
console.log(
"SECRET PHRASE: ",
mnemonic,
"\n (SS58) ADDRESS: ",
keyPair.address,
"\n PUBLIC KEY AS HEX: ",
u8aToHex(keyPair.publicKey)
);
})();
Simply run node <file name>
to execute the code.
If it generated something similar to the example below, Congratulations! You now have an account that can be used on the Aventus Network.
Example Generated Account
SECRET PHRASE: flag dynamic laptop often park illegal equip curve game blame junior warm
PUBLIC KEY AS HEX: 0x6e0b67f4590ac2b4e9daa9f229cc4a0c77b3109e8a622e5a1128518ad76e736b
(SS58) ADDRESS: 5EYzWhGxbogEfwNKL52ZRDCgBxu4t8oWDFAsXXVYvH6dMQTo
The above account details are just an example and are public. Please, DO NOT send anything to it or use it for anything.
Note
The difference between these methods is the first and the second show you your private key (secret seed/mnemonic), while the third does not. However, you can still sign transactions with the third.
Save your account details securely and safely.