New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rpcclient: Export symbols needed for custom commands #1457
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Custom Command Example | ||
====================== | ||
|
||
This example shows how to use custom commands with the rpcclient package, by | ||
implementing the `name_show` command from Namecoin Core. | ||
|
||
## Running the Example | ||
|
||
The first step is to use `go get` to download and install the rpcclient package: | ||
|
||
```bash | ||
$ go get github.com/btcsuite/btcd/rpcclient | ||
``` | ||
|
||
Next, modify the `main.go` source to specify the correct RPC username and | ||
password for the RPC server of your Namecoin Core node: | ||
|
||
```Go | ||
User: "yourrpcuser", | ||
Pass: "yourrpcpass", | ||
``` | ||
|
||
Finally, navigate to the example's directory and run it with: | ||
|
||
```bash | ||
$ cd $GOPATH/src/github.com/btcsuite/btcd/rpcclient/examples/customcommand | ||
$ go run *.go | ||
``` | ||
|
||
## License | ||
|
||
This example is licensed under the [copyfree](http://copyfree.org) ISC License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright (c) 2014-2017 The btcsuite developers | ||
// Copyright (c) 2019-2020 The Namecoin developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
onyb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
|
||
"github.com/btcsuite/btcd/btcjson" | ||
"github.com/btcsuite/btcd/rpcclient" | ||
) | ||
|
||
// NameShowCmd defines the name_show JSON-RPC command. | ||
type NameShowCmd struct { | ||
Name string | ||
} | ||
|
||
// NameShowResult models the data from the name_show command. | ||
type NameShowResult struct { | ||
Name string `json:"name"` | ||
NameEncoding string `json:"name_encoding"` | ||
NameError string `json:"name_error"` | ||
Value string `json:"value"` | ||
ValueEncoding string `json:"value_encoding"` | ||
ValueError string `json:"value_error"` | ||
TxID string `json:"txid"` | ||
Vout uint32 `json:"vout"` | ||
Address string `json:"address"` | ||
IsMine bool `json:"ismine"` | ||
Height int32 `json:"height"` | ||
ExpiresIn int32 `json:"expires_in"` | ||
Expired bool `json:"expired"` | ||
} | ||
|
||
// FutureNameShowResult is a future promise to deliver the result | ||
// of a NameShowAsync RPC invocation (or an applicable error). | ||
type FutureNameShowResult chan *rpcclient.Response | ||
|
||
// Receive waits for the Response promised by the future and returns detailed | ||
// information about a name. | ||
func (r FutureNameShowResult) Receive() (*NameShowResult, error) { | ||
res, err := rpcclient.ReceiveFuture(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Unmarshal result as a name_show result object | ||
var nameShow NameShowResult | ||
err = json.Unmarshal(res, &nameShow) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &nameShow, nil | ||
} | ||
|
||
// NameShowAsync returns an instance of a type that can be used to get the | ||
// result of the RPC at some future time by invoking the Receive function on | ||
// the returned instance. | ||
// | ||
// See NameShow for the blocking version and more details. | ||
func NameShowAsync(c *rpcclient.Client, name string) FutureNameShowResult { | ||
cmd := &NameShowCmd{ | ||
Name: name, | ||
} | ||
return c.SendCmd(cmd) | ||
} | ||
|
||
// NameShow returns detailed information about a name. | ||
func NameShow(c *rpcclient.Client, name string) (*NameShowResult, error) { | ||
return NameShowAsync(c, name).Receive() | ||
} | ||
|
||
func init() { | ||
// No special flags for commands in this file. | ||
flags := btcjson.UsageFlag(0) | ||
|
||
btcjson.MustRegisterCmd("name_show", (*NameShowCmd)(nil), flags) | ||
} | ||
|
||
func main() { | ||
// Connect to local namecoin core RPC server using HTTP POST mode. | ||
connCfg := &rpcclient.ConnConfig{ | ||
Host: "localhost:8336", | ||
User: "yourrpcuser", | ||
Pass: "yourrpcpass", | ||
HTTPPostMode: true, // Namecoin core only supports HTTP POST mode | ||
DisableTLS: true, // Namecoin core does not provide TLS by default | ||
} | ||
// Notice the notification parameter is nil since notifications are | ||
// not supported in HTTP POST mode. | ||
client, err := rpcclient.New(connCfg, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer client.Shutdown() | ||
|
||
// Get the current block count. | ||
result, err := NameShow(client, "d/namecoin") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
value := result.Value | ||
|
||
log.Printf("Value of d/namecoin: %s", value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example assumes that the user is using Go modules, which is no longer the default with newer Go versions. I'd suggest replacing 9-13 with:
The first step is to clone the
btcd
repo:$ git clone github.com/btcsuite/btcd $ cd btcd
And then replace 23-28 with:
Finally, run the example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Readme is copied nearly verbatim from the
bitcoincorehttp
example's Readme; for consistency I prefer not to make the requested changes as part of this PR (but I'd be fine with those changes as a separate PR that covers both examples' Readmes).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the changes suggested here, but I'm happy to take care of it in a separate PR.