+ function isUsingOwnEntropy() {
+ return DOM.useEntropy.prop("checked");
+ }
+
+ function setMnemonicFromEntropy() {
+ hideEntropyError();
+ // Get entropy value
+ var entropyStr = DOM.entropy.val();
+ // Work out minimum base for entropy
+ var entropy = Entropy.fromString(entropyStr);
+ if (entropy.binaryStr.length == 0) {
+ return;
+ }
+ // Show entropy details
+ var extraBits = 32 - (entropy.binaryStr.length % 32);
+ var extraChars = Math.ceil(extraBits * Math.log(2) / Math.log(entropy.base.asInt));
+ var words = Math.floor(entropy.binaryStr.length / 32) * 3;
+ var strength = "an extremely weak";
+ if (words >= 3) {
+ strength = "a very weak";
+ }
+ if (words >= 6) {
+ strength = "a weak";
+ }
+ if (words >= 9) {
+ strength = "a strong";
+ }
+ if (words >= 12) {
+ strength = "a very strong";
+ }
+ if (words >= 15) {
+ strength = "an extremely strong";
+ }
+ if (words >= 18) {
+ strength = "an even stronger"
+ }
+ var msg = "Have " + entropy.binaryStr.length + " bits of entropy, " + extraChars + " more " + entropy.base.str + " chars required to generate " + strength + " mnemonic: " + entropy.cleanStr;
+ showEntropyError(msg);
+ // Discard trailing entropy
+ var bitsToUse = Math.floor(entropy.binaryStr.length / 32) * 32;
+ var binaryStr = entropy.binaryStr.substring(0, bitsToUse);
+ // Convert entropy string to numeric array
+ var entropyArr = [];
+ for (var i=0; i<binaryStr.length / 8; i++) {
+ var byteAsBits = binaryStr.substring(i*8, i*8+8);
+ var entropyByte = parseInt(byteAsBits, 2);
+ entropyArr.push(entropyByte)
+ }
+ // Convert entropy array to mnemonic
+ var phrase = mnemonic.toMnemonic(entropyArr);
+ // Set the mnemonic in the UI
+ DOM.phrase.val(phrase);
+ }
+
+ function hideEntropyError() {
+ DOM.entropyError.addClass("hidden");
+ }
+
+ function showEntropyError(msg) {
+ DOM.entropyError.text(msg);
+ DOM.entropyError.removeClass("hidden");
+ }
+