-
Notifications
You must be signed in to change notification settings - Fork 28
/
inject_node_account.js
149 lines (138 loc) · 4.26 KB
/
inject_node_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const axios = require('axios');
const fs = require('fs');
const _ = require('lodash');
const ainUtil = require('@ainblockchain/ain-util');
const { BlockchainConsts } = require('./common/constants');
const { sleep } = require('./common/common-util');
const prompt = require('prompt');
const { JSON_RPC_METHODS } = require('./json_rpc/constants');
async function sendGetBootstrapPubKeyRequest(endpointUrl) {
return await axios.post(
`${endpointUrl}/json-rpc`,
{
method: JSON_RPC_METHODS.AIN_GET_BOOTSTRAP_PUB_KEY,
params: {
protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION,
},
jsonrpc: '2.0',
id: 0
})
.then(function(resp) {
return _.get(resp, 'data.result.result');
})
.catch((e) => {
console.log(`sendGetBootstrapPubKeyRequest ${e}`);
if (e.code === 'ECONNREFUSED') {
return null;
}
throw e;
});
}
async function sendInjectAccount(endpointUrl, method, params) {
return await axios.post(
`${endpointUrl}/json-rpc`,
{
method,
params: Object.assign(params, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }),
jsonrpc: '2.0',
id: 0
})
.then(function(resp) {
return _.get(resp, 'data.result.result');
});
}
async function injectAccount(endpointUrl, accountInjectionOption) {
const properties = [];
switch (accountInjectionOption) {
case '--private-key':
properties.push({
name: 'privateKey',
description: 'Enter private key:',
hidden: true
})
break;
case '--keystore':
properties.push({
name: 'keystorePath',
description: 'Enter keystore path:',
})
properties.push({
name: 'password',
description: 'Enter keystore password:',
hidden: true
})
break;
case '--mnemonic':
properties.push({
name: 'mnemonic',
description: 'Enter mnemonic:',
hidden: true
})
properties.push({
name: 'index',
validator: /^[0-9]*$/,
description: 'Enter index (default: 0):',
before: (value) => (value === '' ? 0 : Number(value))
})
break;
default:
console.log(`\nInvalid account injection option: ${accountInjectionOption}\n`);
return;
}
prompt.message = '';
prompt.delimiter = '';
prompt.colors = false;
prompt.start();
input = await prompt.get(properties);
let bootstrapPubKey = null;
while (bootstrapPubKey === null) {
bootstrapPubKey = await sendGetBootstrapPubKeyRequest(endpointUrl);
if (bootstrapPubKey !== null) {
break;
}
await sleep(1000);
}
console.log('bootstrapPubKey:', JSON.stringify(bootstrapPubKey, null, 2));
let method = null;
const params = {};
switch (accountInjectionOption) {
case '--private-key':
method = JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY;
Object.assign(params, {
encryptedPrivateKey: await ainUtil.encryptWithPublicKey(bootstrapPubKey, input.privateKey)
})
break;
case '--keystore':
method = JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_KEYSTORE;
const keystore = JSON.stringify(JSON.parse(fs.readFileSync(input.keystorePath)));
Object.assign(params, {
encryptedKeystore: await ainUtil.encryptWithPublicKey(bootstrapPubKey, keystore)
})
Object.assign(params, {
encryptedPassword: await ainUtil.encryptWithPublicKey(bootstrapPubKey, input.password)
})
break;
case '--mnemonic':
method = JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_HD_WALLET;
Object.assign(params, {
encryptedMnemonic: await ainUtil.encryptWithPublicKey(bootstrapPubKey, input.mnemonic),
index: input.index
})
break;
}
const result = await sendInjectAccount(endpointUrl, method, params);
console.log('injectAccount result:', result);
}
async function processArguments() {
if (process.argv.length !== 4) {
usage();
}
const endpointUrl = process.argv[2];
const accountInjectionOption = process.argv[3];
await injectAccount(endpointUrl, accountInjectionOption);
}
function usage() {
console.log('\nExample commandlines:\n node inject_node_account.js <ENDPOINT_URL> <ACCOUNT_INJECTION_OPTION>\n');
process.exit(0);
}
processArguments();