]>
git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blob - src/js/index.js
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;
17 DOM
.network
= $(".network");
18 DOM
.phraseNetwork
= $("#network-phrase");
19 DOM
.phrase
= $(".phrase");
20 DOM
.passphrase
= $(".passphrase");
21 DOM
.generate
= $(".generate");
22 DOM
.seed
= $(".seed");
23 DOM
.rootKey
= $(".root-key");
24 DOM
.extendedPrivKey
= $(".extended-priv-key");
25 DOM
.extendedPubKey
= $(".extended-pub-key");
26 DOM
.bip32tab
= $("#bip32-tab");
27 DOM
.bip44tab
= $("#bip44-tab");
28 DOM
.bip32panel
= $("#bip32");
29 DOM
.bip44panel
= $("#bip44");
30 DOM
.bip32path
= $("#bip32-path");
31 DOM
.bip44path
= $("#bip44-path");
32 DOM
.bip44purpose
= $("#bip44 .purpose");
33 DOM
.bip44coin
= $("#bip44 .coin");
34 DOM
.bip44account
= $("#bip44 .account");
35 DOM
.bip44change
= $("#bip44 .change");
36 DOM
.strength
= $(".strength");
37 DOM
.addresses
= $(".addresses");
38 DOM
.rowsToAdd
= $(".rows-to-add");
39 DOM
.more
= $(".more");
40 DOM
.feedback
= $(".feedback");
41 DOM
.tab
= $(".derivation-type a");
42 DOM
.indexToggle
= $(".index-toggle");
43 DOM
.addressToggle
= $(".address-toggle");
44 DOM
.privateKeyToggle
= $(".private-key-toggle");
46 var derivationPath
= $(".tab-pane.active .path").val();
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
.bip32path
.on("input", bip32Changed
);
56 DOM
.bip44purpose
.on("input", bip44Changed
);
57 DOM
.bip44coin
.on("input", bip44Changed
);
58 DOM
.bip44account
.on("input", bip44Changed
);
59 DOM
.bip44change
.on("input", bip44Changed
);
60 DOM
.tab
.on("click", tabClicked
);
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();
75 delayedPhraseChanged();
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 passphrase
= DOM
.passphrase
.val();
93 var errorText
= findPhraseErrors(phrase
);
95 showValidationError(errorText
);
98 // Get the derivation path
99 var errorText
= findDerivationPathErrors();
101 showValidationError(errorText
);
104 // Calculate and display
105 calcBip32Seed(phrase
, passphrase
, derivationPath
);
110 function generateClicked() {
113 setTimeout(function() {
114 var phrase
= generateRandomPhrase();
122 function tabClicked(e
) {
123 var activePath
= $(e
.target
.getAttribute("href") + " .path");
124 derivationPath
= activePath
.val();
128 function derivationChanged() {
129 delayedPhraseChanged();
132 function bip32Changed() {
133 derivationPath
= DOM
.bip32path
.val();
137 function bip44Changed() {
138 setBip44DerivationPath();
142 function toggleIndexes() {
143 showIndex
= !showIndex
;
144 $("td.index span").toggleClass("invisible");
147 function toggleAddresses() {
148 showAddress
= !showAddress
;
149 $("td.address span").toggleClass("invisible");
152 function togglePrivateKeys() {
153 showPrivKey
= !showPrivKey
;
154 $("td.privkey span").toggleClass("invisible");
159 function generateRandomPhrase() {
160 if (!hasStrongRandom()) {
161 var errorText
= "This browser does not support strong randomness";
162 showValidationError(errorText
);
165 var numWords
= parseInt(DOM
.strength
.val());
166 var strength
= numWords
/ 3 * 32;
167 var words
= mnemonic
.generate(strength
);
168 DOM
.phrase
.val(words
);
172 function calcBip32Seed(phrase
, passphrase
, path
) {
173 seed
= mnemonic
.toSeed(phrase
, passphrase
);
174 bip32RootKey
= bitcoin
.HDNode
.fromSeedHex(seed
, network
);
175 bip32ExtendedKey
= bip32RootKey
;
176 // Derive the key from the path
177 var pathBits
= path
.split("/");
178 for (var i
=0; i
<pathBits
.length
; i
++) {
179 var bit
= pathBits
[i
];
180 var index
= parseInt(bit
);
184 var hardened
= bit
[bit
.length
-1] == "'";
186 bip32ExtendedKey
= bip32ExtendedKey
.deriveHardened(index
);
189 bip32ExtendedKey
= bip32ExtendedKey
.derive(index
);
194 function showValidationError(errorText
) {
200 function hideValidationError() {
206 function findPhraseErrors(phrase
) {
207 // TODO make this right
208 // Preprocess the words
209 phrase
= mnemonic
.normalizeString(phrase
);
210 var parts
= phrase
.split(" ");
212 for (var i
=0; i
<parts
.length
; i
++) {
214 if (part
.length
> 0) {
215 // TODO check that lowercasing is always valid to do
216 proper
.push(part
.toLowerCase());
219 // TODO some levenstein on the words
220 var properPhrase
= proper
.join(' ');
221 // Check the words are valid
222 var isValid
= mnemonic
.check(properPhrase
);
224 return "Invalid mnemonic";
229 function findDerivationPathErrors(path
) {
234 function displayBip32Info() {
237 var rootKey
= bip32RootKey
.toBase58();
238 DOM
.rootKey
.val(rootKey
);
239 var extendedPrivKey
= bip32ExtendedKey
.toBase58();
240 DOM
.extendedPrivKey
.val(extendedPrivKey
);
241 var extendedPubKey
= bip32ExtendedKey
.toBase58(false);
242 DOM
.extendedPubKey
.val(extendedPubKey
);
243 // Display the addresses and privkeys
244 clearAddressesList();
245 displayAddresses(0, 20);
248 function displayAddresses(start
, total
) {
249 for (var i
=0; i
<total
; i
++) {
250 var index
= i
+ start
;
255 function TableRow(index
) {
261 function calculateValues() {
262 setTimeout(function() {
263 var key
= bip32ExtendedKey
.derive(index
);
264 var address
= key
.getAddress().toString();
265 var privkey
= key
.privKey
.toWIF(network
);
266 addAddressToList(index
, address
, privkey
);
274 function showMore() {
275 var start
= DOM
.addresses
.children().length
;
276 var rowsToAdd
= parseInt(DOM
.rowsToAdd
.val());
277 if (isNaN(rowsToAdd
)) {
279 DOM
.rowsToAdd
.val("20");
281 if (rowsToAdd
> 200) {
282 var msg
= "Generating " + rowsToAdd
+ " rows could take a while. ";
283 msg
+= "Do you want to continue?";
288 displayAddresses(start
, rowsToAdd
);
291 function clearDisplay() {
292 clearAddressesList();
294 hideValidationError();
297 function clearAddressesList() {
298 DOM
.addresses
.empty();
301 function clearKey() {
303 DOM
.extendedPrivKey
.val("");
304 DOM
.extendedPubKey
.val("");
307 function addAddressToList(index
, address
, privkey
) {
308 var row
= $(addressRowTemplate
.html());
310 var indexCell
= row
.find(".index span");
311 var addressCell
= row
.find(".address span");
312 var privkeyCell
= row
.find(".privkey span");
314 var indexText
= derivationPath
+ "/" + index
;
315 indexCell
.text(indexText
);
316 addressCell
.text(address
);
317 privkeyCell
.text(privkey
);
320 indexCell
.addClass("invisible");
323 addressCell
.addClass("invisible");
326 privkeyCell
.addClass("invisible");
328 DOM
.addresses
.append(row
);
331 function hasStrongRandom() {
332 return 'crypto' in window
&& window
['crypto'] !== null;
335 function disableForms() {
336 $("form").on("submit", function(e
) {
341 function setBip44DerivationPath() {
342 var purpose
= parseIntNoNaN(DOM
.bip44purpose
.val(), 44);
343 var coin
= parseIntNoNaN(DOM
.bip44coin
.val(), 0);
344 var account
= parseIntNoNaN(DOM
.bip44account
.val(), 0);
345 var change
= parseIntNoNaN(DOM
.bip44change
.val(), 0);
347 path
+= purpose
+ "'/";
349 path
+= account
+ "'/";
351 DOM
.bip44path
.val(path
);
352 derivationPath
= DOM
.bip44path
.val();
355 function parseIntNoNaN(val
, defaultVal
) {
356 var v
= parseInt(val
);
363 function showPending() {
365 .text("Calculating...")
369 function hidePending() {
375 function populateNetworkSelect() {
376 for (var i
=0; i
<networks
.length
; i
++) {
377 var network
= networks
[i
];
378 var option
= $("<option>");
379 option
.attr("value", i
);
380 option
.text(network
.name
);
381 DOM
.phraseNetwork
.append(option
);
388 onSelect: function() {
389 network
= bitcoin
.networks
.bitcoin
;
390 DOM
.bip44coin
.val(0);
394 name: "Bitcoin Testnet",
395 onSelect: function() {
396 network
= bitcoin
.networks
.testnet
;
397 DOM
.bip44coin
.val(1);
402 onSelect: function() {
403 network
= bitcoin
.networks
.litecoin
;
404 DOM
.bip44coin
.val(2);
409 onSelect: function() {
410 network
= bitcoin
.networks
.dogecoin
;
411 DOM
.bip44coin
.val(3);
416 onSelect: function() {
417 network
= bitcoin
.networks
.shadow
;
418 DOM
.bip44coin
.val(35);
422 name: "ShadowCash Testnet",
423 onSelect: function() {
424 network
= bitcoin
.networks
.shadowtn
;
425 DOM
.bip44coin
.val(1);
430 onSelect: function() {
431 network
= bitcoin
.networks
.viacoin
;
432 DOM
.bip44coin
.val(14);
436 name: "Viacoin Testnet",
437 onSelect: function() {
438 network
= bitcoin
.networks
.viacointestnet
;
439 DOM
.bip44coin
.val(1);
444 onSelect: function() {
445 network
= bitcoin
.networks
.jumbucks
;
446 DOM
.bip44coin
.val(26);
451 onSelect: function() {
452 network
= bitcoin
.networks
.clam
;
453 DOM
.bip44coin
.val(23);