-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
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
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
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,41 @@ | ||
Bitcoin Core HTTP POST Over Unix Socket Example | ||
============================== | ||
|
||
This example shows how to use the rpcclient package to connect to a Bitcoin | ||
Core RPC server using HTTP POST mode over a Unix Socket with TLS disabled | ||
and gets the current block count. | ||
|
||
## 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: | ||
|
||
```Go | ||
User: "yourrpcuser", | ||
Pass: "yourrpcpass", | ||
``` | ||
|
||
As Bitcoin Core supports only HTTP, we'll redirect RPC requests from the | ||
Unix Socket to Bitcoin Core. For this example, we'll use the `socat` command: | ||
|
||
``` | ||
socat -d UNIX-LISTEN:"my-unix-socket-path",fork TCP:"host-address" | ||
socat -d UNIX-LISTEN:/tmp/test.XXXX,fork TCP:localhost:8332 | ||
``` | ||
|
||
Finally, navigate to the example's directory and run it with: | ||
|
||
```bash | ||
$ cd $GOPATH/src/github.com/btcsuite/btcd/rpcclient/examples/bitcoincorehttp | ||
$ 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,39 @@ | ||
// Copyright (c) 2014-2017 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/btcsuite/btcd/rpcclient" | ||
) | ||
|
||
func main() { | ||
// Connect to local bitcoin core RPC server using HTTP POST mode over a Unix Socket. | ||
connCfg := &rpcclient.ConnConfig{ | ||
// The value passed doesn't matter as long as it's valid | ||
// To avoid failures, functions relying on a host value still require one to be passed. If the client has a default host, it will be used as long as it's valid. | ||
Host: "localhost:8332", | ||
UnixSocketPath: "/tmp/test.XXXX", | ||
User: "yourrpcuser", | ||
Pass: "yourrpcpass", | ||
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode | ||
DisableTLS: true, // Bitcoin 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. | ||
blockCount, err := client.GetBlockCount() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("Block count: %d", blockCount) | ||
} |
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