The constructor of the Computer class takes one argument as shown below.
const computer = new Computer({seed, // a bip39 mnemonicchain, // 'BSV' or 'BCH'network, // 'livenet' or 'testnet'wocApiKey, // a woc api key (optional)path = "m/44'/0'/0'/0", // a bip 32 path})
The constructor returns an objectcomputer
that contains a sub-object computer.db
of class Db
and a sub-object computer.db.wallet
of class Wallet
. In this section we explain the functions of the Computer
class and we explain the Db
and Wallet
class in the following sections.
The new()
method creates new smart objects. It has two parameters, a class and a list of arguments. The arguments can be basic data types or smart objects.
class A {constructor(n) {this.n = n}}​const a1 = await computer.new(A, [1])
We encode an output as a string of the form <transaction-id>:<output-number>. We refer to an output encoded in this form as a location.
The new()
method returns a smart object generated from the class and the arguments. It broadcasts a transaction that records the creation of the smart object. The smart object has the methods and properties defined in the class, and four extra properties:
_id
: the location where the object is deployed. The id remains fixed throughout the lifecycle of the object.
_rev
: the location where the object is currently stored. Initially _id
and _rev
are identical. When a method of the smart object is called its new state is stored at a new location which is stored in the _rev
property.
_owners
: an array containing string encoded public keys. A function can only be called on a smart object if it was created by a computer instance that contains a private key corresponding to a public keys in the list of owners.
_amount
: a number encoding the number of satoshis stored in the smart object.
The properties _id
and _rev
are read-only but the properties _owners
and _amount
can be assigned in constructor and function calls.
The sync()
method returns the smart object stored at a given revision.
const revision = a1._revconst a2 = await computer.sync(revision)
After running the code above a1
and a2
will be distinct objects with identical values.
The getRevs()
method returns an array containing the latest revisions owned by a given public key. The public keys are string encoded. If no parameter is passed to getRevs()
the public key of the computer
object is used.
const publicKey = '03223d34686d6f19d20519156a030f7216e5d5bd6daa9442572bbaa446d06c8dfe'const revs = await computer.getRevs(publicKey)
The getLatestRev()
method inputs an id and returns the latest revision of the smart object with that id. If no smart object with that id exists an error is thrown.
const id = a1._idconst rev = await computer.getLatestRev(id)
After running the above code rev
will be equal to a._rev
if a1
is a smart object.
The recommended way to create an instance of the Db
class is to create an object of the Computer
class and to access its property computer.db
.
const computer = new Computer({ seed, chain, network })const { db } = computer
The put()
method inputs an array of JSON objects and stores them in a transaction. Each element of the array is stored in a separate output. The method returns the array of locations of the outputs created.
const data = [{a: 1}, {b: { c: 2 }}]const locs = await computer.db.put(data)// locs === ['0322...8dfe:0', '0322...8dfe:1']
The get()
method returns the JSON objects stored at a given array of locations.
const locs = await computer.db.put(data)const fromChain = await computer.db.get(locs)// fromChain === data
The update()
method has two parameters: a list of locations and a list of JSON objects. It broadcasts a transaction that spends the locations and that has one output for each JSON object. The output script of each output is the concatenation of a multisig script and a OP_PUSH that adds the JSON followed by OP_DROP.
const locs1 = await computer.db.put([{ n: 1 }])const locs2 = await computer.db.update(locs, [{ n: 2 }])const fromChain = await computer.db.get(locs2)// fromChain === [{n: 2}]
You can use db.get()
to inspect the smart contract protocol. Try to call db.get()
with the ids or rev of a smart object to see the data on the blockchain. Remember to pass the id inside an array likedb.get([a._id]).
We recommend creating a Wallet
instance by creating a Computer
instance and accessing the nested wallet
property.
const { db } = new Computer({ seed, chain, network })const { wallet } = db
The wallet built into Bitcoin Computer is compatible with the widely used Bitcore library. This makes it easy to integrate Bitcoin Computer into existing apps.
The getMnemonic()
method returns a Bitcore compatible Javascript object encoding a Mnemonic. To return the mnemonic string call the toString()
method.
const mnemonic = wallet.getMnemonic()const mnemonicString = mnemonic.toString()
The getPublicKey()
method returns a Bitcore compatible Javascript object encoding a public key. To return the public key in string encoding call the toString()
method.
const publicKey = wallet.getPublicKey()const publicKeyString = publicKey.toString()
The getPublicKey()
method returns a Bitcore compatible Javascript object encoding a Bitcoin address. To return the address in string encoding call the toString()
method.
const address = wallet.getAddress()const addressString = address.toString()
The getBalance()
method returns the current balance in satoshi.
const balance = await wallet.getBalance()
The send()
method sends an amount of satoshi to an address.
const amount = 100000 // in satoshiConst address = '1FFsHfDBEh57BB1nkeuKAk25H44U7mmMXd'const balance = await wallet.send(amount, address)