Q&A: Give me a javascript example for sending BCH.

Block answers a tech-support question about Give me a javascript example for sending BCH.

Question:

Give me a javascript example for sending BCH.


Answer:

Sending BCH from JavaScript

The recommended high-level library is minimal-slp-wallet (maintained by the Permissionless Software Foundation). It’s the Application Library layer of the Cash Stack — simpler than the low-level bch-js, works in both Node.js and browsers, and handles UTXO selection, fee calculation, change, and broadcasting for you.

Install

npm install minimal-slp-wallet --save

Basic example (send BCH)

const BchWallet = require('minimal-slp-wallet')

async function main () {
  // Create (or import) a wallet. Pass a mnemonic/WIF to import one,
  // or leave it undefined to generate a brand-new wallet.
  const bchWallet = new BchWallet(undefined, {
    interface: 'rest-api',
    restURL: 'https://api.fullstack.cash/v5/'   // Web2 infra (or use a Web3 consumer-api)
  })

  // Reach out to the blockchain to load balance/UTXOs
  await bchWallet.initialize()

  // One or more receivers. Amount is in satoshis (1 BCH = 100,000,000 sats)
  const receivers = [
    {
      address: 'bitcoincash:qp2rmj8heytjrksxm2xrjs0hncnvl08xwgkweawu9h',
      amountSat: 100000
    }
  ]

  const txid = await bchWallet.send(receivers)

  console.log(`TXID: ${txid}`)
  console.log(`https://explorer.bitcoin.com/bch/tx/${txid}`)
}

main()

wallet.send() returns the transaction ID, which you can look up on a block explorer. To pay multiple recipients, just add more objects to the receivers array.

Importing an existing wallet

// From a 12-word mnemonic
const bchWallet = new BchWallet(
  'minor bench until split suffer shine series bag avoid cruel orient aunt'
)

// Or from a private key in WIF format (starts with K or L)
const bchWallet2 = new BchWallet('L3BUek8oq1iijZTkfdRYo8RDxEe3PpB8MyJnh2FSGWAoCjAffQCp')

With error handling

try {
  const txid = await bchWallet.send([
    {
      address: 'bitcoincash:qrlhkg4d9z3y88j246a6482xzregxaxnfsagmd2kh3',
      amountSat: 1000
    }
  ])
} catch (err) {
  if (err.message && err.message.indexOf('Insufficient') > -1) {
    return alert('Insufficient balance on your BCH account.')
  }
  return alert('Error. Try again later.')
}

Key points

  • Amounts are always in satoshis. Fee is 250–550 sats typically; the dust limit is 546 sats per output.
  • Networks: point restURL at a rest-api endpoint (Web2, e.g., FullStack.cash) or a consumer-api endpoint (Web3, e.g., ipfs-bch-wallet-consumer).
  • Never send private keys/mnemonics to a server. Import and manage keys client-side.
  • Lower-level control: If you need to manually build/sign/broadcast transactions (custom scripts, exact UTXO control), use the underlying @psf/bch-js library directly. minimal-slp-wallet embeds bch-js internally, so you can still reach those low-level functions when needed.

Sources


Public Q&A mirror from the Block tech-support bot.


Write a comment