-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·173 lines (158 loc) · 5.39 KB
/
index.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env node
const prog = require('caporal');
const path = require('path');
const rimraf = require('rimraf');
const migrate = require('truffle-core/lib/commands/migrate');
const shell = require('shelljs');
const os = require('os');
const compileSolidityCwd = require('./emerald-solidity');
const ora = require('ora');
const opn = require('opn');
const tmp = require('tmp');
const EmeraldJs = require('emerald-js');
const Wallet = EmeraldJs.Wallet;
const ghdownload = require('github-download')
const { JsonRpc, HttpTransport, Vault, VaultJsonRpcProvider } = require('emerald-js');
const platform = os.platform();
const { EmeraldDeployer } = require('./emerald-contract');
const {eachSeries} = require('async');
const {promisify} = require('util');
const _fs = require('fs');
const fs = {
readFile: promisify(_fs.readFile),
writeFile: promisify(_fs.writeFile)
};
const commands = {
vault() {
let e;
switch (platform) {
case 'darwin':
case 'linux':
e = shell.exec(`${__dirname}/emerald-vault server`, {async: true});
break
case 'win32':
e = shell.exec(`${__dirname}/emerald-vault.exe server`, {async: true})
break
}
return e;
}
}
prog
.version('0.0.2')
.command('new', 'Create a new project')
.action((args, options, logger) => {
const spinner = ora('Creating new project');
spinner.start();
return new Promise((resolve, reject) => {
const tmpobj = tmp.dirSync();
ghdownload({user: 'ETCDEVTeam', repo: 'emerald-starter-kit', ref: 'master'}, tmpobj.name)
.on('err', (e) => {
logger.debug('err', e)
spinner.fail('failed to create ${JSON.stringify(e)}');
})
.on('end', () => {
spinner.succeed('New Emerald project created');
shell.mv(`${tmpobj.name}/*`, './');
resolve();
});
});
})
.command('vault', 'Run emerald vault')
.action((args, options, logger) => {
return commands.vault();
})
.command('testrpc', 'Run testnet for ethereum classic')
.argument('[passphrase]', 'passphrase to use for adding private keys to vault')
.action((args, options, logger) => {
let e;
switch (platform) {
case 'darwin':
case 'linux':
e = shell.exec(`${__dirname}/svmdev`, {async: true});
break
case 'win32':
e = shell.exec(`${__dirname}/svmdev.exe`, {async: true})
break
}
e.stdout.once('data', function(data) {
const lines = data.split('\n');
const group = lines.map((line, i) => {
if (i % 2 === 0) {
return
} else {
const address = lines[i - 1].split('address: ')[1];
const privateKey = lines[i].split('private key: ')[1];
logger.debug('before: import private key to vault:', privateKey);
const keyfile = Wallet.fromPrivateKey(privateKey).toV3String(args.passphrase || "");
const keyfileData = Object.assign(JSON.parse(keyfile), {
name: 'emerald-testrpc',
description: 'a test account for emerald testrpc'
})
return {
address, privateKey, keyfileData
}
}
}).filter(i => i);
const vault = new Vault(new VaultJsonRpcProvider(new JsonRpc(new HttpTransport('http://127.0.0.1:1920'))));
const promises = group.map(({keyfileData}) => {
return vault.importAccount(keyfileData, 'mainnet');
});
Promise.all(promises).catch((e) => {
logger.debug('error importing wallets to emerald-vault', e);
})
});
})
.command('wallet', 'Boot Emerald Wallet')
.action((args, options, logger) => {
switch (platform) {
case 'darwin':
return shell.exec(`open ${__dirname}/EmeraldWallet.app`);
case 'linux':
return shell.exec(`${__dirname}/EmeraldWallet.AppImage`);
case 'win32':
return shell.exec(`${__dirname}/EmeraldWallet.exe`);
}
})
.command('explorer', 'Boot Explorer')
.action((args, options, logger) => {
shell.cd(`${__dirname}/emerald-explorer`);
shell.exec(`${__dirname}/node_modules/.bin/lerna run --stream start --scope emerald-tool-browser --include-filtered-dependencies`);
opn('http://localhost:3000/blocks');
})
.command('compile', 'Compile solidity')
.action((args, options, logger) => {
const p = path.resolve(process.cwd(), 'build/contracts');
rimraf(`${p}/*`, (err) => {
compileSolidityCwd();
});
})
.command('deploy', 'Deploy solidity to network')
.action((args, options, logger) => {
const files = shell.ls(`${process.cwd()}/build/contracts/**/*.json`).concat([]);
eachSeries(files, async (file) => {
const artifactFile = await fs.readFile(file, 'utf8');
const artifact = JSON.parse(artifactFile);
const deployer = new EmeraldDeployer(artifact);
try {
const result = await deployer.deploy();
const transaction = await deployer.waitUntilDeployed();
artifact.networks[deployer.chainId] = {
...artifact.networks[deployer.chainId],
address: deployer.constructedTx.to,
transactionHash: transaction.hash
}
await fs.writeFile(file, JSON.stringify(artifact, null, 4), 'utf8');
return;
} catch (e) {
console.error(e);
process.exit(1);
}
}, (err) => {
console.log('done');
if (err) {
console.error(err);
}
process.exit(0);
});
})
prog.parse(process.argv);