Add a couple of examples.
This commit is contained in:
parent
cf409a8d79
commit
43ca7c0f3c
2 changed files with 101 additions and 0 deletions
35
examples/bitcoincorehttp/main.go
Normal file
35
examples/bitcoincorehttp/main.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
// Copyright (c) 2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/conformal/btcrpcclient"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Connect to local bitcoin core RPC server using HTTP POST mode.
|
||||||
|
connCfg := &btcrpcclient.ConnConfig{
|
||||||
|
Host: "localhost:18332",
|
||||||
|
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 := btcrpcclient.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)
|
||||||
|
}
|
66
examples/btcdwebsockets/main.go
Normal file
66
examples/btcdwebsockets/main.go
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
// Copyright (c) 2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/conformal/btcutil"
|
||||||
|
"github.com/conformal/btcwire"
|
||||||
|
"github.com/conformal/btcrpcclient"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Only override the handlers for notifications you care about.
|
||||||
|
ntfnHandlers := btcrpcclient.NotificationHandlers{
|
||||||
|
OnBlockConnected: func(hash *btcwire.ShaHash, height int32) {
|
||||||
|
log.Printf("Block connected: %v (%d)", hash, height)
|
||||||
|
},
|
||||||
|
OnBlockDisconnected: func(hash *btcwire.ShaHash, height int32) {
|
||||||
|
log.Printf("Block disconnected: %v", hash, height)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect to local btcd RPC server using websockets.
|
||||||
|
btcdHomeDir := btcutil.AppDataDir("btcd", false)
|
||||||
|
certs, err := ioutil.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
connCfg := &btcrpcclient.ConnConfig{
|
||||||
|
Host: "localhost:8334",
|
||||||
|
Endpoint: "ws",
|
||||||
|
User: "yourrpcuser",
|
||||||
|
Pass: "yourrpcpass",
|
||||||
|
Certificates: certs,
|
||||||
|
}
|
||||||
|
client, err := btcrpcclient.New(connCfg, &ntfnHandlers)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the current block count.
|
||||||
|
blockCount, err := client.GetBlockCount()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Printf("Block count: %d", blockCount)
|
||||||
|
|
||||||
|
// For this example gracefully shutdown the client after 10 seconds.
|
||||||
|
// Ordinarily when to shutdown the client is highly application
|
||||||
|
// specific.
|
||||||
|
log.Println("Client shutdown in 10 seconds...")
|
||||||
|
time.AfterFunc(time.Second*10, func() {
|
||||||
|
log.Println("Client shutting down...")
|
||||||
|
client.Shutdown()
|
||||||
|
log.Println("Client shutting complete.")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Wait until the client either shuts down gracefully (or the user
|
||||||
|
// terminates the process with Ctrl+C).
|
||||||
|
client.WaitForShutdown()
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue