]>
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");
43 DOM
.myceliumPath
= $("#mycelium-path");
45 var derivationPath
= $(".tab-pane.active .path").val();
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
.bip32path
.on("input", bip32Changed
);
55 DOM
.bip44purpose
.on("input", bip44Changed
);
56 DOM
.bip44coin
.on("input", bip44Changed
);
57 DOM
.bip44account
.on("input", bip44Changed
);
58 DOM
.bip44change
.on("input", bip44Changed
);
59 DOM
.tab
.on("click", tabClicked
);
60 DOM
.indexToggle
.on("click", toggleIndexes
);
61 DOM
.addressToggle
.on("click", toggleAddresses
);
62 DOM
.privateKeyToggle
.on("click", togglePrivateKeys
);
65 hideValidationError();
70 function networkChanged(e
) {
71 var n
= e
.target
.value
;
73 network
= Bitcoin
.networks
.bitcoin
;
75 DOM
.myceliumPath
.val("m/44'/0'/0'/0");
77 else if (n
== "bitcoin-testnet") {
78 network
= Bitcoin
.networks
.testnet
;
80 DOM
.myceliumPath
.val("m/44'/1'/0'/0");
82 else if (n
== "litecoin") {
83 network
= Bitcoin
.networks
.litecoin
;
86 else if (n
== "dogecoin") {
87 network
= Bitcoin
.networks
.dogecoin
;
88 var UNOFFICIAL_BIP44_COIN
= 9999;
89 DOM
.bip44coin
.val(UNOFFICIAL_BIP44_COIN
); // This coin is not in BIP44
91 setBip44DerivationPath();
92 delayedPhraseChanged();
95 function delayedPhraseChanged() {
96 hideValidationError();
98 if (phraseChangeTimeoutEvent
!= null) {
99 clearTimeout(phraseChangeTimeoutEvent
);
101 phraseChangeTimeoutEvent
= setTimeout(phraseChanged
, 400);
104 function phraseChanged() {
106 hideValidationError();
107 // Get the mnemonic phrase
108 var phrase
= DOM
.phrase
.val();
109 var passphrase
= DOM
.passphrase
.val();
110 var errorText
= findPhraseErrors(phrase
);
112 showValidationError(errorText
);
115 // Get the derivation path
116 var errorText
= findDerivationPathErrors();
118 showValidationError(errorText
);
121 // Calculate and display
122 calcBip32Seed(phrase
, passphrase
, derivationPath
);
127 function generateClicked() {
130 setTimeout(function() {
131 var phrase
= generateRandomPhrase();
139 function tabClicked(e
) {
140 var activePath
= $(e
.target
.getAttribute("href") + " .path");
141 derivationPath
= activePath
.val();
145 function derivationChanged() {
146 delayedPhraseChanged();
149 function bip32Changed() {
150 derivationPath
= DOM
.bip32path
.val();
154 function bip44Changed() {
155 setBip44DerivationPath();
156 derivationPath
= DOM
.bip44path
.val();
160 function toggleIndexes() {
161 showIndex
= !showIndex
;
162 $("td.index span").toggleClass("invisible");
165 function toggleAddresses() {
166 showAddress
= !showAddress
;
167 $("td.address span").toggleClass("invisible");
170 function togglePrivateKeys() {
171 showPrivKey
= !showPrivKey
;
172 $("td.privkey span").toggleClass("invisible");
177 function generateRandomPhrase() {
178 if (!hasStrongRandom()) {
179 var errorText
= "This browser does not support strong randomness";
180 showValidationError(errorText
);
183 var numWords
= parseInt(DOM
.strength
.val());
184 // Check strength is an integer
185 if (isNaN(numWords
)) {
186 DOM
.strength
.val("12");
189 // Check strength is a multiple of 32, if not round it down
190 if (numWords
% 3 != 0) {
191 numWords
= Math
.floor(numWords
/ 3) * 3;
192 DOM
.strength
.val(numWords
);
194 // Check strength is at least 32
197 DOM
.strength
.val(numWords
);
199 var strength
= numWords
/ 3 * 32;
200 var words
= mnemonic
.generate(strength
);
201 DOM
.phrase
.val(words
);
205 function calcBip32Seed(phrase
, passphrase
, path
) {
206 var seed
= mnemonic
.toSeed(phrase
, passphrase
);
207 bip32RootKey
= Bitcoin
.HDNode
.fromSeedHex(seed
, network
);
208 bip32ExtendedKey
= bip32RootKey
;
209 // Derive the key from the path
210 var pathBits
= path
.split("/");
211 for (var i
=0; i
<pathBits
.length
; i
++) {
212 var bit
= pathBits
[i
];
213 var index
= parseInt(bit
);
217 var hardened
= bit
[bit
.length
-1] == "'";
219 bip32ExtendedKey
= bip32ExtendedKey
.deriveHardened(index
);
222 bip32ExtendedKey
= bip32ExtendedKey
.derive(index
);
227 function showValidationError(errorText
) {
233 function hideValidationError() {
239 function findPhraseErrors(phrase
) {
240 // TODO make this right
241 // Preprocess the words
242 phrase
= mnemonic
.normalizeString(phrase
);
243 var parts
= phrase
.split(" ");
245 for (var i
=0; i
<parts
.length
; i
++) {
247 if (part
.length
> 0) {
248 // TODO check that lowercasing is always valid to do
249 proper
.push(part
.toLowerCase());
252 // TODO some levenstein on the words
253 var properPhrase
= proper
.join(' ');
254 // Check the words are valid
255 var isValid
= mnemonic
.check(properPhrase
);
257 return "Invalid mnemonic";
262 function findDerivationPathErrors(path
) {
267 function displayBip32Info() {
269 var rootKey
= bip32RootKey
.toBase58();
270 DOM
.rootKey
.val(rootKey
);
271 var extendedPrivKey
= bip32ExtendedKey
.toBase58();
272 DOM
.extendedPrivKey
.val(extendedPrivKey
);
273 var extendedPubKey
= bip32ExtendedKey
.toBase58(false);
274 DOM
.extendedPubKey
.val(extendedPubKey
);
275 // Display the addresses and privkeys
276 clearAddressesList();
277 displayAddresses(0, 20);
280 function displayAddresses(start
, total
) {
281 for (var i
=0; i
<total
; i
++) {
282 var index
= i
+ start
;
287 function TableRow(index
) {
293 function calculateValues() {
294 setTimeout(function() {
295 var key
= bip32ExtendedKey
.derive(index
);
296 var address
= key
.getAddress().toString();
297 var privkey
= key
.privKey
.toWIF(network
);
298 addAddressToList(index
, address
, privkey
);
306 function showMore() {
307 var start
= DOM
.addresses
.children().length
;
308 var rowsToAdd
= parseInt(DOM
.rowsToAdd
.val());
309 if (isNaN(rowsToAdd
)) {
311 DOM
.rowsToAdd
.val("20");
313 if (rowsToAdd
> 200) {
314 var msg
= "Generating " + rowsToAdd
+ " rows could take a while. ";
315 msg
+= "Do you want to continue?";
320 displayAddresses(start
, rowsToAdd
);
323 function clearDisplay() {
324 clearAddressesList();
326 hideValidationError();
329 function clearAddressesList() {
330 DOM
.addresses
.empty();
333 function clearKey() {
335 DOM
.extendedPrivKey
.val("");
336 DOM
.extendedPubKey
.val("");
339 function addAddressToList(index
, address
, privkey
) {
340 var row
= $(addressRowTemplate
.html());
342 var indexCell
= row
.find(".index span");
343 var addressCell
= row
.find(".address span");
344 var privkeyCell
= row
.find(".privkey span");
346 indexCell
.text(index
);
347 addressCell
.text(address
);
348 privkeyCell
.text(privkey
);
351 indexCell
.addClass("invisible");
354 addressCell
.addClass("invisible");
357 privkeCell
.addClass("invisible");
359 DOM
.addresses
.append(row
);
362 function hasStrongRandom() {
363 return 'crypto' in window
&& window
['crypto'] !== null;
366 function disableForms() {
367 $("form").on("submit", function(e
) {
372 function setBip44DerivationPath() {
373 var purpose
= parseIntNoNaN(DOM
.bip44purpose
.val(), 44);
374 var coin
= parseIntNoNaN(DOM
.bip44coin
.val(), 0);
375 var account
= parseIntNoNaN(DOM
.bip44account
.val(), 0);
376 var change
= parseIntNoNaN(DOM
.bip44change
.val(), 0);
378 path
+= purpose
+ "'/";
380 path
+= account
+ "'/";
382 DOM
.bip44path
.val(path
);
385 function parseIntNoNaN(val
, defaultVal
) {
386 var v
= parseInt(val
);
393 function showPending() {
395 .text("Calculating...")
399 function hidePending() {