]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/commitdiff
Card duplicates and use of full deck is detected
authorIan Coleman <coleman.ian@gmail.com>
Wed, 16 Nov 2016 00:46:25 +0000 (11:46 +1100)
committerIan Coleman <coleman.ian@gmail.com>
Wed, 16 Nov 2016 00:58:26 +0000 (11:58 +1100)
src/js/index.js
tests.js

index 89f8d0dd006053d7f86233f74d313fce00127680..e064185f411124f18f78316ca72114b5fc24898d 100644 (file)
         }
         var bitsStr = getNumberOfEntropyBits(entropy);
         var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3;
+        var entropyTypeStr = getEntropyTypeStr(entropy);
         DOM.entropyFiltered.html(entropy.cleanHtml);
-        DOM.entropyType.text(entropy.base.str);
+        DOM.entropyType.text(entropyTypeStr);
         DOM.entropyStrength.text(strength);
         DOM.entropyEventCount.text(entropy.base.ints.length);
         DOM.entropyBits.text(bitsStr);
         return bitsStr
     }
 
+    function getEntropyTypeStr(entropy) {
+        var typeStr = entropy.base.str;
+        // Add some detail if these are cards
+        if (entropy.base.asInt == 52) {
+            var cardDetail = []; // array of message strings
+            // Detect duplicates
+            var dupes = [];
+            var dupeTracker = {};
+            for (var i=0; i<entropy.base.parts.length; i++) {
+                var card = entropy.base.parts[i];
+                if (card in dupeTracker) {
+                    dupes.push(card);
+                }
+                dupeTracker[card] = true;
+            }
+            if (dupes.length > 0) {
+                var dupeWord = "duplicates";
+                if (dupes.length == 1) {
+                    dupeWord = "duplicate";
+                }
+                var msg = dupes.length + " " + dupeWord + ": " + dupes.slice(0,3).join(" ");
+                if (dupes.length > 3) {
+                    msg += "...";
+                }
+                cardDetail.push(msg);
+            }
+            // Detect full deck
+            var uniqueCards = [];
+            for (var uniqueCard in dupeTracker) {
+                uniqueCards.push(uniqueCard);
+            }
+            if (uniqueCards.length == 52) {
+                cardDetail.unshift("full deck");
+            }
+            // Add card details to typeStr
+            if (cardDetail.length > 0) {
+                typeStr += " (" + cardDetail.join(", ") + ")";
+            }
+        }
+        return typeStr;
+    }
+
     // Depends on BigInteger
     function factorial(n) {
         if (n == 0) {
index 9760d936c885f463d545c1fd9544b8fa3fa61f72..b43d1a15edd8b772f6e9b407e0d62e2763b8d3f0 100644 (file)
--- a/tests.js
+++ b/tests.js
@@ -2624,6 +2624,46 @@ page.open(url, function(status) {
             words: 18,
             strength: "extremely strong",
         },
+        {
+            entropy: "7d",
+            type: "card",
+            events: 1,
+            bits: 5,
+            words: 0,
+            strength: "extremely weak",
+        },
+        {
+            entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
+            type: "card (full deck)",
+            events: 52,
+            bits: 226,
+            words: 27,
+            strength: "extremely strong",
+        },
+        {
+            entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks3d",
+            type: "card (full deck, 1 duplicate: 3d)",
+            events: 53,
+            bits: 226,
+            words: 27,
+            strength: "extremely strong",
+        },
+        {
+            entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqs3d4d",
+            type: "card (2 duplicates: 3d 4d)",
+            events: 53,
+            bits: 226,
+            words: 27,
+            strength: "extremely strong",
+        },
+        {
+            entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqs3d4d5d6d",
+            type: "card (4 duplicates: 3d 4d 5d...)",
+            events: 53,
+            bits: 226,
+            words: 27,
+            strength: "extremely strong",
+        },
     ];
     // use entropy
     page.evaluate(function() {
@@ -2631,7 +2671,7 @@ page.open(url, function(status) {
     });
     var nextTest = function runNextTest(i) {
         function getFeedbackError(expected, actual) {
-            if (actual.indexOf(expected.filtered) == -1) {
+            if ("filtered" in expected && actual.indexOf(expected.filtered) == -1) {
                 return "Filtered value not in feedback";
             }
             if (actual.indexOf(expected.type) == -1) {