]>
git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blob - src/js/index.js
bc1682e99c38ac7dd5e909e3a5f32889fcdc9148
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();
69 function networkChanged(e
) {
70 var n
= e
.target
.value
;
72 network
= Bitcoin
.networks
.bitcoin
;
75 else if (n
== "bitcoin-testnet") {
76 network
= Bitcoin
.networks
.testnet
;
79 else if (n
== "litecoin") {
80 network
= Bitcoin
.networks
.litecoin
;
83 else if (n
== "dogecoin") {
84 network
= Bitcoin
.networks
.dogecoin
;
85 var UNOFFICIAL_BIP44_COIN
= 9999;
86 DOM
.bip44coin
.val(UNOFFICIAL_BIP44_COIN
); // This coin is not in BIP44
88 setBip44DerivationPath();
89 delayedPhraseChanged();
92 function delayedPhraseChanged() {
93 hideValidationError();
95 if (phraseChangeTimeoutEvent
!= null) {
96 clearTimeout(phraseChangeTimeoutEvent
);
98 phraseChangeTimeoutEvent
= setTimeout(phraseChanged
, 400);
101 function phraseChanged() {
103 hideValidationError();
104 // Get the mnemonic phrase
105 var phrase
= DOM
.phrase
.val();
106 var passphrase
= DOM
.passphrase
.val();
107 var errorText
= findPhraseErrors(phrase
);
109 showValidationError(errorText
);
112 // Get the derivation path
113 var errorText
= findDerivationPathErrors();
115 showValidationError(errorText
);
118 // Calculate and display
119 calcBip32Seed(phrase
, passphrase
, derivationPath
);
124 function generateClicked() {
127 setTimeout(function() {
128 var phrase
= generateRandomPhrase();
136 function tabClicked(e
) {
137 var activePath
= $(e
.target
.getAttribute("href") + " .path");
138 derivationPath
= activePath
.val();
142 function derivationChanged() {
143 delayedPhraseChanged();
146 function bip32Changed() {
147 derivationPath
= DOM
.bip32path
.val();
151 function bip44Changed() {
152 setBip44DerivationPath();
153 derivationPath
= DOM
.bip44path
.val();
157 function toggleIndexes() {
158 showIndex
= !showIndex
;
159 $("td.index span").toggleClass("invisible");
162 function toggleAddresses() {
163 showAddress
= !showAddress
;
164 $("td.address span").toggleClass("invisible");
167 function togglePrivateKeys() {
168 showPrivKey
= !showPrivKey
;
169 $("td.privkey span").toggleClass("invisible");
174 function generateRandomPhrase() {
175 if (!hasStrongRandom()) {
176 var errorText
= "This browser does not support strong randomness";
177 showValidationError(errorText
);
180 var numWords
= parseInt(DOM
.strength
.val());
181 // Check strength is an integer
182 if (isNaN(numWords
)) {
183 DOM
.strength
.val("12");
186 // Check strength is a multiple of 32, if not round it down
187 if (numWords
% 3 != 0) {
188 numWords
= Math
.floor(numWords
/ 3) * 3;
189 DOM
.strength
.val(numWords
);
191 // Check strength is at least 32
194 DOM
.strength
.val(numWords
);
196 var strength
= numWords
/ 3 * 32;
197 var words
= mnemonic
.generate(strength
);
198 DOM
.phrase
.val(words
);
202 function calcBip32Seed(phrase
, passphrase
, path
) {
203 var seed
= mnemonic
.toSeed(phrase
, passphrase
);
204 bip32RootKey
= Bitcoin
.HDNode
.fromSeedHex(seed
, network
);
205 bip32ExtendedKey
= bip32RootKey
;
206 // Derive the key from the path
207 var pathBits
= path
.split("/");
208 for (var i
=0; i
<pathBits
.length
; i
++) {
209 var bit
= pathBits
[i
];
210 var index
= parseInt(bit
);
214 var hardened
= bit
[bit
.length
-1] == "'";
216 bip32ExtendedKey
= bip32ExtendedKey
.deriveHardened(index
);
219 bip32ExtendedKey
= bip32ExtendedKey
.derive(index
);
224 function showValidationError(errorText
) {
230 function hideValidationError() {
236 function findPhraseErrors(phrase
) {
237 // TODO make this right
238 // Preprocess the words
239 phrase
= mnemonic
.normalizeString(phrase
);
240 var parts
= phrase
.split(" ");
242 for (var i
=0; i
<parts
.length
; i
++) {
244 if (part
.length
> 0) {
245 // TODO check that lowercasing is always valid to do
246 proper
.push(part
.toLowerCase());
249 // TODO some levenstein on the words
250 var properPhrase
= proper
.join(' ');
251 // Check the words are valid
252 var isValid
= mnemonic
.check(properPhrase
);
254 return "Invalid mnemonic";
259 function findDerivationPathErrors(path
) {
264 function displayBip32Info() {
266 var rootKey
= bip32RootKey
.toBase58();
267 DOM
.rootKey
.val(rootKey
);
268 var extendedPrivKey
= bip32ExtendedKey
.toBase58();
269 DOM
.extendedPrivKey
.val(extendedPrivKey
);
270 var extendedPubKey
= bip32ExtendedKey
.toBase58(false);
271 DOM
.extendedPubKey
.val(extendedPubKey
);
272 // Display the addresses and privkeys
273 clearAddressesList();
274 displayAddresses(0, 20);
277 function displayAddresses(start
, total
) {
278 for (var i
=0; i
<total
; i
++) {
279 var index
= i
+ start
;
284 function TableRow(index
) {
290 function calculateValues() {
291 setTimeout(function() {
292 var key
= bip32ExtendedKey
.derive(index
);
293 var address
= key
.getAddress().toString();
294 var privkey
= key
.privKey
.toWIF(network
);
295 addAddressToList(index
, address
, privkey
);
303 function showMore() {
304 var start
= DOM
.addresses
.children().length
;
305 var rowsToAdd
= parseInt(DOM
.rowsToAdd
.val());
306 if (isNaN(rowsToAdd
)) {
308 DOM
.rowsToAdd
.val("20");
310 if (rowsToAdd
> 200) {
311 var msg
= "Generating " + rowsToAdd
+ " rows could take a while. ";
312 msg
+= "Do you want to continue?";
317 displayAddresses(start
, rowsToAdd
);
320 function clearDisplay() {
321 clearAddressesList();
323 hideValidationError();
326 function clearAddressesList() {
327 DOM
.addresses
.empty();
330 function clearKey() {
332 DOM
.extendedPrivKey
.val("");
333 DOM
.extendedPubKey
.val("");
336 function addAddressToList(index
, address
, privkey
) {
337 var row
= $(addressRowTemplate
.html());
339 var indexCell
= row
.find(".index span");
340 var addressCell
= row
.find(".address span");
341 var privkeyCell
= row
.find(".privkey span");
343 indexCell
.text(index
);
344 addressCell
.text(address
);
345 privkeyCell
.text(privkey
);
348 indexCell
.addClass("invisible");
351 addressCell
.addClass("invisible");
354 privkeCell
.addClass("invisible");
356 DOM
.addresses
.append(row
);
359 function hasStrongRandom() {
360 return 'crypto' in window
&& window
['crypto'] !== null;
363 function disableForms() {
364 $("form").on("submit", function(e
) {
369 function setBip44DerivationPath() {
370 var purpose
= parseIntNoNaN(DOM
.bip44purpose
.val(), 44);
371 var coin
= parseIntNoNaN(DOM
.bip44coin
.val(), 0);
372 var account
= parseIntNoNaN(DOM
.bip44account
.val(), 0);
373 var change
= parseIntNoNaN(DOM
.bip44change
.val(), 0);
375 path
+= purpose
+ "'/";
377 path
+= account
+ "'/";
379 DOM
.bip44path
.val(path
);
382 function parseIntNoNaN(val
, defaultVal
) {
383 var v
= parseInt(val
);
390 function showPending() {
392 .text("Calculating...")
396 function hidePending() {