github.com/lbryio/lbcd@v0.22.119/rpcclient/examples/customcommand/main.go (about)

     1  // Copyright (c) 2014-2017 The btcsuite developers
     2  // Copyright (c) 2019-2020 The Namecoin developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package main
     7  
     8  import (
     9  	"encoding/json"
    10  	"log"
    11  
    12  	"github.com/lbryio/lbcd/btcjson"
    13  	"github.com/lbryio/lbcd/rpcclient"
    14  )
    15  
    16  // NameShowCmd defines the name_show JSON-RPC command.
    17  type NameShowCmd struct {
    18  	Name string
    19  }
    20  
    21  // NameShowResult models the data from the name_show command.
    22  type NameShowResult struct {
    23  	Name          string `json:"name"`
    24  	NameEncoding  string `json:"name_encoding"`
    25  	NameError     string `json:"name_error"`
    26  	Value         string `json:"value"`
    27  	ValueEncoding string `json:"value_encoding"`
    28  	ValueError    string `json:"value_error"`
    29  	TxID          string `json:"txid"`
    30  	Vout          uint32 `json:"vout"`
    31  	Address       string `json:"address"`
    32  	IsMine        bool   `json:"ismine"`
    33  	Height        int32  `json:"height"`
    34  	ExpiresIn     int32  `json:"expires_in"`
    35  	Expired       bool   `json:"expired"`
    36  }
    37  
    38  // FutureNameShowResult is a future promise to deliver the result
    39  // of a NameShowAsync RPC invocation (or an applicable error).
    40  type FutureNameShowResult chan *rpcclient.Response
    41  
    42  // Receive waits for the Response promised by the future and returns detailed
    43  // information about a name.
    44  func (r FutureNameShowResult) Receive() (*NameShowResult, error) {
    45  	res, err := rpcclient.ReceiveFuture(r)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	// Unmarshal result as a name_show result object
    51  	var nameShow NameShowResult
    52  	err = json.Unmarshal(res, &nameShow)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	return &nameShow, nil
    58  }
    59  
    60  // NameShowAsync returns an instance of a type that can be used to get the
    61  // result of the RPC at some future time by invoking the Receive function on
    62  // the returned instance.
    63  //
    64  // See NameShow for the blocking version and more details.
    65  func NameShowAsync(c *rpcclient.Client, name string) FutureNameShowResult {
    66  	cmd := &NameShowCmd{
    67  		Name: name,
    68  	}
    69  	return c.SendCmd(cmd)
    70  }
    71  
    72  // NameShow returns detailed information about a name.
    73  func NameShow(c *rpcclient.Client, name string) (*NameShowResult, error) {
    74  	return NameShowAsync(c, name).Receive()
    75  }
    76  
    77  func init() {
    78  	// No special flags for commands in this file.
    79  	flags := btcjson.UsageFlag(0)
    80  
    81  	btcjson.MustRegisterCmd("name_show", (*NameShowCmd)(nil), flags)
    82  }
    83  
    84  func main() {
    85  	// Connect to local namecoin core RPC server using HTTP POST mode.
    86  	connCfg := &rpcclient.ConnConfig{
    87  		Host:         "localhost:8336",
    88  		User:         "yourrpcuser",
    89  		Pass:         "yourrpcpass",
    90  		HTTPPostMode: true, // Namecoin core only supports HTTP POST mode
    91  		DisableTLS:   true, // Namecoin core does not provide TLS by default
    92  	}
    93  	// Notice the notification parameter is nil since notifications are
    94  	// not supported in HTTP POST mode.
    95  	client, err := rpcclient.New(connCfg, nil)
    96  	if err != nil {
    97  		log.Fatal(err)
    98  	}
    99  	defer client.Shutdown()
   100  
   101  	// Get the current block count.
   102  	result, err := NameShow(client, "d/namecoin")
   103  	if err != nil {
   104  		log.Fatal(err)
   105  	}
   106  
   107  	value := result.Value
   108  
   109  	log.Printf("Value of d/namecoin: %s", value)
   110  }