forked from Bitcoin-com/bitbox-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·218 lines (196 loc) · 7.68 KB
/
index.js
File metadata and controls
executable file
·218 lines (196 loc) · 7.68 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env node
require("babel-register");
let path = require('path');
let program = require('commander');
let chalk = require('chalk');
let mkdirp = require('mkdirp');
let cpFile = require('cp-file');
let figlet = require('figlet');
let clear = require('clear');
let fs = require('fs');
let os = require('os');
let touch = require("touch");
let emoji = require('node-emoji');
let repl = require("repl");
let ini = require('ini');
let BITBOXCli = require('./lib/bitbox-cli').default;
let clone = require('git-clone');
let corsproxy = require('corsproxy');
let cmd = require('node-cmd');
program
.version('1.4.5');
program
.command('new <name>')
.option('-s, --scaffold <scaffold>', 'The framework to use. Options include react, angular, vuejs, nextjs and node.')
.option('-r, --restURL <restURL>', 'The rest URL to use. default: https://rest.bitbox.earth/v1/')
.option('-e, --environment <environment>', 'environment of running BITBOX instance. Ex: production, staging. (Default: development)')
.description(`create a new BITBOX application`)
.action((name, options) => {
if(fs.existsSync(`./${name}`)) {
console.log(chalk.red(`Project ${name} already exists`));
process.exit(1);
}
let config;
let environment = fetchOption('environment=development', config, options);
let restURL = fetchOption('restURL=https://rest.bitbox.earth/v1/', config, options);
if(options && options.scaffold) {
let scaffold = options.scaffold.toLowerCase();
let repo;
let conf = {};
if(scaffold === 'node') {
repo = 'https://github.com/Bitcoin-com/bitbox-scaffold-node.git';
} else if(scaffold === 'angular') {
repo = 'https://github.com/Bitcoin-com/bitbox-scaffold-angular.git';
} else if(scaffold === 'next') {
repo = 'https://github.com/Bitcoin-com/bitbox-scaffold-next.git';
} else if(scaffold === 'react') {
repo = 'https://github.com/Bitcoin-com/bitbox-scaffold-react.git';
} else if(scaffold === 'vue') {
repo = 'https://github.com/Bitcoin-com/bitbox-scaffold-vue.git';
} else {
console.log(chalk.red(`Scaffold ${scaffold} not supported`));
process.exit(1)
}
if(options && options.repo) {
scaffold = 'custom repo';
repo = options.repo.toLowerCase();
}
clear();
console.log(
chalk.blue(
figlet.textSync('BITBOX', {
font: '3-D',
horizontalLayout: 'full'
})
)
);
console.log(chalk.blue(`Scaffolding ${scaffold} app in ${name}`));
clone(repo, `./${name}`, [conf], (res) => {
if(res == "Error: 'git clone' failed with status 128") {
console.log(chalk.red('Must create new app in to an empty directory'));
} else {
console.log(chalk.green('All done.'), emoji.get(':white_check_mark:'));
console.log(chalk.blue('Now `cd` in to your new project and run `npm install && npm start`'), emoji.get(':rocket:'));
}
});
return;
}
console.log(chalk.green(`Creating ${name}/ directory`));
console.log(chalk.green(`Creating src/ directory: ./${name}/src`));
mkdirp(`./${name}/src`, (err) => {});
console.log(chalk.green(`Creating tests/ directory: ./${name}/tests`));
mkdirp(`./${name}/tests`, (err) => {});
console.log(chalk.green(`Creating bitbox.js configuration file: ./${name}/bitbox.js`));
mkdirp(`./${name}`, (err) => {});
touch(`./${name}/bitbox.js`);
fs.writeFileSync( `./${name}/bitbox.js`, `exports.config = {
networks: {
${environment}: {
restURL: "${restURL}"
}
}
};
`);
fs.appendFileSync(`./${name}/.gitignore`, '.console_history');
console.log(chalk.blue('All done.'), emoji.get(':white_check_mark:'));
console.log(chalk.blue('Go get em! Remember--with great power comes great responsibility.'), emoji.get(':rocket:'));
}
);
program
.command('console')
.option('-e, --environment <environment>', 'environment of running BITBOX instance. Ex: production, staging. (Default: development)')
.description('Run a console with Bitcoin Cash RPC commands available')
.action((options) => {
let config;
try {
config = require(process.cwd() + '/bitbox.js').config;
} catch(err) {
console.log(chalk.red('Console command must be run inside a bitbox project'));
process.exit(1);
}
let replServer = repl.start('> ');
let historyFile = path.join(process.cwd(), '.console_history');
require('repl.history')(replServer, historyFile);
let environment = fetchOption('environment=development', config, options);
replServer.context.BITBOX = new BITBOXCli(config.networks[environment]);
}
);
program
.command('paper')
.option('-e, --encoding <encoding>', 'The encoding to use. Options include "cashaddr" and "legacy". (Default: "cashaddr")')
.option('-l, --language <language>', 'language of mnemonic. Options: chinese_simplified, chinese_traditional, english, french, italian, japanese, korean, spanish. (Default: english)')
.description('Create a paper wallet for easy and safe back up')
.action((options) => {
if(!options.encoding || (options.encoding !== 'cashaddr' && options.encoding !== 'legacy')) {
options.encoding = 'cashaddr';
}
if(!options.language || (options.language !== 'chinese_simplified' && options.language !== 'chinese_traditional' && options.language !== 'english' && options.language !== 'french' && options.language !== 'italian' && options.language !== 'japanese' && options.language !== 'korean' && options.language !== 'spanish')) {
options.language = 'english';
}
console.log(chalk.blue(`Creating ${options.language} ${options.encoding} paper wallet`));
let bitbox = new BITBOXCli();
let mnemonic = bitbox.Mnemonic.generate(256, bitbox.Mnemonic.wordLists()[options.language]);
let keypair = bitbox.Mnemonic.toKeypairs(mnemonic, 1)[0];
let privateKeyWIF = keypair.privateKeyWIF;
let address = keypair.address;
if(options.encoding === 'legacy') {
address = bitbox.Address.toLegacyAddress(address);
}
touch(`./paper-wallet.html`);
let QRCode = require('qrcode')
QRCode.toDataURL(privateKeyWIF, (err, privateKeyWIFQR) => {
QRCode.toDataURL(address, (err, addressQR) => {
fs.writeFileSync( `./paper-wallet.html`, `
<div>
<h2>Private Key WIF</h2>
<p>${privateKeyWIF}</p>
<p><img src='${privateKeyWIFQR}' /></p>
</div>
<div>
<h2>Public address</h2>
<p>${address}</p>
<p><img src='${addressQR}' /></p>
</div>
<div>
<p>Mnemonic: ${mnemonic}</p>
<p>HD Path: m/44'/145'/0'/0/0</p>
<p>Encoding: ${options.encoding}</p>
<p>Language: ${options.language}</p>
</div>
`);
})
})
}
);
program
.command('proxy')
.description('localhost proxy for POSTing to full BCH node')
.action((options) => {
console.log(chalk.green(`CORS Proxy running at: http://localhost:1337`));
cmd.run('npm install -g corsproxy');
cmd.get(
'corsproxy',
function(err, data, stderr){
// console.log('')
}
);
}
);
function fetchOption(kv, config, options) {
let parts = kv.split('=');
let key = parts[0];
let defaultVal = parts[1];
if(options && options[key]) {
return options[key];
} else if(config && config.new && config.new[key]) {
return config.new[key];
} else {
return defaultVal;
}
}
program
.parse(process.argv);
// print help if no command given
if (!process.argv.slice(2).length) {
program.outputHelp()
}