Code Monkey home page Code Monkey logo

go-bitcoind's Introduction

bitcoind

A Golang client library wrapping the bitcoind JSON RPC API

Installation

$ go get github.com/toorop/go-bitcoind

Usage

package main

import (
	"github.com/toorop/go-bitcoind"
	"log"
)

const (
	SERVER_HOST        = "You server host"
	SERVER_PORT        = port (int)
	USER               = "user"
	PASSWD             = "passwd"
	USESSL             = false
	WALLET_PASSPHRASE  = "WalletPassphrase"
)

func main() {
	bc, err := bitcoind.New(SERVER_HOST, SERVER_PORT, USER, PASSWD, USESSL)
	if err != nil {
		log.Fatalln(err)
	}

	//walletpassphrase
	err = bc.WalletPassphrase(WALLET_PASSPHRASE, 3600)
	log.Println(err)

	// backupwallet
	err = bc.BackupWallet("/tmp/wallet.dat")
	log.Println(err)


	// dumpprivkey
	privKey, err := bc.DumpPrivKey("1KU5DX7jKECLxh1nYhmQ7CahY7GMNMVLP3")
	log.Println(err, privKey)

}

Mores examples in example.go (in examples folder)

Documentation

Click on the button below to access the full documentation:

GoDoc

Unit tests

Build Status

More than 100 unit tests are made.

To run tests:

$ go get github.com/onsi/ginkgo/ginkgo
$ go get github.com/onsi/gomega
$ ginkgo

Running Suite: Bitcoind Suite	
=============================
Random Seed: 1401120770
Will run 112 of 112 specs

•••••••••••••••••••••••••••••••••••
Ran 112 of 112 Specs in 0.001 seconds
SUCCESS! -- 112 Passed | 0 Failed | 0 Pending | 0 Skipped PASS

Ginkgo ran in 10.856335553s
Test Suite Passed

Todo

  • GetBlockTemplate
  • sendrawtransaction
  • signrawtransaction
  • submitblock
Note on SSL support

Note on ssl support : bitcoind library doesn't verify the server's certificate chain. That means that it accepts any certificate presented by the server and any host name in that certificate. In this mode, TLS is susceptible to man-in-the-middle attacks.

Donation

Donation QR

1HgpsmxV52eAjDcoNpVGpYEhGfgN7mM1JB

go-bitcoind's People

Contributors

dannybabbev avatar dexconv avatar fiatjaf avatar gandlafbtc avatar goqihoo avatar jherrma avatar parth avatar philsong avatar qshuai avatar shade avatar steebchen avatar toorop avatar vpashka avatar wmh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-bitcoind's Issues

sample code in readme is misleading, 500 Internal Server Error

Hi I have a bitcoind run in testnet, I found the sample code is misleading, people who run it the first time will get 500 internal server error, the full reproduce step is as follow:

1. btcd config:

# Generated by https://jlopp.github.io/bitcoin-core-config-generator/

# This config should be placed in following path:
# ~/.bitcoin/bitcoin.conf

# [core]
# Specify the location of the configuration file. To use non-default location, create a default location config file containing this setting.
conf=~/.bitcoind/bitcoin.conf
# Specify a non-default location to store blockchain and other data.
# datadir=/mnt/bitcoin/bitcoind/testnet

# [debug]
# Run this node on the Bitcoin Test Network.
testnet=1

# [rpc]
# Accept command line and JSON-RPC commands.
server=1
# Accept public REST requests.
rest=1
# RPC user
rpcuser=rpcuser
# RPC password
rpcpassword=rpcpass
# RPC allow ip (allow all)
rpcallowip=0.0.0.0/0
# RPC bind
rpcbind=0.0.0.0

2. check the port listend

port 18332 for testnet rpc connection is listening

~ netstat -apnt
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:18332           0.0.0.0:*               LISTEN      16025/bitcoind
tcp        0      0 0.0.0.0:18333           0.0.0.0:*               LISTEN      16025/bitcoind

3. check the port from local

telnet successful

pro:go-bitcoind shiming$ telnet 10.211.55.7 18332
Trying 10.211.55.7...
Connected to ubuntu.shared.
Escape character is '^]'.
Connection closed by foreign host.

4. run bitcoind

bitcoind -debug=1

5. run sample code

package main

import (
	"github.com/toorop/go-bitcoind"
	"log"
)

const (
	SERVER_HOST        = "10.211.55.7"
	SERVER_PORT        = 18332
	USER               = "rpcuser"
	PASSWD             = "rpcpass"
	USESSL             = false
	WALLET_PASSPHRASE  = "WalletPassphrase"
)

func main() {
	bc, err := bitcoind.New(SERVER_HOST, SERVER_PORT, USER, PASSWD, USESSL)
	if err != nil {
		log.Fatalln(err)
	}

	//walletpassphrase
	err = bc.WalletPassphrase(WALLET_PASSPHRASE, 3600)
	log.Println(err)

	// backupwallet
	err = bc.BackupWallet("/tmp/wallet.dat")
	log.Println(err)


	// dumpprivkey
	privKey, err := bc.DumpPrivKey("1KU5DX7jKECLxh1nYhmQ7CahY7GMNMVLP3")
	log.Println(err, privKey)

}

6. get err

2018/10/06 11:26:25 HTTP error: 500 Internal Server Error
2018/10/06 11:26:25 <nil>
2018/10/06 11:26:25 HTTP error: 500 Internal Server Error 

7. bitcoind debug.log

those rows begin with "Received a POST request for" is the log, but no detail logs, dosen't explain the 500 internal server error. But at least it is connected

......
2018-10-06 03:58:35 sending getheaders (933 bytes) peer=0
2018-10-06 03:58:35 Received a POST request for / from 10.211.55.2:62473
2018-10-06 03:58:35 ThreadRPCServer method=walletpassphrase
2018-10-06 03:58:35 Received a POST request for / from 10.211.55.2:62473
2018-10-06 03:58:35 ThreadRPCServer method=backupwallet
2018-10-06 03:58:35 copied wallet.dat to /tmp/wallet.dat
2018-10-06 03:58:35 Received a POST request for / from 10.211.55.2:62473
2018-10-06 03:58:35 ThreadRPCServer method=dumpprivkey
2018-10-06 03:58:35 tor: Error connecting to Tor control socket
2018-10-06 03:58:35 tor: Not connected to Tor control port 127.0.0.1:9051, trying to reconnect
2018-10-06 03:58:36 received: headers (162003 bytes) peer=0
......

8. my sample code suggestion

looking into the issue by add trace info and invoke another simple client methods, I found it actually successful connected, my code is as below

package main

import (
	"fmt"
	"github.com/toorop/go-bitcoind"
	"log"
)

const (
	SERVER_HOST        = "10.211.55.7"
	SERVER_PORT        = 18332
	USER               = "rpcuser"
	PASSWD             = "rpcpass"
	USESSL             = false
	WALLET_PASSPHRASE  = "WalletPassphrase"
)

func main() {
	bc, err := bitcoind.New(SERVER_HOST, SERVER_PORT, USER, PASSWD, USESSL)
	if err != nil {
		log.Fatalln(err)
	}else{
		fmt.Println("connect successful")
	}

	difficult, err := bc.GetDifficulty()
	if err != nil {
		log.Fatalln(err)
	}else{
		fmt.Printf("difficult:%v\n",difficult)
	}

	count, err := bc.GetBlockCount()
	if err != nil{
		log.Fatalln(err)
	}else{
		fmt.Printf("count:%v\n",count)
	}


	////walletpassphrase
	//err = bc.WalletPassphrase(WALLET_PASSPHRASE, 3600)
	//log.Println(err)
	//
	//// backupwallet
	//err = bc.BackupWallet("/tmp/wallet.dat")
	//log.Println(err)
	//
	//
	//// dumpprivkey
	//privKey, err := bc.DumpPrivKey("1KU5DX7jKECLxh1nYhmQ7CahY7GMNMVLP3")
	//log.Println(err, privKey)

}

/*
output:
connect successful
difficult:1
count:306017
*/

RPCCLIENT_TIMEOUT customizable

Good day!
Is it possible to expose somewhere request timeouts? Maybe in NewWithTimeout(...)
According to comments, RPCCLIENT_TIMEOUT was planned as default value, but now no way to override it....

no err reson returned

if there's some error returned from the bitcoind, how can I know the error reason, currently every error returns "HTTP error: 500 Internal Server Error"

Implement RPC batching

It would be cool to be able to do batched RPC calls. However, this would require quite an extensive rewrite of the go-bitcoind core.

set wallet

	var addr string
	if len(btc.walletName) > 0 {
		addr = fmt.Sprintf("%s%s:%d/wallet/%s", schema, cfg.Host, cfg.Port, btc.walletName)
	} else {
		addr = fmt.Sprintf("%s%s:%d", schema, cfg.Host, cfg.Port)
	}

Module path should be "github.com/toorop/go-bitcoind", not "github.com/vpashka/go-bitcoind"

Background

Module path is inconsistent with go import path.
GO111MODULE=on, as doc said, import "github.com/toorop/go-bitcoind", then get this error:

go: finding module for package github.com/toorop/go-bitcoind
go: downloading github.com/toorop/go-bitcoind v0.0.0-20191125160357-28204734ac83
go: found github.com/toorop/go-bitcoind in github.com/toorop/go-bitcoind v0.0.0-20191125160357-28204734ac83
go: test1 imports
        github.com/toorop/go-bitcoind: github.com/toorop/[email protected]: parsing go.mod:
        module declares its path as: github.com/vpashka/go-bitcoind
                but was required as: github.com/toorop/go-bitcoind 

Solution

Fix the module path:

  1. Rename the module path to "github.com/toorop/go-bitcoind": https://github.com/toorop/go-bitcoind/blob/master/go.mod#L1
module github.com/toorop/go-bitcoind
go 1.12 
  1. Warning the users not to use the module, get "github.com/toorop/go-bitcoind" in GOPATH mode.

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.