-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
301 lines (229 loc) · 8 KB
/
wrapper.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package wasp_wallet_sdk
import (
"errors"
"github.com/awnumar/memguard"
"github.com/goccy/go-json"
"github.com/iotaledger/wasp-wallet-sdk/lib_loader"
"github.com/iotaledger/wasp-wallet-sdk/types"
"github.com/ebitengine/purego"
)
type (
IotaClientPtr uintptr
IotaWalletPtr uintptr
IotaSecretManagerPtr uintptr
ProtectedStringPtr *byte
)
type IOTASDK struct {
handle uintptr
libInitLogger func(*byte) bool
libCreateClient func(*byte) IotaClientPtr
libCreateWallet func(*byte) IotaWalletPtr
libCreateSecretManager func(*byte) IotaSecretManagerPtr
libDestroyClient func(IotaClientPtr) bool
libDestroyWallet func(IotaWalletPtr) bool
libDestroySecretManager func(IotaSecretManagerPtr) bool
libDestroyString func(uintptr) bool
libGetClientFromWallet func(IotaWalletPtr) IotaClientPtr
libGetSecretManagerFromWallet func(IotaWalletPtr) IotaSecretManagerPtr
libCallClientMethod func(IotaClientPtr, *byte) uintptr
libCallWalletMethod func(IotaWalletPtr, *byte) uintptr
libCallSecretManagerMethod func(IotaSecretManagerPtr, *byte) uintptr
libCallUtilsMethod func(*byte) uintptr
libListenWallet func(IotaWalletPtr, *byte, uintptr) bool // uintptr is a callback (purego.NewCallback)
libGetLastError func() string
}
// Encodes an object into a JSON string protected by memguard
// Uses an alternative JSON library to mitigate hidden copies of the serialized message.
func SerializeGuarded(obj any) (ProtectedStringPtr, func(), error) {
stream := memguard.NewStream()
err := json.NewEncoder(stream).Encode(obj)
if err != nil {
return nil, func() {}, err
}
preBuffer, err := stream.Flush()
if err != nil {
return nil, func() {}, err
}
enclave := preBuffer.Seal()
buf, err := enclave.Open()
if err != nil {
return nil, func() {}, err
}
strPointer, cfree := CStringGo(buf.Data())
return strPointer, func() {
buf.Destroy()
cfree()
}, nil
}
func NewIotaSDK(libPath string) (*IOTASDK, error) {
iotaSDK, err := lib_loader.LoadLibrary(libPath)
if err != nil {
return nil, err
}
iotaSDKNative := IOTASDK{
handle: iotaSDK,
}
purego.RegisterLibFunc(&iotaSDKNative.libInitLogger, iotaSDK, "init_logger")
purego.RegisterLibFunc(&iotaSDKNative.libCreateClient, iotaSDK, "create_client")
purego.RegisterLibFunc(&iotaSDKNative.libCreateWallet, iotaSDK, "create_wallet")
purego.RegisterLibFunc(&iotaSDKNative.libCreateSecretManager, iotaSDK, "create_secret_manager")
purego.RegisterLibFunc(&iotaSDKNative.libDestroyClient, iotaSDK, "destroy_client")
purego.RegisterLibFunc(&iotaSDKNative.libDestroyWallet, iotaSDK, "destroy_wallet")
purego.RegisterLibFunc(&iotaSDKNative.libDestroySecretManager, iotaSDK, "destroy_secret_manager")
purego.RegisterLibFunc(&iotaSDKNative.libDestroyString, iotaSDK, "destroy_string")
purego.RegisterLibFunc(&iotaSDKNative.libGetClientFromWallet, iotaSDK, "get_client_from_wallet")
purego.RegisterLibFunc(&iotaSDKNative.libGetSecretManagerFromWallet, iotaSDK, "get_secret_manager_from_wallet")
purego.RegisterLibFunc(&iotaSDKNative.libCallClientMethod, iotaSDK, "call_client_method")
purego.RegisterLibFunc(&iotaSDKNative.libCallWalletMethod, iotaSDK, "call_wallet_method")
purego.RegisterLibFunc(&iotaSDKNative.libCallSecretManagerMethod, iotaSDK, "call_secret_manager_method")
purego.RegisterLibFunc(&iotaSDKNative.libCallUtilsMethod, iotaSDK, "call_utils_method")
purego.RegisterLibFunc(&iotaSDKNative.libListenWallet, iotaSDK, "listen_wallet")
purego.RegisterLibFunc(&iotaSDKNative.libGetLastError, iotaSDK, "binding_get_last_error")
return &iotaSDKNative, nil
}
func (i *IOTASDK) Utils() *Utils {
return &Utils{sdk: i}
}
func (i *IOTASDK) Destroy() {
_ = lib_loader.UnloadLibrary(i.handle)
}
func (i *IOTASDK) GetLastError() error {
result := i.libGetLastError()
if result == "" {
return nil
}
return errors.New(result)
}
func (i *IOTASDK) InitLogger(loggerConfig types.ILoggerConfig) (bool, error) {
msg, free, err := SerializeGuarded(loggerConfig)
defer free()
if err != nil {
return false, err
}
if !i.libInitLogger(msg) {
return false, i.GetLastError()
}
return true, nil
}
func (i *IOTASDK) CreateClient(clientOptions types.ClientOptions) (clientPtr IotaClientPtr, err error) {
msg, free, err := SerializeGuarded(clientOptions)
defer free()
if err != nil {
return 0, err
}
if clientPtr = i.libCreateClient(msg); clientPtr == 0 {
return 0, i.GetLastError()
}
return clientPtr, nil
}
func (i *IOTASDK) CreateSecretManager(secretManagerOptions types.WalletOptionsSecretManager) (secretManagerPtr IotaSecretManagerPtr, err error) {
bytes, free, err := SerializeGuarded(secretManagerOptions)
defer free()
if err != nil {
return 0, err
}
if secretManagerPtr = i.libCreateSecretManager(bytes); secretManagerPtr == 0 {
return 0, i.GetLastError()
}
return secretManagerPtr, nil
}
func (i *IOTASDK) GetClientFromWallet(iotaWalletPtr IotaWalletPtr) (clientPtr IotaClientPtr, err error) {
if clientPtr = i.libGetClientFromWallet(iotaWalletPtr); clientPtr == 0 {
return 0, i.GetLastError()
}
return clientPtr, nil
}
func (i *IOTASDK) GetSecretManagerFromWallet(iotaWalletPtr IotaWalletPtr) (secretManagerPtr IotaSecretManagerPtr, err error) {
if secretManagerPtr = i.libGetSecretManagerFromWallet(iotaWalletPtr); secretManagerPtr == 0 {
return 0, i.GetLastError()
}
return secretManagerPtr, nil
}
func (i *IOTASDK) CallUtilsMethod(method any) (response []byte, free func(), err error) {
msg, free, err := SerializeGuarded(method)
defer free()
if err != nil {
return nil, func() {}, err
}
var responsePtr uintptr
if responsePtr = i.libCallUtilsMethod(msg); responsePtr == 0 {
return nil, func() {}, i.GetLastError()
}
return i.CopyAndDestroyOriginalStringPtr(responsePtr)
}
func (i *IOTASDK) CallClientMethod(iotaClientPtr IotaClientPtr, method any) (response []byte, free func(), err error) {
msg, free, err := SerializeGuarded(method)
defer free()
if err != nil {
return nil, func() {}, err
}
var responsePtr uintptr
if responsePtr = i.libCallClientMethod(iotaClientPtr, msg); responsePtr == 0 {
return nil, func() {}, i.GetLastError()
}
return i.CopyAndDestroyOriginalStringPtr(responsePtr)
}
func (i *IOTASDK) CallWalletMethod(iotaWalletPtr IotaWalletPtr, method any) ([]byte, func(), error) {
msg, free, err := SerializeGuarded(method)
defer free()
if err != nil {
return nil, func() {}, err
}
var responsePtr uintptr
if responsePtr = i.libCallWalletMethod(iotaWalletPtr, msg); responsePtr == 0 {
return nil, func() {}, i.GetLastError()
}
return i.CopyAndDestroyOriginalStringPtr(responsePtr)
}
func (i *IOTASDK) CallSecretManagerMethod(iotaSecretManagerPtr IotaSecretManagerPtr, method any) ([]byte, func(), error) {
msg, free, err := SerializeGuarded(method)
defer free()
if err != nil {
return nil, func() {}, err
}
var responsePtr uintptr
if responsePtr = i.libCallSecretManagerMethod(iotaSecretManagerPtr, msg); responsePtr == 0 {
return nil, func() {}, i.GetLastError()
}
return i.CopyAndDestroyOriginalStringPtr(responsePtr)
}
func (i *IOTASDK) DestroyClient(client IotaClientPtr) (err error) {
if client == 0 {
return nil
}
if success := i.libDestroyClient(client); !success {
return i.GetLastError()
}
return nil
}
func (i *IOTASDK) DestroyWallet(wallet IotaWalletPtr) (err error) {
if wallet == 0 {
return nil
}
if success := i.libDestroyWallet(wallet); !success {
return i.GetLastError()
}
return nil
}
func (i *IOTASDK) DestroySecretManager(secretManager IotaSecretManagerPtr) (err error) {
if secretManager == 0 {
return nil
}
if success := i.libDestroySecretManager(secretManager); !success {
return i.GetLastError()
}
return nil
}
func (i *IOTASDK) CopyAndDestroyOriginalStringPtr(response uintptr) ([]byte, func(), error) {
goString, free := GoString(response)
if err := i.DestroyString(response); err != nil {
return nil, free, err
}
return goString, free, nil
}
func (i *IOTASDK) DestroyString(ptr uintptr) (err error) {
if success := i.libDestroyString(ptr); !success {
return i.GetLastError()
}
return nil
}