Skip to content

Instantly share code, notes, and snippets.

@lrazovic
Created February 13, 2024 13:21
Show Gist options
  • Save lrazovic/5e9f83313b06806900be6d6d44257701 to your computer and use it in GitHub Desktop.
Save lrazovic/5e9f83313b06806900be6d6d44257701 to your computer and use it in GitHub Desktop.
Generate X Polkadot/Substrate Accounts
import { Keyring } from "npm:@polkadot/ui-keyring";
import { cryptoWaitReady } from "https://deno.land/x/polkadot@0.2.45/util-crypto/crypto.ts";
import { mnemonicGenerate } from "https://deno.land/x/polkadot@0.2.45/util-crypto/mnemonic/index.ts";
// generate a random mnemonic, 12 words in length
const MNEMONIC = mnemonicGenerate(12);
console.log(MNEMONIC);
cryptoWaitReady().then(async () => {
// load all available addresses and accounts;
const new_keyring = new Keyring();
new_keyring.loadAll({ type: "sr25519" });
console.log("Generating 32 accounts...");
// Generate 32 account using 32 different derivation paths
for (let i = 0; i < 32; i++) {
const pair = new_keyring.createFromUri(`${MNEMONIC}//${i}`, {
name: `account${i}`,
});
new_keyring.addPair(pair, "qwerty");
console.log(pair.address);
}
// backup all account
const addresses: string[] = new_keyring.getAccounts().map((account) =>
account.address
);
console.log(addresses.length);
const accounts = await new_keyring.backupAccounts(addresses, "qwerty");
// save the accounts to a file
const accountsJson = JSON.stringify(accounts);
Deno.writeFileSync("accounts.json", new TextEncoder().encode(accountsJson));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment