]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/commitdiff
Merge pull request #441 from bytefly/master
authoriancoleman <1281387+iancoleman@users.noreply.github.com>
Sun, 18 Oct 2020 22:12:43 +0000 (09:12 +1100)
committerGitHub <noreply@github.com>
Sun, 18 Oct 2020 22:12:43 +0000 (09:12 +1100)
Add TRX coin support

1  2 
src/js/index.js
tests/spec/tests.js

diff --combined src/js/index.js
index dc2496358c75b8f0bc5f8f8d3b3ead32a4f589b2,e959c58ea288de79e2ff65b8c06893e736c316b5..114f74ae0e0094d566443fa20b21d81ab1a14e8f
                          privkey = libs.ethUtil.bufferToHex(keyPair.d.toBuffer());
                      }
                  }
+                 //TRX is different
+                 if (networks[DOM.network.val()].name == "TRX - Tron") {
+                     keyPair = new libs.bitcoin.ECPair(keyPair.d, null, { network: network, compressed: false });
+                     var pubkeyBuffer = keyPair.getPublicKeyBuffer();
+                     var ethPubkey = libs.ethUtil.importPublic(pubkeyBuffer);
+                     var addressBuffer = libs.ethUtil.publicToAddress(ethPubkey);
+                     address = libs.bitcoin.address.toBase58Check(addressBuffer, 0x41);
+                     if (hasPrivkey) {
+                         privkey = keyPair.d.toBuffer().toString('hex');
+                     }
+                 }
  
                  // RSK values are different
                  if (networkIsRsk()) {
          var numberOfBits = entropy.binaryStr.length;
          var timeToCrack = "unknown";
          try {
 -            var z = libs.zxcvbn(entropy.base.parts.join(""));
 +            var z = libs.zxcvbn(entropy.base.events.join(""));
              timeToCrack = z.crack_times_display.offline_fast_hashing_1e10_per_second;
              if (z.feedback.warning != "") {
                  timeToCrack = timeToCrack + " - " + z.feedback.warning;
          DOM.entropyFiltered.html(entropy.cleanHtml);
          DOM.entropyType.text(entropyTypeStr);
          DOM.entropyCrackTime.text(timeToCrack);
 -        DOM.entropyEventCount.text(entropy.base.ints.length);
 +        DOM.entropyEventCount.text(entropy.base.events.length);
          DOM.entropyBits.text(numberOfBits);
          DOM.entropyWordCount.text(wordCount);
          DOM.entropyBinary.text(spacedBinaryStr);
              // Detect duplicates
              var dupes = [];
              var dupeTracker = {};
 -            for (var i=0; i<entropy.base.parts.length; i++) {
 -                var card = entropy.base.parts[i];
 +            for (var i=0; i<entropy.base.events.length; i++) {
 +                var card = entropy.base.events[i];
                  var cardUpper = card.toUpperCase();
                  if (cardUpper in dupeTracker) {
                      dupes.push(card);
                      || (name == "ESN - Ethersocial Network")
                      || (name == "VET - VeChain")
                      || (name == "ERE - EtherCore")
 +                    || (name == "BSC - Binance Smart Chain")
      }
  
      function networkIsRsk() {
                  setHdCoin(183);
              },
          },
 +        {
 +            name: "BSC - Binance Smart Chain",
 +            onSelect: function() {
 +                network = libs.bitcoin.networks.bitcoin;
 +                setHdCoin(60);
 +            },
 +        },
          {
              name: "BSV - BitcoinSV",
              onSelect: function() {
                  setHdCoin(159);
              },
          },
+         {
+             name: "TRX - Tron",
+             onSelect: function() {
+                 setHdCoin(195);
+             },
+         },
          {
              name: "TWINS - TWINS",
              onSelect: function() {
diff --combined tests/spec/tests.js
index 58318db6df5d9f3f5afca9a139342362bcb1a5e6,d73f17d70cc3bca854d9ba03a18e296f2555e91e..8b3b07be1c967bcf9da3e8cff5bc727673c0731f
@@@ -2275,17 -2275,18 +2275,28 @@@ it('Allows selection of Scribe', functi
      };
      testNetwork(done, params);
  });
 +it('Allows selection of Binance Smart Chain', function(done) {
 +    var params = {
 +        selectText: "BSC - Binance Smart Chain",
 +        phrase: "abandon abandon ability",
 +        firstAddress: "0xe5815d5902Ad612d49283DEdEc02100Bd44C2772",
 +        firstPubKey: "0x03e723e5b3aa7d72213f01139aa4783e1b34f74e1a04534e3fd8e29bfe2768af8a",
 +        firstPrivKey: "0x8f253078b73d7498302bb78c171b23ce7a8fb511987d2b2702b731638a4a15e7",
 +    };
 +    testNetwork(done, params);
 +});
  
+ it('Allows selection of TRX on Tron', function(done) {
+     var params = {
+         selectText: "TRX - Tron",
+         phrase: "abandon abandon ability",
+         firstAddress: "TA891Fu7vVz595BGQpNX2MCzr7yBcxuoC7",
+         firstPubKey: "0337bbb060e6166066f7f9e59e52f67bc23a6c9d0cbc815b82b6d89112444842e7",
+         firstPrivKey: "3a8fbd0379a815764979de86a3fcda759cb62d49e784e7b2a9a03206c90cfae2",
+     };
+     testNetwork(done, params);
+ });
  // BIP39 seed is set from phrase
  it('Sets the bip39 seed from the prhase', function(done) {
      driver.findElement(By.css('.phrase'))
@@@ -3130,7 -3131,7 +3141,7 @@@ it("Shows the number of bits of entrop
      testEntropyBits(done, "0000", "4");
  });
  it("Shows the number of bits of entropy for 1 character of base 6 (dice)", function(done) {
 -    // 6 in card is 0 in base 6, 0 in base 6 is 2.6 bits (rounded down to 2 bits)
 +    // 6 in card is 0 in base 6, 0 is mapped to 00 by entropy.js
      testEntropyBits(done, "6", "2");
  });
  it("Shows the number of bits of entropy for 1 character of base 10 with 3 bits", function(done) {
      testEntropyBits(done, "7", "3");
  });
  it("Shows the number of bits of entropy for 1 character of base 10 with 4 bis", function(done) {
 -    testEntropyBits(done, "8", "4");
 +    // 8 in base 10 is mapped to 0 by entropy.js
 +    testEntropyBits(done, "8", "1");
  });
  it("Shows the number of bits of entropy for 1 character of hex", function(done) {
      testEntropyBits(done, "F", "4");
  });
  it("Shows the number of bits of entropy for 2 characters of base 10", function(done) {
 -    testEntropyBits(done, "29", "6");
 +    // 2 as base 10 is binary 010, 9 is mapped to binary 1 by entropy.js
 +    testEntropyBits(done, "29", "4");
  });
  it("Shows the number of bits of entropy for 2 characters of hex", function(done) {
      testEntropyBits(done, "0A", "8");
@@@ -3171,17 -3170,17 +3182,17 @@@ it("Shows the number of bits of entrop
      testEntropyBits(done, "000A", "16");
  });
  it("Shows the number of bits of entropy for 4 characters of base 6", function(done) {
 -    testEntropyBits(done, "5555", "11");
 +    // 5 in base 6 is mapped to binary 1
 +    testEntropyBits(done, "5555", "4");
  });
  it("Shows the number of bits of entropy for 4 characters of base 6 dice", function(done) {
      // uses dice, so entropy is actually 0000 in base 6, which is 4 lots of
 -    // 2.58 bits, which is 10.32 bits (rounded down to 10 bits)
 -    testEntropyBits(done, "6666", "10");
 +    // binary 00
 +    testEntropyBits(done, "6666", "8");
  });
  it("Shows the number of bits of entropy for 4 charactes of base 10", function(done) {
 -    // Uses base 10, which is 4 lots of 3.32 bits, which is 13.3 bits (rounded
 -    // down to 13)
 -    testEntropyBits(done, "2227", "13");
 +    // 2 in base 10 is binary 010 and 7 is binary 111 so is 4 events of 3 bits
 +    testEntropyBits(done, "2227", "12");
  });
  it("Shows the number of bits of entropy for 4 characters of hex with 2 leading zeros", function(done) {
      testEntropyBits(done, "222F", "16");
@@@ -3190,16 -3189,13 +3201,16 @@@ it("Shows the number of bits of entrop
      testEntropyBits(done, "FFFF", "16");
  });
  it("Shows the number of bits of entropy for 10 characters of base 10", function(done) {
 -    // 10 events at 3.32 bits per event
 -    testEntropyBits(done, "0000101017", "33");
 +    // 10 events with 3 bits for each event
 +    testEntropyBits(done, "0000101017", "30");
 +});
 +it("Shows the number of bits of entropy for 10 characters of base 10 account for bias", function(done) {
 +    // 9 events with 3 bits per event and 1 event with 1 bit per event
 +    testEntropyBits(done, "0000101018", "28");
  });
  it("Shows the number of bits of entropy for a full deck of cards", function(done) {
 -    // cards are not replaced, so a full deck is not 52^52 entropy which is 296
 -    // bits, it's 52!, which is 225 bits
 -    testEntropyBits(done, "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", "225");
 +    // removing bias is 32*5 + 16*4 + 4*2
 +    testEntropyBits(done, "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", "232");
  });
  
  it("Shows details about the entered entropy", function(done) {
@@@ -3325,7 -3321,7 +3336,7 @@@ it("Shows details about the entered ent
              entropy: "7d",
              type: "card",
              events: "1",
 -            bits: "4",
 +            bits: "5",
              words: 0,
              strength: "less than a second",
          }
@@@ -3337,7 -3333,7 +3348,7 @@@ it("Shows details about the entered ent
              entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
              type: "card (full deck)",
              events: "52",
 -            bits: "225",
 +            bits: "232",
              words: 21,
              strength: "centuries",
          }
@@@ -3349,7 -3345,7 +3360,7 @@@ it("Shows details about the entered ent
              entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks3d",
              type: "card (full deck, 1 duplicate: 3d)",
              events: "53",
 -            bits: "254",
 +            bits: "237",
              words: 21,
              strength: "centuries",
          }
@@@ -3361,7 -3357,7 +3372,7 @@@ it("Shows details about the entered ent
              entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqs3d4d",
              type: "card (2 duplicates: 3d 4d, 1 missing: KS)",
              events: "53",
 -            bits: "254",
 +            bits: "240",
              words: 21,
              strength: "centuries",
          }
@@@ -3373,8 -3369,8 +3384,8 @@@ it("Shows details about the entered ent
              entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqs3d4d5d6d",
              type: "card (4 duplicates: 3d 4d 5d..., 1 missing: KS)",
              events: "55",
 -            bits: "264",
 -            words: 24,
 +            bits: "250",
 +            words: 21,
              strength: "centuries",
          }
      );
  it("Shows details about the entered entropy", function(done) {
      testEntropyFeedback(done,
          // Next test was throwing uncaught error in zxcvbn
 -        // Also tests 451 bits, ie Math.log2(52!)*2 = 225.58 * 2
          {
              entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsksac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
              type: "card (full deck, 52 duplicates: ac 2c 3c...)",
              events: "104",
 -            bits: "499",
 -            words: 45,
 +            bits: "464",
 +            words: 42,
              strength: "centuries",
          }
      );
@@@ -3399,7 -3396,7 +3410,7 @@@ it("Shows details about the entered ent
              entropy: "asAS",
              type: "card (1 duplicate: AS)",
              events: "2",
 -            bits: "9",
 +            bits: "8",
              words: 0,
              strength: "less than a second",
          }
@@@ -3411,7 -3408,7 +3422,7 @@@ it("Shows details about the entered ent
              entropy: "ASas",
              type: "card (1 duplicate: as)",
              events: "2",
 -            bits: "9",
 +            bits: "8",
              words: 0,
              strength: "less than a second",
          }
@@@ -3424,8 -3421,8 +3435,8 @@@ it("Shows details about the entered ent
              entropy: "ac2c3c4c5c6c7c8c  tcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
              type: "card (1 missing: 9C)",
              events: "51",
 -            bits: "221",
 -            words: 18,
 +            bits: "227",
 +            words: 21,
              strength: "centuries",
          }
      );
@@@ -3436,7 -3433,7 +3447,7 @@@ it("Shows details about the entered ent
              entropy: "ac2c3c4c5c6c7c8c  tcjcqckcad2d3d4d  6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
              type: "card (2 missing: 9C 5D)",
              events: "50",
 -            bits: "216",
 +            bits: "222",
              words: 18,
              strength: "centuries",
          }
@@@ -3448,7 -3445,7 +3459,7 @@@ it("Shows details about the entered ent
              entropy: "ac2c3c4c5c6c7c8c  tcjcqckcad2d3d4d  6d7d8d9dtdjd  kdah2h3h  5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
              type: "card (4 missing: 9C 5D QD...)",
              events: "48",
 -            bits: "208",
 +            bits: "212",
              words: 18,
              strength: "centuries",
          }
@@@ -3461,21 -3458,20 +3472,21 @@@ it("Shows details about the entered ent
              entropy: "ac2c3c4c5c6c7c8c  tcjcqckcad2d3d4d  6d  8d9d  jd  kdah2h3h  5h6h7h8h9hthjhqhkh  2s3s4s5s6s7s8s9stsjsqsks",
              type: "card",
              events: "45",
 -            bits: "195",
 +            bits: "198",
              words: 18,
              strength: "centuries",
          }
      );
  });
  it("Shows details about the entered entropy", function(done) {
 +    // multiple decks does not affect the bits per event
 +    // since the bits are hardcoded in entropy.js
      testEntropyFeedback(done,
 -        // Multiple decks of cards increases bits per event
          {
              entropy: "3d",
              events: "1",
 -            bits: "4",
 -            bitsPerEvent: "4.34",
 +            bits: "5",
 +            bitsPerEvent: "4.46",
          }
      );
  });
@@@ -3484,8 -3480,8 +3495,8 @@@ it("Shows details about the entered ent
          {
              entropy: "3d3d",
              events: "2",
 -            bits: "9",
 -            bitsPerEvent: "4.80",
 +            bits: "10",
 +            bitsPerEvent: "4.46",
          }
      );
  });
@@@ -3495,7 -3491,7 +3506,7 @@@ it("Shows details about the entered ent
              entropy: "3d3d3d",
              events: "3",
              bits: "15",
 -            bitsPerEvent: "5.01",
 +            bitsPerEvent: "4.46",
          }
      );
  });
@@@ -3505,7 -3501,7 +3516,7 @@@ it("Shows details about the entered ent
              entropy: "3d3d3d3d",
              events: "4",
              bits: "20",
 -            bitsPerEvent: "5.14",
 +            bitsPerEvent: "4.46",
          }
      );
  });
@@@ -3514,8 -3510,8 +3525,8 @@@ it("Shows details about the entered ent
          {
              entropy: "3d3d3d3d3d",
              events: "5",
 -            bits: "26",
 -            bitsPerEvent: "5.22",
 +            bits: "25",
 +            bitsPerEvent: "4.46",
          }
      );
  });
@@@ -3524,8 -3520,8 +3535,8 @@@ it("Shows details about the entered ent
          {
              entropy: "3d3d3d3d3d3d",
              events: "6",
 -            bits: "31",
 -            bitsPerEvent: "5.28",
 +            bits: "30",
 +            bitsPerEvent: "4.46",
          }
      );
  });
@@@ -3534,8 -3530,8 +3545,8 @@@ it("Shows details about the entered ent
          {
              entropy: "3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d",
              events: "33",
 -            bits: "184",
 -            bitsPerEvent: "5.59",
 +            bits: "165",
 +            bitsPerEvent: "4.46",
              strength: 'less than a second - Repeats like "abcabcabc" are only slightly harder to guess than "abc"',
          }
      );
@@@ -3586,11 -3582,10 +3597,11 @@@ it('Converts very long entropy to very 
  // https://bip32jp.github.io/english/index.html
  // NOTES:
  // Is incompatible with:
 +//     base 6
  //     base 20
  it('Is compatible with bip32jp.github.io', function(done) {
 -    var entropy  = "543210543210543210543210543210543210543210543210543210543210543210543210543210543210543210543210543";
 -    var expectedPhrase = "train then jungle barely whip fiber purpose puppy eagle cloud clump hospital robot brave balcony utility detect estate old green desk skill multiply virus";
 +    var entropy  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
 +    var expectedPhrase = "primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary foster";
      driver.findElement(By.css('.use-entropy'))
          .click();
      driver.findElement(By.css('.entropy'))