Coder Social home page Coder Social logo

scala-bitcoin-jsonrpc's Introduction

Scala Bitcoin Json RPC

A client library to communicate with bitcoind via JSON-RPC in Scala. The library provides the ability to query the blockchain, the network, and manage a local wallet.

Currently supports the following APIs:

  • decoderawtransaction
  • dumpprivkey
  • getaccountaddress
  • getaddressesbyaccount
  • getbalance
  • getblock
  • getblockcount
  • getblockhash
  • getconnectioncount
  • getdifficulty
  • getinfo
  • generate
  • getnewaddress
  • getmininginfo
  • getnewaddress
  • getrawtransaction
  • getrawmempool
  • importaddress
  • importprivkey
  • listaccounts
  • listunspent
  • sendrawtransaction
  • sendfrom
  • validateaddress

Usage

Configure a client to communicate with a local or remote bitcoind server:

val client = JsonRpcClient("http://<ip>:8332", "username", "password")
// For convenience Bring APIs into scope
import client._

This library exposes APIs equivalent to the standard bitcoin-cli API. For example getinfo() will run the bitcoin-cli command getinfo:

println(getinfo())

prints:

{
  "testnet": false,
  "paytxfee": 0,
  "difficulty": 922724699725,
  "blocks": 485671,
  "timeoffset": -2,
  "errors": "",
  "balance": 0,
  "keypoololdest": 1504659203,
  "version": 140200,
  "protocolversion": 70015,
  "relayfee": 0,
  "proxy": "",
  "walletversion": 130000,
  "connections": 8,
  "keypoolsize": 100
}

The JSON-RPC response is converted to a Scala type. JsObject is converted to a case class, JsArray to a Seq, simple types kept as simple types, etc.

Example 1

This example shows how to retrieve a transaction and print each output.

val client = JsonRpcClient(uri, username, password)
import client._

// A specific transaction id, this one was copied from Example 3-4 in Mastering Bitcoin
val txid = "0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2"

// First, retrieve the raw transaction in hex
val raw_tx = getrawtransaction(txid)

// Decode the transaction hex into a transaction class
val decoded_tx = decoderawtransaction(raw_tx)

// Print each output in the transaction
decoded_tx.vout.foreach { t =>
  println(s"${t.scriptPubKey.addresses.mkString(",")} : ${t.value}")
}  

Which outputs

1GdK9UzpHBzqzX2A9JFP3Di4weBwqgmoQA : 0.015
1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK : 0.0845

Example 2

This example shows how to retrieve a block and sum the output transaction values

val client = JsonRpcClient(uri, username, password)
import client._


// A block height, copied from Example 3-5 in Mastering Bitcoin
val blockHeight = 277316

// Get the block hash with height 277316
val blockHash = getblockhash(blockHeight)

// Retrieve the block by its hash
val block = getblock(blockHash)

// Element tx contains all transactions ids in the block
val transactionIds = block.tx

// Iterate through each transaction id
val blockValue = transactionIds.map { txid =>
  // Get the transaction from its id
  val raw_tx = getrawtransaction(txid)
  val transaction = decoderawtransaction(raw_tx)

  // Iterate over each transaction summing the values
  transaction.vout.map(_.value).sum
}.sum

which prints:

Total value in block 10322.07722534

Bugs & Feature Requests

Please use the GitHub issue to report bugs or feature requests.

Troubleshooting

Ensure bitcoind is configured correctly. To explore the blockchain ensure that the txindex=1 config is set. To manage a local wallet ensure that the wallet functionality is installed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.