// 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 ( "io/ioutil" "log" "path/filepath" "time" "github.com/kaspanet/kaspad/domainmessage" "github.com/kaspanet/kaspad/rpc/client" "github.com/kaspanet/kaspad/util" ) func main() { // Only override the handlers for notifications you care about. // Also note most of these handlers will only be called if you register // for notifications. See the documentation of the rpcclient // NotificationHandlers type for more details about each handler. ntfnHandlers := client.NotificationHandlers{ OnFilteredBlockAdded: func(blueScore uint64, header *domainmessage.BlockHeader, txns []*util.Tx) { log.Printf("Block added: %s (%d) %s", header.BlockHash(), blueScore, header.Timestamp) }, } // Connect to local kaspad RPC server using websockets. kaspadHomeDir := util.AppDataDir("kaspad", false) certs, err := ioutil.ReadFile(filepath.Join(kaspadHomeDir, "rpc.cert")) if err != nil { log.Fatal(err) } connCfg := &client.ConnConfig{ Host: "localhost:16110", Endpoint: "ws", User: "yourrpcuser", Pass: "yourrpcpass", Certificates: certs, } client, err := client.New(connCfg, &ntfnHandlers) if err != nil { log.Fatal(err) } // Register for block added notifications. if err := client.NotifyBlocks(); err != nil { log.Fatal(err) } log.Println("NotifyBlocks: Registration Complete") // 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 shutdown complete.") }) // Wait until the client either shuts down gracefully (or the user // terminates the process with Ctrl+C). client.WaitForShutdown() }