-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvs.go
130 lines (112 loc) · 2.9 KB
/
nvs.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
package gonvs
import (
"strconv"
"strings"
rpcclient "github.com/bitlum/go-bitcoind-rpc/rpcclient"
)
// NewClient - create new Emercoin NVS Client
func NewClient(task CreateClientTask) (*Client, error) {
if task.Host == "" {
task.Host = "127.0.0.1"
}
if task.RPCPort == "" {
task.RPCPort = "6662"
}
c := Client{}
var err error
c.RPC, err = rpcclient.New(&rpcclient.ConnConfig{
HTTPPostMode: true,
DisableTLS: !task.UseSSL,
Host: task.Host + ":" + task.RPCPort,
User: task.RPCUser,
Pass: task.RPCPassword,
}, nil)
if err != nil {
return nil, err
}
return &c, nil
}
func (c *Client) Write(task WriteEntryTask) error {
requestData := []interface{}{
task.Name,
string(task.Value),
task.Days,
}
if task.ToAddress != "" {
requestData = append(requestData, task.ToAddress)
}
if task.ValueType != "" {
requestData = append(requestData, string(task.ValueType))
}
var result interface{}
err := c.sendRequest("name_new", &result, requestData)
if err != nil {
return err
}
return nil
}
// GetEntrysByAddress - get NVS entrys by given EMC address
func (c *Client) GetEntrysByAddress(task GetEntrysByAddressTask) ([]Entry, error) {
requestData := []interface{}{
task.Address,
}
if task.MaxValueLength > 0 {
requestData = append(requestData, task.MaxValueLength)
}
if task.ValueType != "" {
requestData = append(requestData, string(task.ValueType))
}
result := []Entry{}
err := c.sendRequest("name_scan_address", &result, requestData)
if err != nil {
if isNVSNotFound(err) {
return result, nil
}
return nil, err
}
return result, nil
}
// DeleteEntry - delete NVS entry
func (c *Client) DeleteEntry(entryName string) error {
var result interface{}
return c.sendRequest("name_delete", &result, []interface{}{entryName})
}
// GetEntry - get NVS entry value
func (c *Client) GetEntry(task GetEntryTask) (Entry, error) {
var result Entry
requestData := []interface{}{
task.Name, task.ValueType,
}
if task.ValueType != "" {
requestData = append(requestData, string(task.ValueType))
}
if task.Filepath != "" {
requestData = append(requestData, task.Filepath)
}
err := c.sendRequest("name_show", &result, requestData)
if err != nil {
if isNVSNotFound(err) {
return Entry{}, nil
}
return Entry{}, err
}
return result, nil
}
func isNVSNotFound(err error) bool {
return strings.Contains(err.Error(), "found nothing")
}
// GetEntryHistory - look up the current and all past data for the given name.
func (c *Client) GetEntryHistory(task GetEntryHistoryTask) error {
var result interface{}
requestData := []interface{}{
task.Name,
}
if task.LoadFullHistory {
requestData = append(requestData, "true")
}
if task.ValueType != "" {
requestData = append(requestData, strconv.FormatBool(task.LoadFullHistory))
requestData = append(requestData, string(task.ValueType))
}
return c.sendRequest("name_history", &result, requestData)
}