[Code Snippet]Generate Bitcoin Address Using bitcoinjs-lib

ยท

1 min read

import * as bitcoin  from 'bitcoinjs-lib'
import ECPairFactory from 'ecpair';
import * as ecc from 'tiny-secp256k1';

// Generate a new random key pair

const ECPair =  ECPairFactory.ECPairFactory(ecc)
const keyPair = ECPair.makeRandom();
// // Get the private and public keys
const privateKey = keyPair.privateKey.toString('hex');
const publicKey = keyPair.publicKey.toString('hex');

 const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey });
// // Get the Bitcoin address associated with the public key
// const address = keyPair.getAddress();

console.log('Private Key:', privateKey);
console.log('Public Key:', publicKey);
console.log('Bitcoin Address:', address);