]>
git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blob - src/js/index.js
3 var mnemonic
= new Mnemonic("english");
4 var bip32RootKey
= null;
5 var bip32ExtendedKey
= null;
6 var network
= bitcoin
.networks
.bitcoin
;
7 var addressRowTemplate
= $("#address-row-template");
10 var showAddress
= true;
11 var showPrivKey
= true;
13 var phraseChangeTimeoutEvent
= null;
16 DOM
.network
= $(".network");
17 DOM
.phraseNetwork
= $("#network-phrase");
18 DOM
.phrase
= $(".phrase");
19 DOM
.passphrase
= $(".passphrase");
20 DOM
.generate
= $(".generate");
21 DOM
.rootKey
= $(".root-key");
22 DOM
.extendedPrivKey
= $(".extended-priv-key");
23 DOM
.extendedPubKey
= $(".extended-pub-key");
24 DOM
.bip32tab
= $("#bip32-tab");
25 DOM
.bip44tab
= $("#bip44-tab");
26 DOM
.bip32panel
= $("#bip32");
27 DOM
.bip44panel
= $("#bip44");
28 DOM
.bip32path
= $("#bip32-path");
29 DOM
.bip44path
= $("#bip44-path");
30 DOM
.bip44purpose
= $("#bip44 .purpose");
31 DOM
.bip44coin
= $("#bip44 .coin");
32 DOM
.bip44account
= $("#bip44 .account");
33 DOM
.bip44change
= $("#bip44 .change");
34 DOM
.strength
= $(".strength");
35 DOM
.addresses
= $(".addresses");
36 DOM
.rowsToAdd
= $(".rows-to-add");
37 DOM
.more
= $(".more");
38 DOM
.feedback
= $(".feedback");
39 DOM
.tab
= $(".derivation-type a");
40 DOM
.indexToggle
= $(".index-toggle");
41 DOM
.addressToggle
= $(".address-toggle");
42 DOM
.privateKeyToggle
= $(".private-key-toggle");
44 var derivationPath
= $(".tab-pane.active .path").val();
48 DOM
.network
.on("change", networkChanged
);
49 DOM
.phrase
.on("input", delayedPhraseChanged
);
50 DOM
.passphrase
.on("input", delayedPhraseChanged
);
51 DOM
.generate
.on("click", generateClicked
);
52 DOM
.more
.on("click", showMore
);
53 DOM
.bip32path
.on("input", bip32Changed
);
54 DOM
.bip44purpose
.on("input", bip44Changed
);
55 DOM
.bip44coin
.on("input", bip44Changed
);
56 DOM
.bip44account
.on("input", bip44Changed
);
57 DOM
.bip44change
.on("input", bip44Changed
);
58 DOM
.tab
.on("click", tabClicked
);
59 DOM
.indexToggle
.on("click", toggleIndexes
);
60 DOM
.addressToggle
.on("click", toggleAddresses
);
61 DOM
.privateKeyToggle
.on("click", togglePrivateKeys
);
64 hideValidationError();
65 populateNetworkSelect();
70 function networkChanged(e
) {
71 var network
= e
.target
.value
;
72 networks
[network
].onSelect();
73 setBip44DerivationPath();
74 delayedPhraseChanged();
77 function delayedPhraseChanged() {
78 hideValidationError();
80 if (phraseChangeTimeoutEvent
!= null) {
81 clearTimeout(phraseChangeTimeoutEvent
);
83 phraseChangeTimeoutEvent
= setTimeout(phraseChanged
, 400);
86 function phraseChanged() {
88 hideValidationError();
89 // Get the mnemonic phrase
90 var phrase
= DOM
.phrase
.val();
91 var passphrase
= DOM
.passphrase
.val();
92 var errorText
= findPhraseErrors(phrase
);
94 showValidationError(errorText
);
97 // Get the derivation path
98 var errorText
= findDerivationPathErrors();
100 showValidationError(errorText
);
103 // Calculate and display
104 calcBip32Seed(phrase
, passphrase
, derivationPath
);
109 function generateClicked() {
112 setTimeout(function() {
113 var phrase
= generateRandomPhrase();
121 function tabClicked(e
) {
122 var activePath
= $(e
.target
.getAttribute("href") + " .path");
123 derivationPath
= activePath
.val();
127 function derivationChanged() {
128 delayedPhraseChanged();
131 function bip32Changed() {
132 derivationPath
= DOM
.bip32path
.val();
136 function bip44Changed() {
137 setBip44DerivationPath();
141 function toggleIndexes() {
142 showIndex
= !showIndex
;
143 $("td.index span").toggleClass("invisible");
146 function toggleAddresses() {
147 showAddress
= !showAddress
;
148 $("td.address span").toggleClass("invisible");
151 function togglePrivateKeys() {
152 showPrivKey
= !showPrivKey
;
153 $("td.privkey span").toggleClass("invisible");
158 function generateRandomPhrase() {
159 if (!hasStrongRandom()) {
160 var errorText
= "This browser does not support strong randomness";
161 showValidationError(errorText
);
164 var numWords
= parseInt(DOM
.strength
.val());
165 var strength
= numWords
/ 3 * 32;
166 var words
= mnemonic
.generate(strength
);
167 DOM
.phrase
.val(words
);
171 function calcBip32Seed(phrase
, passphrase
, path
) {
172 var seed
= mnemonic
.toSeed(phrase
, passphrase
);
173 bip32RootKey
= bitcoin
.HDNode
.fromSeedHex(seed
, network
);
174 bip32ExtendedKey
= bip32RootKey
;
175 // Derive the key from the path
176 var pathBits
= path
.split("/");
177 for (var i
=0; i
<pathBits
.length
; i
++) {
178 var bit
= pathBits
[i
];
179 var index
= parseInt(bit
);
183 var hardened
= bit
[bit
.length
-1] == "'";
185 bip32ExtendedKey
= bip32ExtendedKey
.deriveHardened(index
);
188 bip32ExtendedKey
= bip32ExtendedKey
.derive(index
);
193 function showValidationError(errorText
) {
199 function hideValidationError() {
205 function findPhraseErrors(phrase
) {
206 // TODO make this right
207 // Preprocess the words
208 phrase
= mnemonic
.normalizeString(phrase
);
209 var parts
= phrase
.split(" ");
211 for (var i
=0; i
<parts
.length
; i
++) {
213 if (part
.length
> 0) {
214 // TODO check that lowercasing is always valid to do
215 proper
.push(part
.toLowerCase());
218 // TODO some levenstein on the words
219 var properPhrase
= proper
.join(' ');
220 // Check the words are valid
221 var isValid
= mnemonic
.check(properPhrase
);
223 return "Invalid mnemonic";
228 function findDerivationPathErrors(path
) {
233 function displayBip32Info() {
235 var rootKey
= bip32RootKey
.toBase58();
236 DOM
.rootKey
.val(rootKey
);
237 var extendedPrivKey
= bip32ExtendedKey
.toBase58();
238 DOM
.extendedPrivKey
.val(extendedPrivKey
);
239 var extendedPubKey
= bip32ExtendedKey
.toBase58(false);
240 DOM
.extendedPubKey
.val(extendedPubKey
);
241 // Display the addresses and privkeys
242 clearAddressesList();
243 displayAddresses(0, 20);
246 function displayAddresses(start
, total
) {
247 for (var i
=0; i
<total
; i
++) {
248 var index
= i
+ start
;
253 function TableRow(index
) {
259 function calculateValues() {
260 setTimeout(function() {
261 var key
= bip32ExtendedKey
.derive(index
);
262 var address
= key
.getAddress().toString();
263 var privkey
= key
.privKey
.toWIF(network
);
264 addAddressToList(index
, address
, privkey
);
272 function showMore() {
273 var start
= DOM
.addresses
.children().length
;
274 var rowsToAdd
= parseInt(DOM
.rowsToAdd
.val());
275 if (isNaN(rowsToAdd
)) {
277 DOM
.rowsToAdd
.val("20");
279 if (rowsToAdd
> 200) {
280 var msg
= "Generating " + rowsToAdd
+ " rows could take a while. ";
281 msg
+= "Do you want to continue?";
286 displayAddresses(start
, rowsToAdd
);
289 function clearDisplay() {
290 clearAddressesList();
292 hideValidationError();
295 function clearAddressesList() {
296 DOM
.addresses
.empty();
299 function clearKey() {
301 DOM
.extendedPrivKey
.val("");
302 DOM
.extendedPubKey
.val("");
305 function addAddressToList(index
, address
, privkey
) {
306 var row
= $(addressRowTemplate
.html());
308 var indexCell
= row
.find(".index span");
309 var addressCell
= row
.find(".address span");
310 var privkeyCell
= row
.find(".privkey span");
312 var indexText
= derivationPath
+ "/" + index
;
313 indexCell
.text(indexText
);
314 addressCell
.text(address
);
315 privkeyCell
.text(privkey
);
318 indexCell
.addClass("invisible");
321 addressCell
.addClass("invisible");
324 privkeyCell
.addClass("invisible");
326 DOM
.addresses
.append(row
);
329 function hasStrongRandom() {
330 return 'crypto' in window
&& window
['crypto'] !== null;
333 function disableForms() {
334 $("form").on("submit", function(e
) {
339 function setBip44DerivationPath() {
340 var purpose
= parseIntNoNaN(DOM
.bip44purpose
.val(), 44);
341 var coin
= parseIntNoNaN(DOM
.bip44coin
.val(), 0);
342 var account
= parseIntNoNaN(DOM
.bip44account
.val(), 0);
343 var change
= parseIntNoNaN(DOM
.bip44change
.val(), 0);
345 path
+= purpose
+ "'/";
347 path
+= account
+ "'/";
349 DOM
.bip44path
.val(path
);
350 derivationPath
= DOM
.bip44path
.val();
353 function parseIntNoNaN(val
, defaultVal
) {
354 var v
= parseInt(val
);
361 function showPending() {
363 .text("Calculating...")
367 function hidePending() {
373 function populateNetworkSelect() {
374 for (var i
=0; i
<networks
.length
; i
++) {
375 var network
= networks
[i
];
376 var option
= $("<option>");
377 option
.attr("value", i
);
378 option
.text(network
.name
);
379 DOM
.phraseNetwork
.append(option
);
386 onSelect: function() {
387 network
= bitcoin
.networks
.bitcoin
;
388 DOM
.bip44coin
.val(0);
392 name: "Bitcoin Testnet",
393 onSelect: function() {
394 network
= bitcoin
.networks
.testnet
;
395 DOM
.bip44coin
.val(1);
400 onSelect: function() {
401 network
= bitcoin
.networks
.litecoin
;
402 DOM
.bip44coin
.val(2);
407 onSelect: function() {
408 network
= bitcoin
.networks
.dogecoin
;
409 DOM
.bip44coin
.val(3);
414 onSelect: function() {
415 network
= bitcoin
.networks
.shadow
;
416 DOM
.bip44coin
.val(35);
420 name: "ShadowCash Testnet",
421 onSelect: function() {
422 network
= bitcoin
.networks
.shadowtn
;
423 DOM
.bip44coin
.val(1);
428 onSelect: function() {
429 network
= bitcoin
.networks
.viacoin
;
430 DOM
.bip44coin
.val(14);
434 name: "Viacoin Testnet",
435 onSelect: function() {
436 network
= bitcoin
.networks
.viacointestnet
;
437 DOM
.bip44coin
.val(1);
442 onSelect: function() {
443 network
= bitcoin
.networks
.jumbucks
;
444 DOM
.bip44coin
.val(26);
449 onSelect: function() {
450 network
= bitcoin
.networks
.clam
;
451 DOM
.bip44coin
.val(23);