How to Create Bitcoin Transactions with Javascript

Claudio Atilano
4 min readJan 28, 2019

Bitcoin documentation can be harsh if you’re just getting started and you’re curious about Bitcoin. There are several Javascript libraries that allow you to interact with the Bitcoin blockchain. Let’s begin by defining some terms:

Brief Origin Story

Me on the right at the Miami Bitcoin Hackathon 2019

I recently experienced the problem while I was hacking a Bitcoin project at the Miami Bitcoin Hackathon. Other fellow hackers were frustrated because they couldn’t create a Bitcoin transaction using Javascript. They tried to use bitcoinjs-lib, a Javascript library for Bitcoin, but then were worrying about downloading the entire Bitcoin blockchain to run a node just to create a Bitcoin project. This process was so tedious, some hackers didn’t even incorporate Bitcoin to their Bitcoin projects!

I needed a Bitcoin node to create a project that could potentially win (didn’t win by the way but I got an npm package out of going). That’s when I discovered bitcore-explorers and saw a way to interact with Bitcoin without downloading over 200 GB of the Bitcoin blockchain. I could focus on creating transactions and start working on the user experience. After seeing their Github documentation and looking over their npm package, I realized it was not clear and up to date. So I decided to add appropriate documentation for the package and actually get the updates up on npm. You can find the package here:

The Bitcoin Transaction

First, to make a Bitcoin transaction you need to:

  • Connect to the Bitcoin network with a full node
  • Create a private key
  • Add funds to the wallet
  • Get the address you’re going to send it to.

We will be using Bitcore to connect to the Bitcoin network.

Bitcore is a full bitcoin node — your apps run directly on the peer-to-peer network. For wallet application development, additional indexes have been added into Bitcoin for querying address balances, transaction history, and unspent outputs.

I highly suggest that you use bitcore-insight to get started with Bitcore and for testing purposes only. Since bitcore-insightrelies on BitPay’s full nodes you shouldn’t run anything on production using their nodes unless you want to trust a third party with your Bitcoin connectivity.

Let’s get started with a simple Bitcoin transaction. First download bitcore-insight.

npm install bitcore-insight --save

Then you will need to create your private key using bitcore-lib. What’s great about bitcore-insight is that when you install it, it brings bitcore-lib.

Create Private Key

You can create a private key in three ways using bitcore-lib.

Randomly

const bitcore = require('bitcore-lib');
const Insight = require('bitcore-insight').Insight;
let insight = new Insight('testnet');const privateKey = new bitcore.PrivateKey();

From a SHA256 hash

const bitcore = require('bitcore-lib');
const Insight = require('bitcore-insight').Insight;
let insight = new Insight('testnet');let value = Buffer.from('cat horse shoe lightning awesome bitcoin');
let hash = bitcore.crypto.Hash.sha256(value);
let bn = bitcore.crypto.BN.fromBuffer(hash);
const privateKey = new bitcore.PrivateKey(bn);

Importing through Wallet Import Format (WIF)

const bitcore = require('bitcore-lib')
const Insight = require('bitcore-insight').Insight;
let insight = new Insight('testnet');const wif = 'xBtatQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct';
const privateKey = new bitcore.PrivateKey(wif);

Getting Testnet Bitcoin

Once you have a private key you can generate a Bitcoin address for your private key by just adding:

const myAddress = privateKey.toAddress();

Then go to a Bitcoin testnet faucet and enter your Bitcoin address for your private key. I used this one:

Now go take a quick break and wait for your transaction to be processed and then you should have some testnet Bitcoin! If only getting real Bitcoins was that easy.

Sending Bitcoin

const addressTo = 'moCEHE5fJgb6yHtF9eLNnS52UQVUkHjnNm';

Now lets send it to a random address. You could send it to my testnet wallet for the sake of the example. Then we should be all set to send some Bitcoin.

To create your Bitcoin transaction you need to get the UTXOs of your Bitcoin address. We do that with the insight.getUtxos(address, callback) function. The callback will return you the UTXO object that you can add to your bitcore.Transaction() object. Then once you have figured out how much you want to send and how much of a fee you want to place on your transaction, you can broadcast your transaction. The amounts that you place on your transaction must be in Satoshis, the smallest unit of Bitcoin.

1 BTC = 100,000,000 Satoshis

Broadcast your transaction to the Bitcoin test network by using insight.broadcast(transaction, callback) and the callback will return you the transaction id that you can lookup on a Bitcoin block explorer.

Your final code should be like the one below.

If there was any part of this that confused you feel free to respond for clarity and be sure to read up more on Bitcoin!

Bitcoin Whitepaper

--

--

Claudio Atilano

Creator of the Primal Trading Card Game | Front-end Software Engineer