-
Notifications
You must be signed in to change notification settings - Fork 5
/
createSR25519Acc.ts
69 lines (55 loc) · 2.21 KB
/
createSR25519Acc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Create Accounts
Script to create a number of accounts. These will be stored in a accounts.json file
Provide the Account Prefix (1 - Polkadot, 2 - Kusama, 42 - Generic Substrate)
Provide the number of accounts to create
*/
import * as fs from 'fs';
import { Keyring } from '@polkadot/api';
import { cryptoWaitReady, mnemonicGenerate, mnemonicToMiniSecret } from '@polkadot/util-crypto';
import { u8aToHex } from '@polkadot/util';
import yargs from 'yargs';
// Global variables
// Account prefix of SR25519 accounts (0 - Polkadot / 2 - Kusama / 42 - Generic Substrate)
const args = yargs.options({
prefix: { type: 'string', demandOption: true, alias: 'p' },
naccounts: { type: 'string', demandOption: true, alias: 'n' },
}).argv;
const createAccount = async () => {
// Initialize WASM
await cryptoWaitReady();
// Create a keyring instance with type SR25519 and given prefix
const keyring = new Keyring({ type: 'sr25519', ss58Format: args['prefix'] });
// Generate mnemonic seed
const mnemonic = mnemonicGenerate();
// Extract private key from mnemonic
const privateKey = u8aToHex(mnemonicToMiniSecret(mnemonic));
// Create account (keypairs or pair) from mnemonic
const pair = keyring.createFromUri(mnemonic, { name: 'sr25519' });
// Log information
console.log(`Account mnemonic is: ${mnemonic}`);
console.log(`Account private key is: ${privateKey}`);
console.log(`Account address is: ${pair.address}\n`);
return [mnemonic, privateKey, pair.address];
};
const main = async () => {
// Variables
let accounts = {};
let mnemonics = Array();
let privateKeys = Array();
let addresses = Array();
console.log(`🤖 - Creating ${args['naccounts']} accounts!`);
// Loop for each Account
for (let i = 0; i < args['naccounts']; i++) {
console.log(`Account ${i + 1} ---`);
[mnemonics[i], privateKeys[i], addresses[i]] = await createAccount();
}
// Save variables into an object for saving
accounts['mnemonics'] = mnemonics;
accounts['privateKeys'] = privateKeys;
accounts['addresses'] = addresses;
// Save data to JSON file
const accountsJSON = JSON.stringify(accounts);
fs.writeFileSync('accounts.json', accountsJSON, 'utf-8');
console.log('\n✔️ Done!');
};
main();