项目作者: KeisukeYamashita

项目描述 :
A simple JSON-RPC pacakge for Bitcoin.
高级语言: Go
项目地址: git://github.com/KeisukeYamashita/go-btcrpc.git
创建时间: 2017-12-12T23:42:39Z
项目社区:https://github.com/KeisukeYamashita/go-btcrpc

开源协议:Apache License 2.0

下载


go-btcrpc

Open Source Love
GoDoc
License: Apache 2.0
CircleCI
Job Status
Report

go-btcrpc is a Go library use for interacting to the bitcoin node from your server with JSON-RPC which is a standard protocol for blockchain.

This library provides the easiest way to send JSON-RPC.

image.png

Installation

Use go get to install and update.

  1. go get github.com/KeisukeYamashita/go-btcrpc.git

Setup

You need to setup environmental variables.

Firstly, copy the .env.sample as .env

  1. cp .env.sample .env

Setup in your .env.

  1. BTCD_ENDPOINT: NODE_ENDPOINT
  2. USERNAME: USERNAME_FOR_BASICAUTH
  3. PASSWORD: PASSWORD_FOR_BASICAUTH

Usage and Example

This shows you that easiest request to the node which is getting the infos.

  1. package main
  2. import (
  3. "fmt"
  4. btcrpc "github.com/KeisukeYamashita/go-btcrpc"
  5. )
  6. func main() {
  7. basicAuth := &BasicAuth{
  8. Username: os.Getenv("USERNAME"),
  9. Password: os.Getenv("PASSWORD"),
  10. }
  11. c := NewRPCClient(os.Getenv("BTCD_ENDPOINT"), basicAuth)
  12. address := "my88QLpf2RYYDdNMmDwYvfx6TFc6NXaELa"
  13. balance := c.GetBalance(address)
  14. fmt.Print(balance) // 0.13514 BTC
  15. }

Equal curl command

  1. curl -X "POST" "<YOUR_BITCOIN_NODE>" \
  2. -H 'Content-Type: application/json; charset=utf-8' \
  3. -u '<YOUR_USER_NAME>:<YOUR_PASSWORD>' \
  4. -d $'{
  5. "method": "getbalance",
  6. "id": "1",
  7. "params": [
  8. "my88QLpf2RYYDdNMmDwYvfx6TFc6NXaELa"
  9. ]
  10. }'

It’ll return a JSON.

Available Methods

method discription
getNewAddress creates a new address to your account.
getBalance get the balance of your address.
getBlockHash get the hash of the block.
getBlock get the block by hash of the block.
getBlockCount get the newest block count by hash of the block.
decodeTransaction decode the raw transaction to humanreadable transaction by hash of the block.
getrawTransaction get the raw transaction hash block count by hash of the block.

Use tests

Set up your environmental valiables in .env to conduct this test.

  1. cp .env.sample .env

Then write in your endpoint in this file.

Finally run your test. It will pass if your bitcoin node is setted up correctly.

  1. GO_ENV = test go test btcrpc

Contribution

To contribute, just send me a pull request!
If it is valid, you will be added on the contribution doc in /doc/contributor.md .

License

Copyright 2017 Keisuke Yamashita.

Licensed under the Apache 2.0 license.