All files / src keys.ts

95.24% Statements 20/21
50% Branches 3/6
100% Functions 4/4
95.24% Lines 20/21

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  1x 1x 1x 1x               1x 17x     17x           1x 15x 15x           1x 48x 48x 48x     48x 48x           1x 32x 32x 32x    
 
import { ECPair, address as baddress, networks } from 'bitcoinjs-lib'
import { randomBytes } from './encryption/cryptoRandom'
import { createSha2Hash } from './encryption/sha2Hash'
import { createHashRipemd160 } from './encryption/hashRipemd160'
 
/**
 * 
 * @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 async function publicKeyToAddress(publicKey: string | Buffer) {
  const publicKeyBuffer = Buffer.isBuffer(publicKey) ? publicKey : Buffer.from(publicKey, 'hex')
  const sha2Hash = await createSha2Hash()
  const publicKeyHash160 = await createHashRipemd160().digest(
    await sha2Hash.digest(publicKeyBuffer)
  )
  const address = baddress.toBase58Check(publicKeyHash160, networks.bitcoin.pubKeyHash)
  return address
}
 
/**
* @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')
}