# JavaScript (in browser)

WARNING

Note: This site is work-in-progress and the mainnet library is currently in a prototype stage. Things may and will change randomly. There is no backward compatibility guarantee yet, even though we try not to break things too often. Use at your own risk. To see the old site with the full plan (yet to be implemented), go here (opens new window).

A working demo

You can see a fully working demo here (opens new window) and a video of it here (opens new window)

# Let's get programming

Note that this tutorial describes Browser + IndexedDB approach, which means that the wallets will be created and persisted inside of a user browser. See calling the REST API or Other programming languages for other approaches.

To get started using Bitcoin Cash on your site, include this tag in your <head> section:

<script src="https://cdn.mainnet.cash/mainnet-0.2.14.js"
 integrity="sha384-xCZt+GMgW7fyHrTbXqA76f1SlPs3RTwxgqHjasjjVIU1Un1MCTAEsLtNS4qUjmsW"
 crossorigin="anonymous"></script>

Note that the integrity part guarantees that the script haven't been tampered with. So if a hacker replaces it, the user's browser will not run the script. Or you can download the library and serve it locally.

Now, you can create a test wallet:

const wallet = await TestNetWallet.newRandom();

This wallet will not be persisted, if a user closes his browser, it's gone forever. See below for persistent wallets.

This creates a TestNet wallet.

What is TestNet? Where to get TestNet money and a wallet?

TestNet is where you test your application. TestNet money has no price. Opposite of TestNet is MainNet, which is what people usually mean when they talk about Bitcoin Cash network. You can get free TestNet money here (opens new window). If you need a wallet that supports the TestNet, download Electron Cash (opens new window) and run it using electron-cash --testnet flag. For example, on MacOS that would be:

/Applications/Electron-Cash.app/Contents/MacOS/Electron-Cash --testnet

To create a MainNet wallet (Bitcoin Cash production network):

const wallet = await Wallet.newRandom();

If you want to create a wallet from a mnemonic seed phrase, use this call:

const wallet = Wallet.fromSeed('.....');

TIP

Seed phrase wallets use the derivation path m/44'/0'/0'/0/0 by default (Bitcoin.com wallet compatibility)

Optionally, a BIP44 (opens new window) derivation path may be added as a second argument.

const wallet = Wallet.fromSeed("horse duck stapler...", "m/44'/1'/145'/0/0");

If you want to create a wallet from a WIF (private key), use this call:

const wallet = Wallet.fromWIF('.....');

Networks:

  • mainnet: Wallet
  • Testnet: TestNetWallet
  • RegTest: RegTestWallet (see below)

# Named wallets (persistent)

Named wallets are used to saved the private key generated by the browser, so that you can run await Wallet.named(`user`) and always get the same wallet back.

Note that it's better to run something like await Wallet.named(`user:${id}`), i.e. use some ID of the user, so that if the same browser has multiple users, they'll all get their own wallet. (Like, if a user has multiple accounts on your site)

To create a persistent wallet (saved to the IndexedDB of user's browser):

const wallet = await TestNetWallet.named('user:1234');

user:1234 is an optional name for the wallet. The wallet is saved in user's browser for future re-use.

# Getting the balance

To get the balance of the wallet you can do this:

await seller.getBalance('usd') // 0.00
await seller.getBalance('bch') // 0.00
await seller.getBalance('sat') // 0

You can ask for usd, sat, bch (or satoshi, satoshis, sats - just in case you forget the exact name).

  • 1 satoshi = 0.000 000 01 Bitcoin Cash
  • 1 Bitcoin Cash = 100,000,000 satoshis

USD returns the amount at the current exchange rate.

# Watch-only wallets

You can find out a balance of any cashaddr (say bchtest:qq1234567) like this:

const wallet = await TestNetWallet.watchOnly('bchtest:qq1234567');
await wallet.getBalance('usd');

# Sending money

Let's create another wallet and send some of our money there:

const seller = await TestNetWallet.named('seller');

const txData = await wallet.send([
    [seller.depositAddress(), 0.01, 'USD'],
]);

Note that you can send to many addresses at once.

Let's print the balance of the seller's wallet:

console.log(await seller.getBalance('USD'));
// 0.01

Great! You've just made your first transaction!

Now you can send all of your money somewhere else:

const txData = await seller.sendMax(wallet.depositAddress());

# Waiting for a transaction

# QR codes

Let's say you want to display a QR code for you user to pay you money and show an alert when money arrives?

Let's display a QR code first. Create a placeholder first:

<p style="text-align: center;">
    <img src="https://cdn.mainnet.cash/wait.svg" style="width: 15em;" id="deposit">
</p>

Then you can replace it with an actual QR code of the deposit address:

document.querySelector('#deposit').src = wallet.getDepositQr().src;

# Waiting for balance

You can wait for a certain minimal balance on the wallet using the waitForBalance function.

const balance = await wallet.waitForBalance(1.0, 'usd');

The balance variable contains the actual balance of the wallet.

# Waiting for transaction

You can wait for a wallet transaction and halt the program execution until it arrives.

const rawTransaction = await wallet.waitForTransaction();

The returned object follows this specification (opens new window)

If you are willing to spy on monitor transactions of an address you do not own, you can create a watchOnly wallet.

# Waiting for block

If you want to wait for the next block or wait for blockhain to reach certain block height you can use the following method of the wallet's network provider:

const nextBlockInfo = await wallet.provider.waitForBlock();

const futureBlockInfo = await wallet.provider.waitForBlock(770000);

The response object's schema (opens new window) is simple:

{
  height: number;
  hex: string;
}

# Simple Ledger Protocol (SLP)

We currently fully support the SLP type 1 tokens specification (opens new window)

The interfaces were designed to be largely similar to those of BCH wallets.

The slp functionality is available via Wallet.slp accessor:

const wallet = await TestNetWallet.fromId("testnet:wif:qq...")

const slpAddress = wallet.slp.getDepositAddress()

const qrCode = wallet.slp.getDepositQr()

Note, that working with slp tokens requires a certain amount of BCH available in your wallet so that you can pay miners for the slp transactions.

# Token creation - Genesis

To create your own token you should prepare and broadcast a special genesis transaction containing all the information about the token being created: token name, ticker, decimals - number of significant digits after comma, initial token amount, url you want to associate with token. Some of these properties are optional.

With the endBaton parameter you can decide to keep the possibility of additional token creation, which is governed by so called minting baton or immediately discard this baton to make the token circulation amount to be fixed.

The transaction id in which the token is created will become its permanent and unique identifier.

Note, that there might be many tokens with the same name. Remember, that only 64 character long string-ids do identify your token uniquely und unambiguously.

In the following example 10000.00 MNC tokens will be created.

const genesisOptions = {
  name: "Mainnet coin",
  ticker: "MNC",
  decimals: 2,
  initialAmount: 10000,
  documentUrl: "https://mainnet.cash",
  endBaton: false
};
const {tokenId} =  await wallet.slp.genesis(genesisOptions)

# Looking up token information

If you want to get the genesis information of some token, you can simply call getTokenInfo:

const info = await wallet.slp.getTokenInfo(tokenId);

# Additional token creation - Minting

If you decide to increase the token circulation supply, you would need to mint more tokens. You are required to have the ownership of the minting baton to do so. Similarly to genesis, you can keep the baton or discard it.

In the following example we issue 50 more tokens we just created in genesis:

const {txId, balance} = await wallet.slp.mint(50, tokenId)

# Sending tokens

Sending tokens around is easy and is very similar to sending BCH. You can include many send requests in one call too!

const {txId, balance} = await wallet.slp.send([
  {
    slpaddr: bobWallet.slp.slpaddr,
    value: 5,
    tokenId: tokenId
  }
]);

Or you can send all tokens available with a simple sendMax method

const result = await wallet.slp.sendMax(otherWallet.slp.slpaddr, tokenId);

Note, you can not send several different tokens in one go.

# Token balances

You can get all token balances of your wallet or a balance of a specific token with the following methods:

const tokenBalance = wallet.slp.getBalance(tokenId);
const allBalances = wallet.slp.getAllBalances();

# SLP address UTXOs

If you want to get the information about SLP UTXOs of an address, look up the locked satoshi values, etc., you can do the following call:

const utxos = wallet.slp.getFormattedSlpUtxos();

# Watching/waiting for transactions

You can set up a hook to monitor SLP transactions with the following:

const cancelFn = wallet.slp.watchTransactions((tx) => {
  ...
}, tokenId);

Leave out the tokenId to monitor all token transactions.

Call the cancelFn() to unsubscribe from receiving callback calls.

If you want just to wait for the next transaction you can use the following call:

const tx = await wallet.slp.waitForTransaction(tokenId);

This will halt the program execution until the transaction arrives.

# Watching/waiting for balance

Similarly a to transaction watching/waiting we provide the convenient methods to watch/wait for balance.

const cancelFn = wallet.slp.watchBalance((balance) => {
  ...
}, tokenId);

You can wait for the slp wallet to reach a certain minimal token balance:

const actualBalance = await wallet.slp.waitForBalance(10, tokenId);

This will halt the program execution until the balance reaches the target value.

# TestNet faucet

You can have some TestNet satoshi or SLP tokens for your convenience. Visit our faucet refilling station at https://rest-unstable.mainnet.cash/faucet.html (opens new window)

Your address will be refilled up to 10000 TestNet satoshi or up to 10 SLP tokens upon a call. There are request rate limiters set up to prevent abuse.

We've integrated the faucet into the library so that you can do easy calls like the following:

const txid = await wallet.getTestnetSatoshis();
...
const sendResponse = await wallet.returnTestnetSatoshis();

Same for SLP:

const txid = await wallet.getTestnetSlp(tokenId);
...
const slpSendResponse = await wallet.returnTestnetSlp(tokenId);

# Escrow contracts

WARNING

Not fully tested yet

Ok, let's now assume that you are building a service where you want to connect a buyer and a seller (a freelance marketplace or a non-custodial exchange), but at the same time you don't want to hold anyone's money, but only act as an arbiter in case something goes wrong. It's possible in Bitcoin Cash and it's called "an escrow contract".

You'll need three addresses for this: buyer, seller and arbiter.

Note: The source for the contract is here (opens new window).

  1. Buyer sends the money to the contract and could then release the money to the seller
  2. The seller could refund the buyer
  3. Arbiter can either complete the transaction to the seller or refund to the buyer, but cannot steal the money
let escrow = new EscrowContract({
  arbiterAddr: arbiter.getDepositAddress(),
  buyerAddr: buyer.getDepositAddress(),
  sellerAddr: seller.getDepositAddress(),
});

You can now send money to the contract:

await buyer.send([ [ escrow.getAddress(), 3700, "satoshis" ], ]);

Check the balance of the contract (in satoshis):

await escrow.getBalance()
// 3700

Note: Escrow contract is big (in bytes) and requires a big fee, so the minimum what you can send to it is about 3700 satoshis.

Now, we can execute the necessary functions:

  1. Buyer releases the funds
await escrow.run(buyer.privateKeyWif, "spend");
  1. Seller refunds
await escrow.run(seller.privateKeyWif, "refund");
  1. Arbiter releases the funds or refunds
await escrow.run(arbiter.privateKeyWif, "spend");
await escrow.run(arbiter.privateKeyWif, "refund");

# Saving the contract to the database

The arbiter needs to save the contract somewhere (in a database or some other storage), so that when the time comes, he could execute the necessary functions:

Save:

const contractId = escrow.toString();

Restore it later:

const restoredEscrow = EscrowContract.fromId(contractId);

# CashScript

Somewhat done, but not yet documented...

# Utilities

# Currency conversions

Need to find out how many BCH are there currently in 1 USD, or find out how many satoshis are there in 100 USD? Easy!

await convert(100, "usd", "sat")
// 28067024

filename

# RegTest wallets

During the local development and testing, you might not want to use TestNet coins, so you can use so-called "RegTest wallets".

What is RegTest?

RegTest is a mode, in which you can run your Bitcoin Cash node locally, and you can get as many test coins as you need, but they exist on your machine only. RegTest wallets are supported by the mainnet library.

A full Bitcoin node, an Electrum server and open Postgres server configuration is available for testing in a Docker Compose file at jest/regtest-docker-compose.yml

It can be brought up with:

./jest/docker/start.sh 

To stop it:

./jest/docker/stop.sh

The Electrum server (Fulcrum) is available at ws://127.0.0.1:60003 on your local machine.
The regtest BCHN node is on port 18443 available with RPC using credentials in .env.regtest. An open Postgres server is also available on port 15432

To use this wallet from your code:

const wallet = await RegTestWallet.newRandom();

# WebSockets

We provide some functionality over websockets where traditional REST servers would timeout. Examples are waiting for transactions and watching balances . Websockets allow to subscribe to server events, sending responses and notifications asynchronously.

Check out the jsfiddle demo (opens new window)

Websockets are supported by all major browsers and using them is easy, no external libraries are needed:

let socket = new WebSocket("wss://rest-unstable.mainnet.cash/api/v1/wallet");
socket.onopen = (event) => {
  const request = {method: "watchBalance", data: {cashaddr: address}};
  socket.send(JSON.stringify(request));
};

socket.onmessage = (event) => {
  const transaction = JSON.parse(event.data);
  // do something
};

The output of this code snippet will look like this:

{
  bch: 0.00009383,
  sat: 9383,
  usd: 0.029358468699999998
}

See websocket API reference

# Changing Electrum Servers

By default, creating a new wallet will use a common connection to a single server communicating with the electrum cash protocol (opens new window).

These connections are stored on globalThis under a variable matching ticker for the network (BCH, tBCH, rBCH).

If you need to create a new connection manually, it can be done by passing the network and servers, where servers is either a single url or an array of urls.

let conn = new Connection(
  "mainnet",
  "wss://bch.imaginary.cash:50004" 
  )
await conn.networkProvider.getBlockHeight()
// 669347

This connection can be used to replace the common provider on glboalThis.BCH or assigned to a particular wallet by overwriting the provider object of the wallet.