Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Repository files navigation

bitcore-lib-tutorial

This sameple code will let you generate a private key, create a transaction and submit it via with bitcore-lib and Javascript


Creating a private key and sending a bitcoin transaction in 30 lines of code

  • run node create-key.js to create a new private key (saved by the script in private-key.txt)
  • fund the account with 1 millibit or less
  • run node index.js (by default it will create and submit a transaction paying 0.1 btc to the same address)

The code is the following:

const{ readFileSync }=require('fs')constbitcoin=require('bitcore-lib')const{ PrivateKey, Transaction }=bitcoinconstpvtKeyString=readFileSync('./private-key.txt').toString().trim()constpvtKey=newPrivateKey(pvtKeyString)constaddress=pvtKey.toAddress().toString()console.log("private key:",pvtKey.toString())console.log("address:",address)constgetUTXOs=require('./blockchain-api/get-utxos')constpushTx=require('./blockchain-api/push-tx')// const { pushTx, getUTXOs } = require('./blockchain-api') // TODO: NPM MODULE;(async()=>{try{letutxos=awaitgetUTXOs({ address })utxos=[utxos[0]]console.log("utxos:",utxos)constamount=10000// 10k sats (0.1mbtc)consttx=newTransaction().from(utxos).to(address,amount).change(address).fee(1000).sign(pvtKey)consttxHex=tx.serialize()console.log("tx:",txHex)const{ success, txHash }=pushTx(txHex)console.log("success:",success,"txHash:",txHash)}catch(err){console.error(err)}})()

Creating a private key and sending a bitcoin transaction in 30 lines of code - #coding #bitcoin #tx #utxo #bitcoin-tx #bitcoin-utxo


notes about js JS bitcoin libraries:

bitcore-lib.js (purely didatical version - [this version])

bitcoinjs-lib.js (TODO: create a more practical version using bitcoinjs-lib or a wrapper)


@makevoid

Releases

Packages

Contributors

Languages