Beginner

 

Creating Wallets using the CLI or programmatically

In the world of blockchain and decentralized applications, a wallet is an essential tool for managing digital assets and executing transactions. In this tutorial, we will explore different methods for creating wallets in MultiversX, including using the Command Line Interface (CLI) and programmatically. By the end of this tutorial, you will have a good understanding of how to create and manage wallets in MultiversX, and the benefits of using each method. Whether you are a seasoned blockchain developer or a beginner, this tutorial will provide you with the knowledge and skills to effectively create and manage wallets in MultiversX.

How to create wallets using the CLI or programmatically

Although wallets are commonly created through the MultiversX Web Wallet or the MultiversX Ledger App, one can also use the CLI or the SDK.

Generate a new mnemonic

Using mxjs-wallet, a mnemonic phrase (24 words) can be generated as follows:

mxjs-wallet new-mnemonic --mnemonic-file=mnemonicOfAlice.txt

Programmatically using sdk-wallet, the same can be achieved through:

import { Mnemonic } from "@multiversx/sdk-wallet/mnemonic";

let mnemonic = Mnemonic.generate();
let words = mnemonic.getWords();
console.log(words);

Deriving a JSON key-file (from mnemonic)

Using mxjs-wallet, a JSON key-file can be obtained as follows:

mxjs-wallet derive-key --mnemonic-file=mnemonicOfAlice.txt \
 --account-index=0 \
 --key-file=keyOfAlice.json --password-file=passwordOfAlice.txt

Programmatically using sdk-wallet, the same can be achieved through:

const mnemonic = Mnemonic.generate();
const password = "insert a password here";
const addressIndex = 0;

const privateKey = mnemonic.deriveKey(addressIndex);

const userWallet = new UserWallet(privateKey, password);

const jsonFileContent = userWallet.toJSON();

fs.writeFileSync("myKeyFile.json", JSON.stringify(jsonFileContent));

With this, you complete this workshop successfully!!