]>
Commit | Line | Data |
---|---|---|
ebd8d4e8 IC |
1 | (function() { |
2 | ||
5ee7bb9e IC |
3 | // mnemonics is populated as required by getLanguage |
4 | var mnemonics = { "english": new Mnemonic("english") }; | |
5 | var mnemonic = mnemonics["english"]; | |
3725abb5 | 6 | var seed = null; |
ebd8d4e8 IC |
7 | var bip32RootKey = null; |
8 | var bip32ExtendedKey = null; | |
a0091a40 | 9 | var network = bitcoinjs.bitcoin.networks.bitcoin; |
ebd8d4e8 IC |
10 | var addressRowTemplate = $("#address-row-template"); |
11 | ||
700901cd IC |
12 | var showIndex = true; |
13 | var showAddress = true; | |
1b12b2f5 | 14 | var showPubKey = true; |
700901cd | 15 | var showPrivKey = true; |
8a93952c | 16 | var showQr = false; |
3abab9b0 | 17 | var litecoinUseLtub = false; |
700901cd | 18 | |
c6624d51 | 19 | var entropyChangeTimeoutEvent = null; |
ebd8d4e8 | 20 | var phraseChangeTimeoutEvent = null; |
efe41586 | 21 | var rootKeyChangedTimeoutEvent = null; |
ebd8d4e8 | 22 | |
40892aba IC |
23 | var generationProcesses = []; |
24 | ||
ebd8d4e8 | 25 | var DOM = {}; |
d6cedc94 | 26 | DOM.network = $(".network"); |
29bf60f5 | 27 | DOM.bip32Client = $("#bip32-client"); |
d6cedc94 | 28 | DOM.phraseNetwork = $("#network-phrase"); |
c6624d51 IC |
29 | DOM.useEntropy = $(".use-entropy"); |
30 | DOM.entropyContainer = $(".entropy-container"); | |
31 | DOM.entropy = $(".entropy"); | |
0a84fe6a IC |
32 | DOM.entropyFiltered = DOM.entropyContainer.find(".filtered"); |
33 | DOM.entropyType = DOM.entropyContainer.find(".type"); | |
20f459ce | 34 | DOM.entropyCrackTime = DOM.entropyContainer.find(".crack-time"); |
0a84fe6a IC |
35 | DOM.entropyEventCount = DOM.entropyContainer.find(".event-count"); |
36 | DOM.entropyBits = DOM.entropyContainer.find(".bits"); | |
37 | DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event"); | |
38 | DOM.entropyWordCount = DOM.entropyContainer.find(".word-count"); | |
39 | DOM.entropyBinary = DOM.entropyContainer.find(".binary"); | |
40 | DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length"); | |
ebd8d4e8 | 41 | DOM.phrase = $(".phrase"); |
1abcc511 | 42 | DOM.passphrase = $(".passphrase"); |
c6624d51 | 43 | DOM.generateContainer = $(".generate-container"); |
ebd8d4e8 | 44 | DOM.generate = $(".generate"); |
3e0ed16a | 45 | DOM.seed = $(".seed"); |
ebd8d4e8 | 46 | DOM.rootKey = $(".root-key"); |
3abab9b0 IC |
47 | DOM.litecoinLtubContainer = $(".litecoin-ltub-container"); |
48 | DOM.litecoinUseLtub = $(".litecoin-use-ltub"); | |
ebd8d4e8 IC |
49 | DOM.extendedPrivKey = $(".extended-priv-key"); |
50 | DOM.extendedPubKey = $(".extended-pub-key"); | |
d6cedc94 IC |
51 | DOM.bip32tab = $("#bip32-tab"); |
52 | DOM.bip44tab = $("#bip44-tab"); | |
6c08f364 | 53 | DOM.bip49tab = $("#bip49-tab"); |
d6cedc94 IC |
54 | DOM.bip32panel = $("#bip32"); |
55 | DOM.bip44panel = $("#bip44"); | |
6c08f364 | 56 | DOM.bip49panel = $("#bip49"); |
ebd8d4e8 IC |
57 | DOM.bip32path = $("#bip32-path"); |
58 | DOM.bip44path = $("#bip44-path"); | |
59 | DOM.bip44purpose = $("#bip44 .purpose"); | |
60 | DOM.bip44coin = $("#bip44 .coin"); | |
61 | DOM.bip44account = $("#bip44 .account"); | |
c554e6ff IC |
62 | DOM.bip44accountXprv = $("#bip44 .account-xprv"); |
63 | DOM.bip44accountXpub = $("#bip44 .account-xpub"); | |
ebd8d4e8 | 64 | DOM.bip44change = $("#bip44 .change"); |
6c08f364 IC |
65 | DOM.bip49unavailable = $("#bip49 .unavailable"); |
66 | DOM.bip49available = $("#bip49 .available"); | |
67 | DOM.bip49path = $("#bip49-path"); | |
68 | DOM.bip49purpose = $("#bip49 .purpose"); | |
69 | DOM.bip49coin = $("#bip49 .coin"); | |
70 | DOM.bip49account = $("#bip49 .account"); | |
71 | DOM.bip49accountXprv = $("#bip49 .account-xprv"); | |
72 | DOM.bip49accountXpub = $("#bip49 .account-xpub"); | |
73 | DOM.bip49change = $("#bip49 .change"); | |
88df3739 | 74 | DOM.generatedStrength = $(".generate-container .strength"); |
146e089e | 75 | DOM.hardenedAddresses = $(".hardened-addresses"); |
88311463 | 76 | DOM.useP2wpkhNestedInP2sh = $(".p2wpkh-nested-in-p2sh"); |
ebd8d4e8 IC |
77 | DOM.addresses = $(".addresses"); |
78 | DOM.rowsToAdd = $(".rows-to-add"); | |
79 | DOM.more = $(".more"); | |
80 | DOM.feedback = $(".feedback"); | |
81 | DOM.tab = $(".derivation-type a"); | |
82 | DOM.indexToggle = $(".index-toggle"); | |
83 | DOM.addressToggle = $(".address-toggle"); | |
1b12b2f5 | 84 | DOM.publicKeyToggle = $(".public-key-toggle"); |
ebd8d4e8 | 85 | DOM.privateKeyToggle = $(".private-key-toggle"); |
5ee7bb9e | 86 | DOM.languages = $(".languages a"); |
e00964cc | 87 | DOM.qrContainer = $(".qr-container"); |
97811c29 | 88 | DOM.qrHider = DOM.qrContainer.find(".qr-hider"); |
e00964cc IC |
89 | DOM.qrImage = DOM.qrContainer.find(".qr-image"); |
90 | DOM.qrHint = DOM.qrContainer.find(".qr-hint"); | |
91 | DOM.showQrEls = $("[data-show-qr]"); | |
ebd8d4e8 | 92 | |
ebd8d4e8 IC |
93 | function init() { |
94 | // Events | |
d6cedc94 | 95 | DOM.network.on("change", networkChanged); |
29bf60f5 | 96 | DOM.bip32Client.on("change", bip32ClientChanged); |
c6624d51 IC |
97 | DOM.useEntropy.on("change", setEntropyVisibility); |
98 | DOM.entropy.on("input", delayedEntropyChanged); | |
3599674d | 99 | DOM.entropyMnemonicLength.on("change", entropyChanged); |
a19a5498 IC |
100 | DOM.phrase.on("input", delayedPhraseChanged); |
101 | DOM.passphrase.on("input", delayedPhraseChanged); | |
ebd8d4e8 IC |
102 | DOM.generate.on("click", generateClicked); |
103 | DOM.more.on("click", showMore); | |
efe41586 | 104 | DOM.rootKey.on("input", delayedRootKeyChanged); |
3abab9b0 | 105 | DOM.litecoinUseLtub.on("change", litecoinUseLtubChanged); |
efe41586 | 106 | DOM.bip32path.on("input", calcForDerivationPath); |
efe41586 IC |
107 | DOM.bip44account.on("input", calcForDerivationPath); |
108 | DOM.bip44change.on("input", calcForDerivationPath); | |
6c08f364 IC |
109 | DOM.bip49account.on("input", calcForDerivationPath); |
110 | DOM.bip49change.on("input", calcForDerivationPath); | |
efe41586 | 111 | DOM.tab.on("shown.bs.tab", calcForDerivationPath); |
146e089e | 112 | DOM.hardenedAddresses.on("change", calcForDerivationPath); |
88311463 | 113 | DOM.useP2wpkhNestedInP2sh.on("change", calcForDerivationPath); |
ebd8d4e8 IC |
114 | DOM.indexToggle.on("click", toggleIndexes); |
115 | DOM.addressToggle.on("click", toggleAddresses); | |
1b12b2f5 | 116 | DOM.publicKeyToggle.on("click", togglePublicKeys); |
ebd8d4e8 | 117 | DOM.privateKeyToggle.on("click", togglePrivateKeys); |
5ee7bb9e | 118 | DOM.languages.on("click", languageChanged); |
e00964cc | 119 | setQrEvents(DOM.showQrEls); |
ebd8d4e8 IC |
120 | disableForms(); |
121 | hidePending(); | |
122 | hideValidationError(); | |
7f15cb6e | 123 | populateNetworkSelect(); |
b4fd763c | 124 | populateClientSelect(); |
ebd8d4e8 IC |
125 | } |
126 | ||
127 | // Event handlers | |
128 | ||
d6cedc94 | 129 | function networkChanged(e) { |
6c08f364 IC |
130 | clearDerivedKeys(); |
131 | clearAddressesList(); | |
3abab9b0 | 132 | DOM.litecoinLtubContainer.addClass("hidden"); |
54563907 | 133 | var networkIndex = e.target.value; |
6c08f364 IC |
134 | var network = networks[networkIndex]; |
135 | network.onSelect(); | |
0cda44d5 IC |
136 | if (network.p2wpkhNestedInP2shAvailable) { |
137 | showP2wpkhNestedInP2shAvailable(); | |
6c08f364 IC |
138 | } |
139 | else { | |
0cda44d5 | 140 | showP2wpkhNestedInP2shUnavailable(); |
6c08f364 | 141 | } |
54563907 IC |
142 | if (seed != null) { |
143 | phraseChanged(); | |
144 | } | |
145 | else { | |
146 | rootKeyChanged(); | |
147 | } | |
d6cedc94 | 148 | } |
29bf60f5 IC |
149 | |
150 | function bip32ClientChanged(e) { | |
151 | var clientIndex = DOM.bip32Client.val(); | |
152 | if (clientIndex == "custom") { | |
153 | DOM.bip32path.prop("readonly", false); | |
b4fd763c AG |
154 | } |
155 | else { | |
29bf60f5 IC |
156 | DOM.bip32path.prop("readonly", true); |
157 | clients[clientIndex].onSelect(); | |
158 | if (seed != null) { | |
159 | phraseChanged(); | |
160 | } | |
161 | else { | |
162 | rootKeyChanged(); | |
163 | } | |
b4fd763c AG |
164 | } |
165 | } | |
d6cedc94 | 166 | |
c6624d51 IC |
167 | function setEntropyVisibility() { |
168 | if (isUsingOwnEntropy()) { | |
169 | DOM.entropyContainer.removeClass("hidden"); | |
170 | DOM.generateContainer.addClass("hidden"); | |
171 | DOM.phrase.prop("readonly", true); | |
172 | DOM.entropy.focus(); | |
173 | entropyChanged(); | |
174 | } | |
175 | else { | |
176 | DOM.entropyContainer.addClass("hidden"); | |
177 | DOM.generateContainer.removeClass("hidden"); | |
178 | DOM.phrase.prop("readonly", false); | |
057722b0 | 179 | hidePending(); |
c6624d51 IC |
180 | } |
181 | } | |
182 | ||
ebd8d4e8 IC |
183 | function delayedPhraseChanged() { |
184 | hideValidationError(); | |
ed6d9d39 IC |
185 | seed = null; |
186 | bip32RootKey = null; | |
187 | bip32ExtendedKey = null; | |
188 | clearAddressesList(); | |
ebd8d4e8 IC |
189 | showPending(); |
190 | if (phraseChangeTimeoutEvent != null) { | |
191 | clearTimeout(phraseChangeTimeoutEvent); | |
192 | } | |
193 | phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400); | |
194 | } | |
195 | ||
196 | function phraseChanged() { | |
197 | showPending(); | |
5ee7bb9e | 198 | setMnemonicLanguage(); |
ebd8d4e8 IC |
199 | // Get the mnemonic phrase |
200 | var phrase = DOM.phrase.val(); | |
201 | var errorText = findPhraseErrors(phrase); | |
202 | if (errorText) { | |
203 | showValidationError(errorText); | |
204 | return; | |
205 | } | |
efe41586 IC |
206 | // Calculate and display |
207 | var passphrase = DOM.passphrase.val(); | |
208 | calcBip32RootKeyFromSeed(phrase, passphrase); | |
209 | calcForDerivationPath(); | |
efe41586 IC |
210 | } |
211 | ||
c6624d51 IC |
212 | function delayedEntropyChanged() { |
213 | hideValidationError(); | |
214 | showPending(); | |
215 | if (entropyChangeTimeoutEvent != null) { | |
216 | clearTimeout(entropyChangeTimeoutEvent); | |
217 | } | |
218 | entropyChangeTimeoutEvent = setTimeout(entropyChanged, 400); | |
219 | } | |
220 | ||
221 | function entropyChanged() { | |
057722b0 IC |
222 | // If blank entropy, clear mnemonic, addresses, errors |
223 | if (DOM.entropy.val().trim().length == 0) { | |
224 | clearDisplay(); | |
0a84fe6a | 225 | clearEntropyFeedback(); |
057722b0 IC |
226 | DOM.phrase.val(""); |
227 | showValidationError("Blank entropy"); | |
228 | return; | |
229 | } | |
230 | // Get the current phrase to detect changes | |
231 | var phrase = DOM.phrase.val(); | |
232 | // Set the phrase from the entropy | |
c6624d51 | 233 | setMnemonicFromEntropy(); |
057722b0 IC |
234 | // Recalc addresses if the phrase has changed |
235 | var newPhrase = DOM.phrase.val(); | |
236 | if (newPhrase != phrase) { | |
237 | if (newPhrase.length == 0) { | |
238 | clearDisplay(); | |
239 | } | |
240 | else { | |
241 | phraseChanged(); | |
242 | } | |
243 | } | |
244 | else { | |
245 | hidePending(); | |
246 | } | |
c6624d51 IC |
247 | } |
248 | ||
efe41586 IC |
249 | function delayedRootKeyChanged() { |
250 | // Warn if there is an existing mnemonic or passphrase. | |
251 | if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) { | |
252 | if (!confirm("This will clear existing mnemonic and passphrase")) { | |
253 | DOM.rootKey.val(bip32RootKey); | |
254 | return | |
255 | } | |
256 | } | |
257 | hideValidationError(); | |
258 | showPending(); | |
259 | // Clear existing mnemonic and passphrase | |
260 | DOM.phrase.val(""); | |
261 | DOM.passphrase.val(""); | |
262 | seed = null; | |
263 | if (rootKeyChangedTimeoutEvent != null) { | |
264 | clearTimeout(rootKeyChangedTimeoutEvent); | |
265 | } | |
266 | rootKeyChangedTimeoutEvent = setTimeout(rootKeyChanged, 400); | |
267 | } | |
268 | ||
269 | function rootKeyChanged() { | |
270 | showPending(); | |
271 | hideValidationError(); | |
efe41586 IC |
272 | var rootKeyBase58 = DOM.rootKey.val(); |
273 | var errorText = validateRootKey(rootKeyBase58); | |
274 | if (errorText) { | |
275 | showValidationError(errorText); | |
276 | return; | |
277 | } | |
278 | // Calculate and display | |
279 | calcBip32RootKeyFromBase58(rootKeyBase58); | |
280 | calcForDerivationPath(); | |
efe41586 IC |
281 | } |
282 | ||
3abab9b0 IC |
283 | function litecoinUseLtubChanged() { |
284 | litecoinUseLtub = DOM.litecoinUseLtub.prop("checked"); | |
285 | if (litecoinUseLtub) { | |
286 | network = bitcoinjs.bitcoin.networks.litecoinLtub; | |
287 | } | |
288 | else { | |
289 | network = bitcoinjs.bitcoin.networks.litecoin; | |
290 | } | |
291 | phraseChanged(); | |
292 | } | |
293 | ||
efe41586 | 294 | function calcForDerivationPath() { |
6c08f364 | 295 | clearDerivedKeys(); |
ba3cb9ec | 296 | clearAddressesList(); |
0eda54f5 | 297 | showPending(); |
6c08f364 IC |
298 | // Don't show bip49 if it's selected but network doesn't support it |
299 | if (bip49TabSelected() && !networkHasBip49()) { | |
300 | return; | |
301 | } | |
ebd8d4e8 | 302 | // Get the derivation path |
38523d36 IC |
303 | var derivationPath = getDerivationPath(); |
304 | var errorText = findDerivationPathErrors(derivationPath); | |
ebd8d4e8 IC |
305 | if (errorText) { |
306 | showValidationError(errorText); | |
307 | return; | |
308 | } | |
5eaa6877 | 309 | bip32ExtendedKey = calcBip32ExtendedKey(derivationPath); |
c554e6ff IC |
310 | if (bip44TabSelected()) { |
311 | displayBip44Info(); | |
312 | } | |
6c08f364 IC |
313 | if (bip49TabSelected()) { |
314 | displayBip49Info(); | |
315 | } | |
ebd8d4e8 | 316 | displayBip32Info(); |
ebd8d4e8 IC |
317 | } |
318 | ||
319 | function generateClicked() { | |
c6624d51 IC |
320 | if (isUsingOwnEntropy()) { |
321 | return; | |
322 | } | |
ebd8d4e8 IC |
323 | clearDisplay(); |
324 | showPending(); | |
325 | setTimeout(function() { | |
5ee7bb9e | 326 | setMnemonicLanguage(); |
ebd8d4e8 IC |
327 | var phrase = generateRandomPhrase(); |
328 | if (!phrase) { | |
329 | return; | |
330 | } | |
331 | phraseChanged(); | |
332 | }, 50); | |
333 | } | |
334 | ||
5ee7bb9e IC |
335 | function languageChanged() { |
336 | setTimeout(function() { | |
337 | setMnemonicLanguage(); | |
338 | if (DOM.phrase.val().length > 0) { | |
339 | var newPhrase = convertPhraseToNewLanguage(); | |
340 | DOM.phrase.val(newPhrase); | |
341 | phraseChanged(); | |
342 | } | |
343 | else { | |
344 | DOM.generate.trigger("click"); | |
345 | } | |
346 | }, 50); | |
347 | } | |
348 | ||
ebd8d4e8 | 349 | function toggleIndexes() { |
700901cd | 350 | showIndex = !showIndex; |
ebd8d4e8 IC |
351 | $("td.index span").toggleClass("invisible"); |
352 | } | |
353 | ||
354 | function toggleAddresses() { | |
700901cd | 355 | showAddress = !showAddress; |
ebd8d4e8 IC |
356 | $("td.address span").toggleClass("invisible"); |
357 | } | |
358 | ||
1b12b2f5 IC |
359 | function togglePublicKeys() { |
360 | showPubKey = !showPubKey; | |
361 | $("td.pubkey span").toggleClass("invisible"); | |
362 | } | |
363 | ||
ebd8d4e8 | 364 | function togglePrivateKeys() { |
700901cd | 365 | showPrivKey = !showPrivKey; |
ebd8d4e8 IC |
366 | $("td.privkey span").toggleClass("invisible"); |
367 | } | |
368 | ||
369 | // Private methods | |
370 | ||
371 | function generateRandomPhrase() { | |
372 | if (!hasStrongRandom()) { | |
373 | var errorText = "This browser does not support strong randomness"; | |
374 | showValidationError(errorText); | |
375 | return; | |
376 | } | |
88df3739 | 377 | var numWords = parseInt(DOM.generatedStrength.val()); |
ebd8d4e8 IC |
378 | var strength = numWords / 3 * 32; |
379 | var words = mnemonic.generate(strength); | |
380 | DOM.phrase.val(words); | |
381 | return words; | |
382 | } | |
383 | ||
efe41586 | 384 | function calcBip32RootKeyFromSeed(phrase, passphrase) { |
3e0ed16a | 385 | seed = mnemonic.toSeed(phrase, passphrase); |
a0091a40 | 386 | bip32RootKey = bitcoinjs.bitcoin.HDNode.fromSeedHex(seed, network); |
efe41586 IC |
387 | } |
388 | ||
389 | function calcBip32RootKeyFromBase58(rootKeyBase58) { | |
a0091a40 | 390 | bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network); |
efe41586 IC |
391 | } |
392 | ||
393 | function calcBip32ExtendedKey(path) { | |
0a1f0259 IC |
394 | // Check there's a root key to derive from |
395 | if (!bip32RootKey) { | |
396 | return bip32RootKey; | |
397 | } | |
5eaa6877 | 398 | var extendedKey = bip32RootKey; |
ebd8d4e8 IC |
399 | // Derive the key from the path |
400 | var pathBits = path.split("/"); | |
401 | for (var i=0; i<pathBits.length; i++) { | |
402 | var bit = pathBits[i]; | |
403 | var index = parseInt(bit); | |
404 | if (isNaN(index)) { | |
405 | continue; | |
406 | } | |
407 | var hardened = bit[bit.length-1] == "'"; | |
a0091a40 | 408 | var isPriv = !(extendedKey.isNeutered()); |
ba3cb9ec IC |
409 | var invalidDerivationPath = hardened && !isPriv; |
410 | if (invalidDerivationPath) { | |
411 | extendedKey = null; | |
412 | } | |
413 | else if (hardened) { | |
5eaa6877 | 414 | extendedKey = extendedKey.deriveHardened(index); |
ebd8d4e8 IC |
415 | } |
416 | else { | |
5eaa6877 | 417 | extendedKey = extendedKey.derive(index); |
ebd8d4e8 IC |
418 | } |
419 | } | |
5eaa6877 | 420 | return extendedKey |
ebd8d4e8 IC |
421 | } |
422 | ||
423 | function showValidationError(errorText) { | |
424 | DOM.feedback | |
425 | .text(errorText) | |
426 | .show(); | |
427 | } | |
428 | ||
429 | function hideValidationError() { | |
430 | DOM.feedback | |
431 | .text("") | |
432 | .hide(); | |
433 | } | |
434 | ||
435 | function findPhraseErrors(phrase) { | |
ebd8d4e8 | 436 | // Preprocess the words |
783981de | 437 | phrase = mnemonic.normalizeString(phrase); |
5ee7bb9e | 438 | var words = phraseToWordArray(phrase); |
057722b0 IC |
439 | // Detect blank phrase |
440 | if (words.length == 0) { | |
441 | return "Blank mnemonic"; | |
442 | } | |
563e401a | 443 | // Check each word |
5ee7bb9e IC |
444 | for (var i=0; i<words.length; i++) { |
445 | var word = words[i]; | |
446 | var language = getLanguage(); | |
447 | if (WORDLISTS[language].indexOf(word) == -1) { | |
563e401a IC |
448 | console.log("Finding closest match to " + word); |
449 | var nearestWord = findNearestWord(word); | |
450 | return word + " not in wordlist, did you mean " + nearestWord + "?"; | |
451 | } | |
452 | } | |
ebd8d4e8 | 453 | // Check the words are valid |
5ee7bb9e | 454 | var properPhrase = wordArrayToPhrase(words); |
ebd8d4e8 IC |
455 | var isValid = mnemonic.check(properPhrase); |
456 | if (!isValid) { | |
457 | return "Invalid mnemonic"; | |
458 | } | |
459 | return false; | |
460 | } | |
461 | ||
efe41586 IC |
462 | function validateRootKey(rootKeyBase58) { |
463 | try { | |
cd7c8327 | 464 | bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network); |
efe41586 IC |
465 | } |
466 | catch (e) { | |
467 | return "Invalid root key"; | |
468 | } | |
469 | return ""; | |
470 | } | |
471 | ||
38523d36 | 472 | function getDerivationPath() { |
32fab2c3 | 473 | if (bip44TabSelected()) { |
38523d36 IC |
474 | var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); |
475 | var coin = parseIntNoNaN(DOM.bip44coin.val(), 0); | |
476 | var account = parseIntNoNaN(DOM.bip44account.val(), 0); | |
477 | var change = parseIntNoNaN(DOM.bip44change.val(), 0); | |
478 | var path = "m/"; | |
479 | path += purpose + "'/"; | |
480 | path += coin + "'/"; | |
481 | path += account + "'/"; | |
482 | path += change; | |
483 | DOM.bip44path.val(path); | |
484 | var derivationPath = DOM.bip44path.val(); | |
485 | console.log("Using derivation path from BIP44 tab: " + derivationPath); | |
486 | return derivationPath; | |
487 | } | |
6c08f364 IC |
488 | if (bip49TabSelected()) { |
489 | var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49); | |
490 | var coin = parseIntNoNaN(DOM.bip49coin.val(), 0); | |
491 | var account = parseIntNoNaN(DOM.bip49account.val(), 0); | |
492 | var change = parseIntNoNaN(DOM.bip49change.val(), 0); | |
493 | var path = "m/"; | |
494 | path += purpose + "'/"; | |
495 | path += coin + "'/"; | |
496 | path += account + "'/"; | |
497 | path += change; | |
498 | DOM.bip49path.val(path); | |
499 | var derivationPath = DOM.bip49path.val(); | |
500 | console.log("Using derivation path from BIP49 tab: " + derivationPath); | |
501 | return derivationPath; | |
502 | } | |
32fab2c3 | 503 | else if (bip32TabSelected()) { |
38523d36 IC |
504 | var derivationPath = DOM.bip32path.val(); |
505 | console.log("Using derivation path from BIP32 tab: " + derivationPath); | |
506 | return derivationPath; | |
507 | } | |
508 | else { | |
509 | console.log("Unknown derivation path"); | |
510 | } | |
511 | } | |
512 | ||
ebd8d4e8 | 513 | function findDerivationPathErrors(path) { |
30c9e79d IC |
514 | // TODO is not perfect but is better than nothing |
515 | // Inspired by | |
516 | // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vectors | |
517 | // and | |
518 | // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#extended-keys | |
519 | var maxDepth = 255; // TODO verify this!! | |
520 | var maxIndexValue = Math.pow(2, 31); // TODO verify this!! | |
521 | if (path[0] != "m") { | |
522 | return "First character must be 'm'"; | |
523 | } | |
524 | if (path.length > 1) { | |
525 | if (path[1] != "/") { | |
526 | return "Separator must be '/'"; | |
527 | } | |
528 | var indexes = path.split("/"); | |
529 | if (indexes.length > maxDepth) { | |
530 | return "Derivation depth is " + indexes.length + ", must be less than " + maxDepth; | |
531 | } | |
532 | for (var depth = 1; depth<indexes.length; depth++) { | |
533 | var index = indexes[depth]; | |
534 | var invalidChars = index.replace(/^[0-9]+'?$/g, "") | |
535 | if (invalidChars.length > 0) { | |
536 | return "Invalid characters " + invalidChars + " found at depth " + depth; | |
537 | } | |
538 | var indexValue = parseInt(index.replace("'", "")); | |
539 | if (isNaN(depth)) { | |
540 | return "Invalid number at depth " + depth; | |
541 | } | |
542 | if (indexValue > maxIndexValue) { | |
543 | return "Value of " + indexValue + " at depth " + depth + " must be less than " + maxIndexValue; | |
544 | } | |
545 | } | |
546 | } | |
0a1f0259 IC |
547 | // Check root key exists or else derivation path is useless! |
548 | if (!bip32RootKey) { | |
549 | return "No root key"; | |
550 | } | |
ba3cb9ec | 551 | // Check no hardened derivation path when using xpub keys |
b18eb97a IC |
552 | var hardenedPath = path.indexOf("'") > -1; |
553 | var hardenedAddresses = bip32TabSelected() && DOM.hardenedAddresses.prop("checked"); | |
554 | var hardened = hardenedPath || hardenedAddresses; | |
a0091a40 | 555 | var isXpubkey = bip32RootKey.isNeutered(); |
ba3cb9ec IC |
556 | if (hardened && isXpubkey) { |
557 | return "Hardened derivation path is invalid with xpub key"; | |
558 | } | |
ebd8d4e8 IC |
559 | return false; |
560 | } | |
561 | ||
c554e6ff IC |
562 | function displayBip44Info() { |
563 | // Get the derivation path for the account | |
564 | var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); | |
565 | var coin = parseIntNoNaN(DOM.bip44coin.val(), 0); | |
566 | var account = parseIntNoNaN(DOM.bip44account.val(), 0); | |
567 | var path = "m/"; | |
568 | path += purpose + "'/"; | |
569 | path += coin + "'/"; | |
570 | path += account + "'/"; | |
571 | // Calculate the account extended keys | |
572 | var accountExtendedKey = calcBip32ExtendedKey(path); | |
573 | var accountXprv = accountExtendedKey.toBase58(); | |
a0091a40 | 574 | var accountXpub = accountExtendedKey.neutered().toBase58(); |
c554e6ff IC |
575 | // Display the extended keys |
576 | DOM.bip44accountXprv.val(accountXprv); | |
577 | DOM.bip44accountXpub.val(accountXpub); | |
578 | } | |
579 | ||
6c08f364 IC |
580 | function displayBip49Info() { |
581 | // Get the derivation path for the account | |
582 | var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49); | |
583 | var coin = parseIntNoNaN(DOM.bip49coin.val(), 0); | |
584 | var account = parseIntNoNaN(DOM.bip49account.val(), 0); | |
585 | var path = "m/"; | |
586 | path += purpose + "'/"; | |
587 | path += coin + "'/"; | |
588 | path += account + "'/"; | |
589 | // Calculate the account extended keys | |
590 | var accountExtendedKey = calcBip32ExtendedKey(path); | |
591 | var accountXprv = accountExtendedKey.toBase58(); | |
592 | var accountXpub = accountExtendedKey.neutered().toBase58(); | |
593 | // Display the extended keys | |
594 | DOM.bip49accountXprv.val(accountXprv); | |
595 | DOM.bip49accountXpub.val(accountXpub); | |
596 | } | |
597 | ||
ebd8d4e8 IC |
598 | function displayBip32Info() { |
599 | // Display the key | |
3e0ed16a | 600 | DOM.seed.val(seed); |
ebd8d4e8 IC |
601 | var rootKey = bip32RootKey.toBase58(); |
602 | DOM.rootKey.val(rootKey); | |
ba3cb9ec | 603 | var xprvkeyB58 = "NA"; |
a0091a40 | 604 | if (!bip32ExtendedKey.isNeutered()) { |
ba3cb9ec IC |
605 | xprvkeyB58 = bip32ExtendedKey.toBase58(); |
606 | } | |
607 | var extendedPrivKey = xprvkeyB58; | |
ebd8d4e8 | 608 | DOM.extendedPrivKey.val(extendedPrivKey); |
a0091a40 | 609 | var extendedPubKey = bip32ExtendedKey.neutered().toBase58(); |
ebd8d4e8 IC |
610 | DOM.extendedPubKey.val(extendedPubKey); |
611 | // Display the addresses and privkeys | |
612 | clearAddressesList(); | |
613 | displayAddresses(0, 20); | |
614 | } | |
615 | ||
616 | function displayAddresses(start, total) { | |
40892aba IC |
617 | generationProcesses.push(new (function() { |
618 | ||
619 | var rows = []; | |
620 | ||
621 | this.stop = function() { | |
622 | for (var i=0; i<rows.length; i++) { | |
623 | rows[i].shouldGenerate = false; | |
624 | } | |
0eda54f5 | 625 | hidePending(); |
40892aba IC |
626 | } |
627 | ||
628 | for (var i=0; i<total; i++) { | |
629 | var index = i + start; | |
0eda54f5 IC |
630 | var isLast = i == total - 1; |
631 | rows.push(new TableRow(index, isLast)); | |
40892aba IC |
632 | } |
633 | ||
634 | })()); | |
ebd8d4e8 IC |
635 | } |
636 | ||
0eda54f5 | 637 | function TableRow(index, isLast) { |
a8c45487 | 638 | |
40892aba IC |
639 | var self = this; |
640 | this.shouldGenerate = true; | |
146e089e | 641 | var useHardenedAddresses = DOM.hardenedAddresses.prop("checked"); |
88311463 | 642 | var isP2wpkhNestedInP2sh = bip49TabSelected() || (bip32TabSelected() && useP2wpkhNestedInP2sh()); |
0cda44d5 | 643 | var p2wpkhNestedInP2shAvailable = networkHasBip49(); |
146e089e | 644 | |
a8c45487 IC |
645 | function init() { |
646 | calculateValues(); | |
647 | } | |
648 | ||
649 | function calculateValues() { | |
650 | setTimeout(function() { | |
40892aba IC |
651 | if (!self.shouldGenerate) { |
652 | return; | |
653 | } | |
a0091a40 | 654 | var key = "NA"; |
146e089e IC |
655 | if (useHardenedAddresses) { |
656 | key = bip32ExtendedKey.deriveHardened(index); | |
657 | } | |
658 | else { | |
659 | key = bip32ExtendedKey.derive(index); | |
660 | } | |
a8c45487 | 661 | var address = key.getAddress().toString(); |
ba3cb9ec | 662 | var privkey = "NA"; |
a0091a40 IC |
663 | if (!key.isNeutered()) { |
664 | privkey = key.keyPair.toWIF(network); | |
ba3cb9ec | 665 | } |
a0091a40 | 666 | var pubkey = key.getPublicKeyBuffer().toString('hex'); |
38523d36 | 667 | var indexText = getDerivationPath() + "/" + index; |
146e089e IC |
668 | if (useHardenedAddresses) { |
669 | indexText = indexText + "'"; | |
670 | } | |
0edac945 | 671 | // Ethereum values are different |
534481b6 | 672 | if (networks[DOM.network.val()].name == "ETH - Ethereum") { |
a0091a40 | 673 | var privKeyBuffer = key.keyPair.d.toBuffer(); |
0edac945 IC |
674 | privkey = privKeyBuffer.toString('hex'); |
675 | var addressBuffer = ethUtil.privateToAddress(privKeyBuffer); | |
49b21f12 IC |
676 | var hexAddress = addressBuffer.toString('hex'); |
677 | var checksumAddress = ethUtil.toChecksumAddress(hexAddress); | |
678 | address = ethUtil.addHexPrefix(checksumAddress); | |
d0239db4 IC |
679 | privkey = ethUtil.addHexPrefix(privkey); |
680 | pubkey = ethUtil.addHexPrefix(pubkey); | |
0edac945 | 681 | } |
64a7d2aa | 682 | // Ripple values are different |
534481b6 | 683 | if (networks[DOM.network.val()].name == "XRP - Ripple") { |
64a7d2aa | 684 | privkey = convertRipplePriv(privkey); |
685 | address = convertRippleAdrr(address); | |
686 | } | |
6c08f364 | 687 | // BIP49 addresses are different |
0cda44d5 IC |
688 | if (isP2wpkhNestedInP2sh) { |
689 | if (!p2wpkhNestedInP2shAvailable) { | |
6c08f364 IC |
690 | return; |
691 | } | |
692 | var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer()); | |
693 | var scriptsig = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash); | |
694 | var addressbytes = bitcoinjs.bitcoin.crypto.hash160(scriptsig); | |
695 | var scriptpubkey = bitcoinjs.bitcoin.script.scriptHash.output.encode(addressbytes); | |
696 | address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network) | |
697 | } | |
1b12b2f5 | 698 | addAddressToList(indexText, address, pubkey, privkey); |
0eda54f5 IC |
699 | if (isLast) { |
700 | hidePending(); | |
701 | } | |
a8c45487 IC |
702 | }, 50) |
703 | } | |
704 | ||
705 | init(); | |
706 | ||
707 | } | |
708 | ||
ebd8d4e8 IC |
709 | function showMore() { |
710 | var start = DOM.addresses.children().length; | |
711 | var rowsToAdd = parseInt(DOM.rowsToAdd.val()); | |
712 | if (isNaN(rowsToAdd)) { | |
713 | rowsToAdd = 20; | |
714 | DOM.rowsToAdd.val("20"); | |
715 | } | |
716 | if (rowsToAdd > 200) { | |
717 | var msg = "Generating " + rowsToAdd + " rows could take a while. "; | |
718 | msg += "Do you want to continue?"; | |
719 | if (!confirm(msg)) { | |
720 | return; | |
721 | } | |
722 | } | |
ebd8d4e8 | 723 | displayAddresses(start, rowsToAdd); |
ebd8d4e8 IC |
724 | } |
725 | ||
726 | function clearDisplay() { | |
727 | clearAddressesList(); | |
fa2e4e93 | 728 | clearKeys(); |
ebd8d4e8 IC |
729 | hideValidationError(); |
730 | } | |
731 | ||
732 | function clearAddressesList() { | |
733 | DOM.addresses.empty(); | |
40892aba IC |
734 | stopGenerating(); |
735 | } | |
736 | ||
737 | function stopGenerating() { | |
738 | while (generationProcesses.length > 0) { | |
739 | var generation = generationProcesses.shift(); | |
740 | generation.stop(); | |
741 | } | |
ebd8d4e8 IC |
742 | } |
743 | ||
fa2e4e93 IC |
744 | function clearKeys() { |
745 | clearRootKey(); | |
746 | clearDerivedKeys(); | |
747 | } | |
748 | ||
749 | function clearRootKey() { | |
ebd8d4e8 | 750 | DOM.rootKey.val(""); |
fa2e4e93 IC |
751 | } |
752 | ||
753 | function clearDerivedKeys() { | |
ebd8d4e8 IC |
754 | DOM.extendedPrivKey.val(""); |
755 | DOM.extendedPubKey.val(""); | |
fa2e4e93 IC |
756 | DOM.bip44accountXprv.val(""); |
757 | DOM.bip44accountXpub.val(""); | |
ebd8d4e8 IC |
758 | } |
759 | ||
1b12b2f5 | 760 | function addAddressToList(indexText, address, pubkey, privkey) { |
ebd8d4e8 | 761 | var row = $(addressRowTemplate.html()); |
700901cd IC |
762 | // Elements |
763 | var indexCell = row.find(".index span"); | |
764 | var addressCell = row.find(".address span"); | |
1b12b2f5 | 765 | var pubkeyCell = row.find(".pubkey span"); |
700901cd IC |
766 | var privkeyCell = row.find(".privkey span"); |
767 | // Content | |
ae30fed8 | 768 | indexCell.text(indexText); |
700901cd | 769 | addressCell.text(address); |
1b12b2f5 | 770 | pubkeyCell.text(pubkey); |
700901cd IC |
771 | privkeyCell.text(privkey); |
772 | // Visibility | |
773 | if (!showIndex) { | |
774 | indexCell.addClass("invisible"); | |
775 | } | |
776 | if (!showAddress) { | |
777 | addressCell.addClass("invisible"); | |
778 | } | |
1b12b2f5 IC |
779 | if (!showPubKey) { |
780 | pubkeyCell.addClass("invisible"); | |
781 | } | |
700901cd | 782 | if (!showPrivKey) { |
6d628db7 | 783 | privkeyCell.addClass("invisible"); |
700901cd | 784 | } |
ebd8d4e8 | 785 | DOM.addresses.append(row); |
e00964cc IC |
786 | var rowShowQrEls = row.find("[data-show-qr]"); |
787 | setQrEvents(rowShowQrEls); | |
ebd8d4e8 IC |
788 | } |
789 | ||
790 | function hasStrongRandom() { | |
791 | return 'crypto' in window && window['crypto'] !== null; | |
792 | } | |
793 | ||
794 | function disableForms() { | |
795 | $("form").on("submit", function(e) { | |
796 | e.preventDefault(); | |
797 | }); | |
798 | } | |
799 | ||
ebd8d4e8 IC |
800 | function parseIntNoNaN(val, defaultVal) { |
801 | var v = parseInt(val); | |
802 | if (isNaN(v)) { | |
803 | return defaultVal; | |
804 | } | |
805 | return v; | |
806 | } | |
807 | ||
808 | function showPending() { | |
809 | DOM.feedback | |
810 | .text("Calculating...") | |
811 | .show(); | |
812 | } | |
813 | ||
563e401a | 814 | function findNearestWord(word) { |
5ee7bb9e IC |
815 | var language = getLanguage(); |
816 | var words = WORDLISTS[language]; | |
563e401a IC |
817 | var minDistance = 99; |
818 | var closestWord = words[0]; | |
819 | for (var i=0; i<words.length; i++) { | |
820 | var comparedTo = words[i]; | |
6ea15134 IC |
821 | if (comparedTo.indexOf(word) == 0) { |
822 | return comparedTo; | |
823 | } | |
563e401a IC |
824 | var distance = Levenshtein.get(word, comparedTo); |
825 | if (distance < minDistance) { | |
826 | closestWord = comparedTo; | |
827 | minDistance = distance; | |
828 | } | |
829 | } | |
830 | return closestWord; | |
831 | } | |
832 | ||
ebd8d4e8 IC |
833 | function hidePending() { |
834 | DOM.feedback | |
835 | .text("") | |
836 | .hide(); | |
837 | } | |
838 | ||
7f15cb6e IC |
839 | function populateNetworkSelect() { |
840 | for (var i=0; i<networks.length; i++) { | |
841 | var network = networks[i]; | |
842 | var option = $("<option>"); | |
843 | option.attr("value", i); | |
844 | option.text(network.name); | |
7b742f87 IC |
845 | if (network.name == "BTC - Bitcoin") { |
846 | option.prop("selected", true); | |
847 | } | |
7f15cb6e IC |
848 | DOM.phraseNetwork.append(option); |
849 | } | |
850 | } | |
29bf60f5 | 851 | |
b4fd763c AG |
852 | function populateClientSelect() { |
853 | for (var i=0; i<clients.length; i++) { | |
854 | var client = clients[i]; | |
855 | var option = $("<option>"); | |
856 | option.attr("value", i); | |
857 | option.text(client.name); | |
29bf60f5 | 858 | DOM.bip32Client.append(option); |
b4fd763c AG |
859 | } |
860 | } | |
7f15cb6e | 861 | |
5ee7bb9e IC |
862 | function getLanguage() { |
863 | var defaultLanguage = "english"; | |
864 | // Try to get from existing phrase | |
865 | var language = getLanguageFromPhrase(); | |
866 | // Try to get from url if not from phrase | |
867 | if (language.length == 0) { | |
868 | language = getLanguageFromUrl(); | |
869 | } | |
870 | // Default to English if no other option | |
871 | if (language.length == 0) { | |
872 | language = defaultLanguage; | |
873 | } | |
874 | return language; | |
875 | } | |
876 | ||
877 | function getLanguageFromPhrase(phrase) { | |
878 | // Check if how many words from existing phrase match a language. | |
879 | var language = ""; | |
880 | if (!phrase) { | |
881 | phrase = DOM.phrase.val(); | |
882 | } | |
883 | if (phrase.length > 0) { | |
884 | var words = phraseToWordArray(phrase); | |
885 | var languageMatches = {}; | |
886 | for (l in WORDLISTS) { | |
887 | // Track how many words match in this language | |
888 | languageMatches[l] = 0; | |
889 | for (var i=0; i<words.length; i++) { | |
890 | var wordInLanguage = WORDLISTS[l].indexOf(words[i]) > -1; | |
891 | if (wordInLanguage) { | |
892 | languageMatches[l]++; | |
893 | } | |
894 | } | |
895 | // Find languages with most word matches. | |
896 | // This is made difficult due to commonalities between Chinese | |
897 | // simplified vs traditional. | |
898 | var mostMatches = 0; | |
899 | var mostMatchedLanguages = []; | |
900 | for (var l in languageMatches) { | |
901 | var numMatches = languageMatches[l]; | |
902 | if (numMatches > mostMatches) { | |
903 | mostMatches = numMatches; | |
904 | mostMatchedLanguages = [l]; | |
905 | } | |
906 | else if (numMatches == mostMatches) { | |
907 | mostMatchedLanguages.push(l); | |
908 | } | |
909 | } | |
910 | } | |
911 | if (mostMatchedLanguages.length > 0) { | |
912 | // Use first language and warn if multiple detected | |
913 | language = mostMatchedLanguages[0]; | |
914 | if (mostMatchedLanguages.length > 1) { | |
915 | console.warn("Multiple possible languages"); | |
916 | console.warn(mostMatchedLanguages); | |
917 | } | |
918 | } | |
919 | } | |
920 | return language; | |
921 | } | |
922 | ||
923 | function getLanguageFromUrl() { | |
c6624d51 IC |
924 | for (var language in WORDLISTS) { |
925 | if (window.location.hash.indexOf(language) > -1) { | |
926 | return language; | |
927 | } | |
928 | } | |
929 | return ""; | |
5ee7bb9e IC |
930 | } |
931 | ||
932 | function setMnemonicLanguage() { | |
933 | var language = getLanguage(); | |
934 | // Load the bip39 mnemonic generator for this language if required | |
935 | if (!(language in mnemonics)) { | |
936 | mnemonics[language] = new Mnemonic(language); | |
937 | } | |
938 | mnemonic = mnemonics[language]; | |
939 | } | |
940 | ||
941 | function convertPhraseToNewLanguage() { | |
942 | var oldLanguage = getLanguageFromPhrase(); | |
943 | var newLanguage = getLanguageFromUrl(); | |
944 | var oldPhrase = DOM.phrase.val(); | |
945 | var oldWords = phraseToWordArray(oldPhrase); | |
946 | var newWords = []; | |
947 | for (var i=0; i<oldWords.length; i++) { | |
948 | var oldWord = oldWords[i]; | |
949 | var index = WORDLISTS[oldLanguage].indexOf(oldWord); | |
950 | var newWord = WORDLISTS[newLanguage][index]; | |
951 | newWords.push(newWord); | |
952 | } | |
953 | newPhrase = wordArrayToPhrase(newWords); | |
954 | return newPhrase; | |
955 | } | |
956 | ||
957 | // TODO look at jsbip39 - mnemonic.splitWords | |
958 | function phraseToWordArray(phrase) { | |
959 | var words = phrase.split(/\s/g); | |
960 | var noBlanks = []; | |
961 | for (var i=0; i<words.length; i++) { | |
962 | var word = words[i]; | |
963 | if (word.length > 0) { | |
964 | noBlanks.push(word); | |
965 | } | |
966 | } | |
967 | return noBlanks; | |
968 | } | |
969 | ||
970 | // TODO look at jsbip39 - mnemonic.joinWords | |
971 | function wordArrayToPhrase(words) { | |
972 | var phrase = words.join(" "); | |
973 | var language = getLanguageFromPhrase(phrase); | |
974 | if (language == "japanese") { | |
975 | phrase = words.join("\u3000"); | |
976 | } | |
977 | return phrase; | |
978 | } | |
979 | ||
c6624d51 IC |
980 | function isUsingOwnEntropy() { |
981 | return DOM.useEntropy.prop("checked"); | |
982 | } | |
983 | ||
984 | function setMnemonicFromEntropy() { | |
0a84fe6a | 985 | clearEntropyFeedback(); |
057722b0 | 986 | // Get entropy value |
c6624d51 | 987 | var entropyStr = DOM.entropy.val(); |
057722b0 | 988 | // Work out minimum base for entropy |
c6624d51 | 989 | var entropy = Entropy.fromString(entropyStr); |
057722b0 | 990 | if (entropy.binaryStr.length == 0) { |
c6624d51 IC |
991 | return; |
992 | } | |
993 | // Show entropy details | |
1cf1bbaf | 994 | showEntropyFeedback(entropy); |
3599674d IC |
995 | // Use entropy hash if not using raw entropy |
996 | var bits = entropy.binaryStr; | |
997 | var mnemonicLength = DOM.entropyMnemonicLength.val(); | |
998 | if (mnemonicLength != "raw") { | |
999 | // Get bits by hashing entropy with SHA256 | |
1000 | var hash = sjcl.hash.sha256.hash(entropy.cleanStr); | |
1001 | var hex = sjcl.codec.hex.fromBits(hash); | |
1002 | bits = BigInteger.parse(hex, 16).toString(2); | |
53aaab27 | 1003 | while (bits.length % 256 != 0) { |
3599674d IC |
1004 | bits = "0" + bits; |
1005 | } | |
1006 | // Truncate hash to suit number of words | |
1007 | mnemonicLength = parseInt(mnemonicLength); | |
1008 | var numberOfBits = 32 * mnemonicLength / 3; | |
1009 | bits = bits.substring(0, numberOfBits); | |
1010 | } | |
c6624d51 | 1011 | // Discard trailing entropy |
3599674d | 1012 | var bitsToUse = Math.floor(bits.length / 32) * 32; |
d6fd8ebf IC |
1013 | var start = bits.length - bitsToUse; |
1014 | var binaryStr = bits.substring(start); | |
c6624d51 IC |
1015 | // Convert entropy string to numeric array |
1016 | var entropyArr = []; | |
adc8ce12 IC |
1017 | for (var i=0; i<binaryStr.length / 8; i++) { |
1018 | var byteAsBits = binaryStr.substring(i*8, i*8+8); | |
1019 | var entropyByte = parseInt(byteAsBits, 2); | |
c6624d51 IC |
1020 | entropyArr.push(entropyByte) |
1021 | } | |
1022 | // Convert entropy array to mnemonic | |
1023 | var phrase = mnemonic.toMnemonic(entropyArr); | |
1024 | // Set the mnemonic in the UI | |
1025 | DOM.phrase.val(phrase); | |
1026 | } | |
1027 | ||
0a84fe6a | 1028 | function clearEntropyFeedback() { |
20f459ce | 1029 | DOM.entropyCrackTime.text("..."); |
1cf1bbaf | 1030 | DOM.entropyType.text(""); |
0a84fe6a IC |
1031 | DOM.entropyWordCount.text("0"); |
1032 | DOM.entropyEventCount.text("0"); | |
1033 | DOM.entropyBitsPerEvent.text("0"); | |
1034 | DOM.entropyBits.text("0"); | |
1035 | DOM.entropyFiltered.html(" "); | |
1036 | DOM.entropyBinary.html(" "); | |
c6624d51 IC |
1037 | } |
1038 | ||
1cf1bbaf | 1039 | function showEntropyFeedback(entropy) { |
6422c1cd | 1040 | var numberOfBits = entropy.binaryStr.length; |
20f459ce | 1041 | var timeToCrack = "unknown"; |
9bc39377 IC |
1042 | try { |
1043 | var z = zxcvbn(entropy.base.parts.join("")); | |
20f459ce IC |
1044 | timeToCrack = z.crack_times_display.offline_fast_hashing_1e10_per_second; |
1045 | if (z.feedback.warning != "") { | |
1046 | timeToCrack = timeToCrack + " - " + z.feedback.warning; | |
1047 | }; | |
9bc39377 IC |
1048 | } |
1049 | catch (e) { | |
9bc39377 IC |
1050 | console.log("Error detecting entropy strength with zxcvbn:"); |
1051 | console.log(e); | |
b299a6a7 | 1052 | } |
391c7f26 | 1053 | var entropyTypeStr = getEntropyTypeStr(entropy); |
6422c1cd | 1054 | var wordCount = Math.floor(numberOfBits / 32) * 3; |
94959756 | 1055 | var bitsPerEvent = entropy.bitsPerEvent.toFixed(2); |
b54c1218 | 1056 | DOM.entropyFiltered.html(entropy.cleanHtml); |
391c7f26 | 1057 | DOM.entropyType.text(entropyTypeStr); |
20f459ce | 1058 | DOM.entropyCrackTime.text(timeToCrack); |
1cf1bbaf | 1059 | DOM.entropyEventCount.text(entropy.base.ints.length); |
6422c1cd | 1060 | DOM.entropyBits.text(numberOfBits); |
0a84fe6a IC |
1061 | DOM.entropyWordCount.text(wordCount); |
1062 | DOM.entropyBinary.text(entropy.binaryStr); | |
6422c1cd | 1063 | DOM.entropyBitsPerEvent.text(bitsPerEvent); |
02f05d3e IC |
1064 | } |
1065 | ||
391c7f26 IC |
1066 | function getEntropyTypeStr(entropy) { |
1067 | var typeStr = entropy.base.str; | |
1068 | // Add some detail if these are cards | |
1069 | if (entropy.base.asInt == 52) { | |
1070 | var cardDetail = []; // array of message strings | |
1071 | // Detect duplicates | |
1072 | var dupes = []; | |
1073 | var dupeTracker = {}; | |
1074 | for (var i=0; i<entropy.base.parts.length; i++) { | |
1075 | var card = entropy.base.parts[i]; | |
5c653a12 IC |
1076 | var cardUpper = card.toUpperCase(); |
1077 | if (cardUpper in dupeTracker) { | |
391c7f26 IC |
1078 | dupes.push(card); |
1079 | } | |
5c653a12 | 1080 | dupeTracker[cardUpper] = true; |
391c7f26 IC |
1081 | } |
1082 | if (dupes.length > 0) { | |
1083 | var dupeWord = "duplicates"; | |
1084 | if (dupes.length == 1) { | |
1085 | dupeWord = "duplicate"; | |
1086 | } | |
1087 | var msg = dupes.length + " " + dupeWord + ": " + dupes.slice(0,3).join(" "); | |
1088 | if (dupes.length > 3) { | |
1089 | msg += "..."; | |
1090 | } | |
1091 | cardDetail.push(msg); | |
1092 | } | |
1093 | // Detect full deck | |
1094 | var uniqueCards = []; | |
1095 | for (var uniqueCard in dupeTracker) { | |
1096 | uniqueCards.push(uniqueCard); | |
1097 | } | |
1098 | if (uniqueCards.length == 52) { | |
1099 | cardDetail.unshift("full deck"); | |
1100 | } | |
bbc29c80 IC |
1101 | // Detect missing cards |
1102 | var values = "A23456789TJQK"; | |
1103 | var suits = "CDHS"; | |
1104 | var missingCards = []; | |
1105 | for (var i=0; i<suits.length; i++) { | |
1106 | for (var j=0; j<values.length; j++) { | |
1107 | var card = values[j] + suits[i]; | |
1108 | if (!(card in dupeTracker)) { | |
1109 | missingCards.push(card); | |
1110 | } | |
1111 | } | |
1112 | } | |
1113 | // Display missing cards if six or less, ie clearly going for full deck | |
1114 | if (missingCards.length > 0 && missingCards.length <= 6) { | |
1115 | var msg = missingCards.length + " missing: " + missingCards.slice(0,3).join(" "); | |
1116 | if (missingCards.length > 3) { | |
1117 | msg += "..."; | |
1118 | } | |
1119 | cardDetail.push(msg); | |
1120 | } | |
391c7f26 IC |
1121 | // Add card details to typeStr |
1122 | if (cardDetail.length > 0) { | |
1123 | typeStr += " (" + cardDetail.join(", ") + ")"; | |
1124 | } | |
1125 | } | |
1126 | return typeStr; | |
1127 | } | |
1128 | ||
e00964cc IC |
1129 | function setQrEvents(els) { |
1130 | els.on("mouseenter", createQr); | |
1131 | els.on("mouseleave", destroyQr); | |
1132 | els.on("click", toggleQr); | |
1133 | } | |
1134 | ||
1135 | function createQr(e) { | |
1136 | var content = e.target.textContent || e.target.value; | |
1137 | if (content) { | |
1138 | var size = 130; | |
1139 | DOM.qrImage.qrcode({width: size, height: size, text: content}); | |
1140 | if (!showQr) { | |
97811c29 IC |
1141 | DOM.qrHider.addClass("hidden"); |
1142 | } | |
1143 | else { | |
1144 | DOM.qrHider.removeClass("hidden"); | |
e00964cc IC |
1145 | } |
1146 | DOM.qrContainer.removeClass("hidden"); | |
1147 | } | |
1148 | } | |
1149 | ||
1150 | function destroyQr() { | |
1151 | DOM.qrImage.text(""); | |
1152 | DOM.qrContainer.addClass("hidden"); | |
1153 | } | |
1154 | ||
1155 | function toggleQr() { | |
1156 | showQr = !showQr; | |
97811c29 | 1157 | DOM.qrHider.toggleClass("hidden"); |
e00964cc IC |
1158 | DOM.qrHint.toggleClass("hidden"); |
1159 | } | |
1160 | ||
32fab2c3 IC |
1161 | function bip44TabSelected() { |
1162 | return DOM.bip44tab.hasClass("active"); | |
1163 | } | |
1164 | ||
1165 | function bip32TabSelected() { | |
1166 | return DOM.bip32tab.hasClass("active"); | |
1167 | } | |
1168 | ||
88311463 IC |
1169 | function useP2wpkhNestedInP2sh() { |
1170 | return DOM.useP2wpkhNestedInP2sh.prop("checked"); | |
1171 | } | |
1172 | ||
6c08f364 | 1173 | function networkHasBip49() { |
0cda44d5 | 1174 | return networks[DOM.network.val()].p2wpkhNestedInP2shAvailable; |
6c08f364 IC |
1175 | } |
1176 | ||
1177 | function bip49TabSelected() { | |
1178 | return DOM.bip49tab.hasClass("active"); | |
1179 | } | |
1180 | ||
1181 | function setHdCoin(coinValue) { | |
1182 | DOM.bip44coin.val(coinValue); | |
1183 | DOM.bip49coin.val(coinValue); | |
1184 | } | |
1185 | ||
0cda44d5 | 1186 | function showP2wpkhNestedInP2shAvailable() { |
6c08f364 IC |
1187 | DOM.bip49unavailable.addClass("hidden"); |
1188 | DOM.bip49available.removeClass("hidden"); | |
88311463 | 1189 | DOM.useP2wpkhNestedInP2sh.prop("disabled", false); |
6c08f364 IC |
1190 | } |
1191 | ||
0cda44d5 | 1192 | function showP2wpkhNestedInP2shUnavailable() { |
6c08f364 IC |
1193 | DOM.bip49available.addClass("hidden"); |
1194 | DOM.bip49unavailable.removeClass("hidden"); | |
88311463 IC |
1195 | DOM.useP2wpkhNestedInP2sh.prop("disabled", true); |
1196 | DOM.useP2wpkhNestedInP2sh.prop("checked", false); | |
6c08f364 IC |
1197 | } |
1198 | ||
7f15cb6e | 1199 | var networks = [ |
daab55dc IC |
1200 | { |
1201 | name: "BCH - Bitcoin Cash", | |
0cda44d5 | 1202 | p2wpkhNestedInP2shAvailable: false, |
daab55dc IC |
1203 | onSelect: function() { |
1204 | network = bitcoinjs.bitcoin.networks.bitcoin; | |
1205 | setHdCoin(145); | |
1206 | }, | |
1207 | }, | |
7f15cb6e | 1208 | { |
534481b6 | 1209 | name: "BTC - Bitcoin", |
0cda44d5 | 1210 | p2wpkhNestedInP2shAvailable: true, |
7a995731 | 1211 | onSelect: function() { |
a0091a40 | 1212 | network = bitcoinjs.bitcoin.networks.bitcoin; |
6c08f364 | 1213 | setHdCoin(0); |
7a995731 IC |
1214 | }, |
1215 | }, | |
7f15cb6e | 1216 | { |
534481b6 | 1217 | name: "BTC - Bitcoin Testnet", |
0cda44d5 | 1218 | p2wpkhNestedInP2shAvailable: true, |
7a995731 | 1219 | onSelect: function() { |
a0091a40 | 1220 | network = bitcoinjs.bitcoin.networks.testnet; |
6c08f364 | 1221 | setHdCoin(1); |
7a995731 IC |
1222 | }, |
1223 | }, | |
7f15cb6e | 1224 | { |
534481b6 | 1225 | name: "CLAM - Clams", |
0cda44d5 | 1226 | p2wpkhNestedInP2shAvailable: false, |
7a995731 | 1227 | onSelect: function() { |
a0091a40 | 1228 | network = bitcoinjs.bitcoin.networks.clam; |
6c08f364 | 1229 | setHdCoin(23); |
7a995731 IC |
1230 | }, |
1231 | }, | |
0921f370 | 1232 | { |
1233 | name: "CRW - Crown", | |
0cda44d5 | 1234 | p2wpkhNestedInP2shAvailable: false, |
0921f370 | 1235 | onSelect: function() { |
6c08f364 IC |
1236 | network = bitcoinjs.bitcoin.networks.crown; |
1237 | setHdCoin(72); | |
0921f370 | 1238 | }, |
1239 | }, | |
7f15cb6e | 1240 | { |
534481b6 | 1241 | name: "DASH - Dash", |
0cda44d5 | 1242 | p2wpkhNestedInP2shAvailable: false, |
e3a9508c | 1243 | onSelect: function() { |
a0091a40 | 1244 | network = bitcoinjs.bitcoin.networks.dash; |
6c08f364 | 1245 | setHdCoin(5); |
e3a9508c IC |
1246 | }, |
1247 | }, | |
c0386f3b | 1248 | { |
534481b6 | 1249 | name: "DASH - Dash Testnet", |
0cda44d5 | 1250 | p2wpkhNestedInP2shAvailable: false, |
c0386f3b | 1251 | onSelect: function() { |
a0091a40 | 1252 | network = bitcoinjs.bitcoin.networks.dashtn; |
6c08f364 | 1253 | setHdCoin(1); |
c0386f3b KR |
1254 | }, |
1255 | }, | |
e3a9508c | 1256 | { |
534481b6 | 1257 | name: "DOGE - Dogecoin", |
0cda44d5 | 1258 | p2wpkhNestedInP2shAvailable: false, |
534481b6 | 1259 | onSelect: function() { |
6c08f364 IC |
1260 | network = bitcoinjs.bitcoin.networks.dogecoin; |
1261 | setHdCoin(3); | |
534481b6 | 1262 | }, |
1263 | }, | |
1264 | { | |
1265 | name: "ETH - Ethereum", | |
0cda44d5 | 1266 | p2wpkhNestedInP2shAvailable: false, |
e3a9508c | 1267 | onSelect: function() { |
a0091a40 | 1268 | network = bitcoinjs.bitcoin.networks.bitcoin; |
6c08f364 | 1269 | setHdCoin(60); |
e3a9508c IC |
1270 | }, |
1271 | }, | |
f88fab20 | 1272 | { |
534481b6 | 1273 | name: "GAME - GameCredits", |
0cda44d5 | 1274 | p2wpkhNestedInP2shAvailable: false, |
f88fab20 | 1275 | onSelect: function() { |
a0091a40 | 1276 | network = bitcoinjs.bitcoin.networks.game; |
6c08f364 | 1277 | setHdCoin(101); |
f88fab20 | 1278 | }, |
1279 | }, | |
a3baa26e | 1280 | { |
534481b6 | 1281 | name: "JBS - Jumbucks", |
0cda44d5 | 1282 | p2wpkhNestedInP2shAvailable: false, |
a3baa26e | 1283 | onSelect: function() { |
a0091a40 | 1284 | network = bitcoinjs.bitcoin.networks.jumbucks; |
6c08f364 | 1285 | setHdCoin(26); |
a3baa26e IC |
1286 | }, |
1287 | }, | |
1288 | { | |
534481b6 | 1289 | name: "LTC - Litecoin", |
0cda44d5 | 1290 | p2wpkhNestedInP2shAvailable: false, |
a3baa26e | 1291 | onSelect: function() { |
a0091a40 | 1292 | network = bitcoinjs.bitcoin.networks.litecoin; |
6c08f364 | 1293 | setHdCoin(2); |
3abab9b0 | 1294 | DOM.litecoinLtubContainer.removeClass("hidden"); |
a3baa26e IC |
1295 | }, |
1296 | }, | |
56ad9601 JS |
1297 | { |
1298 | name: "MAZA - Maza", | |
0cda44d5 | 1299 | p2wpkhNestedInP2shAvailable: false, |
56ad9601 JS |
1300 | onSelect: function() { |
1301 | network = bitcoinjs.bitcoin.networks.maza; | |
1302 | setHdCoin(13); | |
1303 | }, | |
1304 | }, | |
1305 | ||
a3baa26e | 1306 | { |
534481b6 | 1307 | name: "NMC - Namecoin", |
0cda44d5 | 1308 | p2wpkhNestedInP2shAvailable: false, |
a3baa26e | 1309 | onSelect: function() { |
a0091a40 | 1310 | network = bitcoinjs.bitcoin.networks.namecoin; |
6c08f364 | 1311 | setHdCoin(7); |
a3baa26e IC |
1312 | }, |
1313 | }, | |
c0df0189 | 1314 | { |
1315 | name: "PIVX - PIVX", | |
0cda44d5 | 1316 | p2wpkhNestedInP2shAvailable: false, |
c0df0189 | 1317 | onSelect: function() { |
1318 | network = bitcoinjs.bitcoin.networks.pivx; | |
1319 | setHdCoin(119); | |
1320 | }, | |
1321 | }, | |
1322 | { | |
1323 | name: "PIVX - PIVX Testnet", | |
0cda44d5 | 1324 | p2wpkhNestedInP2shAvailable: false, |
c0df0189 | 1325 | onSelect: function() { |
1326 | network = bitcoinjs.bitcoin.networks.pivxtestnet; | |
1327 | setHdCoin(1); | |
1328 | }, | |
1329 | }, | |
5c434a8a | 1330 | { |
534481b6 | 1331 | name: "PPC - Peercoin", |
0cda44d5 | 1332 | p2wpkhNestedInP2shAvailable: false, |
5c434a8a | 1333 | onSelect: function() { |
a0091a40 | 1334 | network = bitcoinjs.bitcoin.networks.peercoin; |
6c08f364 | 1335 | setHdCoin(6); |
5c434a8a CM |
1336 | }, |
1337 | }, | |
64a7d2aa | 1338 | { |
534481b6 | 1339 | name: "SDC - ShadowCash", |
0cda44d5 | 1340 | p2wpkhNestedInP2shAvailable: false, |
82f91834 | 1341 | onSelect: function() { |
a0091a40 | 1342 | network = bitcoinjs.bitcoin.networks.shadow; |
6c08f364 | 1343 | setHdCoin(35); |
82f91834 DG |
1344 | }, |
1345 | }, | |
07ac4350 | 1346 | { |
534481b6 | 1347 | name: "SDC - ShadowCash Testnet", |
0cda44d5 | 1348 | p2wpkhNestedInP2shAvailable: false, |
07ac4350 | 1349 | onSelect: function() { |
a0091a40 | 1350 | network = bitcoinjs.bitcoin.networks.shadowtn; |
6c08f364 | 1351 | setHdCoin(1); |
07ac4350 | 1352 | }, |
1353 | }, | |
7a5a87a0 | 1354 | { |
534481b6 | 1355 | name: "SLM - Slimcoin", |
0cda44d5 | 1356 | p2wpkhNestedInP2shAvailable: false, |
7a5a87a0 | 1357 | onSelect: function() { |
a0091a40 | 1358 | network = bitcoinjs.bitcoin.networks.slimcoin; |
6c08f364 | 1359 | setHdCoin(63); |
7a5a87a0 GH |
1360 | }, |
1361 | }, | |
1362 | { | |
534481b6 | 1363 | name: "SLM - Slimcoin Testnet", |
0cda44d5 | 1364 | p2wpkhNestedInP2shAvailable: false, |
7a5a87a0 | 1365 | onSelect: function() { |
a0091a40 | 1366 | network = bitcoinjs.bitcoin.networks.slimcointn; |
6c08f364 | 1367 | setHdCoin(111); |
7a5a87a0 GH |
1368 | }, |
1369 | }, | |
07ac4350 | 1370 | { |
534481b6 | 1371 | name: "VIA - Viacoin", |
0cda44d5 | 1372 | p2wpkhNestedInP2shAvailable: false, |
07ac4350 | 1373 | onSelect: function() { |
a0091a40 | 1374 | network = bitcoinjs.bitcoin.networks.viacoin; |
6c08f364 | 1375 | setHdCoin(14); |
07ac4350 | 1376 | }, |
1377 | }, | |
0edac945 | 1378 | { |
534481b6 | 1379 | name: "VIA - Viacoin Testnet", |
0cda44d5 | 1380 | p2wpkhNestedInP2shAvailable: false, |
0edac945 | 1381 | onSelect: function() { |
a0091a40 | 1382 | network = bitcoinjs.bitcoin.networks.viacointestnet; |
6c08f364 | 1383 | setHdCoin(1); |
0edac945 IC |
1384 | }, |
1385 | }, | |
534481b6 | 1386 | { |
7ebdf61c | 1387 | name: "XMY - Myriadcoin", |
0cda44d5 | 1388 | p2wpkhNestedInP2shAvailable: false, |
534481b6 | 1389 | onSelect: function() { |
7ebdf61c IC |
1390 | network = bitcoinjs.bitcoin.networks.myriadcoin; |
1391 | setHdCoin(90); | |
534481b6 | 1392 | }, |
adedbf91 | 1393 | }, |
1394 | { | |
7ebdf61c | 1395 | name: "XRP - Ripple", |
0cda44d5 | 1396 | p2wpkhNestedInP2shAvailable: false, |
adedbf91 | 1397 | onSelect: function() { |
7ebdf61c IC |
1398 | network = bitcoinjs.bitcoin.networks.bitcoin; |
1399 | setHdCoin(144); | |
adedbf91 | 1400 | }, |
534481b6 | 1401 | } |
7f15cb6e | 1402 | ] |
6ee4fb7d | 1403 | |
b4fd763c AG |
1404 | var clients = [ |
1405 | { | |
1406 | name: "Bitcoin Core", | |
1407 | onSelect: function() { | |
1408 | DOM.bip32path.val("m/0'/0'"); | |
1409 | DOM.hardenedAddresses.prop('checked', true); | |
1410 | }, | |
1411 | }, | |
1412 | { | |
1413 | name: "blockchain.info", | |
1414 | onSelect: function() { | |
1415 | DOM.bip32path.val("m/44'/0'/0'"); | |
1416 | DOM.hardenedAddresses.prop('checked', false); | |
1417 | }, | |
1418 | }, | |
1419 | { | |
1420 | name: "MultiBit HD", | |
1421 | onSelect: function() { | |
1422 | DOM.bip32path.val("m/0'/0"); | |
1423 | DOM.hardenedAddresses.prop('checked', false); | |
1424 | }, | |
1425 | } | |
1426 | ] | |
7a995731 | 1427 | |
ebd8d4e8 IC |
1428 | init(); |
1429 | ||
1430 | })(); |