-
Notifications
You must be signed in to change notification settings - Fork 32
/
8-createNewAccounts.js
51 lines (42 loc) · 1.45 KB
/
8-createNewAccounts.js
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
import { Secp256k1HdWallet,makeCosmoshubPath } from "@cosmjs/amino";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
async function walletGenerate() {
let wallet = await Secp256k1HdWallet.generate(24);
return wallet.secret.data;
}
// Genearte encrypted json
async function walletToJson(wallet) {
let walletStr = await wallet.serialize("123456");
console.log("wallet string: ", walletStr);
}
// Populate addresses from mnemonic
function getWalletWithAccountSize(mnemonic, accountSize, prefix) {
return new Promise(async(resolve)=>{
let ops = {
bip39Password: "",
hdPaths: [],
prefix: prefix,
}
for (let i = 0; i < accountSize; i++) {
ops.hdPaths.push(makeCosmoshubPath(i));
}
let wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, ops);
resolve(wallet);
})
}
// Populate addresses from mnemonic
async function getAccounts(mnemonic, accountSize,prefix){
let wallet = await getWalletWithAccountSize(mnemonic, accountSize,prefix);
let accounts = await wallet.getAccounts();
return accounts;
}
(async () => {
for (let i = 0; i < 1; i++) {
let mnemonic = await walletGenerate();
console.log(`MNEMONIC: `+mnemonic);
let accounts = await getAccounts(mnemonic,10,'cosmos');
for(let account of accounts){
console.log("ADDRESS: "+account.address);
}
}
})();