-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_accounts.js
87 lines (69 loc) · 2.94 KB
/
generate_accounts.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Import classes etc.
import { Connection } from "./modules/connection.js";
import { Keys } from "./modules/keys.js";
import { AccountCreateTransaction, Hbar } from "@hashgraph/sdk";
async function main(){
// Setup Client
let client = new Connection().client;
//List of accounts we want to generate
let accounts = [
{
"name" : "EVM Account",
"evm" : true
},
{
"name" : "Non-EVM Account",
"evm" : false
},
];
//Fair Warning
console.log('*****************************************');
console.log('* Save this information somewhere safe! *');
console.log('*****************************************');
//Iterate over the array and generate accounts for each item
asyncForEach(accounts, async (account) => {
//Title
console.log('*****************************************');
console.log('* Information for: ' + account["name"]);
console.log('*****************************************');
//Generate New Keys for the Account (we need the public key before creating the account)
let credentials = Keys.generateKeys(account["evm"]);
const phrase = (await credentials).phrase;
const publicKey = (await credentials).public;
const privateKey = (await credentials).private;
const EVMpublicKey = (await credentials).EVMpublic;
const EVMprivateKey = (await credentials).EVMprivate;
const EVMAddress = (await credentials).EVMAddress;
//Create the Account (with 50 hbars)
let transaction = new AccountCreateTransaction()
.setKey(publicKey)
.setInitialBalance(new Hbar(50));
//Add Account Alias for EVM Accounts
if(account["evm"]) transaction = transaction.setAlias(EVMAddress)
//Sign the transaction with our client
const txResponse = await transaction.execute(client);
//Request the receipt of the transaction (so we can get the account Id we've created)
const receipt = await txResponse.getReceipt(client);
//Get the Account ID
const accountId = receipt.accountId;
//Credentials - Only logged here, store safely please.
console.log("Account ID is " + accountId);
console.log('Mnemonic phrase: ' + phrase);
console.log('Private key: ' + privateKey);
console.log('Public key: ' + publicKey);
//Output EVM information for EVM Keys
if(account["evm"]){
console.log('Public EVM key: ' + EVMpublicKey);
console.log('Private EVM key: ' + EVMprivateKey);
console.log('EVM Address: ' + EVMAddress);
}
//Add space between key outputs
console.log('');
});
}
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
main();