-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
236 lines (203 loc) · 6.27 KB
/
lib.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
package main
/*
#include <stdlib.h>
#ifndef RESPONSE_H
#define RESPONSE_H
#include "response.h"
#endif
*/
import "C"
import (
"bytes"
"github.com/AxLabs/neofs-api-shared-lib/accounting"
"github.com/AxLabs/neofs-api-shared-lib/client"
"github.com/AxLabs/neofs-api-shared-lib/container"
"github.com/AxLabs/neofs-api-shared-lib/netmap"
"github.com/AxLabs/neofs-api-shared-lib/object"
"github.com/AxLabs/neofs-api-shared-lib/response"
"github.com/google/uuid"
"unsafe"
)
// region client
//export CreateClient
func CreateClient(privateKey *C.char, neofsEndpoint *C.char) C.pointerResponse {
privKey := GetECDSAPrivKey(privateKey)
endpoint := toGoString(neofsEndpoint)
return responseToC(client.CreateClient(privKey, endpoint))
}
//export DeleteClient
func DeleteClient(clientID *C.char) C.pointerResponse {
cliID, err := uuid.Parse(C.GoString(clientID))
if err != nil {
return responseToC(response.Error(err))
}
return responseToC(client.DeleteClient(cliID))
}
// endregion client
// region accounting
//export GetBalance
func GetBalance(clientID *C.char, publicKey *C.char) C.pointerResponse {
key, err := UserIDFromPublicKey(publicKey)
if err != nil {
return responseToC(response.Error(err))
}
c, err := GetClient(clientID)
if err != nil {
return responseToC(response.Error(err))
}
return responseToC(accounting.GetBalance(c, key))
}
// endregion accounting
// region netmap
//export GetEndpoint
func GetEndpoint(clientID *C.char) C.pointerResponse {
c, err := GetClient(clientID)
if err != nil {
return responseToC(response.Error(err))
}
return responseToC(netmap.GetEndpoint(c))
}
//export GetNetworkInfo
func GetNetworkInfo(clientID *C.char) C.pointerResponse {
c, err := GetClient(clientID)
if err != nil {
return responseToC(response.Error(err))
}
return responseToC(netmap.GetNetworkInfo(c))
}
// endregion netmap
// region container
//export PutContainer
func PutContainer(clientID *C.char, v2Container *C.char) C.response {
sdkContainer, err := getContainerFromC(v2Container)
if err != nil {
return stringResponseToC(response.StringError(err))
}
c, err := GetClient(clientID)
if err != nil {
return stringResponseToC(response.StringError(err))
}
return stringResponseToC(container.PutContainer(c, sdkContainer))
}
//export GetContainer
func GetContainer(clientID *C.char, containerID *C.char) C.pointerResponse {
cid, err := getContainerIDFromC(containerID)
if err != nil {
return responseToC(response.Error(err))
}
c, err := GetClient(clientID)
if err != nil {
return responseToC(response.Error(err))
}
return responseToC(container.GetContainer(c, cid))
}
//export DeleteContainer
func DeleteContainer(clientID *C.char, containerID *C.char) C.pointerResponse {
cid, err := getContainerIDFromC(containerID)
if err != nil {
return responseToC(response.Error(err))
}
c, err := GetClient(clientID)
if err != nil {
return responseToC(response.Error(err))
}
container.DeleteContainer(c, cid)
if err != nil {
return responseToC(response.Error(err))
}
return responseToC(response.NewBoolean(true))
}
//export ListContainer
func ListContainer(clientID *C.char, ownerPubKey *C.char) C.pointerResponse {
userID, err := UserIDFromPublicKey(ownerPubKey)
if err != nil {
return responseToC(response.Error(err))
}
c, err := GetClient(clientID)
if err != nil {
return responseToC(response.Error(err))
}
return responseToC(container.ListContainer(c, userID))
}
// endregion container
// region object
//export CreateObjectWithoutAttributes
func CreateObjectWithoutAttributes(clientID *C.char, containerID *C.char, fileBytes unsafe.Pointer,
fileSize C.int, sessionSignerPrivKey *C.char) C.response {
return CreateObject(clientID, containerID, fileBytes, fileSize, sessionSignerPrivKey, nil, nil)
}
//export CreateObject
func CreateObject(clientID *C.char, containerID *C.char, fileBytes unsafe.Pointer, fileSize C.int,
sessionSignerPrivKey *C.char, attributeKey *C.char, attributeValue *C.char) C.response {
// Todo: Add functionality to pass more attributes to C (dynamic number of attributes).
readBytes := C.GoBytes(fileBytes, fileSize)
reader := bytes.NewReader(readBytes)
privKey := GetECDSAPrivKey(sessionSignerPrivKey)
cid, err := getContainerIDFromC(containerID)
if err != nil {
return stringResponseToC(response.StringError(err))
}
var attributes [][2]string
if attributeKey != nil {
key := C.GoString(attributeKey)
value := C.GoString(attributeValue)
attributes = append(attributes, [2]string{key, value})
}
c, err := GetClient(clientID)
if err != nil {
return stringResponseToC(response.StringError(err))
}
return stringResponseToC(object.CreateObject(c, *cid, *privKey, attributes, reader))
}
//export ReadObject
func ReadObject(clientID *C.char, containerID *C.char, objectID *C.char, signer *C.char) C.pointerResponse {
cid, err := getContainerIDFromC(containerID)
if err != nil {
return responseToC(response.Error(err))
}
oid, err := getObjectIDFromC(objectID)
if err != nil {
return responseToC(response.Error(err))
}
privKey := GetECDSAPrivKey(signer)
c, err := GetClient(clientID)
if err != nil {
return responseToC(response.Error(err))
}
return responseToC(object.ReadObject(c, *cid, *oid, *privKey))
}
//export GetObjectHead
func GetObjectHead(clientID *C.char, containerID *C.char, objectID *C.char, signer *C.char) C.pointerResponse {
cid, err := getContainerIDFromC(containerID)
if err != nil {
return responseToC(response.Error(err))
}
oid, err := getObjectIDFromC(objectID)
if err != nil {
return responseToC(response.Error(err))
}
privKey := GetECDSAPrivKey(signer)
c, err := GetClient(clientID)
if err != nil {
return responseToC(response.Error(err))
}
return responseToC(object.GetObjectHead(c, *cid, *oid, *privKey))
}
//export DeleteObject
func DeleteObject(clientID *C.char, containerID *C.char, objectID *C.char, signer *C.char) C.response {
cid, err := getContainerIDFromC(containerID)
if err != nil {
return stringResponseToC(response.StringError(err))
}
oid, err := getObjectIDFromC(objectID)
if err != nil {
return stringResponseToC(response.StringError(err))
}
privKey := GetECDSAPrivKey(signer)
c, err := GetClient(clientID)
if err != nil {
return stringResponseToC(response.StringError(err))
}
return stringResponseToC(object.DeleteObject(c, *cid, *oid, *privKey))
}
// endregion object