2013-10-09 03:18:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-10-28 17:45:44 +01:00
|
|
|
"encoding/json"
|
2013-10-09 03:18:49 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/conformal/btcjson"
|
2013-10-17 01:19:26 +02:00
|
|
|
"github.com/conformal/go-flags"
|
2013-10-09 03:18:49 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2013-11-07 17:25:11 +01:00
|
|
|
"io/ioutil"
|
2013-10-17 01:19:26 +02:00
|
|
|
"os"
|
2013-10-22 17:56:56 +02:00
|
|
|
"sort"
|
2013-10-09 03:18:49 +02:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2013-10-22 05:56:02 +02:00
|
|
|
// conversionHandler is a handler that is used to convert parameters from the
|
|
|
|
// command line to a specific type. This is needed since the btcjson API
|
|
|
|
// expects certain types for various parameters.
|
|
|
|
type conversionHandler func(string) (interface{}, error)
|
|
|
|
|
|
|
|
// displayHandler is a handler that takes an interface and displays it to
|
|
|
|
// standard out. It is used by the handler data to type assert replies and
|
|
|
|
// show them formatted as desired.
|
|
|
|
type displayHandler func(interface{}) error
|
|
|
|
|
|
|
|
// handlerData contains information about how a command should be handled.
|
|
|
|
type handlerData struct {
|
|
|
|
requiredArgs int
|
|
|
|
optionalArgs int
|
|
|
|
displayHandler displayHandler
|
|
|
|
conversionHandlers []conversionHandler
|
2013-10-28 17:45:44 +01:00
|
|
|
makeCmd func([]interface{}) (btcjson.Cmd, error)
|
2013-10-22 17:56:56 +02:00
|
|
|
usage string
|
2013-10-22 05:56:02 +02:00
|
|
|
}
|
|
|
|
|
2013-10-09 03:18:49 +02:00
|
|
|
var (
|
2013-10-22 05:56:02 +02:00
|
|
|
ErrNoData = errors.New("No data returned.")
|
|
|
|
ErrNoDisplayHandler = errors.New("No display handler specified.")
|
|
|
|
ErrUsage = errors.New("btcctl usage") // Real usage is shown.
|
2013-10-09 03:18:49 +02:00
|
|
|
)
|
|
|
|
|
2013-10-22 17:56:56 +02:00
|
|
|
// commandHandlers is a map of commands and associated handler data that is used
|
|
|
|
// to validate correctness and perform the command.
|
2013-10-22 05:56:02 +02:00
|
|
|
var commandHandlers = map[string]*handlerData{
|
2013-10-28 17:45:44 +01:00
|
|
|
"addnode": &handlerData{2, 0, displaySpewDump, nil, makeAddNode, "<ip> <add/remove/onetry>"},
|
|
|
|
"decoderawtransaction": &handlerData{1, 0, displaySpewDump, nil, makeDecodeRawTransaction, "<txhash>"},
|
|
|
|
"getbestblockhash": &handlerData{0, 0, displayGeneric, nil, makeGetBestBlockHash, ""},
|
2013-11-08 00:07:26 +01:00
|
|
|
"getblock": &handlerData{1, 0, displaySpewDump, nil, makeGetBlock, "<blockhash>"},
|
|
|
|
"getblockcount": &handlerData{0, 0, displayFloat64, nil, makeGetBlockCount, ""},
|
|
|
|
"getblockhash": &handlerData{1, 0, displayGeneric, []conversionHandler{toInt64}, makeGetBlockHash, "<blocknumber>"},
|
|
|
|
"getconnectioncount": &handlerData{0, 0, displayFloat64, nil, makeGetConnectionCount, ""},
|
|
|
|
"getdifficulty": &handlerData{0, 0, displayFloat64, nil, makeGetDifficulty, ""},
|
|
|
|
"getgenerate": &handlerData{0, 0, displayGeneric, nil, makeGetGenerate, ""},
|
|
|
|
"getpeerinfo": &handlerData{0, 0, displaySpewDump, nil, makeGetPeerInfo, ""},
|
|
|
|
"getrawmempool": &handlerData{0, 0, displaySpewDump, nil, makeGetRawMempool, ""},
|
|
|
|
"getrawtransaction": &handlerData{1, 1, displaySpewDump, []conversionHandler{nil, toInt}, makeGetRawTransaction, "<txhash> [verbose=0]"},
|
2013-11-18 20:51:31 +01:00
|
|
|
"importprivkey": &handlerData{1, 2, displayGeneric, []conversionHandler{nil, nil, toBool}, makeImportPrivKey, "<wifprivkey> [label] [rescan=true]"},
|
2013-11-14 19:19:20 +01:00
|
|
|
"verifychain": &handlerData{0, 2, displaySpewDump, []conversionHandler{toInt, toInt}, makeVerifyChain, "[level] [depth]"},
|
2013-11-08 00:07:26 +01:00
|
|
|
"stop": &handlerData{0, 0, displayGeneric, nil, makeStop, ""},
|
2013-10-22 05:56:02 +02:00
|
|
|
}
|
2013-10-17 01:19:26 +02:00
|
|
|
|
2013-10-22 07:38:51 +02:00
|
|
|
// toInt attempts to convert the passed string to an integer. It returns the
|
|
|
|
// integer packed into an interface so it can be used in the calls which expect
|
2013-10-22 05:56:02 +02:00
|
|
|
// interfaces. An error will be returned if the string can't be converted to an
|
|
|
|
// integer.
|
|
|
|
func toInt(val string) (interface{}, error) {
|
|
|
|
idx, err := strconv.Atoi(val)
|
2013-10-17 01:19:26 +02:00
|
|
|
if err != nil {
|
2013-10-22 05:56:02 +02:00
|
|
|
return nil, err
|
2013-10-17 01:19:26 +02:00
|
|
|
}
|
2013-10-09 03:18:49 +02:00
|
|
|
|
2013-10-22 05:56:02 +02:00
|
|
|
return idx, nil
|
|
|
|
}
|
2013-10-09 03:18:49 +02:00
|
|
|
|
2013-11-08 00:07:26 +01:00
|
|
|
// toInt64 attempts to convert the passed string to an int64. It returns the
|
|
|
|
// integer packed into an interface so it can be used in the calls which expect
|
|
|
|
// interfaces. An error will be returned if the string can't be converted to an
|
|
|
|
// integer.
|
|
|
|
func toInt64(val string) (interface{}, error) {
|
|
|
|
idx, err := strconv.ParseInt(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, nil
|
|
|
|
}
|
|
|
|
|
2013-11-18 20:51:31 +01:00
|
|
|
// toBool attempts to convert the passed string to a bool. It returns the
|
|
|
|
// bool packed into the empty interface so it can be used in the calls which
|
|
|
|
// accept interfaces. An error will be returned if the string can't be
|
|
|
|
// converted to a bool.
|
|
|
|
func toBool(val string) (interface{}, error) {
|
|
|
|
return strconv.ParseBool(val)
|
|
|
|
}
|
|
|
|
|
2013-10-22 05:56:02 +02:00
|
|
|
// displayGeneric is a displayHandler that simply displays the passed interface
|
|
|
|
// using fmt.Println.
|
|
|
|
func displayGeneric(reply interface{}) error {
|
|
|
|
fmt.Println(reply)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// displayFloat64 is a displayHandler that ensures the concrete type of the
|
|
|
|
// passed interface is a float64 and displays it using fmt.Println. An error
|
|
|
|
// is returned if a float64 is not passed.
|
|
|
|
func displayFloat64(reply interface{}) error {
|
|
|
|
if val, ok := reply.(float64); ok {
|
|
|
|
fmt.Println(val)
|
|
|
|
return nil
|
2013-10-09 03:18:49 +02:00
|
|
|
}
|
2013-10-22 05:56:02 +02:00
|
|
|
|
|
|
|
return fmt.Errorf("reply type is not a float64: %v", spew.Sdump(reply))
|
|
|
|
}
|
|
|
|
|
|
|
|
// displaySpewDump is a displayHandler that simply uses spew.Dump to display the
|
|
|
|
// passed interface.
|
|
|
|
func displaySpewDump(reply interface{}) error {
|
|
|
|
spew.Dump(reply)
|
|
|
|
return nil
|
2013-10-09 03:18:49 +02:00
|
|
|
}
|
|
|
|
|
2013-10-28 17:45:44 +01:00
|
|
|
// makeAddNode generates the cmd structure for addnode comands.
|
|
|
|
func makeAddNode(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewAddNodeCmd("btcctl", args[0].(string),
|
|
|
|
args[1].(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeDecodeRawTransaction generates the cmd structure for
|
|
|
|
// decoderawtransaction comands.
|
|
|
|
func makeDecodeRawTransaction(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewDecodeRawTransactionCmd("btcctl", args[0].(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeGetBestBlockHash generates the cmd structure for
|
|
|
|
// makebestblockhash comands.
|
|
|
|
func makeGetBestBlockHash(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewGetBestBlockHashCmd("btcctl")
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeGetBlock generates the cmd structure for getblock comands.
|
|
|
|
func makeGetBlock(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewGetBlockCmd("btcctl", args[0].(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeGetBlockCount generates the cmd structure for getblockcount comands.
|
|
|
|
func makeGetBlockCount(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewGetBlockCountCmd("btcctl")
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeGetBlockHash generates the cmd structure for getblockhash comands.
|
|
|
|
func makeGetBlockHash(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewGetBlockHashCmd("btcctl", args[0].(int64))
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeGetConnectionCount generates the cmd structure for
|
|
|
|
// getconnectioncount comands.
|
|
|
|
func makeGetConnectionCount(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewGetConnectionCountCmd("btcctl")
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeGetDifficulty generates the cmd structure for
|
|
|
|
// getdifficulty comands.
|
|
|
|
func makeGetDifficulty(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewGetDifficultyCmd("btcctl")
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeGetGenerate generates the cmd structure for
|
|
|
|
// getgenerate comands.
|
|
|
|
func makeGetGenerate(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewGetGenerateCmd("btcctl")
|
|
|
|
}
|
|
|
|
|
|
|
|
// makePeerInfo generates the cmd structure for
|
|
|
|
// getpeerinfo comands.
|
|
|
|
func makeGetPeerInfo(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewGetPeerInfoCmd("btcctl")
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeRawMempool generates the cmd structure for
|
|
|
|
// getrawmempool comands.
|
|
|
|
func makeGetRawMempool(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewGetRawMempoolCmd("btcctl")
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeRawTransaction generates the cmd structure for
|
|
|
|
// getrawtransaction comands.
|
|
|
|
func makeGetRawTransaction(args []interface{}) (btcjson.Cmd, error) {
|
2013-11-14 19:19:20 +01:00
|
|
|
i := 0
|
|
|
|
if len(args) > 1 {
|
|
|
|
i = args[1].(int)
|
|
|
|
}
|
2013-10-28 17:45:44 +01:00
|
|
|
|
|
|
|
bi := i != 0
|
|
|
|
return btcjson.NewGetRawTransactionCmd("btcctl", args[0].(string), bi)
|
|
|
|
}
|
|
|
|
|
2013-11-18 20:51:31 +01:00
|
|
|
// makeImportPrivKey generates the cmd structure for
|
|
|
|
// importprivkey commands.
|
|
|
|
func makeImportPrivKey(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
var label string
|
|
|
|
var rescan bool
|
|
|
|
if len(args) > 1 {
|
|
|
|
label = args[1].(string)
|
|
|
|
}
|
|
|
|
if len(args) > 2 {
|
|
|
|
rescan = args[2].(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
return btcjson.NewImportPrivKeyCmd("btcctl", args[0].(string), label,
|
|
|
|
rescan)
|
|
|
|
}
|
|
|
|
|
2013-10-28 17:45:44 +01:00
|
|
|
// makeStop generates the cmd structure for stop comands.
|
|
|
|
func makeStop(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
return btcjson.NewStopCmd("btcctl")
|
|
|
|
}
|
|
|
|
|
2013-11-12 17:39:10 +01:00
|
|
|
func makeVerifyChain(args []interface{}) (btcjson.Cmd, error) {
|
|
|
|
iargs := make([]int32, 0, 2)
|
|
|
|
for _, i := range args {
|
|
|
|
iargs = append(iargs, int32(i.(int)))
|
|
|
|
}
|
|
|
|
return btcjson.NewVerifyChainCmd("btcctl", iargs...)
|
|
|
|
}
|
|
|
|
|
2013-10-22 05:56:02 +02:00
|
|
|
// send sends a JSON-RPC command to the specified RPC server and examines the
|
|
|
|
// results for various error conditions. It either returns a valid result or
|
|
|
|
// an appropriate error.
|
2013-10-17 01:19:26 +02:00
|
|
|
func send(cfg *config, msg []byte) (interface{}, error) {
|
2013-11-07 17:25:11 +01:00
|
|
|
var reply btcjson.Reply
|
|
|
|
var err error
|
|
|
|
if cfg.RpcCert != "" || cfg.TlsSkipVerify {
|
|
|
|
var pem []byte
|
|
|
|
if cfg.RpcCert != "" {
|
|
|
|
pem, err = ioutil.ReadFile(cfg.RpcCert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reply, err = btcjson.TlsRpcCommand(cfg.RpcUser,
|
|
|
|
cfg.RpcPassword, cfg.RpcServer, msg, pem,
|
|
|
|
cfg.TlsSkipVerify)
|
|
|
|
} else {
|
|
|
|
reply, err = btcjson.RpcCommand(cfg.RpcUser, cfg.RpcPassword,
|
|
|
|
cfg.RpcServer, msg)
|
|
|
|
}
|
2013-10-09 03:18:49 +02:00
|
|
|
if err != nil {
|
2013-10-21 18:59:07 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-10-22 05:56:02 +02:00
|
|
|
|
2013-10-21 18:59:07 +02:00
|
|
|
if reply.Error != nil {
|
|
|
|
return nil, reply.Error
|
2013-10-09 03:18:49 +02:00
|
|
|
}
|
2013-10-22 05:56:02 +02:00
|
|
|
|
2013-10-09 03:18:49 +02:00
|
|
|
if reply.Result == nil {
|
2013-10-22 05:56:02 +02:00
|
|
|
return nil, ErrNoData
|
2013-10-09 03:18:49 +02:00
|
|
|
}
|
2013-10-22 05:56:02 +02:00
|
|
|
|
2013-10-09 03:18:49 +02:00
|
|
|
return reply.Result, nil
|
|
|
|
}
|
|
|
|
|
2013-10-22 05:56:02 +02:00
|
|
|
// sendCommand creates a JSON-RPC command using the passed command and arguments
|
|
|
|
// and then sends it. A prefix is added to any errors that occur indicating
|
|
|
|
// what step failed.
|
2013-10-28 17:45:44 +01:00
|
|
|
func sendCommand(cfg *config, command btcjson.Cmd) (interface{}, error) {
|
|
|
|
msg, err := json.Marshal(command)
|
2013-10-22 05:56:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("CreateMessage: %v", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
reply, err := send(cfg, msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("RpcCommand: %v", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// commandHandler handles commands provided via the cli using the specific
|
|
|
|
// handler data to instruct the handler what to do.
|
|
|
|
func commandHandler(cfg *config, command string, data *handlerData, args []string) error {
|
|
|
|
// Ensure the number of arguments are the expected value.
|
|
|
|
if len(args) < data.requiredArgs {
|
|
|
|
return ErrUsage
|
|
|
|
}
|
|
|
|
if len(args) > data.requiredArgs+data.optionalArgs {
|
|
|
|
return ErrUsage
|
|
|
|
}
|
|
|
|
|
2013-10-22 07:38:51 +02:00
|
|
|
// Ensure there is a display handler.
|
|
|
|
if data.displayHandler == nil {
|
|
|
|
return ErrNoDisplayHandler
|
|
|
|
}
|
|
|
|
|
2013-10-22 05:56:02 +02:00
|
|
|
// Ensure the number of conversion handlers is valid if any are
|
|
|
|
// specified.
|
|
|
|
convHandlers := data.conversionHandlers
|
|
|
|
if convHandlers != nil && len(convHandlers) < len(args) {
|
|
|
|
return fmt.Errorf("The number of conversion handlers is invalid.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert input parameters per the conversion handlers.
|
|
|
|
iargs := make([]interface{}, len(args))
|
|
|
|
for i, arg := range args {
|
|
|
|
iargs[i] = arg
|
|
|
|
}
|
|
|
|
for i := range iargs {
|
2013-10-22 06:03:11 +02:00
|
|
|
if convHandlers != nil {
|
|
|
|
converter := convHandlers[i]
|
|
|
|
if converter != nil {
|
|
|
|
convertedArg, err := converter(args[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
iargs[i] = convertedArg
|
2013-10-22 05:56:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 17:45:44 +01:00
|
|
|
cmd, err := data.makeCmd(iargs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-22 05:56:02 +02:00
|
|
|
|
|
|
|
// Create and send the appropriate JSON-RPC command.
|
2013-10-28 17:45:44 +01:00
|
|
|
reply, err := sendCommand(cfg, cmd)
|
2013-10-22 05:56:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-22 07:38:51 +02:00
|
|
|
// Display the results of the JSON-RPC command using the provided
|
|
|
|
// display handler.
|
2013-10-22 05:56:02 +02:00
|
|
|
err = data.displayHandler(reply)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// usage displays the command usage.
|
2013-10-17 01:19:26 +02:00
|
|
|
func usage(parser *flags.Parser) {
|
|
|
|
parser.WriteHelp(os.Stderr)
|
2013-10-22 17:56:56 +02:00
|
|
|
|
|
|
|
// Extract usage information for each command from the command handler
|
|
|
|
// data and sort by command name.
|
|
|
|
fmt.Fprintf(os.Stderr, "\nCommands:\n")
|
|
|
|
usageStrings := make([]string, 0, len(commandHandlers))
|
|
|
|
for command, data := range commandHandlers {
|
|
|
|
usage := command
|
|
|
|
if len(data.usage) > 0 {
|
|
|
|
usage += " " + data.usage
|
|
|
|
}
|
|
|
|
usageStrings = append(usageStrings, usage)
|
|
|
|
}
|
|
|
|
sort.Sort(sort.StringSlice(usageStrings))
|
|
|
|
for _, usage := range usageStrings {
|
|
|
|
fmt.Fprintf(os.Stderr, "\t%s\n", usage)
|
|
|
|
}
|
2013-10-22 05:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2013-11-19 21:38:58 +01:00
|
|
|
parser, cfg, args, err := loadConfig()
|
2013-10-22 05:56:02 +02:00
|
|
|
if err != nil {
|
2013-11-19 20:44:52 +01:00
|
|
|
usage(parser)
|
2013-10-22 17:01:13 +02:00
|
|
|
os.Exit(1)
|
2013-10-22 05:56:02 +02:00
|
|
|
}
|
2013-11-19 20:44:52 +01:00
|
|
|
if len(args) < 1 {
|
2013-10-22 05:56:02 +02:00
|
|
|
usage(parser)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display usage if the command is not supported.
|
|
|
|
data, exists := commandHandlers[args[0]]
|
|
|
|
if !exists {
|
|
|
|
fmt.Fprintf(os.Stderr, "Unrecognized command: %s\n", args[0])
|
|
|
|
usage(parser)
|
2013-10-22 17:01:13 +02:00
|
|
|
os.Exit(1)
|
2013-10-22 05:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the command.
|
2013-11-19 21:38:58 +01:00
|
|
|
err = commandHandler(cfg, args[0], data, args[1:])
|
2013-10-22 05:56:02 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrUsage {
|
|
|
|
usage(parser)
|
2013-10-22 17:01:13 +02:00
|
|
|
os.Exit(1)
|
2013-10-22 05:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
2013-10-22 17:01:13 +02:00
|
|
|
os.Exit(1)
|
2013-10-22 05:56:02 +02:00
|
|
|
}
|
2013-10-09 03:18:49 +02:00
|
|
|
}
|