]>
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
.generate
= $(".generate");
14 DOM
.rootKey
= $(".root-key");
15 DOM
.extendedPrivKey
= $(".extended-priv-key");
16 DOM
.extendedPubKey
= $(".extended-pub-key");
17 DOM
.bip32path
= $("#bip32-path");
18 DOM
.bip44path
= $("#bip44-path");
19 DOM
.bip44purpose
= $("#bip44 .purpose");
20 DOM
.bip44coin
= $("#bip44 .coin");
21 DOM
.bip44account
= $("#bip44 .account");
22 DOM
.bip44change
= $("#bip44 .change");
23 DOM
.strength
= $(".strength");
24 DOM
.addresses
= $(".addresses");
25 DOM
.rowsToAdd
= $(".rows-to-add");
26 DOM
.more
= $(".more");
27 DOM
.feedback
= $(".feedback");
28 DOM
.tab
= $(".derivation-type a");
29 DOM
.indexToggle
= $(".index-toggle");
30 DOM
.addressToggle
= $(".address-toggle");
31 DOM
.privateKeyToggle
= $(".private-key-toggle");
33 var derivationPath
= DOM
.bip44path
.val();
37 DOM
.phrase
.on("keyup", delayedPhraseChanged
);
38 DOM
.generate
.on("click", generateClicked
);
39 DOM
.more
.on("click", showMore
);
40 DOM
.bip32path
.on("keyup", bip32Changed
);
41 DOM
.bip44purpose
.on("keyup", bip44Changed
);
42 DOM
.bip44coin
.on("keyup", bip44Changed
);
43 DOM
.bip44account
.on("keyup", bip44Changed
);
44 DOM
.bip44change
.on("keyup", bip44Changed
);
45 DOM
.tab
.on("click", tabClicked
);
46 DOM
.indexToggle
.on("click", toggleIndexes
);
47 DOM
.addressToggle
.on("click", toggleAddresses
);
48 DOM
.privateKeyToggle
.on("click", togglePrivateKeys
);
51 hideValidationError();
56 function delayedPhraseChanged() {
57 hideValidationError();
59 if (phraseChangeTimeoutEvent
!= null) {
60 clearTimeout(phraseChangeTimeoutEvent
);
62 phraseChangeTimeoutEvent
= setTimeout(phraseChanged
, 400);
65 function phraseChanged() {
67 hideValidationError();
68 // Get the mnemonic phrase
69 var phrase
= DOM
.phrase
.val();
70 var errorText
= findPhraseErrors(phrase
);
72 showValidationError(errorText
);
75 // Get the derivation path
76 var errorText
= findDerivationPathErrors();
78 showValidationError(errorText
);
81 // Calculate and display
82 calcBip32Seed(phrase
, derivationPath
);
87 function generateClicked() {
90 setTimeout(function() {
91 var phrase
= generateRandomPhrase();
99 function tabClicked(e
) {
100 var activePath
= $(e
.target
.getAttribute("href") + " .path");
101 derivationPath
= activePath
.val();
105 function derivationChanged() {
106 delayedPhraseChanged();
109 function bip32Changed() {
110 derivationPath
= DOM
.bip32path
.val();
114 function bip44Changed() {
115 setBip44DerivationPath();
116 derivationPath
= DOM
.bip44path
.val();
120 function toggleIndexes() {
121 $("td.index span").toggleClass("invisible");
124 function toggleAddresses() {
125 $("td.address span").toggleClass("invisible");
128 function togglePrivateKeys() {
129 $("td.privkey span").toggleClass("invisible");
134 function generateRandomPhrase() {
135 if (!hasStrongRandom()) {
136 var errorText
= "This browser does not support strong randomness";
137 showValidationError(errorText
);
140 var numWords
= parseInt(DOM
.strength
.val());
141 // Check strength is an integer
142 if (isNaN(numWords
)) {
143 DOM
.strength
.val("12");
146 // Check strength is a multiple of 32, if not round it down
147 if (numWords
% 3 != 0) {
148 numWords
= Math
.floor(numWords
/ 3) * 3;
149 DOM
.strength
.val(numWords
);
151 // Check strength is at least 32
154 DOM
.strength
.val(numWords
);
156 var strength
= numWords
/ 3 * 32;
157 var words
= mnemonic
.generate(strength
);
158 DOM
.phrase
.val(words
);
162 function calcBip32Seed(phrase
, path
) {
163 var seed
= mnemonic
.toSeed(phrase
);
164 var seedHash
= Bitcoin
.crypto
.sha256(seed
).toString("hex");
165 bip32RootKey
= Bitcoin
.HDNode
.fromSeedHex(seedHash
, network
);
166 bip32ExtendedKey
= bip32RootKey
;
167 // Derive the key from the path
168 var pathBits
= path
.split("/");
169 for (var i
=0; i
<pathBits
.length
; i
++) {
170 var bit
= pathBits
[i
];
171 var index
= parseInt(bit
);
175 var hardened
= bit
[bit
.length
-1] == "'";
177 bip32ExtendedKey
= bip32ExtendedKey
.deriveHardened(index
);
180 bip32ExtendedKey
= bip32ExtendedKey
.derive(index
);
185 function showValidationError(errorText
) {
191 function hideValidationError() {
197 function findPhraseErrors(phrase
) {
198 // TODO make this right
199 // Preprocess the words
200 var parts
= phrase
.split(" ");
202 for (var i
=0; i
<parts
.length
; i
++) {
204 if (part
.length
> 0) {
205 // TODO check that lowercasing is always valid to do
206 proper
.push(part
.toLowerCase());
209 // TODO some levenstein on the words
210 var properPhrase
= proper
.join(' ');
211 // Check the words are valid
212 var isValid
= mnemonic
.check(properPhrase
);
214 return "Invalid mnemonic";
219 function findDerivationPathErrors(path
) {
224 function displayBip32Info() {
226 var rootKey
= bip32RootKey
.toBase58();
227 DOM
.rootKey
.val(rootKey
);
228 var extendedPrivKey
= bip32ExtendedKey
.toBase58();
229 DOM
.extendedPrivKey
.val(extendedPrivKey
);
230 var extendedPubKey
= bip32ExtendedKey
.toBase58(false);
231 DOM
.extendedPubKey
.val(extendedPubKey
);
232 // Display the addresses and privkeys
233 clearAddressesList();
234 displayAddresses(0, 20);
237 function displayAddresses(start
, total
) {
238 for (var i
=0; i
<total
; i
++) {
239 var index
= i
+ start
;
240 var key
= bip32ExtendedKey
.derive(index
);
241 var address
= key
.getAddress().toString();
242 var privkey
= key
.privKey
.toWIF();
243 addAddressToList(index
, address
, privkey
);
247 function showMore() {
248 var start
= DOM
.addresses
.children().length
;
249 var rowsToAdd
= parseInt(DOM
.rowsToAdd
.val());
250 if (isNaN(rowsToAdd
)) {
252 DOM
.rowsToAdd
.val("20");
254 if (rowsToAdd
> 200) {
255 var msg
= "Generating " + rowsToAdd
+ " rows could take a while. ";
256 msg
+= "Do you want to continue?";
262 setTimeout(function() {
263 displayAddresses(start
, rowsToAdd
);
268 function clearDisplay() {
269 clearAddressesList();
271 hideValidationError();
274 function clearAddressesList() {
275 DOM
.addresses
.empty();
278 function clearKey() {
280 DOM
.extendedPrivKey
.val("");
281 DOM
.extendedPubKey
.val("");
284 function addAddressToList(index
, address
, privkey
) {
285 var row
= $(addressRowTemplate
.html());
286 row
.find(".index span").text(index
);
287 row
.find(".address span").text(address
);
288 row
.find(".privkey span").text(privkey
);
289 DOM
.addresses
.append(row
);
292 function hasStrongRandom() {
293 return 'crypto' in window
&& window
['crypto'] !== null;
296 function disableForms() {
297 $("form").on("submit", function(e
) {
302 function setBip44DerivationPath() {
303 var purpose
= parseIntNoNaN(DOM
.bip44purpose
.val(), 44);
304 var coin
= parseIntNoNaN(DOM
.bip44coin
.val(), 0);
305 var account
= parseIntNoNaN(DOM
.bip44account
.val(), 0);
306 var change
= parseIntNoNaN(DOM
.bip44change
.val(), 0);
308 path
+= purpose
+ "'/";
310 path
+= account
+ "'/";
312 DOM
.bip44path
.val(path
);
315 function parseIntNoNaN(val
, defaultVal
) {
316 var v
= parseInt(val
);
323 function showPending() {
325 .text("Calculating...")
329 function hidePending() {