Q&A: Give me a javascript example for creating a key pay in BCH.
Question:
Give me a javascript example for creating a key pay in BCH.
Answer:
Assuming you mean a key pair (private + public key) for a BCH wallet — here are the two most common JavaScript approaches.
Option 1: bch-js (PSF Cash Stack)
bch-js is the PSF’s JavaScript library and sits at the Interface Library layer of the Cash Stack. It wraps bitcoinjs-lib, so it exposes an ECPair namespace.
const BCHJS = require('@psf/bch-js')
const bchjs = new BCHJS()
// 1. Generate a random secp256k1 key pair
const keyPair = bchjs.ECPair.makeRandom()
// 2. Export the private key as WIF (Wallet Import Format)
const wif = bchjs.ECPair.toWIF(keyPair)
// 3. Derive the public key + addresses
const cashAddress = bchjs.ECPair.toCashAddress(keyPair) // e.g. bitcoincash:q...
const legacyAddress = bchjs.ECPair.toLegacyAddress(keyPair)
const publicKey = keyPair.publicKey.toString('hex')
console.log({ wif, publicKey, cashAddress, legacyAddress })
Note: key generation is purely local — no network or blockchain access is required (source: digital-wallets.md, private-keys.md).
HD wallet variant (BIP39/BIP44)
For a recoverable wallet, derive from a mnemonic seed. BCH uses BIP44 coin type 145 (derivation path m/44'/145'/0'/0/0) — the SLP variant uses 245 (source: bch-js.md).
const mnemonic = bchjs.Mnemonic.generate(128) // 12 words
const rootSeed = await bchjs.Mnemonic.toSeed(mnemonic)
const masterHDNode = bchjs.HDNode.fromSeed(rootSeed)
const childNode = masterHDNode.derivePath("m/44'/145'/0'/0/0")
const wif = bchjs.HDNode.toWIF(childNode)
const cashAddress = bchjs.HDNode.toCashAddress(childNode)
Option 2: bitcoinjs-lib directly
const bitcoin = require('bitcoinjs-lib') // bitcoinjs-lib v6+/cjs variants vary
const ECPairFactory = require('ecpair').default
const ecc = require('tiny-secp256k1')
const ECPair = ECPairFactory(ecc)
const keyPair = ECPair.makeRandom() // random private key
const wif = keyPair.toWIF()
const publicKey = Buffer.from(keyPair.publicKey).toString('hex')
// P2PKH ("legacy" base58) address
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey })
For a CashAddr (q...) address from bitcoinjs-lib you’d need an extra conversion step (cashaddrjs), or just use bch-js’s toCashAddress().
Key facts to remember
- Keys are secp256k1: a private key is a random 256-bit value in
[1, n-1]; the public key isK = k × G(source:private-keys.md). - Public keys are compressed (33 bytes) or uncompressed (65 bytes) (source:
private-keys.md). - A P2PKH address =
RIPEMD-160(SHA-256(publicKey)), then Base58Check or CashAddr encoded (source:addresses.md). - Never expose a private key / WIF — anyone holding it controls the funds (source:
private-keys.md).
Working examples are collected at the psf-js-examples repo, and full API docs are at bchjs.fullstack.cash.
If by “key pay” you actually meant something else (e.g. the KeyPay payment/withdrawal service, or paying to a public key rather than a key hash), let me know and I’ll target the right example.
Public Q&A mirror from the Block tech-support bot.
Write a comment