npm install bitcoin3
Create a bitcoin3
instance
const Bitcoin3 = require("bitcoin3")
const btc3 = new Bitcoin3("testnet")
//OPTIONAL: default blockcypher API
btc3.setProvider("blockcypher")
Get the balance
// Get balance of address
btc3.getBalance("2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF").then(console.log)
Unlock the privatekey
//set the privatekey for signing
btc3.privateKeyToAccount("<privateKey>")
//Now the default account has been set
console.log(btc3.defaultAccount)
Send a transaction to a Bitcoin address
//Send the actual transaction to an address with the amount of satoshi comprhensive of actual value + fee
btc3.sendTransaction(addressTo, 20000).then(console.log).catch(console.log)
All getters returns a Promise
String
- The blockexplorer API provider. Only blockcypher supported
none
btc3.setProvider("blockcypher")
String
- The Bitcoin blockchain to work against. One of testnet and mainnet
none
btc3.setNetwork("testnet")
String
- The WIF encoded private key. Mandatory for usingsendTransaction
none
btc3.privateKeyToAccount("e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109")
String
- The base58 Bitcoin address to watch the balance for.
Promise:String|String
- Returns the balance in satoshis or catch the error
btc3.getBalance("2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF")
.then( satoshis => console.log('The balance is ' + satoshis))
.catch( reason => console.log(reason))
//The balance is 1500000
String
- The base58 Bitcoin address to get the utxos of.
Promise:Array|String
- Returns the array of utxos per address in the standard format{txid, vout, satoshis, confirmations}
btc3.getUtxo("2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF")
.then( utxos => console.log(utxos))
.catch( reason => console.log(reason))
//[...]
none
Promise:String|String
- Returns the amount in satoshi for the fastest fee
btc3.estimateFee()
.then( fastestFee => console.log(fastestFee))
.catch( reason => console.log(reason))
//20000
String
- The Bitcoin address to validate
bool
- Returns true if it is a valid base58 address
const isValid = btc3.isValidAddress("000")
console.log(isValid)
//false
String
- The Bitcoin address to sent sathosisNumber
- The integer value of satoshis to be sent
Promise:Object|String
- Return the transaction object receipt
const dest = "2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF"
const amount = 15000000
btc3.sendTransaction(dest, amount)
.then(console.log)
// {...}
Array
- Array of public keys in hex formatm
- treshold needed for sign a valid transaction
String
- Return a m-of-n P2SH Multisig Address
const keys = [
'032b4c06c06c3ec0b7fa29519dfa5aae193ee2cc35ca127f29f14ec605d62fb63d',
'0216c92abe433106491bdeb4a261226f20f5a4ac86220cc6e37655aac6bf3c1f2a',
'039e05da8b8ea4f9868ecebb25998c7701542986233f4401799551fbecf316b18f'
]
const m = 2
const address = btc3.createMultisig(keys, 2)
console.log(address)
// testnet: 2N2VHjejHUCbJSRnoVyoesXMDsaiUey5ptp
//mainnet 3Aw5fuoFrk5xEeAFprBnFaMxfEWJp6vqNX