# Start

# Use in the Browser

Create the file below and open it in a browser. It show *, then 0 and finally 1.

index.html
<html>
  <head>
    <script type="module">
      import {
        Computer,
        Contract,
      } from 'https://unpkg.com/@bitcoin-computer/lib/dist/bc-lib.browser.min.mjs'

      // Define a smart contract
      class Counter extends Contract {
        constructor() {
          super({ n: 0 })
        }

        inc() {
          this.n += 1
        }
      }

      // Create and fund a wallet
      const computer = new Computer()
      await computer.faucet(1e5)

      // Create an on-chain object
      const counter = await computer.new(Counter)
      document.getElementById('count').innerHTML = counter.n

      // Update the on-chain object
      await counter.inc()
      document.getElementById('count').innerHTML = counter.n
    </script>
  </head>

  <body>
    <span id="count">*</span>
  </body>
</html>

# Use in Node.js

You need to have node.js installed. First download and install the Bitcoin Computer library from npm:

Terminal
# Create packages.json file
npm init

# Install library
npm install @bitcoin-computer/lib

Then create a file index.mjs.

index.mjs
import { Computer, Contract } from '@bitcoin-computer/lib'

// A smart contract
class Counter extends Contract {
  constructor() {
    super({ n: 0 })
  }

  inc() {
    this.n += 1
  }
}

// Create and fund a wallet
const computer = new Computer()
await computer.faucet(1e5)

// Create an on-chain object
const counter = await computer.new(Counter)

// Update the on-chain object
await counter.inc()

// Log the on-chain object
console.log(counter)

Execute the smart contract.

Terminal
node index.mjs

The expected output is:

Terminal
Counter {
  n: 1,
  _id: '656...024:0',
  _rev: '90f...73f:0',
  _root: '656...024:0',
  _amount: 7860,
  _owners: ['037...954']
}

# Run a Node

In the examples above you are using a Bitcoin Computer Node that we provide at rltc.node.bitcoincomputer.io configured to Litecoin Regtest. You can use this node for free but it is rate limited. For serious development we recommend to clone the monorepo so you can run your own, unlimited, node. To run a node we recommend to clone the monorepo with all Bitcoin Computer related materials.

# Clone and Install

# Clone
git clone https://github.com/bitcoin-computer/monorepo.git

# Install
cd monorepo
npm install

# Start the node

To start your node at http://localhost:1031 run the commands below. The node is ready once the log activity subsides. On regtest this will take a few minutes, on mainnet and testnet it can take hours or even days, depending on your hardware and network connection.

# Run the node
cd packages/node

# Copy the .env file and litecoin.conf file from the examples
cp chain-setup/ltc/regtest/.env.example .env
cp chain-setup/ltc/regtest/litecoin.conf.example litecoin.conf

# Run the node
npm run up

# Run the Tests

Once the node is up an running, open a separate terminal window and navigate the monorepo folder. You can run the following commands. The commands will be executed in each package. You can also navigate the a package and run the same scripts there.

# Test
npm run test

# Lint
npm run lint

# Types
npm run types

# Configure Client Side Library

To run the examples above with your own node, change the line that creates to Computer instance as below.

const computer = new Computer({
  chain: 'LTC',
  network: 'regtest',
  url: 'http://localhost:1031',
})

# Start A Project

We provide two templates, vite-template for client side projects and node-template for server side projects. Alternatively have a look at our example apps (e.g. our wallet, blockchain explorer, or nft app) to see if any of them are a good starting point for your project.

# Getting Help