<commit_before>/*
 * @fileOverview Stealth support.
 */
'use strict';

define(['util/stealth', 'bitcoinjs-lib'], function(Stealth, Bitcoin) {

  var bufToArray = function(obj) {return Array.prototype.slice.call(obj, 0);};

  // Data for the test
  var ephemKeyBytes = [95, 112, 167, 123, 50, 38, 10, 122, 50, 198, 34, 66, 56, 31, 186, 44, 244, 12, 14, 32, 158, 102, 90, 121, 89, 65, 142, 174, 79, 45, 162, 43, 1];
  var scanKeyBytes = [250, 99, 82, 30, 51, 62, 75, 159, 106, 152, 161, 66, 104, 13, 58, 239, 77, 142, 127, 121, 114, 60, 224, 4, 54, 145, 219, 85, 195, 107, 217, 5];
  var spendKeyBytes = [220, 193, 37, 11, 81, 192, 240, 58, 228, 233, 120, 224, 37, 110, 222, 81, 220, 17, 68, 227, 69, 201, 38, 38, 43, 151, 23, 177, 188, 201, 189, 27];

  var ephemKey = Bitcoin.ECKey.fromBytes(ephemKeyBytes, true);
  var scanKey = Bitcoin.ECKey.fromBytes(scanKeyBytes, true);
  var spendKey = Bitcoin.ECKey.fromBytes(spendKeyBytes, true);

  var ephemKeyPubBytes = ephemKey.pub.toBytes();
  var scanKeyPubBytes = scanKey.pub.toBytes();
  var spendKeyPubBytes = spendKey.pub.toBytes();

  // Address created from the above keys
  var testAddress = "<KEY>";

  describe('Stealth library', function() {
    it('imports a public component', function() {
      var Q = scanKeyPubBytes;
      var pubKey = Stealth.importPublic(Q);
      expect(pubKey.toBytes()).toEqual(Q)
    });
    
    it('performs curvedh and stealth formatting', function() {
      var e = scanKey.d;
      var decKey = spendKey.pub;
      Stealth.stealthDH(e, decKey);
    });
    
    it('formats a stealth address in base58', function() {
      var spendPubKeys = [spendKeyPubBytes];
      var formatted = Stealth.formatAddress(scanKeyPubBytes, spendPubKeys);

      expect(formatted).toEqual(testAddress);
    });
    
    it('parses a stealth address into its forming parts', function() {
      var stealthAddress = testAddress;
      var parsed = Stealth.parseAddress(stealthAddress);

      expect(parsed.options.reuseScan).toEqual(0);
      expect(parsed.scanKey).toEqual(scanKeyPubBytes);
      expect(parsed.spendKeys).toEqual([spendKeyPubBytes]);
      expect(parsed.sigs).toEqual(1);
      expect(parsed.prefix).toEqual([0]);
    });
    
    it('generates a key and related address to send to for a stealth address', function() {
      var scanBytes = scanKeyPubBytes;
      var spendBytes = spendKeyPubBytes;

      var res = Stealth.initiateStealth(scanBytes, spendBytes, undefined, ephemKeyBytes);
      var address = res[0];
      var ephemKey = res[1];

      expect(bufToArray(ephemKey)).toEqual(ephemKeyPubBytes)
      expect(address.toString()).toEqual("1Gvq8pSTRocNLDyf858o4PL3yhZm5qQDgB")

      // try with no ephemKeyBytes so it will be generated (normal case)
      res = Stealth.initiateStealth(scanBytes, spendBytes);
      expect(res.length).toBe(3);
    });

    it('uncovers the stealth secret', function() {
      var secret = "34053245118207293422371795779243906008263774607703744573712190846512548332868";
      var c = Stealth.uncoverStealth(scanKeyBytes, ephemKeyPubBytes);
      expect(c.toString()).toEqual(secret.toString());
    });

    it('generates an address for receiving for a spend key with the given ephemkey', function() {
      var scanSecret = scanKeyBytes;
      var ephemBytes = ephemKeyPubBytes;
      var spendBytes = spendKeyPubBytes;
      var keyBytes = Stealth.uncoverPublic(scanSecret, ephemBytes, spendBytes);

      expect(Array.prototype.slice.call(keyBytes, 0)).toEqual([3, 5, 246, 185, 154, 68, 162, 189, 236, 139, 72, 79, 252, 238, 86, 28, 249, 160, 195, 183, 234, 146, 234, 142, 99, 52, 230, 251, 196, 241, 193, 120, 153]);

      var keyHash = Bitcoin.crypto.hash160(keyBytes);
      var address = new Bitcoin.Address(keyHash, Bitcoin.networks.bitcoin.pubKeyHash);

      expect(address.toString()).toBe("1Gvq8pSTRocNLDyf858o4PL3yhZm5qQDgB");
    });
    
    it('derives a private key from spend key and shared secret', function() {
      var privKey = Stealth.uncoverPrivate(scanKeyBytes, ephemKeyPubBytes, spendKeyBytes);
      var keyBytes = privKey.pub.toBytes();
<commit_msg>No need to convert here.<commit_after>/*
 * @fileOverview Stealth support.
 */
'use strict';

define(['util/stealth', 'bitcoinjs-lib'], function(Stealth, Bitcoin) {

  var bufToArray = function(obj) {return Array.prototype.slice.call(obj, 0);};

  // Data for the test
  var ephemKeyBytes = [95, 112, 167, 123, 50, 38, 10, 122, 50, 198, 34, 66, 56, 31, 186, 44, 244, 12, 14, 32, 158, 102, 90, 121, 89, 65, 142, 174, 79, 45, 162, 43, 1];
  var scanKeyBytes = [250, 99, 82, 30, 51, 62, 75, 159, 106, 152, 161, 66, 104, 13, 58, 239, 77, 142, 127, 121, 114, 60, 224, 4, 54, 145, 219, 85, 195, 107, 217, 5];
  var spendKeyBytes = [220, 193, 37, 11, 81, 192, 240, 58, 228, 233, 120, 224, 37, 110, 222, 81, 220, 17, 68, 227, 69, 201, 38, 38, 43, 151, 23, 177, 188, 201, 189, 27];

  var ephemKey = Bitcoin.ECKey.fromBytes(ephemKeyBytes, true);
  var scanKey = Bitcoin.ECKey.fromBytes(scanKeyBytes, true);
  var spendKey = Bitcoin.ECKey.fromBytes(spendKeyBytes, true);

  var ephemKeyPubBytes = ephemKey.pub.toBytes();
  var scanKeyPubBytes = scanKey.pub.toBytes();
  var spendKeyPubBytes = spendKey.pub.toBytes();

  // Address created from the above keys
  var testAddress = "<KEY>";

  describe('Stealth library', function() {
    it('imports a public component', function() {
      var Q = scanKeyPubBytes;
      var pubKey = Stealth.importPublic(Q);
      expect(pubKey.toBytes()).toEqual(Q)
    });
    
    it('performs curvedh and stealth formatting', function() {
      var e = scanKey.d;
      var decKey = spendKey.pub;
      Stealth.stealthDH(e, decKey);
    });
    
    it('formats a stealth address in base58', function() {
      var spendPubKeys = [spendKeyPubBytes];
      var formatted = Stealth.formatAddress(scanKeyPubBytes, spendPubKeys);

      expect(formatted).toEqual(testAddress);
    });
    
    it('parses a stealth address into its forming parts', function() {
      var stealthAddress = testAddress;
      var parsed = Stealth.parseAddress(stealthAddress);

      expect(parsed.options.reuseScan).toEqual(0);
      expect(parsed.scanKey).toEqual(scanKeyPubBytes);
      expect(parsed.spendKeys).toEqual([spendKeyPubBytes]);
      expect(parsed.sigs).toEqual(1);
      expect(parsed.prefix).toEqual([0]);
    });
    
    it('generates a key and related address to send to for a stealth address', function() {
      var scanBytes = scanKeyPubBytes;
      var spendBytes = spendKeyPubBytes;

      var res = Stealth.initiateStealth(scanBytes, spendBytes, undefined, ephemKeyBytes);
      var address = res[0];
      var ephemKey = res[1];

      expect(ephemKey).toEqual(ephemKeyPubBytes)
      expect(address.toString()).toEqual("1Gvq8pSTRocNLDyf858o4PL3yhZm5qQDgB")

      // try with no ephemKeyBytes so it will be generated (normal case)
      res = Stealth.initiateStealth(scanBytes, spendBytes);
      expect(res.length).toBe(3);
    });

    it('uncovers the stealth secret', function() {
      var secret = "34053245118207293422371795779243906008263774607703744573712190846512548332868";
      var c = Stealth.uncoverStealth(scanKeyBytes, ephemKeyPubBytes);
      expect(c.toString()).toEqual(secret.toString());
    });

    it('generates an address for receiving for a spend key with the given ephemkey', function() {
      var scanSecret = scanKeyBytes;
      var ephemBytes = ephemKeyPubBytes;
      var spendBytes = spendKeyPubBytes;
      var keyBytes = Stealth.uncoverPublic(scanSecret, ephemBytes, spendBytes);

      expect(Array.prototype.slice.call(keyBytes, 0)).toEqual([3, 5, 246, 185, 154, 68, 162, 189, 236, 139, 72, 79, 252, 238, 86, 28, 249, 160, 195, 183, 234, 146, 234, 142, 99, 52, 230, 251, 196, 241, 193, 120, 153]);

      var keyHash = Bitcoin.crypto.hash160(keyBytes);
      var address = new Bitcoin.Address(keyHash, Bitcoin.networks.bitcoin.pubKeyHash);

      expect(address.toString()).toBe("1Gvq8pSTRocNLDyf858o4PL3yhZm5qQDgB");
    });
    
    it('derives a private key from spend key and shared secret', function() {
      var privKey = Stealth.uncoverPrivate(scanKeyBytes, ephemKeyPubBytes, spendKeyBytes);
      var keyBytes = privKey.pub.toBytes();

{'fraction_non_alphanumeric': 0.1318507716611982, 'fraction_numerical': 0.10523757443188723, 'mean_word_length': 3.7407834101382487, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2620635', 'n_tokens_mistral': 3501, 'n_tokens_neox': 2726, 'n_words': 742}