forked from SkynetLabs/go-skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.go
211 lines (183 loc) · 5.42 KB
/
encryption.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
package skynet
import (
"bytes"
"encoding/json"
"net/url"
"gitlab.com/NebulousLabs/errors"
)
type (
// Skykey contains information about a skykey.
Skykey struct {
Skykey string `json:"skykey"`
Name string `json:"name"`
ID string `json:"id"`
Type string `json:"type"`
}
// AddSkykeyOptions contains the options used for addskykey.
AddSkykeyOptions struct {
Options
}
// CreateSkykeyOptions contains the options used for createskykey.
CreateSkykeyOptions struct {
Options
}
// GetSkykeyOptions contains the options used for skykey GET.
GetSkykeyOptions struct {
Options
}
// GetSkykeysOptions contains the options used for skykeys GET.
GetSkykeysOptions struct {
Options
}
// CreateSkykeyResponse contains the response for creating a skykey.
CreateSkykeyResponse Skykey
// GetSkykeyResponse contains the response for getting a skykey.
GetSkykeyResponse Skykey
// GetSkykeysResponse contains the response for listing skykeys.
GetSkykeysResponse struct {
// Skykeys is the returned list of skykeys.
Skykeys []Skykey `json:"skykeys"`
}
)
var (
// DefaultAddSkykeyOptions contains the default addskykey options.
DefaultAddSkykeyOptions = AddSkykeyOptions{
Options: DefaultOptions("/skynet/addskykey"),
}
// DefaultCreateSkykeyOptions contains the default createskykey options.
DefaultCreateSkykeyOptions = CreateSkykeyOptions{
Options: DefaultOptions("/skynet/createskykey"),
}
// DefaultGetSkykeyOptions contains the default skykey GET options.
DefaultGetSkykeyOptions = GetSkykeyOptions{
Options: DefaultOptions("/skynet/skykey"),
}
// DefaultGetSkykeysOptions contains the default skykeys options.
DefaultGetSkykeysOptions = GetSkykeysOptions{
Options: DefaultOptions("/skynet/skykeys"),
}
)
// AddSkykey stores the given base-64 encoded skykey with the skykey manager.
func (sc *SkynetClient) AddSkykey(skykey string, opts AddSkykeyOptions) error {
body := &bytes.Buffer{}
values := url.Values{}
values.Set("skykey", skykey)
_, err := sc.executeRequest(
requestOptions{
Options: opts.Options,
method: "POST",
reqBody: body,
query: values,
},
)
if err != nil {
return errors.AddContext(err, "could not execute request")
}
return nil
}
// CreateSkykey returns a new skykey created and stored under the given name
// with the given type. skykeyType can be either "public-id" or "private-id".
func (sc *SkynetClient) CreateSkykey(name, skykeyType string, opts CreateSkykeyOptions) (Skykey, error) {
body := &bytes.Buffer{}
values := url.Values{}
values.Set("name", name)
values.Set("type", skykeyType)
resp, err := sc.executeRequest(
requestOptions{
Options: opts.Options,
method: "POST",
reqBody: body,
query: values,
},
)
if err != nil {
return Skykey{}, errors.AddContext(err, "could not execute request")
}
respBody, err := parseResponseBody(resp)
if err != nil {
return Skykey{}, errors.AddContext(err, "could not parse response body")
}
var apiResponse CreateSkykeyResponse
err = json.Unmarshal(respBody.Bytes(), &apiResponse)
if err != nil {
return Skykey{}, errors.AddContext(err, "could not unmarshal response JSON")
}
return Skykey(apiResponse), nil
}
// GetSkykeyByName returns the given skykey given its name.
func (sc *SkynetClient) GetSkykeyByName(name string, opts GetSkykeyOptions) (Skykey, error) {
body := &bytes.Buffer{}
values := url.Values{}
values.Set("name", name)
resp, err := sc.executeRequest(
requestOptions{
Options: opts.Options,
method: "GET",
reqBody: body,
query: values,
},
)
if err != nil {
return Skykey{}, errors.AddContext(err, "could not execute request")
}
respBody, err := parseResponseBody(resp)
if err != nil {
return Skykey{}, errors.AddContext(err, "could not parse response body")
}
var apiResponse GetSkykeyResponse
err = json.Unmarshal(respBody.Bytes(), &apiResponse)
if err != nil {
return Skykey{}, errors.AddContext(err, "could not unmarshal response JSON")
}
return Skykey(apiResponse), nil
}
// GetSkykeyByID returns the given skykey given its ID.
func (sc *SkynetClient) GetSkykeyByID(id string, opts GetSkykeyOptions) (Skykey, error) {
body := &bytes.Buffer{}
values := url.Values{}
values.Set("id", id)
resp, err := sc.executeRequest(
requestOptions{
Options: opts.Options,
method: "GET",
reqBody: body,
query: values,
},
)
if err != nil {
return Skykey{}, errors.AddContext(err, "could not execute request")
}
respBody, err := parseResponseBody(resp)
if err != nil {
return Skykey{}, errors.AddContext(err, "could not parse response body")
}
var apiResponse GetSkykeyResponse
err = json.Unmarshal(respBody.Bytes(), &apiResponse)
if err != nil {
return Skykey{}, errors.AddContext(err, "could not unmarshal response JSON")
}
return Skykey(apiResponse), nil
}
// GetSkykeys returns a list of all skykeys.
func (sc *SkynetClient) GetSkykeys(opts GetSkykeysOptions) ([]Skykey, error) {
resp, err := sc.executeRequest(
requestOptions{
Options: opts.Options,
method: "GET",
reqBody: &bytes.Buffer{},
},
)
if err != nil {
return nil, errors.AddContext(err, "could not execute request")
}
respBody, err := parseResponseBody(resp)
if err != nil {
return nil, errors.AddContext(err, "could not parse response body")
}
var apiResponse GetSkykeysResponse
err = json.Unmarshal(respBody.Bytes(), &apiResponse)
if err != nil {
return nil, errors.AddContext(err, "could not unmarshal response JSON")
}
return apiResponse.Skykeys, nil
}