-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_account.js
78 lines (64 loc) · 2.75 KB
/
create_account.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
// Load environment variables
require("dotenv").config();
// Load Near Javascript API components
const near = require("near-api-js");
const fs = require("fs");
// Configure the directory where NEAR credentials are going to be stored
const credentialsPath = "./credentials";
// Configure the keyStore to be used with the NEAR Javascript API
const UnencryptedFileSystemKeyStore = near.keyStores.UnencryptedFileSystemKeyStore;
const keyStore = new UnencryptedFileSystemKeyStore(credentialsPath);
// Setup default client options
const options = {
networkId: process.env.NEAR_NETWORK,
nodeUrl: process.env.NEAR_NODE_URL,
walletUrl: `https://wallet.${process.env.NEAR_NETWORK}.near.org`,
helperUrl: `https://helper.${process.env.NEAR_NETWORK}.near.org`,
explorerUrl: `https://explorer.${process.env.NEAR_NETWORK}.near.org`,
accountId: process.env.NEAR_ACCOUNT,
keyStore: keyStore
}
async function main() {
let keyPair;
// Configure the client with options and our local key store
const client = await near.connect(options);
// Configure the key pair file location
const keyRootPath = client.connection.signer.keyStore.keyDir;
const keyFilePath = `${keyRootPath}/${options.networkId}/${options.accountId}.json`;
// Check if the key pair exists, and create a new one if it does not
if (!fs.existsSync(keyFilePath)) {
console.log("Generating a new key pair")
keyPair = near.KeyPair.fromRandom("ed25519");
} else {
let content = JSON.parse(fs.readFileSync(keyFilePath).toString());
keyPair = near.KeyPair.fromString(content.private_key);
console.log(`Key pair for account ${options.accountId} already exists, skipping creation`);
}
// Create a key pair in credentials directory
await client.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair);
// Determine if account already exists
try {
await client.account(options.accountId);
return console.log(`Sorry, account '${options.accountId}' already exists.`);
}
catch (e) {
if (!e.message.includes("does not exist while viewing")) {
throw e;
}
}
// Generate a public key for account creation step
const publicKey = keyPair.getPublicKey()
// Create the account
try {
const response = await client.createAccount(options.accountId, publicKey);
console.log(`Account ${response.accountId} for network "${options.networkId}" was created.`);
console.log("----------------------------------------------------------------");
console.log("OPEN LINK BELOW to see account in NEAR Explorer!");
console.log(`${options.explorerUrl}/accounts/${response.accountId}`);
console.log("----------------------------------------------------------------");
}
catch(error) {
console.log("ERROR:", error);
}
}
main();