VERSION v0.0.1-alpha.5
This page contains instructions for how to install BitcoinToken in a browser and in node. If you have problems during installation, check out the troubleshooting guide below and ask for help in the Telegram group.
You need node.js and npm installed. Create an empty project using npm i -y
and install BitcoinToken using
npm i bitcointoken
If you want to use BitcoinWallet you are done with the installation and can move to the next chapter.
If you want to use BitcoinDb or BitcoinToken you need to build and run a non-standard server. We recommend to use Docker and Docker Compose. If you have these installed, you can build and run the non-standard server by typing the following into a terminal window:
git clone https://github.com/BitcoinDB/bitcoin-non-standard-server.git cd bitcoin-non-standard-server/ docker-compose up
Instruction for how to build and run the non-standard server without Docker are in the Github repo.
After installing BitcoinToken and a empty project as described above, create a file index.js
const Bitcoin = require('bitcointoken') const wallet = new Bitcoin.Wallet() console.log(`address: ${wallet.getAddress()}`)
Run the code using
node --experimental-repl-await index.js
You should see output similar to this:
address: mwZd1bgRYhxY4JLyx1sdEGBYFZnXVDvgmp
We recommend using Parcel to bundle the files for the browser.
npm install -g parcel-bundler
Create a file index.js
const Bitcoin = require('bitcointoken') const wallet = new Bitcoin.Wallet() document.body.innerHTML = `address: ${wallet.getAddress()}`
and a file index.html
<html> <body> <script src="./index.js"></script> </body> </html>
Start a development server
parcel index.html
Then open http://localhost:1234/
in a browser to see the output.
If you have a problem that is not discussed here, please let us know in the Telegram group.
You need to wrap your code into a immediately invoked async function if you are using await
, for example by defining and async
function like this:
async function main () { const Bitcoin = require('bitcointoken') const db = new Bitcoin.Db() await db.put({ a: 'a' }) } main()
You are most likely calling a function like wallet.send
or db.put
that broadcasts a transaction behind the scenes. There is a cost associated with broadcasting a transaction, and the error indicates that the balance of your wallet is too low to cover the cost.
To fix the problem you have to fund your wallet. When you do that you need to make sure that the same wallet is generated every time you run your code. You can check that by running your code multiple times and checking if the error message contains the same address every time.
If the same address gets logged every time you can fund that address using a testnet faucet to fix the problem.
Otherwise the source of the error is likely that you are creating a BitcoinWallet/Db/Token object from a constructor. When this happens a new object is created from a random private key every time you run the code. If you were to fund the address that is logged, that would not help because a different wallet with a different (unfunded) address would be generated next time you run your code.
To make sure that the same object gets generated every time, you need to generate a fixed hd private key and initialize your object from it. To generate the key run
node --experimental-repl-await <file-name>
on a file
const Bitcoin = require('bitcointoken') console.log(Bitcoin.Wallet.getHdPrivateKey())
The output will be a hd private key similar to this
tprv8ZgxMBicQKsPdyfL88tUskirPMQSQvAhYcit7UgSkeSb4pjMxfipjsykwb2TZ8r3vGRhXgHuS6QFmSLxcg4u5xZphAbNzQQq77EeZrvkZgN
You can generate the same object every time the code runs by calling the static method fromHdPrivateKey
of BitcoinDb
, BitcoinWallet
, or BitcoinToken
, for example like this:
async function main () {
const Bitcoin = require('bitcointoken')
const wallet = Bitcoin.Wallet.fromHdPrivateKey('tprv8ZgxMBicQKsPdstbuTnsMm1tpr7btJpNwAfBQTZ1edFWd86Wb4TXveoGSp8ktTwwQ27EeQ46JEQtwYjbP7VSPfkXoEnUDWZ81Z1VQFJ5zZc')
console.log(wallet.getAddress())
}
main()
If you run the code multiple times, you will see that the same address is generated every time. You can now fund that address using the testnet faucet to fix the problem.
The problem is most likely that you are not running the non-standard server. To fix the problem build and run a non-standard server as described above.
This error will happen if you use await
in the browser. In this case create a file called .babelrc
with the content
{
"presets": [
["env", {
"targets": {
"browsers": ["last 2 Chrome versions"]
}
}]
]
}