2015-05-01 19:03:51 +02:00
|
|
|
// Copyright (c) 2014-2015 The btcsuite developers
|
2014-05-02 06:16:31 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
2014-07-03 02:33:54 +02:00
|
|
|
|
2015-02-05 22:50:03 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-16 04:21:20 +01:00
|
|
|
"github.com/btcsuite/btcrpcclient"
|
2015-01-15 17:41:20 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2014-05-02 06:16:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Only override the handlers for notifications you care about.
|
2014-05-09 07:44:19 +02:00
|
|
|
// Also note most of these handlers will only be called if you register
|
|
|
|
// for notifications. See the documentation of the btcrpcclient
|
|
|
|
// NotificationHandlers type for more details about each handler.
|
2014-05-02 06:16:31 +02:00
|
|
|
ntfnHandlers := btcrpcclient.NotificationHandlers{
|
2015-02-05 22:50:03 +01:00
|
|
|
OnBlockConnected: func(hash *wire.ShaHash, height int32) {
|
2014-05-02 06:16:31 +02:00
|
|
|
log.Printf("Block connected: %v (%d)", hash, height)
|
|
|
|
},
|
2015-02-05 22:50:03 +01:00
|
|
|
OnBlockDisconnected: func(hash *wire.ShaHash, height int32) {
|
2014-06-05 19:48:56 +02:00
|
|
|
log.Printf("Block disconnected: %v (%d)", hash, height)
|
2014-05-02 06:16:31 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2014-05-09 07:44:19 +02:00
|
|
|
// Register for block connect and disconnect notifications.
|
|
|
|
if err := client.NotifyBlocks(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Println("NotifyBlocks: Registration Complete")
|
|
|
|
|
2014-05-02 06:16:31 +02:00
|
|
|
// 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()
|
2014-05-11 20:30:25 +02:00
|
|
|
log.Println("Client shutdown complete.")
|
2014-05-02 06:16:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Wait until the client either shuts down gracefully (or the user
|
|
|
|
// terminates the process with Ctrl+C).
|
|
|
|
client.WaitForShutdown()
|
|
|
|
}
|