|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -let cli = require("../lib/cli") |
4 | 3 | let argv = require("../lib/argv") |
5 | | -let inquirer = require("inquirer"); |
| 4 | +let {secretPrompt, mnemonicPrompt, hdPrompt,cli} = require("../lib/propmt") |
6 | 5 |
|
7 | | -let mnemonicPrompt = language => { |
8 | | - let prompet = [{ |
9 | | - type: 'input', |
10 | | - name: "secret", |
11 | | - message: "随机生成助记词", |
12 | | - default: function () { |
13 | | - return cli.generateMnemonic({lang: language}) |
14 | | - }, |
15 | | - }] |
16 | | - return inquirer.prompt(prompet) |
17 | | -} |
18 | | - |
19 | | -// 验证 seed 长度 |
20 | | -function utf8Length(s) { |
21 | | - var l = 0; |
22 | | - for (var i = 0; i < s.length; i++) { |
23 | | - var c = s.charCodeAt(i); |
24 | | - if (c <= 0x007f) l += 1; |
25 | | - else if (c <= 0x07ff) l += 2; |
26 | | - else if (c >= 0xd800 && c <= 0xdfff) l += 2; // surrogates |
27 | | - else l += 3; |
28 | | - } |
29 | | - return l; |
30 | | -} |
31 | | - |
32 | | -let secretPrompt = () => { |
33 | | - let prompt = [{ |
34 | | - type: "list", |
35 | | - name: "secretType", |
36 | | - message: "钱包根类型", |
37 | | - choices: [ |
38 | | - {name: "生成助记词", value: "randomMnemo", checked: true}, |
39 | | - {name: "生成种子", value: "randomSeed"}, |
40 | | - {name: "生成Base58", value: "randomBase58"}, |
41 | | - new inquirer.Separator("-------输入(input)-------"), |
42 | | - {name: "输入助记词", value: "mnemo"}, |
43 | | - {name: "输入种子", value: "seed"}, |
44 | | - {name: "输入Base58", value: "base58"}, |
45 | | - ] |
46 | | - }, |
47 | | - { |
48 | | - type: "list", |
49 | | - name: "language", |
50 | | - message: "注记词的语言", |
51 | | - choices: [ |
52 | | - {name: "Chinese", value: "CN", checked: true}, |
53 | | - {name: "English", value: "EN"}, |
54 | | - {name: "Japanese", value: "JA"}, |
55 | | - ], |
56 | | - when: function (answers) { |
57 | | - return answers.secretType == "randomMnemo" |
58 | | - }, |
59 | | - }, |
60 | | - { |
61 | | - type: "password", |
62 | | - name: "secret", |
63 | | - mask: '*', |
64 | | - message: "输入助记词", |
65 | | - when: function (answers) { |
66 | | - return answers.secretType == "mnemo" |
67 | | - }, |
68 | | - validate: function (val) { |
69 | | - // // 验证是否是中文 |
70 | | - // let pattern = new RegExp("[\u4E00-\u9FA5]+"); |
71 | | - |
72 | | - let wards = val.split(" ") |
73 | | - if (wards < 2) { |
74 | | - return "助记词必须空格分割"; |
75 | | - } else { |
76 | | - return true |
77 | | - } |
78 | | - } |
79 | | - }, |
80 | | - { |
81 | | - type: 'input', |
82 | | - name: "secret", |
83 | | - message: "随机生成种子", |
84 | | - default: function () { |
85 | | - let {seed} = cli.generateSeed() |
86 | | - return seed.toString("hex") |
87 | | - }, |
88 | | - when: function (answers) { // 当watch为true的时候才会提问当前问题 |
89 | | - return answers.secretType == "randomSeed" |
90 | | - }, |
91 | | - }, |
92 | | - { |
93 | | - type: "password", |
94 | | - name: "secret", |
95 | | - message: "输入助种子", |
96 | | - mask: '*', |
97 | | - when: function (answers) { |
98 | | - return answers.secretType == "seed" |
99 | | - }, |
100 | | - validate: function (val) { |
101 | | - if (utf8Length(val) > 127) { |
102 | | - return true |
103 | | - } else { |
104 | | - return "Seed should be at least 128 bits" |
105 | | - } |
106 | | - } |
107 | | - }, |
108 | | - { |
109 | | - type: 'input', |
110 | | - name: "secret", |
111 | | - message: "随机生成Base58", |
112 | | - default: function () { |
113 | | - let {base58} = cli.generateBase58() |
114 | | - return base58 |
115 | | - }, |
116 | | - when: function (answers) { // 当watch为true的时候才会提问当前问题 |
117 | | - return answers.secretType == "randomBase58" |
118 | | - }, |
119 | | - }, |
120 | | - { |
121 | | - type: "password", |
122 | | - name: "secret", |
123 | | - mask: '*', |
124 | | - message: "输入Base58", |
125 | | - when: function (answers) { // 当watch为true的时候才会提问当前问题 |
126 | | - return answers.secretType == "base58" |
127 | | - }, |
128 | | - validate: function (val) { |
129 | | - if (utf8Length(val) > 127) { |
130 | | - return true |
131 | | - } else { |
132 | | - return "Base58 should be at least 128 bits" |
133 | | - } |
134 | | - } |
135 | | - }, |
136 | | - ] |
137 | | - return inquirer.prompt(prompt) |
138 | | -} |
139 | | - |
140 | | -let hdQuestion = (answers) => { |
141 | | - let prompt = [ |
142 | | - { |
143 | | - type: "list", |
144 | | - name: "coin", |
145 | | - message: "选择需要生成的币种(HD coinType)", |
146 | | - choices: [ |
147 | | - {name: "BTC", value: "BTC", checked: true}, |
148 | | - {name: "BCH", value: "BCH"}, |
149 | | - {name: "LTC", value: "LTC"}, |
150 | | - new inquirer.Separator(), |
151 | | - {name: "ETH", value: "ETH"}, |
152 | | - {name: "TRX", value: "TRX"}, |
153 | | - ] |
154 | | - }, |
155 | | - { |
156 | | - type: "list", |
157 | | - name: "account", |
158 | | - message: "选择账户", |
159 | | - choices: [ |
160 | | - {name: "默认账户(0)", value: "0", checked: true}, |
161 | | - {name: "对公账户(1)", value: "1"}, |
162 | | - {name: "对私账户(2)", value: "2"}, |
163 | | - {name: "其他(3)", value: "3"}, |
164 | | - ] |
165 | | - }, |
166 | | - { |
167 | | - type: "input", |
168 | | - name: "accountOther", |
169 | | - message: "输入账户", |
170 | | - when: function (answers) { |
171 | | - return answers.account == "3" |
172 | | - }, |
173 | | - validate: function (val) { |
174 | | - let reg = new RegExp("^[0-9]*$"); |
175 | | - if (reg.test(val)) { // 校验位数 |
176 | | - return true; |
177 | | - } |
178 | | - return "请输入数字"; |
179 | | - } |
180 | | - }, |
181 | | - { |
182 | | - type: "list", |
183 | | - name: "change", |
184 | | - message: "选择交易类型", |
185 | | - choices: [ |
186 | | - {name: "对外收款(0)", value: "0", checked: true}, |
187 | | - {name: "交易找零(1)", value: "1"}, |
188 | | - {name: "归集地址(2)", value: "2"}, |
189 | | - {name: "其他(3)", value: "3"}, |
190 | | - ] |
191 | | - }, |
192 | | - { |
193 | | - type: "input", |
194 | | - name: "changeOther", |
195 | | - message: "定义交易类型", |
196 | | - when: function (answers) { |
197 | | - return answers.change == "3" |
198 | | - }, |
199 | | - validate: function (val) { |
200 | | - let reg = new RegExp("^[0-9]*$"); |
201 | | - if (reg.test(val)) { // 校验位数 |
202 | | - return true; |
203 | | - } |
204 | | - return "请输入数字"; |
205 | | - } |
206 | | - }, |
207 | | - { |
208 | | - type: "input", |
209 | | - name: "indexRange", |
210 | | - message: "输入账户生成索引 (e.g 1-100 or 1)", |
211 | | - validate: function (val) { |
212 | | - let reg = new RegExp("^[0-9]*$"); |
213 | | - if (reg.test(val)) { // 校验位数 |
214 | | - return true; |
215 | | - } else { |
216 | | - let pass = val.match(/^([0-9]+)(-)([0-9]+)$/) |
217 | | - if (pass && pass[1] < pass[3]) { |
218 | | - return true |
219 | | - } |
220 | | - } |
221 | | - return "请输入数字或者范围(e.g 1-100 or 1)"; |
222 | | - } |
223 | | - }, |
224 | | - ] |
225 | | - return inquirer.prompt(prompt).then(res => { |
226 | | - let account = res.account || res.accountOther |
227 | | - let change = res.change || res.changeOther |
228 | | - let path = "m/" + account + "'/" + change + "/" |
229 | | - let index, range |
230 | | - let parts = res.indexRange.split('-'); |
231 | | - if (parts.length == 2) { |
232 | | - range = res.indexRange |
233 | | - } else { |
234 | | - index = res.indexRange |
235 | | - } |
236 | | - let mnemonic, seed, base58 |
237 | | - let {secretType, secret} = answers |
238 | | - if (secretType == "randomMnemo" || secretType == "mnemo") { |
239 | | - mnemonic = secret |
240 | | - } |
241 | | - |
242 | | - if (secretType == "randomBase58" || secretType == "base58") { |
243 | | - base58 = secret |
244 | | - } |
245 | | - |
246 | | - if (secretType == "randomSeed" || secretType == "seed") { |
247 | | - seed = secret |
248 | | - } |
249 | | - |
250 | | - |
251 | | - let columns = "path,pri,pub" |
252 | | - let options = {coin: res.coin, mnemonic, seed, base58, index, range, path, columns} |
253 | | - console.log(options) |
254 | | - |
255 | | - return cli.generateAddress(options) |
256 | | - }) |
257 | | -} |
258 | | - |
259 | | - |
260 | | -let hdAnswers = (answers) => { |
261 | | - let isRandom = answers.secretType.substr(0, 6) == "random" |
262 | | - if (isRandom && answers.language == "CN") { |
263 | | - console.warn("注意备份", answers.secret) |
264 | | - } |
265 | | - hdQuestion(answers).then(res => { |
266 | | - console.log(res) |
267 | | - }) |
268 | | -} |
269 | 6 |
|
270 | 7 | let args = process.argv.splice(2); |
271 | 8 | if (args.length) { |
@@ -303,10 +40,10 @@ if (args.length) { |
303 | 40 | if (answers.language) { |
304 | 41 | mnemonicPrompt(answers.language).then(res => { |
305 | 42 | answers.secret = res.secret |
306 | | - hdAnswers(answers) |
| 43 | + hdPrompt(answers) |
307 | 44 | }) |
308 | 45 | } else { |
309 | | - hdAnswers(answers) |
| 46 | + hdPrompt(answers) |
310 | 47 | } |
311 | 48 | } |
312 | 49 | ); |
|
0 commit comments