3 var mnemonic
= new Mnemonic("english");
5 var bip32RootKey
= null;
6 var bip32ExtendedKey
= null;
7 var network
= bitcoin
.networks
.bitcoin
;
8 var addressRowTemplate
= $("#address-row-template");
11 var showAddress
= true;
12 var showPrivKey
= true;
14 var phraseChangeTimeoutEvent
= null;
15 var rootKeyChangedTimeoutEvent
= null;
18 DOM
.network
= $(".network");
19 DOM
.phraseNetwork
= $("#network-phrase");
20 DOM
.phrase
= $(".phrase");
21 DOM
.passphrase
= $(".passphrase");
22 DOM
.generate
= $(".generate");
23 DOM
.seed
= $(".seed");
24 DOM
.rootKey
= $(".root-key");
25 DOM
.extendedPrivKey
= $(".extended-priv-key");
26 DOM
.extendedPubKey
= $(".extended-pub-key");
27 DOM
.bip32tab
= $("#bip32-tab");
28 DOM
.bip44tab
= $("#bip44-tab");
29 DOM
.bip32panel
= $("#bip32");
30 DOM
.bip44panel
= $("#bip44");
31 DOM
.bip32path
= $("#bip32-path");
32 DOM
.bip44path
= $("#bip44-path");
33 DOM
.bip44purpose
= $("#bip44 .purpose");
34 DOM
.bip44coin
= $("#bip44 .coin");
35 DOM
.bip44account
= $("#bip44 .account");
36 DOM
.bip44change
= $("#bip44 .change");
37 DOM
.strength
= $(".strength");
38 DOM
.hardenedAddresses
= $(".hardened-addresses");
39 DOM
.addresses
= $(".addresses");
40 DOM
.rowsToAdd
= $(".rows-to-add");
41 DOM
.more
= $(".more");
42 DOM
.feedback
= $(".feedback");
43 DOM
.tab
= $(".derivation-type a");
44 DOM
.indexToggle
= $(".index-toggle");
45 DOM
.addressToggle
= $(".address-toggle");
46 DOM
.privateKeyToggle
= $(".private-key-toggle");
50 DOM
.network
.on("change", networkChanged
);
51 DOM
.phrase
.on("input", delayedPhraseChanged
);
52 DOM
.passphrase
.on("input", delayedPhraseChanged
);
53 DOM
.generate
.on("click", generateClicked
);
54 DOM
.more
.on("click", showMore
);
55 DOM
.rootKey
.on("input", delayedRootKeyChanged
);
56 DOM
.bip32path
.on("input", calcForDerivationPath
);
57 DOM
.bip44purpose
.on("input", calcForDerivationPath
);
58 DOM
.bip44coin
.on("input", calcForDerivationPath
);
59 DOM
.bip44account
.on("input", calcForDerivationPath
);
60 DOM
.bip44change
.on("input", calcForDerivationPath
);
61 DOM
.tab
.on("shown.bs.tab", calcForDerivationPath
);
62 DOM
.hardenedAddresses
.on("change", calcForDerivationPath
);
63 DOM
.indexToggle
.on("click", toggleIndexes
);
64 DOM
.addressToggle
.on("click", toggleAddresses
);
65 DOM
.privateKeyToggle
.on("click", togglePrivateKeys
);
68 hideValidationError();
69 populateNetworkSelect();
74 function networkChanged(e
) {
75 var network
= e
.target
.value
;
76 networks
[network
].onSelect();
80 function delayedPhraseChanged() {
81 hideValidationError();
83 if (phraseChangeTimeoutEvent
!= null) {
84 clearTimeout(phraseChangeTimeoutEvent
);
86 phraseChangeTimeoutEvent
= setTimeout(phraseChanged
, 400);
89 function phraseChanged() {
91 hideValidationError();
92 // Get the mnemonic phrase
93 var phrase
= DOM
.phrase
.val();
94 var errorText
= findPhraseErrors(phrase
);
96 showValidationError(errorText
);
99 // Calculate and display
100 var passphrase
= DOM
.passphrase
.val();
101 calcBip32RootKeyFromSeed(phrase
, passphrase
);
102 calcForDerivationPath();
106 function delayedRootKeyChanged() {
107 // Warn if there is an existing mnemonic or passphrase.
108 if (DOM
.phrase
.val().length
> 0 || DOM
.passphrase
.val().length
> 0) {
109 if (!confirm("This will clear existing mnemonic and passphrase")) {
110 DOM
.rootKey
.val(bip32RootKey
);
114 hideValidationError();
116 // Clear existing mnemonic and passphrase
118 DOM
.passphrase
.val("");
120 if (rootKeyChangedTimeoutEvent
!= null) {
121 clearTimeout(rootKeyChangedTimeoutEvent
);
123 rootKeyChangedTimeoutEvent
= setTimeout(rootKeyChanged
, 400);
126 function rootKeyChanged() {
128 hideValidationError();
129 // Validate the root key TODO
130 var rootKeyBase58
= DOM
.rootKey
.val();
131 var errorText
= validateRootKey(rootKeyBase58
);
133 showValidationError(errorText
);
136 // Calculate and display
137 calcBip32RootKeyFromBase58(rootKeyBase58
);
138 calcForDerivationPath();
142 function calcForDerivationPath() {
144 hideValidationError();
145 // Get the derivation path
146 var derivationPath
= getDerivationPath();
147 var errorText
= findDerivationPathErrors(derivationPath
);
149 showValidationError(errorText
);
152 calcBip32ExtendedKey(derivationPath
);
157 function generateClicked() {
160 setTimeout(function() {
161 var phrase
= generateRandomPhrase();
169 function toggleIndexes() {
170 showIndex
= !showIndex
;
171 $("td.index span").toggleClass("invisible");
174 function toggleAddresses() {
175 showAddress
= !showAddress
;
176 $("td.address span").toggleClass("invisible");
179 function togglePrivateKeys() {
180 showPrivKey
= !showPrivKey
;
181 $("td.privkey span").toggleClass("invisible");
186 function generateRandomPhrase() {
187 if (!hasStrongRandom()) {
188 var errorText
= "This browser does not support strong randomness";
189 showValidationError(errorText
);
192 var numWords
= parseInt(DOM
.strength
.val());
193 var strength
= numWords
/ 3 * 32;
194 var words
= mnemonic
.generate(strength
);
195 DOM
.phrase
.val(words
);
199 function calcBip32RootKeyFromSeed(phrase
, passphrase
) {
200 seed
= mnemonic
.toSeed(phrase
, passphrase
);
201 bip32RootKey
= bitcoin
.HDNode
.fromSeedHex(seed
, network
);
204 function calcBip32RootKeyFromBase58(rootKeyBase58
) {
205 bip32RootKey
= bitcoin
.HDNode
.fromBase58(rootKeyBase58
);
208 function calcBip32ExtendedKey(path
) {
209 bip32ExtendedKey
= bip32RootKey
;
210 // Derive the key from the path
211 var pathBits
= path
.split("/");
212 for (var i
=0; i
<pathBits
.length
; i
++) {
213 var bit
= pathBits
[i
];
214 var index
= parseInt(bit
);
218 var hardened
= bit
[bit
.length
-1] == "'";
220 bip32ExtendedKey
= bip32ExtendedKey
.deriveHardened(index
);
223 bip32ExtendedKey
= bip32ExtendedKey
.derive(index
);
228 function showValidationError(errorText
) {
234 function hideValidationError() {
240 function findPhraseErrors(phrase
) {
241 // TODO make this right
242 // Preprocess the words
243 phrase
= mnemonic
.normalizeString(phrase
);
244 var parts
= phrase
.split(" ");
246 for (var i
=0; i
<parts
.length
; i
++) {
248 if (part
.length
> 0) {
249 // TODO check that lowercasing is always valid to do
250 proper
.push(part
.toLowerCase());
253 var properPhrase
= proper
.join(' ');
255 for (var i
=0; i
<proper
.length
; i
++) {
256 var word
= proper
[i
];
257 if (WORDLISTS
["english"].indexOf(word
) == -1) {
258 console
.log("Finding closest match to " + word
);
259 var nearestWord
= findNearestWord(word
);
260 return word
+ " not in wordlist, did you mean " + nearestWord
+ "?";
263 // Check the words are valid
264 var isValid
= mnemonic
.check(properPhrase
);
266 return "Invalid mnemonic";
271 function validateRootKey(rootKeyBase58
) {
273 bitcoin
.HDNode
.fromBase58(rootKeyBase58
);
276 return "Invalid root key";
281 function getDerivationPath() {
282 if (DOM
.bip44tab
.hasClass("active")) {
283 var purpose
= parseIntNoNaN(DOM
.bip44purpose
.val(), 44);
284 var coin
= parseIntNoNaN(DOM
.bip44coin
.val(), 0);
285 var account
= parseIntNoNaN(DOM
.bip44account
.val(), 0);
286 var change
= parseIntNoNaN(DOM
.bip44change
.val(), 0);
288 path
+= purpose
+ "'/";
290 path
+= account
+ "'/";
292 DOM
.bip44path
.val(path
);
293 var derivationPath
= DOM
.bip44path
.val();
294 console
.log("Using derivation path from BIP44 tab: " + derivationPath
);
295 return derivationPath
;
297 else if (DOM
.bip32tab
.hasClass("active")) {
298 var derivationPath
= DOM
.bip32path
.val();
299 console
.log("Using derivation path from BIP32 tab: " + derivationPath
);
300 return derivationPath
;
303 console
.log("Unknown derivation path");
307 function findDerivationPathErrors(path
) {
308 // TODO is not perfect but is better than nothing
310 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vectors
312 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#extended-keys
313 var maxDepth
= 255; // TODO verify this!!
314 var maxIndexValue
= Math
.pow(2, 31); // TODO verify this!!
315 if (path
[0] != "m") {
316 return "First character must be 'm'";
318 if (path
.length
> 1) {
319 if (path
[1] != "/") {
320 return "Separator must be '/'";
322 var indexes
= path
.split("/");
323 if (indexes
.length
> maxDepth
) {
324 return "Derivation depth is " + indexes
.length
+ ", must be less than " + maxDepth
;
326 for (var depth
= 1; depth
<indexes
.length
; depth
++) {
327 var index
= indexes
[depth
];
328 var invalidChars
= index
.replace(/^[0-9]+'?$/g, "")
329 if (invalidChars
.length
> 0) {
330 return "Invalid characters " + invalidChars
+ " found at depth " + depth
;
332 var indexValue
= parseInt(index
.replace("'", ""));
334 return "Invalid number at depth " + depth
;
336 if (indexValue
> maxIndexValue
) {
337 return "Value of " + indexValue
+ " at depth " + depth
+ " must be less than " + maxIndexValue
;
344 function displayBip32Info() {
347 var rootKey
= bip32RootKey
.toBase58();
348 DOM
.rootKey
.val(rootKey
);
349 var extendedPrivKey
= bip32ExtendedKey
.toBase58();
350 DOM
.extendedPrivKey
.val(extendedPrivKey
);
351 var extendedPubKey
= bip32ExtendedKey
.toBase58(false);
352 DOM
.extendedPubKey
.val(extendedPubKey
);
353 // Display the addresses and privkeys
354 clearAddressesList();
355 displayAddresses(0, 20);
358 function displayAddresses(start
, total
) {
359 for (var i
=0; i
<total
; i
++) {
360 var index
= i
+ start
;
365 function TableRow(index
) {
367 var useHardenedAddresses
= DOM
.hardenedAddresses
.prop("checked");
373 function calculateValues() {
374 setTimeout(function() {
376 if (useHardenedAddresses
) {
377 key
= bip32ExtendedKey
.deriveHardened(index
);
380 key
= bip32ExtendedKey
.derive(index
);
382 var address
= key
.getAddress().toString();
383 var privkey
= key
.privKey
.toWIF(network
);
384 var indexText
= getDerivationPath() + "/" + index
;
385 if (useHardenedAddresses
) {
386 indexText
= indexText
+ "'";
388 addAddressToList(indexText
, address
, privkey
);
396 function showMore() {
397 var start
= DOM
.addresses
.children().length
;
398 var rowsToAdd
= parseInt(DOM
.rowsToAdd
.val());
399 if (isNaN(rowsToAdd
)) {
401 DOM
.rowsToAdd
.val("20");
403 if (rowsToAdd
> 200) {
404 var msg
= "Generating " + rowsToAdd
+ " rows could take a while. ";
405 msg
+= "Do you want to continue?";
410 displayAddresses(start
, rowsToAdd
);
413 function clearDisplay() {
414 clearAddressesList();
416 hideValidationError();
419 function clearAddressesList() {
420 DOM
.addresses
.empty();
423 function clearKey() {
425 DOM
.extendedPrivKey
.val("");
426 DOM
.extendedPubKey
.val("");
429 function addAddressToList(indexText
, address
, privkey
) {
430 var row
= $(addressRowTemplate
.html());
432 var indexCell
= row
.find(".index span");
433 var addressCell
= row
.find(".address span");
434 var privkeyCell
= row
.find(".privkey span");
436 indexCell
.text(indexText
);
437 addressCell
.text(address
);
438 privkeyCell
.text(privkey
);
441 indexCell
.addClass("invisible");
444 addressCell
.addClass("invisible");
447 privkeyCell
.addClass("invisible");
449 DOM
.addresses
.append(row
);
452 function hasStrongRandom() {
453 return 'crypto' in window
&& window
['crypto'] !== null;
456 function disableForms() {
457 $("form").on("submit", function(e
) {
462 function parseIntNoNaN(val
, defaultVal
) {
463 var v
= parseInt(val
);
470 function showPending() {
472 .text("Calculating...")
476 function findNearestWord(word
) {
477 var words
= WORDLISTS
["english"];
478 var minDistance
= 99;
479 var closestWord
= words
[0];
480 for (var i
=0; i
<words
.length
; i
++) {
481 var comparedTo
= words
[i
];
482 var distance
= Levenshtein
.get(word
, comparedTo
);
483 if (distance
< minDistance
) {
484 closestWord
= comparedTo
;
485 minDistance
= distance
;
491 function hidePending() {
497 function populateNetworkSelect() {
498 for (var i
=0; i
<networks
.length
; i
++) {
499 var network
= networks
[i
];
500 var option
= $("<option>");
501 option
.attr("value", i
);
502 option
.text(network
.name
);
503 DOM
.phraseNetwork
.append(option
);
510 onSelect: function() {
511 network
= bitcoin
.networks
.bitcoin
;
512 DOM
.bip44coin
.val(0);
516 name: "Bitcoin Testnet",
517 onSelect: function() {
518 network
= bitcoin
.networks
.testnet
;
519 DOM
.bip44coin
.val(1);
524 onSelect: function() {
525 network
= bitcoin
.networks
.litecoin
;
526 DOM
.bip44coin
.val(2);
531 onSelect: function() {
532 network
= bitcoin
.networks
.dogecoin
;
533 DOM
.bip44coin
.val(3);
538 onSelect: function() {
539 network
= bitcoin
.networks
.shadow
;
540 DOM
.bip44coin
.val(35);
544 name: "ShadowCash Testnet",
545 onSelect: function() {
546 network
= bitcoin
.networks
.shadowtn
;
547 DOM
.bip44coin
.val(1);
552 onSelect: function() {
553 network
= bitcoin
.networks
.viacoin
;
554 DOM
.bip44coin
.val(14);
558 name: "Viacoin Testnet",
559 onSelect: function() {
560 network
= bitcoin
.networks
.viacointestnet
;
561 DOM
.bip44coin
.val(1);
566 onSelect: function() {
567 network
= bitcoin
.networks
.jumbucks
;
568 DOM
.bip44coin
.val(26);
573 onSelect: function() {
574 network
= bitcoin
.networks
.clam
;
575 DOM
.bip44coin
.val(23);