# All files / src keys.ts

88.24% Statements 15/17
100% Branches 0/0
100% Functions 4/4
88.24% Lines 15/17

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
    1x
    1x

    1x

    17x

    1x
    15x
    15x

    1x
    48x
    48x
    48x
    48x

    1x
    30x
    30x

    import { randomBytes, randomFillSync } from 'crypto'
    import { ECPair, address as baddress, crypto as bcrypto } from 'bitcoinjs-lib'

    /**
     *
     * @param numberOfBytes
     *
     * @ignore
     */
    export function getEntropy(arg: Buffer | number): Buffer {
      if (!arg) {
        arg = 32
      }
      if (typeof arg === 'number') {
        return randomBytes(arg)
      } else {
        return randomFillSync(arg)
      }
    }

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

    /**
    * @ignore
    */
    export function publicKeyToAddress(publicKey: string) {
      const publicKeyBuffer = Buffer.from(publicKey, 'hex')
      const publicKeyHash160 = bcrypto.hash160(publicKeyBuffer)
      const address = baddress.toBase58Check(publicKeyHash160, 0x00)
      return address
    }

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