-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConnectAndAddBlockToServer.go
109 lines (95 loc) · 3.36 KB
/
ConnectAndAddBlockToServer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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"
"time"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/chaincfg"
)
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 := rpcclient.NotificationHandlers{
OnFilteredBlockConnected: func(height int32, header *wire.BlockHeader, txns []*btcutil.Tx) {
log.Printf("Block connected: %v (%d) %v",
header.BlockHash(), height, header.Timestamp)
},
OnFilteredBlockDisconnected: func(height int32, header *wire.BlockHeader) {
log.Printf("Block disconnected: %v (%d) %v",
header.BlockHash(), height, header.Timestamp)
},
}
// Connect to local btcd RPC server using websockets.
certs, err := ioutil.ReadFile("rpc.cert")
if err != nil {
log.Fatal(err)
}
connCfg := &rpcclient.ConnConfig{
Host: "localhost:18556",
Endpoint: "ws",
User: "zjMvszU1Wbv8ApnlMjbc/6kVJGE=",
Pass: "FiX+xbrC+rsdajHrbs68Oiav5tc=",
Certificates: certs,
}
client, err := rpcclient.New(connCfg, &ntfnHandlers)
if err != nil {
log.Fatal(err)
}
// Register for block connect and disconnect 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)
// Create a new transaction object and send it to the server. Used dummy parameters (currently the server rejects this transaction
// because it treats it is an orphan transaction; likely an issue due to not linking with the original genesis block)
var array = []btcjson.TransactionInput{{Txid: "e6da89de7a6b8508ce8f371a3d0535b04b5e108cb1a6e9284602d3bfd357c018", Vout: 1}}
addr, err := btcutil.DecodeAddress("SW8yPHHVAeedS5JMsACPuARdb3AxidHUkv", &chaincfg.SimNetParams)
if err != nil {
log.Fatal(err)
}
amt, err := btcutil.NewAmount(0.49213337)
if err != nil {
log.Fatal(err)
}
var obj = map[btcutil.Address]btcutil.Amount{addr: amt}
var num = int64(0)
val, err := client.CreateRawTransaction(array, obj, &num)
if err != nil {
log.Fatal(err)
}
log.Printf("Check: %d\n", val)
client.AddNode("127.0.0.1:12345", "add")
countOfConnects, err := client.GetConnectionCount()
log.Printf("Num Connects: %v\n", countOfConnects)
retVal, err := client.SendRawTransaction(val, false)
if err != nil {
log.Fatal(err)
}
log.Printf("Check 2 Second: %v\n", retVal)
// 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()
}