Skip to content

Query and open a socket connection to a bitdb node

Notifications You must be signed in to change notification settings

dylankilkenny/go-bitdb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-bitdb

Using go-bitdb you can query a bitdb node and connect to a bitsocket node in golang.

BitDB

BitDb is an autonomous database that continuously synchronizes itself with Bitcoin. BitDB stores every bitcoin transaction in a structured document format that can be queried against like a regular database. With a simple MongoDB query, anyone can easily query, filter, and build powerful decentralized applications on Bitcoin.

Bitsocket

Bitsocket is a bitcoin notification service. By constructing a bitquery, you can subscribe to the bitcoin blockchain and recieve transactions that meet a certain criteria.

Install

go get github.com/dylankilkenny/go-bitdb

BitDb Usage

The following code creates a BitDb instance and queries a bitdb node for a transaction by its TxID. The jq string is the processing function which will return an object containing the amount in the first output of the transaction

version := 3 // API version
bitdbURL := "https://bitdb.network/q/" // API url
apiKey := "qq54zc33pttdp6l8ycnnj99ahan8a2hfrygqyz0fc3"
BitDb := bitdb.New(version, bitdbURL, apiKey) // Create new instance

txHash := BitDb.TxHash{Hash: "ffff5c6d0660068381b26fe3546eb2a51faf1a0a1a707db1ca32a5b168a7301b"}
jq := ".[] | .out[0] | {amount: .e.v}"
response, err := BitDb.Request(txHash, jq)
if err != nil {
  fmt.Println(err)
}
confirmed, _ := response.Confirmed.(map[string]interface{})
fmt.Println(confirmed["amount"])

Output:

7051

Rather than using structs you can query with a json string:

var bitquery = []byte(`
{
  "v": 3,
  "q": {
    "find": {
      "tx.h": "ffff5c6d0660068381b26fe3546eb2a51faf1a0a1a707db1ca32a5b168a7301b"
    }
  },
  "r": {
    "f": ".[] | .out[0] | {amount: .e.v}"
  }
}
`)
response, err := BitDb.RawRequest(bitquery)
if err != nil {
  fmt.Println(err)
}
confirmed, _ := response.Confirmed.(map[string]interface{})
fmt.Println(confirmed["amount"])

Bitsocket Usage

version := 3
bitdbURL := "https://bitdb.network/q/"
bitsocket := bitdb.NewSocket(version, bitdbURL)

events, err := bitsocket.Stream("", ".[] | .out[0] | {amount: .e.v}")
if err != nil {
  t.Error(err)
}
for tx := range events {
  // do something with transaction
}

About

Query and open a socket connection to a bitdb node

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages