-
Notifications
You must be signed in to change notification settings - Fork 38
/
network.go
276 lines (220 loc) · 6.08 KB
/
network.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"math"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"
)
var flagNodeAddress string
var flagNetworkAddress string
var flagTransaction string
var flagTestnet bool
var magicBytes string
func main() {
//Everything is done at the byte level, and most all network messages
//are sent in little-endian byte order.
//Only IP and port numbers are sent in big endian.
flag.StringVar(&flagTransaction, "transaction", "", "")
flag.StringVar(&flagNetworkAddress, "network-address", "", "")
flag.StringVar(&flagNodeAddress, "node-address", "", "")
flag.BoolVar(&flagTestnet, "testnet", false, "Whether or not to use the bitcoin testnet. Defaults to false")
flag.Parse()
var port string
if flagTestnet {
magicBytes = "0b110907"
port = ":18333"
} else {
magicBytes = "f9beb4d9"
port = ":8333"
}
bufaddr := new(bytes.Buffer)
bufaddr.WriteString(flagNodeAddress)
bufaddr.WriteString(port)
//Attempt to connect to the node
servAddr := bufaddr.String()
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
if err != nil {
println("ResolveTCPAddr failed:", err.Error())
os.Exit(1)
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
fmt.Println(err)
}
//Send a version message to the node.
versionMessage := makeMessage(magicBytes, "version", getVersionMessage())
n, err := conn.Write(versionMessage)
if err != nil {
fmt.Println(err, n)
}
fmt.Println(hex.EncodeToString(versionMessage))
reply := make([]byte, 1024)
_, err = conn.Read(reply)
if err != nil {
println("Write to server failed:", err.Error())
os.Exit(1)
}
fmt.Println("reply from server=", string(reply))
reply2 := make([]byte, 1024)
_, err = conn.Read(reply2)
if err != nil {
println("Write to server failed:", err.Error())
os.Exit(1)
}
fmt.Println("reply from server=", string(reply2))
rawTransaction, err := hex.DecodeString(flagTransaction)
if err != nil {
println("Write of rawTransaction fails", err.Error())
os.Exit(1)
}
//Send the transaction message to the node
txMessage := makeMessage(magicBytes, "tx", rawTransaction)
n, err = conn.Write(txMessage)
if err != nil {
fmt.Println(err, n)
}
for {
reply3 := make([]byte, 1024)
length, err := conn.Read(reply3)
if err != nil {
//do nothing
}
if length > 0 {
fmt.Println("reply from server=")
fmt.Println(string(reply3))
fmt.Println(hex.EncodeToString(reply3))
}
}
conn.Close()
}
func makeMessage(magic string, command string, payload []byte) []byte {
//Messages on the bitcoin protocol consist of
//4 bytes magic value indicating the origin network.
//12 bytes which contains the command you're sending.
//4 bytes which represent the length of your payload
//4 byte checksum which is the first 4 bytes of sha256(sha256(payload))
//your payload
magicBytes, err := hex.DecodeString(magic)
if err != nil {
fmt.Println(err)
}
shaHash := sha256.New()
shaHash.Write(payload)
shaHashFirst := shaHash.Sum(nil)
shaHash2 := sha256.New()
shaHash2.Write(shaHashFirst)
hashedPayload := shaHash2.Sum(nil)
checksum := hashedPayload[0:4]
length := uint32(len(payload))
lengthBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(lengthBytes, length)
commandBytes := make([]byte, 12)
for i := 0; i < 12; i++ {
if i >= len(command) {
commandBytes[i] = 0
} else {
commandBytes[i] = command[i]
}
}
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.LittleEndian, magicBytes)
binary.Write(buffer, binary.LittleEndian, commandBytes)
buffer.Write(lengthBytes)
binary.Write(buffer, binary.LittleEndian, checksum)
buffer.Write(payload)
return buffer.Bytes()
}
func getNetworkAddress(ip string) []byte {
//Network addresses in the bitcoin protocol are represented with
//8 bytes services
//16 bytes IPv6
//2 bytes port
services, err := hex.DecodeString("0100000000000000")
if err != nil {
fmt.Println(err)
}
ipv4Strings := strings.Split(ip, ".")
ipv4Bytes := make([]byte, 4)
for i := 0; i < 4; i++ {
ipByte, err := strconv.Atoi(ipv4Strings[i])
if err != nil {
fmt.Println(err)
}
ipv4Bytes[i] = byte(ipByte)
}
ipv64 := new(bytes.Buffer)
prefix, err := hex.DecodeString("00000000000000000000FFFF")
if err != nil {
fmt.Println(err)
}
ipv64.Write(prefix)
binary.Write(ipv64, binary.BigEndian, ipv4Bytes)
port := make([]byte, 2)
binary.BigEndian.PutUint16(port, uint16(8333))
networkAddressBuffer := new(bytes.Buffer)
binary.Write(networkAddressBuffer, binary.LittleEndian, services)
networkAddressBuffer.Write(ipv64.Bytes())
networkAddressBuffer.Write(port)
return networkAddressBuffer.Bytes()
}
func getVersionMessage() []byte {
//Version messages is the initial message we send
//to the node after the TCP handshake has completed
//and we are connected.
//It consists of
//4 bytes protocol version
//8 bytes services (same as network address)
//8 bytes unix timestamp
//26 bytes addr_recv
//26 bytes addr_from
//8 bytes nonce
//1 byte user agent
//4 bytes start_height
version, err := hex.DecodeString("62EA0000")
if err != nil {
fmt.Println(err)
}
services, err := hex.DecodeString("0100000000000000")
if err != nil {
fmt.Println(err)
}
timestamp := make([]byte, 8)
binary.LittleEndian.PutUint64(timestamp, uint64(time.Now().Unix()))
addrRecv := getNetworkAddress(flagNodeAddress)
addrFrom := getNetworkAddress(flagNetworkAddress) //me
nonce := make([]byte, 8)
for i := 0; i < 8; i++ {
nonce[i] = byte(randInt(0, math.MaxUint8))
}
userAgent, err := hex.DecodeString("00")
if err != nil {
fmt.Println(err)
}
startHeight, err := hex.DecodeString("00000000")
if err != nil {
fmt.Println(err)
}
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.LittleEndian, version)
binary.Write(buffer, binary.LittleEndian, services)
buffer.Write(timestamp)
buffer.Write(addrRecv)
buffer.Write(addrFrom)
binary.Write(buffer, binary.LittleEndian, nonce)
buffer.Write(userAgent)
buffer.Write(startHeight)
return buffer.Bytes()
}
func randInt(min int, max int) uint8 {
rand.Seed(time.Now().UTC().UnixNano())
return uint8(min + rand.Intn(max-min))
}