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
.addresses
= $(".addresses");
39 DOM
.rowsToAdd
= $(".rows-to-add");
40 DOM
.more
= $(".more");
41 DOM
.feedback
= $(".feedback");
42 DOM
.tab
= $(".derivation-type a");
43 DOM
.indexToggle
= $(".index-toggle");
44 DOM
.addressToggle
= $(".address-toggle");
45 DOM
.privateKeyToggle
= $(".private-key-toggle");
49 DOM
.network
.on("change", networkChanged
);
50 DOM
.phrase
.on("input", delayedPhraseChanged
);
51 DOM
.passphrase
.on("input", delayedPhraseChanged
);
52 DOM
.generate
.on("click", generateClicked
);
53 DOM
.more
.on("click", showMore
);
54 DOM
.rootKey
.on("input", delayedRootKeyChanged
);
55 DOM
.bip32path
.on("input", calcForDerivationPath
);
56 DOM
.bip44purpose
.on("input", calcForDerivationPath
);
57 DOM
.bip44coin
.on("input", calcForDerivationPath
);
58 DOM
.bip44account
.on("input", calcForDerivationPath
);
59 DOM
.bip44change
.on("input", calcForDerivationPath
);
60 DOM
.tab
.on("shown.bs.tab", calcForDerivationPath
);
61 DOM
.indexToggle
.on("click", toggleIndexes
);
62 DOM
.addressToggle
.on("click", toggleAddresses
);
63 DOM
.privateKeyToggle
.on("click", togglePrivateKeys
);
66 hideValidationError();
67 populateNetworkSelect();
72 function networkChanged(e
) {
73 var network
= e
.target
.value
;
74 networks
[network
].onSelect();
78 function delayedPhraseChanged() {
79 hideValidationError();
81 if (phraseChangeTimeoutEvent
!= null) {
82 clearTimeout(phraseChangeTimeoutEvent
);
84 phraseChangeTimeoutEvent
= setTimeout(phraseChanged
, 400);
87 function phraseChanged() {
89 hideValidationError();
90 // Get the mnemonic phrase
91 var phrase
= DOM
.phrase
.val();
92 var errorText
= findPhraseErrors(phrase
);
94 showValidationError(errorText
);
97 // Calculate and display
98 var passphrase
= DOM
.passphrase
.val();
99 calcBip32RootKeyFromSeed(phrase
, passphrase
);
100 calcForDerivationPath();
104 function delayedRootKeyChanged() {
105 // Warn if there is an existing mnemonic or passphrase.
106 if (DOM
.phrase
.val().length
> 0 || DOM
.passphrase
.val().length
> 0) {
107 if (!confirm("This will clear existing mnemonic and passphrase")) {
108 DOM
.rootKey
.val(bip32RootKey
);
112 hideValidationError();
114 // Clear existing mnemonic and passphrase
116 DOM
.passphrase
.val("");
118 if (rootKeyChangedTimeoutEvent
!= null) {
119 clearTimeout(rootKeyChangedTimeoutEvent
);
121 rootKeyChangedTimeoutEvent
= setTimeout(rootKeyChanged
, 400);
124 function rootKeyChanged() {
126 hideValidationError();
127 // Validate the root key TODO
128 var rootKeyBase58
= DOM
.rootKey
.val();
129 var errorText
= validateRootKey(rootKeyBase58
);
131 showValidationError(errorText
);
134 // Calculate and display
135 calcBip32RootKeyFromBase58(rootKeyBase58
);
136 calcForDerivationPath();
140 function calcForDerivationPath() {
142 hideValidationError();
143 // Get the derivation path
144 var derivationPath
= getDerivationPath();
145 var errorText
= findDerivationPathErrors(derivationPath
);
147 showValidationError(errorText
);
150 calcBip32ExtendedKey(derivationPath
);
155 function generateClicked() {
158 setTimeout(function() {
159 var phrase
= generateRandomPhrase();
167 function toggleIndexes() {
168 showIndex
= !showIndex
;
169 $("td.index span").toggleClass("invisible");
172 function toggleAddresses() {
173 showAddress
= !showAddress
;
174 $("td.address span").toggleClass("invisible");
177 function togglePrivateKeys() {
178 showPrivKey
= !showPrivKey
;
179 $("td.privkey span").toggleClass("invisible");
184 function generateRandomPhrase() {
185 if (!hasStrongRandom()) {
186 var errorText
= "This browser does not support strong randomness";
187 showValidationError(errorText
);
190 var numWords
= parseInt(DOM
.strength
.val());
191 var strength
= numWords
/ 3 * 32;
192 var words
= mnemonic
.generate(strength
);
193 DOM
.phrase
.val(words
);
197 function calcBip32RootKeyFromSeed(phrase
, passphrase
) {
198 seed
= mnemonic
.toSeed(phrase
, passphrase
);
199 bip32RootKey
= bitcoin
.HDNode
.fromSeedHex(seed
, network
);
202 function calcBip32RootKeyFromBase58(rootKeyBase58
) {
203 bip32RootKey
= bitcoin
.HDNode
.fromBase58(rootKeyBase58
);
206 function calcBip32ExtendedKey(path
) {
207 bip32ExtendedKey
= bip32RootKey
;
208 // Derive the key from the path
209 var pathBits
= path
.split("/");
210 for (var i
=0; i
<pathBits
.length
; i
++) {
211 var bit
= pathBits
[i
];
212 var index
= parseInt(bit
);
216 var hardened
= bit
[bit
.length
-1] == "'";
218 bip32ExtendedKey
= bip32ExtendedKey
.deriveHardened(index
);
221 bip32ExtendedKey
= bip32ExtendedKey
.derive(index
);
226 function showValidationError(errorText
) {
232 function hideValidationError() {
238 function findPhraseErrors(phrase
) {
239 // TODO make this right
240 // Preprocess the words
241 phrase
= mnemonic
.normalizeString(phrase
);
242 var parts
= phrase
.split(" ");
244 for (var i
=0; i
<parts
.length
; i
++) {
246 if (part
.length
> 0) {
247 // TODO check that lowercasing is always valid to do
248 proper
.push(part
.toLowerCase());
251 var properPhrase
= proper
.join(' ');
253 for (var i
=0; i
<proper
.length
; i
++) {
254 var word
= proper
[i
];
255 if (WORDLISTS
["english"].indexOf(word
) == -1) {
256 console
.log("Finding closest match to " + word
);
257 var nearestWord
= findNearestWord(word
);
258 return word
+ " not in wordlist, did you mean " + nearestWord
+ "?";
261 // Check the words are valid
262 var isValid
= mnemonic
.check(properPhrase
);
264 return "Invalid mnemonic";
269 function validateRootKey(rootKeyBase58
) {
271 bitcoin
.HDNode
.fromBase58(rootKeyBase58
);
274 return "Invalid root key";
279 function getDerivationPath() {
280 if (DOM
.bip44tab
.hasClass("active")) {
281 var purpose
= parseIntNoNaN(DOM
.bip44purpose
.val(), 44);
282 var coin
= parseIntNoNaN(DOM
.bip44coin
.val(), 0);
283 var account
= parseIntNoNaN(DOM
.bip44account
.val(), 0);
284 var change
= parseIntNoNaN(DOM
.bip44change
.val(), 0);
286 path
+= purpose
+ "'/";
288 path
+= account
+ "'/";
290 DOM
.bip44path
.val(path
);
291 var derivationPath
= DOM
.bip44path
.val();
292 console
.log("Using derivation path from BIP44 tab: " + derivationPath
);
293 return derivationPath
;
295 else if (DOM
.bip32tab
.hasClass("active")) {
296 var derivationPath
= DOM
.bip32path
.val();
297 console
.log("Using derivation path from BIP32 tab: " + derivationPath
);
298 return derivationPath
;
301 console
.log("Unknown derivation path");
305 function findDerivationPathErrors(path
) {
306 // TODO is not perfect but is better than nothing
308 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vectors
310 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#extended-keys
311 var maxDepth
= 255; // TODO verify this!!
312 var maxIndexValue
= Math
.pow(2, 31); // TODO verify this!!
313 if (path
[0] != "m") {
314 return "First character must be 'm'";
316 if (path
.length
> 1) {
317 if (path
[1] != "/") {
318 return "Separator must be '/'";
320 var indexes
= path
.split("/");
321 if (indexes
.length
> maxDepth
) {
322 return "Derivation depth is " + indexes
.length
+ ", must be less than " + maxDepth
;
324 for (var depth
= 1; depth
<indexes
.length
; depth
++) {
325 var index
= indexes
[depth
];
326 var invalidChars
= index
.replace(/^[0-9]+'?$/g, "")
327 if (invalidChars
.length
> 0) {
328 return "Invalid characters " + invalidChars
+ " found at depth " + depth
;
330 var indexValue
= parseInt(index
.replace("'", ""));
332 return "Invalid number at depth " + depth
;
334 if (indexValue
> maxIndexValue
) {
335 return "Value of " + indexValue
+ " at depth " + depth
+ " must be less than " + maxIndexValue
;
342 function displayBip32Info() {
345 var rootKey
= bip32RootKey
.toBase58();
346 DOM
.rootKey
.val(rootKey
);
347 var extendedPrivKey
= bip32ExtendedKey
.toBase58();
348 DOM
.extendedPrivKey
.val(extendedPrivKey
);
349 var extendedPubKey
= bip32ExtendedKey
.toBase58(false);
350 DOM
.extendedPubKey
.val(extendedPubKey
);
351 // Display the addresses and privkeys
352 clearAddressesList();
353 displayAddresses(0, 20);
356 function displayAddresses(start
, total
) {
357 for (var i
=0; i
<total
; i
++) {
358 var index
= i
+ start
;
363 function TableRow(index
) {
369 function calculateValues() {
370 setTimeout(function() {
371 var key
= bip32ExtendedKey
.derive(index
);
372 var address
= key
.getAddress().toString();
373 var privkey
= key
.privKey
.toWIF(network
);
374 var indexText
= getDerivationPath() + "/" + index
;
375 addAddressToList(indexText
, address
, privkey
);
383 function showMore() {
384 var start
= DOM
.addresses
.children().length
;
385 var rowsToAdd
= parseInt(DOM
.rowsToAdd
.val());
386 if (isNaN(rowsToAdd
)) {
388 DOM
.rowsToAdd
.val("20");
390 if (rowsToAdd
> 200) {
391 var msg
= "Generating " + rowsToAdd
+ " rows could take a while. ";
392 msg
+= "Do you want to continue?";
397 displayAddresses(start
, rowsToAdd
);
400 function clearDisplay() {
401 clearAddressesList();
403 hideValidationError();
406 function clearAddressesList() {
407 DOM
.addresses
.empty();
410 function clearKey() {
412 DOM
.extendedPrivKey
.val("");
413 DOM
.extendedPubKey
.val("");
416 function addAddressToList(indexText
, address
, privkey
) {
417 var row
= $(addressRowTemplate
.html());
419 var indexCell
= row
.find(".index span");
420 var addressCell
= row
.find(".address span");
421 var privkeyCell
= row
.find(".privkey span");
423 indexCell
.text(indexText
);
424 addressCell
.text(address
);
425 privkeyCell
.text(privkey
);
428 indexCell
.addClass("invisible");
431 addressCell
.addClass("invisible");
434 privkeyCell
.addClass("invisible");
436 DOM
.addresses
.append(row
);
439 function hasStrongRandom() {
440 return 'crypto' in window
&& window
['crypto'] !== null;
443 function disableForms() {
444 $("form").on("submit", function(e
) {
449 function parseIntNoNaN(val
, defaultVal
) {
450 var v
= parseInt(val
);
457 function showPending() {
459 .text("Calculating...")
463 function findNearestWord(word
) {
464 var words
= WORDLISTS
["english"];
465 var minDistance
= 99;
466 var closestWord
= words
[0];
467 for (var i
=0; i
<words
.length
; i
++) {
468 var comparedTo
= words
[i
];
469 var distance
= Levenshtein
.get(word
, comparedTo
);
470 if (distance
< minDistance
) {
471 closestWord
= comparedTo
;
472 minDistance
= distance
;
478 function hidePending() {
484 function populateNetworkSelect() {
485 for (var i
=0; i
<networks
.length
; i
++) {
486 var network
= networks
[i
];
487 var option
= $("<option>");
488 option
.attr("value", i
);
489 option
.text(network
.name
);
490 DOM
.phraseNetwork
.append(option
);
497 onSelect: function() {
498 network
= bitcoin
.networks
.bitcoin
;
499 DOM
.bip44coin
.val(0);
503 name: "Bitcoin Testnet",
504 onSelect: function() {
505 network
= bitcoin
.networks
.testnet
;
506 DOM
.bip44coin
.val(1);
511 onSelect: function() {
512 network
= bitcoin
.networks
.litecoin
;
513 DOM
.bip44coin
.val(2);
518 onSelect: function() {
519 network
= bitcoin
.networks
.dogecoin
;
520 DOM
.bip44coin
.val(3);
525 onSelect: function() {
526 network
= bitcoin
.networks
.shadow
;
527 DOM
.bip44coin
.val(35);
531 name: "ShadowCash Testnet",
532 onSelect: function() {
533 network
= bitcoin
.networks
.shadowtn
;
534 DOM
.bip44coin
.val(1);
539 onSelect: function() {
540 network
= bitcoin
.networks
.viacoin
;
541 DOM
.bip44coin
.val(14);
545 name: "Viacoin Testnet",
546 onSelect: function() {
547 network
= bitcoin
.networks
.viacointestnet
;
548 DOM
.bip44coin
.val(1);
553 onSelect: function() {
554 network
= bitcoin
.networks
.jumbucks
;
555 DOM
.bip44coin
.val(26);
560 onSelect: function() {
561 network
= bitcoin
.networks
.clam
;
562 DOM
.bip44coin
.val(23);