Creating and opening an account

Create a new account and open it.

Prerequisites

Getting into some code

An account is a key pair (private and public key) associated with a mutable state stored in the NEM blockchain.

const account = Account.generateNewAccount(NetworkType.MIJIN_TEST);

console.log('Your new account address is:', account.address.pretty(), 'and its private key', account.privateKey);
const account = Account.generateNewAccount(NetworkType.MIJIN_TEST);

console.log('Your new account address is:', account.address.pretty(), 'and its private key', account.privateKey);
nem2-cli account generate --network MIJIN_TEST

The private key uniquely identifies a NEM account and holds all of its power. It is a priority to make sure it is stored safely somewhere offline and not to share it with anyone.

The public key is cryptographically derived from the private key and safe to be shared. In spite of that, it is preferable to share the address, which contains further information such as network and validity check.

If you already have a private key, it is not necessary to generate a new account:

const privateKey = process.env.PRIVATE_KEY as string;

const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

console.log('Your account address is:', account.address.pretty(), 'and its private key', account.privateKey);
const privateKey = process.env.PRIVATE_KEY;

const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

console.log('Your account address is:', account.address.pretty(), 'and its private key', account.privateKey);

Using a Wallet

If the programming language of the SDK you are using allows client-side development, you can create a wallet.

A wallet enables you to store your account to sign transactions, encrypting your private key with a password.

const password = new Password('password');

const wallet = SimpleWallet.create('wallet-name', password, NetworkType.MIJIN_TEST);

const account = wallet.open(password);

console.log('Your new account address is:', account.address.pretty(), 'and its private key', account.privateKey);
const password = new Password('password');

const wallet = SimpleWallet.create('wallet-name', password, NetworkType.MIJIN_TEST);

const account = wallet.open(password);

console.log('Your new account address is:', account.address.pretty(), 'and its private key', account.privateKey);
nem2-cli account generate --network MIJIN_TEST --save --url http://localhost:3000 --profile test

Do you have a private key? You can create and open a wallet importing your private key.

const password = new Password('password');

const privateKey = process.env.PRIVATE_KEY as string;

const wallet = SimpleWallet.createFromPrivateKey('wallet-name', password, privateKey, NetworkType.MIJIN_TEST);

const account = wallet.open(password);

console.log('Your account address is:', account.address.pretty(), 'and its private key', account.privateKey);
const password = new Password('password');

const privateKey = process.env.PRIVATE_KEY;

const wallet = SimpleWallet.createFromPrivateKey('wallet-name', password, privateKey, NetworkType.MIJIN_TEST);

const account = wallet.open(password);

console.log('Your account address is:', account.address.pretty(), 'and its private key', account.privateKey);
nem2-cli profile create --privatekey your_private_key --network MIJIN_TEST --url http://localhost:3000 --profile test