// TestRpcCommand tests RpcCommand by generating some commands and // trying to send them off. func TestRpcCommand(t *testing.T) { user := "******" pass := "******" server := "invalid" var msg []byte _, err := btcjson.RpcCommand(user, pass, server, msg) if err == nil { t.Errorf("Should fail.") } msg, err = btcjson.CreateMessage("getinfo") if err != nil { t.Errorf("Cannot create valid json message") } _, err = btcjson.RpcCommand(user, pass, server, msg) if err == nil { t.Errorf("Should not connect to server.") } badMsg := []byte("{\"jsonrpc\":\"1.0\",\"id\":\"btcd\",\"method\":\"\"}") _, err = btcjson.RpcCommand(user, pass, server, badMsg) if err == nil { t.Errorf("Cannot have no method in msg..") } return }
// 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. func send(cfg *config, msg []byte) (interface{}, error) { var reply btcjson.Reply var err error if cfg.NoTLS || (cfg.RPCCert == "" && !cfg.TLSSkipVerify) { reply, err = btcjson.RpcCommand(cfg.RPCUser, cfg.RPCPassword, cfg.RPCServer, msg) } else { 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) } if err != nil { return nil, err } if reply.Error != nil { return nil, reply.Error } return reply.Result, nil }
func verifyBtcSig(reqHash []byte, addr string, sig string, conf *util.BitcoindConf) (bool, error) { hb64 := base64.StdEncoding.EncodeToString(reqHash) msg, err := btcjson.NewVerifyMessageCmd(nil, addr, sig, hb64) if err != nil { return false, fmt.Errorf("error while making cmd: %v", err.Error()) } json, err := msg.MarshalJSON() if err != nil { return false, fmt.Errorf("error while marshaling: %v", err.Error()) } resp, err := btcjson.RpcCommand(conf.User, conf.Password, conf.Server, json) if err != nil { return false, fmt.Errorf( "error while making bitcoind JSON-RPC: %v", err.Error()) } if resp.Error != nil { return false, resp.Error } verifyResult, ok := resp.Result.(bool) if !ok { return false, fmt.Errorf("error during bitcoind JSON-RPC: %v", err.Error()) } return verifyResult, nil }
func (bc *BtcCred) SignOcReq(req *msg.OcReq, conf *util.BitcoindConf) error { h, err := getReqSigDataHash(req) if err != nil { return err } hb64 := base64.StdEncoding.EncodeToString(h) msg, err := btcjson.NewSignMessageCmd(nil, bc.Addr, hb64) if err != nil { return fmt.Errorf("error while making cmd: %v", err.Error()) } json, err := msg.MarshalJSON() if err != nil { return fmt.Errorf("error while marshaling: %v", err.Error()) } resp, err := btcjson.RpcCommand(conf.User, conf.Password, conf.Server, json) if err != nil { return fmt.Errorf("error while making bitcoind JSON-RPC: %v", err.Error()) } sig, ok := resp.Result.(string) if !ok { return errors.New("error during bitcoind JSON-RPC") } req.Coins = append(req.Coins, fmt.Sprintf(bc.Addr)) req.CoinSigs = append(req.CoinSigs, fmt.Sprintf(sig)) return nil }
// 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. func send(cfg *config, msg []byte) (interface{}, error) { reply, err := btcjson.RpcCommand(cfg.RpcUser, cfg.RpcPassword, cfg.RpcServer, msg) if err != nil { return nil, err } if reply.Error != nil { return nil, reply.Error } if reply.Result == nil { return nil, ErrNoData } return reply.Result, nil }
func getAnyBtcAddr(conf *util.BitcoindConf) (string, error) { msg, err := btcjson.NewListReceivedByAddressCmd(nil, 0, true) if err != nil { return "", err } json, err := msg.MarshalJSON() if err != nil { return "", err } resp, err := btcjson.RpcCommand(conf.User, conf.Password, conf.Server, json) if err != nil { return "", err } for _, r := range resp.Result.([]interface{}) { result := r.(map[string]interface{}) return result["address"].(string), nil } return "", errors.New("no address found") }