# All files / src keys.ts

87.5% Statements 35/40
57.14% Branches 8/14
100% Functions 7/7
87.5% Lines 35/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    1x
    1x
    1x
    1x
    1x

    1x
    17x

    17x

    1x
    15x
    15x

    1x
    48x
    48x
    48x
    48x

    1x
    32x
    32x
    32x

    1x
    46x

    46x
    45x

    45x
    1x

    1x

    1x
    15x
    15x
    15x

    1x
    88x
    88x
    88x

    import { ECPair, address, networks } from 'bitcoinjs-lib'
    import { randomBytes } from './encryption/cryptoRandom'
    import { hashSha256Sync } from './encryption/sha2Hash'
    import { hashRipemd160 } from './encryption/hashRipemd160'
    import { config } from './config'

    /**
     *
     * @param numberOfBytes
     *
     * @ignore
     */
    export function getEntropy(arg: number): Buffer {
      Iif (!arg) {
        arg = 32
      }
      return randomBytes(arg)
    }

    /**
    * @ignore
    */
    export function makeECPrivateKey() {
      const keyPair = ECPair.makeRandom({ rng: getEntropy })
      return keyPair.privateKey.toString('hex')
    }

    /**
    * @ignore
    */
    export function publicKeyToAddress(publicKey: string | Buffer) {
      const publicKeyBuffer = Buffer.isBuffer(publicKey) ? publicKey : Buffer.from(publicKey, 'hex')
      const publicKeyHash160 = hashRipemd160(hashSha256Sync(publicKeyBuffer))
      const result = address.toBase58Check(publicKeyHash160, networks.bitcoin.pubKeyHash)
      return result
    }

    /**
    * @ignore
    */
    export function getPublicKeyFromPrivate(privateKey: string | Buffer) {
      const privateKeyBuffer = Buffer.isBuffer(privateKey) ? privateKey : Buffer.from(privateKey, 'hex')
      const keyPair = ECPair.fromPrivateKey(privateKeyBuffer)
      return keyPair.publicKey.toString('hex')
    }

    /**
     * Time
     * @private
     * @ignore
     */
    export function hexStringToECPair(skHex: string): ECPair.ECPairInterface {
      const ecPairOptions = {
        network: config.network.layer1,
        compressed: true
      }

      if (skHex.length === 66) {
        Iif (skHex.slice(64) !== '01') {
          throw new Error('Improperly formatted private-key hex string. 66-length hex usually '
                          + 'indicates compressed key, but last byte must be == 1')
        }
        return ECPair.fromPrivateKey(Buffer.from(skHex.slice(0, 64), 'hex'), ecPairOptions)
      } else Iif (skHex.length === 64) {
        ecPairOptions.compressed = false
        return ECPair.fromPrivateKey(Buffer.from(skHex, 'hex'), ecPairOptions)
      } else {
        throw new Error('Improperly formatted private-key hex string: length should be 64 or 66.')
      }
    }

    /**
     *
     * @ignore
     */
    export function ecPairToHexString(secretKey: ECPair.ECPairInterface) {
      const ecPointHex = secretKey.privateKey.toString('hex')
      Eif (secretKey.compressed) {
        return `${ecPointHex}01`
      } else {
        return ecPointHex
      }
    }

    /**
     * Creates a bitcoin address string from an ECPair
     * @private
     * @ignore
     */
    export function ecPairToAddress(keyPair: ECPair.ECPairInterface) {
      const sha256 = hashSha256Sync(keyPair.publicKey)
      const hash160 = hashRipemd160(sha256)
      return address.toBase58Check(hash160, keyPair.network.pubKeyHash)
    }