<commit_before>package org.bitcoins.core.crypto

import org.bitcoins.core.config.TestNet3
import org.bitcoins.core.util.{BitcoinJTestUtil, BitcoinSLogger, BitcoinSUtil, CryptoTestUtil}
import org.scalatest.{FlatSpec, MustMatchers}

/**
 * Created by chris on 3/7/16.
 */
class ECPrivateKeyTest extends FlatSpec with MustMatchers with BitcoinSLogger {

  "ECPrivateKey" must "have the same byte representation as a bitcoinj private key" in {
    val bitcoinjPrivateKey = CryptoTestUtil.bitcoinjPrivateKey.getPrivateKeyAsHex
    CryptoTestUtil.privateKey.hex must be (bitcoinjPrivateKey)
  }

  it must "derive the same public from a private key as bitcoinj" in {
    val bitcoinjPublicKeyBytes = CryptoTestUtil.bitcoinjPrivateKey.getPubKey
    CryptoTestUtil.privateKey.publicKey.hex must be (BitcoinSUtil.encodeHex(bitcoinjPublicKeyBytes))
  }

  it must "create a bitcoin-s private key from a bitcoinj private key, then convert to the same public key" in {
    val bitcoinjKey = new org.bitcoinj.core.ECKey()
    val bitcoinsPrivKey = ECPrivateKey(bitcoinjKey.getSecretBytes)
    val bitcoinsPublicKey = bitcoinsPrivKey.publicKey
    val bitcoinjPublicKey = bitcoinjKey.getPubKey

    bitcoinsPublicKey.bytes must be (bitcoinjPublicKey)
  }

  it must "create a bitcionj private key from a bitcoins private key and get the same public key" in {
    val bitcoinsPrivKey = ECPrivateKey.freshPrivateKey
    val bitcoinjPrivKey = org.bitcoinj.core.ECKey.fromPrivate(bitcoinsPrivKey.bytes.toArray)
    val bitcoinjPublicKey = bitcoinjPrivKey.getPubKey
    val bitcoinsPublicKey = bitcoinsPrivKey.publicKey

    bitcoinsPublicKey.bytes must be (bitcoinjPublicKey)
  }

  it must "create a private key from the dumped base58 in bitcoin-cli" in {
    val bitcoinjDumpedPrivateKey = CryptoTestUtil.bitcoinjDumpedPrivateKey
    val bitcoinjPrivateKey = bitcoinjDumpedPrivateKey.getKey
    val privateKey = ECPrivateKey.fromWIFToPrivateKey(CryptoTestUtil.privateKeyBase58)
    privateKey.hex must be (bitcoinjPrivateKey.getPrivateKeyAsHex)
  }

  it must "create a private key from a sequence of bytes that has the same byte representation of bitcoinj ECKeys" in {
    val bitcoinJKey = CryptoTestUtil.bitcoinjPrivateKey
    val privateKey : ECPrivateKey = ECPrivateKey(bitcoinJKey.getPrivKeyBytes)
    privateKey.hex must be (bitcoinJKey.getPrivateKeyAsHex)
  }

  it must "create a private key from its hex representation" in {
    val privateKeyHex = "<KEY>"
    val key: ECPrivateKey = ECPrivateKey(privateKeyHex)
    key.hex must be (privateKeyHex)
  }

  it must "determine if a private key corresponds to a compressed public key or not" in {
    val compressedKey = "<KEY>"
    val uncompressedKey = "<KEY>"
    ECPrivateKey.isCompressed(compressedKey) must be (true)
    ECPrivateKey.isCompressed(uncompressedKey) must be (false)
  }

  it must "create a fresh private key" in {
    ECPrivateKey() must not equal (ECPrivateKey())
  }


  it must "serialize a private key to WIF and then be able to deserialize it" in {
    val hex = "<KEY>"
    val privKey = ECPrivateKey(hex)
    require(privKey.isCompressed == true)
    val wif = privKey.toWIF(TestNet3)
    val privKeyFromWIF = ECPrivateKey.fromWIFToPrivateKey(wif)
    require(privKeyFromWIF.isCompressed == true)

    privKeyFromWIF must be (privKey)
  }

  it must "serialize a private key to WIF when the private key is prefixed with 0 bytes" in {
    val hex = "00fc391adf4d6063a16a2e38b14d2be10133c4dacd4348b49d23ee0ce5ff4f1701"
    val privKey = ECPrivateKey(hex)
    require(privKey.isCompressed == true)
    val wif = privKey.toWIF(TestNet3)
    require(ECPrivateKey.isCompressed(wif) == true)
    logger.debug("WIF:" + wif)
    val privKeyFromWIF = ECPrivateKey.fromWIFToPrivateKey(wif)
    privKeyFromWIF must be (privKey)
  }


  it must "correctly decode a private key from WIF" in {
    val privateKey = ECPrivateKey.fromWIFToPrivateKey("<KEY>")
    //derived hex on bitcore's playground
    privateKey.hex must be ("<KEY>")
  }

  it must "decode a WIF private key corresponding to uncompressed public key" in {
    val wif = "<KEY>"
    val privKey = ECPrivateKey.fromWIFToPrivateKey(wif)
    privKey.publicKey.hex must be ("<KEY>")
  }<commit_msg>fixing a few nits<commit_after>package org.bitcoins.core.crypto

import org.bitcoins.core.config.TestNet3
import org.bitcoins.core.util.{BitcoinJTestUtil, BitcoinSLogger, BitcoinSUtil, CryptoTestUtil}
import org.scalatest.{FlatSpec, MustMatchers}

/**
 * Created by chris on 3/7/16.
 */
class ECPrivateKeyTest extends FlatSpec with MustMatchers with BitcoinSLogger {

  "ECPrivateKey" must "have the same byte representation as a bitcoinj private key" in {
    val bitcoinjPrivateKey = CryptoTestUtil.bitcoinjPrivateKey.getPrivateKeyAsHex
    CryptoTestUtil.privateKey.hex must be (bitcoinjPrivateKey)
  }

  it must "derive the same public from a private key as bitcoinj" in {
    val bitcoinjPublicKeyBytes = CryptoTestUtil.bitcoinjPrivateKey.getPubKey
    CryptoTestUtil.privateKey.publicKey.hex must be (BitcoinSUtil.encodeHex(bitcoinjPublicKeyBytes))
  }

  it must "create a bitcoin-s private key from a bitcoinj private key, then convert to the same public key" in {
    val bitcoinjKey = new org.bitcoinj.core.ECKey()
    val bitcoinsPrivKey = ECPrivateKey(bitcoinjKey.getSecretBytes)
    val bitcoinsPublicKey = bitcoinsPrivKey.publicKey
    val bitcoinjPublicKey = bitcoinjKey.getPubKey

    bitcoinsPublicKey.bytes must be (bitcoinjPublicKey)
  }

  it must "create a bitcionj private key from a bitcoins private key and get the same public key" in {
    val bitcoinsPrivKey = ECPrivateKey.freshPrivateKey
    val bitcoinjPrivKey = org.bitcoinj.core.ECKey.fromPrivate(bitcoinsPrivKey.bytes.toArray)
    val bitcoinjPublicKey = bitcoinjPrivKey.getPubKey
    val bitcoinsPublicKey = bitcoinsPrivKey.publicKey

    bitcoinsPublicKey.bytes must be (bitcoinjPublicKey)
  }

  it must "create a private key from the dumped base58 in bitcoin-cli" in {
    val bitcoinjDumpedPrivateKey = CryptoTestUtil.bitcoinjDumpedPrivateKey
    val bitcoinjPrivateKey = bitcoinjDumpedPrivateKey.getKey
    val privateKey = ECPrivateKey.fromWIFToPrivateKey(CryptoTestUtil.privateKeyBase58)
    privateKey.hex must be (bitcoinjPrivateKey.getPrivateKeyAsHex)
  }

  it must "create a private key from a sequence of bytes that has the same byte representation of bitcoinj ECKeys" in {
    val bitcoinJKey = CryptoTestUtil.bitcoinjPrivateKey
    val privateKey : ECPrivateKey = ECPrivateKey(bitcoinJKey.getPrivKeyBytes)
    privateKey.hex must be (bitcoinJKey.getPrivateKeyAsHex)
  }

  it must "create a private key from its hex representation" in {
    val privateKeyHex = "<KEY>"
    val key: ECPrivateKey = ECPrivateKey(privateKeyHex)
    key.hex must be (privateKeyHex)
  }

  it must "determine if a private key corresponds to a compressed public key or not" in {
    val compressedKey = "<KEY>"
    val uncompressedKey = "<KEY>"
    ECPrivateKey.isCompressed(compressedKey) must be (true)
    ECPrivateKey.isCompressed(uncompressedKey) must be (false)
  }

  it must "create a fresh private key" in {
    ECPrivateKey() must not equal (ECPrivateKey())
  }


  it must "serialize a private key to WIF and then be able to deserialize it" in {
    val hex = "2cecbfb72f8d5146d7fe7e5a3f80402c6dd688652c332dff2e44618d2d3372"
    val privKey = ECPrivateKey(hex)
    require(privKey.isCompressed == true)
    val wif = privKey.toWIF(TestNet3)
    val privKeyFromWIF = ECPrivateKey.fromWIFToPrivateKey(wif)
    require(privKeyFromWIF.isCompressed == true)

    privKeyFromWIF must be (privKey)
  }

  it must "serialize a private key to WIF when the private key is prefixed with 0 bytes" in {
    val hex = "00fc391adf4d6063a16a2e38b14d2be10133c4dacd4348b49d23ee0ce5ff4f1701"
    val privKey = ECPrivateKey(hex)
    val wif = privKey.toWIF(TestNet3)
    val privKeyFromWIF = ECPrivateKey.fromWIFToPrivateKey(wif)
    privKeyFromWIF must be (privKey)
  }


  it must "correctly decode a private key from WIF" in {
    val privateKey = ECPrivateKey.fromWIFToPrivateKey("<KEY>")
    //derived hex on bitcore's playground
    privateKey.hex must be ("<KEY>")
  }

  it must "decode a WIF private key corresponding to uncompressed public key" in {
    val wif = "<KEY>"
    val privKey = ECPrivateKey.fromWIFToPrivateKey(wif)
    privKey.publicKey.hex must be ("<KEY>")
  }
{'fraction_non_alphanumeric': 0.07014818355640536, 'fraction_numerical': 0.016730401529636712, 'mean_word_length': 4.323791348600509, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4229203', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2685, 'n_words': 759}