# All files / src keys.js

90.91% Statements 10/11
50% Branches 1/2
100% Functions 4/4
90.91% Lines 10/11

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

    15x

    15x

    13x
    13x

    47x
    47x
    47x
    47x

    28x
    28x

     /* @flow */
    import { randomBytes } from 'crypto'
    import { ECPair, address as baddress, crypto as bcrypto } from 'bitcoinjs-lib'

    export function getEntropy(numberOfBytes: number) {
      Iif (!numberOfBytes) {
        numberOfBytes = 32
      }
      return randomBytes(numberOfBytes)
    }

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

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

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