-
Notifications
You must be signed in to change notification settings - Fork 0
/
electrumx.go
65 lines (54 loc) · 1.47 KB
/
electrumx.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
package main
import (
"bufio"
"fmt"
"net"
)
const (
defaultElectrumxServerHost = "127.0.0.1"
defaultElectrumxServerPort = 60401
)
type ElectrumxClient struct {
host string
port int
conn net.Conn
}
func NewElectrumxClient(host string, port int) *ElectrumxClient {
return &ElectrumxClient{
host: host,
port: port,
}
}
func (client *ElectrumxClient) Dial() error {
conn, err := net.Dial("tcp", fmt.Sprintf("%v:%v", client.host, client.port))
if err != nil {
return err
}
client.conn = conn
return nil
}
func (client *ElectrumxClient) call0(id int, method string) error {
tmpl := `{"id":%v, "method": "%v", "params": []}` + "\n"
raw := fmt.Sprintf(tmpl, id, method)
_, err := client.conn.Write([]byte(raw))
return err
}
func (client *ElectrumxClient) call1(id int, method string, params ...interface{}) error {
tmpl := `{"id":%v, "method": "%v", "params": [%v]}` + "\n"
allParams := append([]interface{}{id, method}, params...)
raw := fmt.Sprintf(tmpl, allParams...)
fmt.Println(raw)
_, err := client.conn.Write([]byte(raw))
return err
}
func (client *ElectrumxClient) call2(id int, method string, params ...interface{}) error {
tmpl := `{"id":%v, "method": "%v", "params": [%v, %v]}` + "\n"
allParams := append([]interface{}{id, method}, params...)
raw := fmt.Sprintf(tmpl, allParams...)
_, err := client.conn.Write([]byte(raw))
return err
}
func (client *ElectrumxClient) recv() ([]byte, error) {
r := bufio.NewReader(client.conn)
return r.ReadBytes('\n')
}