2017-07-22 04:53:54 +02:00
|
|
|
// Copyright (c) 2014-2017 The btcsuite developers
|
2014-06-06 03:24:03 +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
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2021-10-15 07:45:32 +02:00
|
|
|
"github.com/lbryio/lbcd/rpcclient"
|
|
|
|
btcutil "github.com/lbryio/lbcutil"
|
2014-06-06 03:24:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Only override the handlers for notifications you care about.
|
|
|
|
// Also note most of the handlers will only be called if you register
|
2017-07-22 04:53:54 +02:00
|
|
|
// for notifications. See the documentation of the rpcclient
|
2014-06-06 03:24:03 +02:00
|
|
|
// NotificationHandlers type for more details about each handler.
|
2017-07-22 04:53:54 +02:00
|
|
|
ntfnHandlers := rpcclient.NotificationHandlers{
|
2014-06-06 03:24:03 +02:00
|
|
|
OnAccountBalance: func(account string, balance btcutil.Amount, confirmed bool) {
|
|
|
|
log.Printf("New balance for account %s: %v", account,
|
|
|
|
balance)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-15 07:45:32 +02:00
|
|
|
// Connect to local lbcwallet RPC server using websockets.
|
|
|
|
certHomeDir := btcutil.AppDataDir("lbcwallet", false)
|
2014-06-06 03:24:03 +02:00
|
|
|
certs, err := ioutil.ReadFile(filepath.Join(certHomeDir, "rpc.cert"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-07-22 04:53:54 +02:00
|
|
|
connCfg := &rpcclient.ConnConfig{
|
2018-08-15 02:59:48 +02:00
|
|
|
Host: "localhost:19245",
|
2014-06-12 20:00:42 +02:00
|
|
|
Endpoint: "ws",
|
2014-06-06 03:24:03 +02:00
|
|
|
User: "yourrpcuser",
|
|
|
|
Pass: "yourrpcpass",
|
|
|
|
Certificates: certs,
|
|
|
|
}
|
2017-07-22 04:53:54 +02:00
|
|
|
client, err := rpcclient.New(connCfg, &ntfnHandlers)
|
2014-06-06 03:24:03 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the list of unspent transaction outputs (utxos) that the
|
|
|
|
// connected wallet has at least one private key for.
|
|
|
|
unspent, err := client.ListUnspent()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Printf("Num unspent outputs (utxos): %d", len(unspent))
|
|
|
|
if len(unspent) > 0 {
|
|
|
|
log.Printf("First utxo:\n%v", spew.Sdump(unspent[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 shutdown complete.")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Wait until the client either shuts down gracefully (or the user
|
|
|
|
// terminates the process with Ctrl+C).
|
|
|
|
client.WaitForShutdown()
|
|
|
|
}
|