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