Bitcoin Computer Library
A Javascript library for smart contracts on Bitcoin and Litecoin
website · docs
Prerequisites
You need to have node.js installed.
Use on a Server
Install
Run the commands below in an empty folder.
# Create packages.json file
npm init
# Install library
npm install @bitcoin-computer/lib
Write a Smart Contract
Create a file 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 a Bitcoin Computer wallet
const computer = new Computer({ mnemonic: 'drip audit speed belt gallery tribe bus poet used scrub view spike' })
// Deploy a smart contract and create a smart object
const counter = await computer.new(Counter)
// Update the smart object
await counter.inc()
// Log the smart object
console.log(counter)
Fund the Wallet
On Litecoin with testnet (as configured in the example above) you can use a Litecoin testnet faucet to fund the address of the computer
object:
mvbAVZJF68WASLbqteTToE45r69L53Q1vL
You can also run your own node. In this case you can refill the wallet from the command line on regtest.
npm run fund-ltc -- mvbAVZJF68WASLbqteTToE45r69L53Q1vL
Run the Smart Contract
node index.mjs
The expected output is:
Counter {
n: 1,
_id: '656...024:0',
_rev: '90f...73f:0',
_root: '656...024:0',
_amount: 7860,
_owners: ['037...954']
}
Use in a Browser
Write a Smart contract
Create a file index.mjs
.
import { Computer, Contract } from "https://unpkg.com/@bitcoin-computer/lib/dist/bc-lib.browser.min.mjs";
class Counter extends Contract {
constructor() {
super({ n: 0 })
}
inc() {
this.n += 1
}
}
const computer = new Computer({ mnemonic: 'drip audit speed belt gallery tribe bus poet used scrub view spike' })
const counter = await computer.new(Counter)
document.getElementById("count").innerHTML = counter.n
await counter.inc()
document.getElementById("count").innerHTML = counter.n
Make a Website
Create a file index.html
<html>
<body>
<script type="module" src="./index.mjs"></script>
Counter value: <span id='count'></span>
</body>
</html>
Start a Web Server
Run the following code in an empty directory and open http://localhost:8080.
# Create a packages.json file
npm init -y
# Install web server
npm i http-server
# Start web server
http-server
Fund Wallet
See above.
Use on Mainnet or Regtest
Install a Bitcoin Computer Node and run it with the command below:
# Start a node
npm run up -- -litecoin -regtest
When you call the Computer
constructor on the client, pass the url of your node to the url
parameter. Make sure that chain
and network
match your node's configuration.
// Connect computer object to node url
const computer = new Computer({
url: 'http://localhost:1031', // Bitcoin Computer Node node url
chain: 'LTC', // LTC or BTC
network: 'regtest', // regtest, testnet, or mainnet
mnemonic: 'drip audit speed belt gallery tribe bus poet used scrub view spike', // BIP 39 mnemonic sentence
})
Documentation
Have a look at the docs.
Getting Help
If you have any questions, please let us know on Telegram, Twitter, or by email clemens@bitcoincomputer.io.
Price
It is free to develop and test on testnet and regtest. On mainnet we charge a small fee to support the development:
- The fee for a constructor or function call is satoshis-per-byte * 475. This is about as much as the average transaction fee for a payment.
- The fee for deploying a module makes use of the segwit discount. It is satoshis-per-byte * data size * 1/4.
You can configure satoshis per byte. This fee is in addition to the mining fee.
Development Status
There are no known security vulnerabilities. However we do not yet recommend to use the Bitcoin Computer in production yet.
License
This software is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported license.
You are free to: share, copy, and redistribute the material in any medium or format for any purpose, even commercially under the following terms:
- Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- NoDerivatives — If you remix, transform, or build upon the material, you may not distribute the modified material.
This is a human-readable summary of (and not a substitute for) the license.