Building one block at a time
Learn to submit transactions, track their status, and handle errors effectively. Master the art of building reliable blockchain applications.
0 of 5 lessons completed
Complete previous lesson
Complete previous lesson
Complete previous lesson
Complete previous lesson
Create account from mnemonic or inject via Polkadot{.js} extension
To submit transactions, you need a keypair that can sign transactions. There are several ways to create and manage keypairs.
import { Keyring } from '@polkadot/keyring';
import { mnemonicGenerate } from '@polkadot/util-crypto';
// Method 1: Generate new mnemonic
const mnemonic = mnemonicGenerate();
console.log('Generated mnemonic:', mnemonic);
// Method 2: Create keyring and add account
const keyring = new Keyring({ type: 'sr25519' });
const account = keyring.addFromMnemonic(mnemonic, { name: 'My Account' });
console.log('Account address:', account.address);
// Method 3: Add account with existing mnemonic
const existingMnemonic = 'your twelve word mnemonic here';
const existingAccount = keyring.addFromMnemonic(existingMnemonic, { name: 'Existing Account' });
// Method 4: Polkadot{.js} Extension (if available)
if (window.injectedWeb3) {
const extension = await window.injectedWeb3['polkadot-js'].enable('Your App Name');
const accounts = await extension.accounts.get();
console.log('Extension accounts:', accounts);
}
Create a keyring and add an account from mnemonic, then display the address
Run your code to see the output here...