]>
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");
9 var phraseChangeTimeoutEvent
= null;
12 DOM
.phrase
= $(".phrase");
13 DOM
.passphrase
= $(".passphrase");
14 DOM
.generate
= $(".generate");
15 DOM
.rootKey
= $(".root-key");
16 DOM
.extendedPrivKey
= $(".extended-priv-key");
17 DOM
.extendedPubKey
= $(".extended-pub-key");
18 DOM
.bip32path
= $("#bip32-path");
19 DOM
.bip44path
= $("#bip44-path");
20 DOM
.bip44purpose
= $("#bip44 .purpose");
21 DOM
.bip44coin
= $("#bip44 .coin");
22 DOM
.bip44account
= $("#bip44 .account");
23 DOM
.bip44change
= $("#bip44 .change");
24 DOM
.strength
= $(".strength");
25 DOM
.addresses
= $(".addresses");
26 DOM
.rowsToAdd
= $(".rows-to-add");
27 DOM
.more
= $(".more");
28 DOM
.feedback
= $(".feedback");
29 DOM
.tab
= $(".derivation-type a");
30 DOM
.indexToggle
= $(".index-toggle");
31 DOM
.addressToggle
= $(".address-toggle");
32 DOM
.privateKeyToggle
= $(".private-key-toggle");
34 var derivationPath
= DOM
.bip44path
.val();
38 DOM
.phrase
.on("input", delayedPhraseChanged
);
39 DOM
.passphrase
.on("input", delayedPhraseChanged
);
40 DOM
.generate
.on("click", generateClicked
);
41 DOM
.more
.on("click", showMore
);
42 DOM
.bip32path
.on("input", bip32Changed
);
43 DOM
.bip44purpose
.on("input", bip44Changed
);
44 DOM
.bip44coin
.on("input", bip44Changed
);
45 DOM
.bip44account
.on("input", bip44Changed
);
46 DOM
.bip44change
.on("input", bip44Changed
);
47 DOM
.tab
.on("click", tabClicked
);
48 DOM
.indexToggle
.on("click", toggleIndexes
);
49 DOM
.addressToggle
.on("click", toggleAddresses
);
50 DOM
.privateKeyToggle
.on("click", togglePrivateKeys
);
53 hideValidationError();
58 function delayedPhraseChanged() {
59 hideValidationError();
61 if (phraseChangeTimeoutEvent
!= null) {
62 clearTimeout(phraseChangeTimeoutEvent
);
64 phraseChangeTimeoutEvent
= setTimeout(phraseChanged
, 400);
67 function phraseChanged() {
69 hideValidationError();
70 // Get the mnemonic phrase
71 var phrase
= DOM
.phrase
.val();
72 var passphrase
= DOM
.passphrase
.val();
73 var errorText
= findPhraseErrors(phrase
);
75 showValidationError(errorText
);
78 // Get the derivation path
79 var errorText
= findDerivationPathErrors();
81 showValidationError(errorText
);
84 // Calculate and display
85 calcBip32Seed(phrase
, passphrase
, derivationPath
);
90 function generateClicked() {
93 setTimeout(function() {
94 var phrase
= generateRandomPhrase();
102 function tabClicked(e
) {
103 var activePath
= $(e
.target
.getAttribute("href") + " .path");
104 derivationPath
= activePath
.val();
108 function derivationChanged() {
109 delayedPhraseChanged();
112 function bip32Changed() {
113 derivationPath
= DOM
.bip32path
.val();
117 function bip44Changed() {
118 setBip44DerivationPath();
119 derivationPath
= DOM
.bip44path
.val();
123 function toggleIndexes() {
124 $("td.index span").toggleClass("invisible");
127 function toggleAddresses() {
128 $("td.address span").toggleClass("invisible");
131 function togglePrivateKeys() {
132 $("td.privkey span").toggleClass("invisible");
137 function generateRandomPhrase() {
138 if (!hasStrongRandom()) {
139 var errorText
= "This browser does not support strong randomness";
140 showValidationError(errorText
);
143 var numWords
= parseInt(DOM
.strength
.val());
144 // Check strength is an integer
145 if (isNaN(numWords
)) {
146 DOM
.strength
.val("12");
149 // Check strength is a multiple of 32, if not round it down
150 if (numWords
% 3 != 0) {
151 numWords
= Math
.floor(numWords
/ 3) * 3;
152 DOM
.strength
.val(numWords
);
154 // Check strength is at least 32
157 DOM
.strength
.val(numWords
);
159 var strength
= numWords
/ 3 * 32;
160 var words
= mnemonic
.generate(strength
);
161 DOM
.phrase
.val(words
);
165 function calcBip32Seed(phrase
, passphrase
, path
) {
166 var seed
= mnemonic
.toSeed(phrase
, passphrase
);
167 bip32RootKey
= Bitcoin
.HDNode
.fromSeedHex(seed
, network
);
168 bip32ExtendedKey
= bip32RootKey
;
169 // Derive the key from the path
170 var pathBits
= path
.split("/");
171 for (var i
=0; i
<pathBits
.length
; i
++) {
172 var bit
= pathBits
[i
];
173 var index
= parseInt(bit
);
177 var hardened
= bit
[bit
.length
-1] == "'";
179 bip32ExtendedKey
= bip32ExtendedKey
.deriveHardened(index
);
182 bip32ExtendedKey
= bip32ExtendedKey
.derive(index
);
187 function showValidationError(errorText
) {
193 function hideValidationError() {
199 function findPhraseErrors(phrase
) {
200 // TODO make this right
201 // Preprocess the words
202 phrase
= mnemonic
.normalizeString(phrase
);
203 var parts
= phrase
.split(" ");
205 for (var i
=0; i
<parts
.length
; i
++) {
207 if (part
.length
> 0) {
208 // TODO check that lowercasing is always valid to do
209 proper
.push(part
.toLowerCase());
212 // TODO some levenstein on the words
213 var properPhrase
= proper
.join(' ');
214 // Check the words are valid
215 var isValid
= mnemonic
.check(properPhrase
);
217 return "Invalid mnemonic";
222 function findDerivationPathErrors(path
) {
227 function displayBip32Info() {
229 var rootKey
= bip32RootKey
.toBase58();
230 DOM
.rootKey
.val(rootKey
);
231 var extendedPrivKey
= bip32ExtendedKey
.toBase58();
232 DOM
.extendedPrivKey
.val(extendedPrivKey
);
233 var extendedPubKey
= bip32ExtendedKey
.toBase58(false);
234 DOM
.extendedPubKey
.val(extendedPubKey
);
235 // Display the addresses and privkeys
236 clearAddressesList();
237 displayAddresses(0, 20);
240 function displayAddresses(start
, total
) {
241 for (var i
=0; i
<total
; i
++) {
242 var index
= i
+ start
;
247 function TableRow(index
) {
253 function calculateValues() {
254 setTimeout(function() {
255 var key
= bip32ExtendedKey
.derive(index
);
256 var address
= key
.getAddress().toString();
257 var privkey
= key
.privKey
.toWIF(network
);
258 addAddressToList(index
, address
, privkey
);
266 function showMore() {
267 var start
= DOM
.addresses
.children().length
;
268 var rowsToAdd
= parseInt(DOM
.rowsToAdd
.val());
269 if (isNaN(rowsToAdd
)) {
271 DOM
.rowsToAdd
.val("20");
273 if (rowsToAdd
> 200) {
274 var msg
= "Generating " + rowsToAdd
+ " rows could take a while. ";
275 msg
+= "Do you want to continue?";
280 displayAddresses(start
, rowsToAdd
);
283 function clearDisplay() {
284 clearAddressesList();
286 hideValidationError();
289 function clearAddressesList() {
290 DOM
.addresses
.empty();
293 function clearKey() {
295 DOM
.extendedPrivKey
.val("");
296 DOM
.extendedPubKey
.val("");
299 function addAddressToList(index
, address
, privkey
) {
300 var row
= $(addressRowTemplate
.html());
301 row
.find(".index span").text(index
);
302 row
.find(".address span").text(address
);
303 row
.find(".privkey span").text(privkey
);
304 DOM
.addresses
.append(row
);
307 function hasStrongRandom() {
308 return 'crypto' in window
&& window
['crypto'] !== null;
311 function disableForms() {
312 $("form").on("submit", function(e
) {
317 function setBip44DerivationPath() {
318 var purpose
= parseIntNoNaN(DOM
.bip44purpose
.val(), 44);
319 var coin
= parseIntNoNaN(DOM
.bip44coin
.val(), 0);
320 var account
= parseIntNoNaN(DOM
.bip44account
.val(), 0);
321 var change
= parseIntNoNaN(DOM
.bip44change
.val(), 0);
323 path
+= purpose
+ "'/";
325 path
+= account
+ "'/";
327 DOM
.bip44path
.val(path
);
330 function parseIntNoNaN(val
, defaultVal
) {
331 var v
= parseInt(val
);
338 function showPending() {
340 .text("Calculating...")
344 function hidePending() {