1 // Copyright (c) 2014-2017 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
13 "github.com/btcsuite/btcd/rpcclient"
14 "github.com/btcsuite/btcutil"
15 "github.com/davecgh/go-spew/spew"
19 // Only override the handlers for notifications you care about.
20 // Also note most of the handlers will only be called if you register
21 // for notifications. See the documentation of the rpcclient
22 // NotificationHandlers type for more details about each handler.
23 ntfnHandlers := rpcclient.NotificationHandlers{
24 OnAccountBalance: func(account string, balance btcutil.Amount, confirmed bool) {
25 log.Printf("New balance for account %s: %v", account,
30 // Connect to local btcwallet RPC server using websockets.
31 certHomeDir := btcutil.AppDataDir("btcwallet", false)
32 certs, err := ioutil.ReadFile(filepath.Join(certHomeDir, "rpc.cert"))
36 connCfg := &rpcclient.ConnConfig{
37 Host: "localhost:18332",
43 client, err := rpcclient.New(connCfg, &ntfnHandlers)
48 // Get the list of unspent transaction outputs (utxos) that the
49 // connected wallet has at least one private key for.
50 unspent, err := client.ListUnspent()
54 log.Printf("Num unspent outputs (utxos): %d", len(unspent))
56 log.Printf("First utxo:\n%v", spew.Sdump(unspent[0]))
59 // For this example gracefully shutdown the client after 10 seconds.
60 // Ordinarily when to shutdown the client is highly application
62 log.Println("Client shutdown in 10 seconds...")
63 time.AfterFunc(time.Second*10, func() {
64 log.Println("Client shutting down...")
66 log.Println("Client shutdown complete.")
69 // Wait until the client either shuts down gracefully (or the user
70 // terminates the process with Ctrl+C).
71 client.WaitForShutdown()