DEV Community

Swatantra goswami
Swatantra goswami

Posted on

BitCoin Chain Integration

  1. run node on server
  2. create a bitcoin core instance
const Client = require("bitcoin-core");
const client = new Client({
    network: "testnet",
    host: "116.202.196.59",
    port: "8332",
    username: "btcuser",
    password: "btcpassword",
    version: "0.18.1",
});
Enter fullscreen mode Exit fullscreen mode
  1. generate a new address
const getNewAddr = async () => {
    const newAddr = await client.getNewAddress();
    return newAddr;
};
Enter fullscreen mode Exit fullscreen mode
  1. get total balance
const getTotalBal = async () => {
  const balance = await client.getBalance("*", 0);
  return balance;
};
Enter fullscreen mode Exit fullscreen mode
  1. get trx details
const getTxnDetails = async (txId) => {
  const txn = await client.getTransaction(txId);
  return txn;
};
Enter fullscreen mode Exit fullscreen mode
  1. get blocks
const getBlock = async (blockHash) => {
  const block = await client.getBlock(blockHash);
  return block;
};
Enter fullscreen mode Exit fullscreen mode
  1. transfer bitcoin
const transfer = async (toAddr, amt) => {
  const txn = await client.sendToAddress(toAddr, amt);
  return txn;
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.