-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
1062 lines (897 loc) · 34.9 KB
/
helpers.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
/*
#include <stdlib.h>
#include "./seald_sdk.h"
*/
import "C"
import (
"fmt"
"github.com/rs/zerolog"
"github.com/seald/go-seald-sdk/common_models"
"github.com/seald/go-seald-sdk/sdk"
"github.com/seald/go-seald-sdk/symmetric_key"
"github.com/seald/go-seald-sdk/utils"
"github.com/ztrue/tracerr"
"sync"
"time"
"unsafe"
)
var (
ErrorFileDbRequiresEncKeyC = utils.NewSealdError("C_FILE_DB_REQUIRES_ENC_KEY", "Using a file database requires encryption key : if you pass DatabasePath you must also pass DatabaseEncryptionKey and DatabaseEncryptionKeyLen - C")
ErrorFileDbInvalidKeyLengthC = utils.NewSealdError("C_FILE_DB_INVALID_KEY_LENGTH", "DatabaseEncryptionKey must be a 64-byte buffer - C")
)
func boolToCInt(b bool) C.int {
if b {
return C.int(1)
} else {
return C.int(0)
}
}
// Helper SealdError
func sealdErrorFromGo(err error) *C.SealdError {
serializedError := utils.ToSerializableError(err)
res := (*C.SealdError)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdError{}))))
res.Status = C.int(serializedError.Status)
res.Code = C.CString(serializedError.Code)
res.Id = C.CString(serializedError.Id)
res.Description = C.CString(serializedError.Description)
res.Details = C.CString(serializedError.Details)
res.Raw = C.CString(serializedError.Raw)
res.NativeStack = C.CString(serializedError.Stack)
return res
}
//export SealdError_Free
func SealdError_Free(err *C.SealdError) {
if err == nil {
return
}
C.free(unsafe.Pointer(err.Code))
C.free(unsafe.Pointer(err.Id))
C.free(unsafe.Pointer(err.Description))
C.free(unsafe.Pointer(err.Details))
C.free(unsafe.Pointer(err.Raw))
C.free(unsafe.Pointer(err.NativeStack))
C.free(unsafe.Pointer(err))
}
// Helper SealdStringArray
type SealdStringArray struct {
items []string
}
func stringArrayToGo(array *C.SealdStringArray) *SealdStringArray {
if array == nil {
return nil
}
return (*SealdStringArray)(unsafe.Pointer(array))
}
var sealdStringArrayRefMap = sync.Map{}
//export SealdStringArray_New
func SealdStringArray_New() *C.SealdStringArray {
array := &SealdStringArray{}
sealdStringArrayRefMap.Store(uintptr(unsafe.Pointer(array)), array)
return (*C.SealdStringArray)(unsafe.Pointer(array))
}
//export SealdStringArray_Free
func SealdStringArray_Free(array *C.SealdStringArray) {
sealdStringArrayRefMap.Delete(uintptr(unsafe.Pointer(array)))
}
//export SealdStringArray_Add
func SealdStringArray_Add(array *C.SealdStringArray, s *C.char) {
goArray := stringArrayToGo(array)
goArray.items = append(goArray.items, C.GoString(s))
}
//export SealdStringArray_Get
func SealdStringArray_Get(array *C.SealdStringArray, i C.int) *C.char {
goArray := stringArrayToGo(array)
return C.CString(goArray.items[int(i)])
}
//export SealdStringArray_Size
func SealdStringArray_Size(array *C.SealdStringArray) C.int {
goArray := stringArrayToGo(array)
return C.int(len(goArray.items))
}
func (array *SealdStringArray) getSlice() []string {
if array == nil {
return nil
}
return array.items
}
func sliceToStringArray(slice []string) *C.SealdStringArray {
array := SealdStringArray_New()
goArray := stringArrayToGo(array)
goArray.items = append([]string{}, slice...)
return array
}
// Helper SealdClearFile
//export SealdClearFile_Free
func SealdClearFile_Free(cf *C.SealdClearFile) {
if cf == nil {
return
}
C.free(unsafe.Pointer(cf.Filename))
C.free(unsafe.Pointer(cf.SessionId))
C.free(unsafe.Pointer(cf.FileContent))
C.free(unsafe.Pointer(cf))
}
func clearFileFromCommon(cf *common_models.ClearFile) *C.SealdClearFile {
res := (*C.SealdClearFile)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdClearFile{}))))
res.Filename = C.CString(cf.Filename)
res.SessionId = C.CString(cf.SessionId)
res.FileContent = (*C.uchar)(C.CBytes(cf.FileContent))
res.FileContentLen = C.int(len(cf.FileContent))
return res
}
// Helper SealdAccountInfo
//export SealdAccountInfo_Free
func SealdAccountInfo_Free(info *C.SealdAccountInfo) {
if info == nil {
return
}
C.free(unsafe.Pointer(info.UserId))
C.free(unsafe.Pointer(info.DeviceId))
C.free(unsafe.Pointer(info))
}
func accountInfoFromCommon(accountInfo *sdk.AccountInfo) *C.SealdAccountInfo {
res := (*C.SealdAccountInfo)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdAccountInfo{}))))
res.UserId = C.CString(accountInfo.UserId)
res.DeviceId = C.CString(accountInfo.DeviceId)
if accountInfo.DeviceExpires != nil {
res.DeviceExpires = C.longlong(accountInfo.DeviceExpires.Unix())
} else {
res.DeviceExpires = 0
}
return res
}
// Helper SealdInitializeOptions
func initializeOptionsToGo(cOpts *C.SealdInitializeOptions) (*sdk.InitializeOptions, error) {
var storage sdk.Database
if cOpts.DatabasePath == nil || *cOpts.DatabasePath == 0 { // NULL or empty string (first char is '\0')
storage = &sdk.MemoryStorage{}
} else {
if cOpts.DatabaseEncryptionKeyLen != 64 {
return nil, tracerr.Wrap(ErrorFileDbInvalidKeyLengthC)
}
if cOpts.DatabaseEncryptionKey == nil {
return nil, tracerr.Wrap(ErrorFileDbRequiresEncKeyC)
}
encryptionKeyBytes := C.GoBytes(unsafe.Pointer(cOpts.DatabaseEncryptionKey), cOpts.DatabaseEncryptionKeyLen)
key, err := symmetric_key.Decode(encryptionKeyBytes)
if err != nil {
return nil, tracerr.Wrap(err)
}
storage = &sdk.FileStorage{
EncryptionKey: key,
DatabaseDir: C.GoString(cOpts.DatabasePath),
}
}
return &sdk.InitializeOptions{
ApiURL: C.GoString(cOpts.ApiURL),
AppId: C.GoString(cOpts.AppId),
KeySize: int(cOpts.KeySize),
EncryptionSessionCacheTTL: time.Duration(int64(cOpts.EncryptionSessionCacheTTL)) * time.Millisecond,
LogLevel: zerolog.Level(int8(cOpts.LogLevel)),
LogNoColor: int(cOpts.LogNoColor) != 0,
InstanceName: C.GoString(cOpts.InstanceName),
Platform: C.GoString(cOpts.Platform),
Database: storage,
LogWriter: logWriter,
}, nil
}
//TODO: SealdInitializeOptions_Defaults
// Helper SealdCreateSubIdentityResponse
//export SealdCreateSubIdentityResponse_Free
func SealdCreateSubIdentityResponse_Free(resp *C.SealdCreateSubIdentityResponse) {
if resp == nil {
return
}
C.free(unsafe.Pointer(resp.DeviceId))
C.free(unsafe.Pointer(resp.BackupKey))
C.free(unsafe.Pointer(resp))
}
func createSubIdentityResponseFromCommon(createSubIdentityResponse *sdk.CreateSubIdentityResponse) *C.SealdCreateSubIdentityResponse {
res := (*C.SealdCreateSubIdentityResponse)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdCreateSubIdentityResponse{}))))
res.DeviceId = C.CString(createSubIdentityResponse.DeviceId)
res.BackupKey = (*C.uchar)(C.CBytes(createSubIdentityResponse.BackupKey))
res.BackupKeyLen = C.int(len(createSubIdentityResponse.BackupKey))
return res
}
// Helper SealdConnector
//export SealdConnector_Free
func SealdConnector_Free(c *C.SealdConnector) {
if c == nil {
return
}
C.free(unsafe.Pointer(c.SealdId))
C.free(unsafe.Pointer(c.Type))
C.free(unsafe.Pointer(c.Value))
C.free(unsafe.Pointer(c.Id))
C.free(unsafe.Pointer(c.State))
C.free(unsafe.Pointer(c))
}
func connectorFromCommon(c *common_models.Connector) *C.SealdConnector {
if c == nil {
return nil
}
res := (*C.SealdConnector)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdConnector{}))))
res.SealdId = C.CString(c.SealdId)
res.Type = C.CString(string(c.Type))
res.Value = C.CString(c.Value)
res.Id = C.CString(c.Id)
res.State = C.CString(string(c.State))
return res
}
// Helper SealdPreValidationToken
func preValidationTokenToGo(t *C.SealdPreValidationToken) *utils.PreValidationToken {
if t == nil {
return nil
}
return &utils.PreValidationToken{
DomainValidationKeyId: C.GoString(t.DomainValidationKeyId),
Nonce: C.GoString(t.Nonce),
Token: C.GoString(t.Token),
}
}
//export SealdPreValidationToken_Free
func SealdPreValidationToken_Free(t *C.SealdPreValidationToken) {
if t == nil {
return
}
C.free(unsafe.Pointer(t.DomainValidationKeyId))
C.free(unsafe.Pointer(t.Nonce))
C.free(unsafe.Pointer(t.Token))
C.free(unsafe.Pointer(t))
}
// Helper SealdConnectorsArray
type SealdConnectorsArray struct {
items []*C.SealdConnector
}
func connectorsArrayToGo(array *C.SealdConnectorsArray) *SealdConnectorsArray {
return (*SealdConnectorsArray)(unsafe.Pointer(array))
}
var sealdConnectorsArrayRefMap = sync.Map{}
//export SealdConnectorsArray_New
func SealdConnectorsArray_New() *C.SealdConnectorsArray {
array := &SealdConnectorsArray{}
sealdConnectorsArrayRefMap.Store(uintptr(unsafe.Pointer(array)), array)
return (*C.SealdConnectorsArray)(unsafe.Pointer(array))
}
//export SealdConnectorsArray_Free
func SealdConnectorsArray_Free(array *C.SealdConnectorsArray) {
goArray := connectorsArrayToGo(array)
items := goArray.items
goArray.items = nil
for _, c := range items {
SealdConnector_Free(c)
}
sealdConnectorsArrayRefMap.Delete(uintptr(unsafe.Pointer(array)))
}
//export SealdConnectorsArray_Add
func SealdConnectorsArray_Add(array *C.SealdConnectorsArray, c *C.SealdConnector) {
goArray := connectorsArrayToGo(array)
goArray.items = append(goArray.items, c)
}
//export SealdConnectorsArray_Get
func SealdConnectorsArray_Get(array *C.SealdConnectorsArray, i C.int) *C.SealdConnector {
goArray := connectorsArrayToGo(array)
return goArray.items[int(i)]
}
//export SealdConnectorsArray_Size
func SealdConnectorsArray_Size(array *C.SealdConnectorsArray) C.int {
goArray := connectorsArrayToGo(array)
return C.int(len(goArray.items))
}
func (array *SealdConnectorsArray) getSlice() []*C.SealdConnector {
return array.items
}
func sliceToConnectorsArray(slice []common_models.Connector) *C.SealdConnectorsArray {
ca := SealdConnectorsArray_New()
for _, el := range slice {
SealdConnectorsArray_Add(ca, connectorFromCommon(&el))
}
return ca
}
// Helper SealdConnectorTypeValueArray
type SealdConnectorTypeValueArray struct {
items []*sdk.ConnectorTypeValue
}
func connectorTypeValueArrayToGo(array *C.SealdConnectorTypeValueArray) *SealdConnectorTypeValueArray {
return (*SealdConnectorTypeValueArray)(unsafe.Pointer(array))
}
var sealdConnectorTypeValueArrayRefMap = sync.Map{}
//export SealdConnectorTypeValueArray_New
func SealdConnectorTypeValueArray_New() *C.SealdConnectorTypeValueArray {
array := &SealdConnectorTypeValueArray{}
sealdConnectorTypeValueArrayRefMap.Store(uintptr(unsafe.Pointer(array)), array)
return (*C.SealdConnectorTypeValueArray)(unsafe.Pointer(array))
}
//export SealdConnectorTypeValueArray_Free
func SealdConnectorTypeValueArray_Free(array *C.SealdConnectorTypeValueArray) {
sealdConnectorTypeValueArrayRefMap.Delete(uintptr(unsafe.Pointer(array)))
}
//export SealdConnectorTypeValueArray_Add
func SealdConnectorTypeValueArray_Add(array *C.SealdConnectorTypeValueArray, connectorType *C.char, connectorValue *C.char) {
goArray := connectorTypeValueArrayToGo(array)
goArray.items = append(goArray.items, &sdk.ConnectorTypeValue{
Type: common_models.ConnectorType(C.GoString(connectorType)),
Value: C.GoString(connectorValue),
})
}
//export SealdConnectorTypeValueArray_Get
func SealdConnectorTypeValueArray_Get(array *C.SealdConnectorTypeValueArray, i C.int, connectorType **C.char, connectorValue **C.char) {
goArray := connectorTypeValueArrayToGo(array)
ctv := goArray.items[int(i)]
*connectorType = C.CString(string(ctv.Type))
*connectorValue = C.CString(ctv.Value)
}
//export SealdConnectorTypeValueArray_Size
func SealdConnectorTypeValueArray_Size(array *C.SealdConnectorTypeValueArray) C.int {
goArray := connectorTypeValueArrayToGo(array)
return C.int(len(goArray.items))
}
func (array *SealdConnectorTypeValueArray) getSlice() []*sdk.ConnectorTypeValue {
return array.items
}
// Helper SealdRecipientsWithRightsArray
type SealdRecipientsWithRightsArray struct {
items []*sdk.RecipientWithRights
}
func recipientsWithRightsArrayToGo(array *C.SealdRecipientsWithRightsArray) *SealdRecipientsWithRightsArray {
return (*SealdRecipientsWithRightsArray)(unsafe.Pointer(array))
}
var sealdRecipientsWithRightsArrayRefMap = sync.Map{}
//export SealdRecipientsWithRightsArray_New
func SealdRecipientsWithRightsArray_New() *C.SealdRecipientsWithRightsArray {
array := &SealdRecipientsWithRightsArray{}
sealdRecipientsWithRightsArrayRefMap.Store(uintptr(unsafe.Pointer(array)), array)
return (*C.SealdRecipientsWithRightsArray)(unsafe.Pointer(array))
}
//export SealdRecipientsWithRightsArray_Free
func SealdRecipientsWithRightsArray_Free(array *C.SealdRecipientsWithRightsArray) {
sealdRecipientsWithRightsArrayRefMap.Delete(uintptr(unsafe.Pointer(array)))
}
//export SealdRecipientsWithRightsArray_Add
func SealdRecipientsWithRightsArray_Add(array *C.SealdRecipientsWithRightsArray, sealdId *C.char, readRight C.int, forwardRight C.int, revokeRight C.int) {
goArray := recipientsWithRightsArrayToGo(array)
goArray.items = append(goArray.items, &sdk.RecipientWithRights{
Id: C.GoString(sealdId),
Rights: &sdk.RecipientRights{
Read: readRight == 1,
Forward: forwardRight == 1,
Revoke: revokeRight == 1,
},
})
}
//export SealdRecipientsWithRightsArray_AddWithDefaultRights
func SealdRecipientsWithRightsArray_AddWithDefaultRights(array *C.SealdRecipientsWithRightsArray, sealdId *C.char) {
goArray := recipientsWithRightsArrayToGo(array)
goArray.items = append(goArray.items, &sdk.RecipientWithRights{
Id: C.GoString(sealdId),
Rights: nil,
})
}
//export SealdRecipientsWithRightsArray_Get
func SealdRecipientsWithRightsArray_Get(array *C.SealdRecipientsWithRightsArray, i C.int, recipientId **C.char, recipientRightRead *C.int, recipientRightForward *C.int, recipientRightRevoke *C.int) {
goArray := recipientsWithRightsArrayToGo(array)
rwr := goArray.items[int(i)]
*recipientId = C.CString(string(rwr.Id))
if rwr.Rights != nil {
*recipientRightRead = boolToCInt(rwr.Rights.Read)
*recipientRightForward = boolToCInt(rwr.Rights.Forward)
*recipientRightRevoke = boolToCInt(rwr.Rights.Revoke)
} else {
*recipientRightRead = C.int(-1)
*recipientRightForward = C.int(-1)
*recipientRightRevoke = C.int(-1)
}
}
//export SealdRecipientsWithRightsArray_Size
func SealdRecipientsWithRightsArray_Size(array *C.SealdRecipientsWithRightsArray) C.int {
goArray := recipientsWithRightsArrayToGo(array)
return C.int(len(goArray.items))
}
func (array *SealdRecipientsWithRightsArray) getSlice() []*sdk.RecipientWithRights {
return array.items
}
// Helper SealdMassReencryptOptions
//export SealdMassReencryptOptions_Defaults
func SealdMassReencryptOptions_Defaults() C.SealdMassReencryptOptions {
return C.SealdMassReencryptOptions{ // no need to do C.malloc and everything, as we are returning the struct itself on the stack
Retries: 3,
RetrieveBatchSize: 1000,
WaitBetweenRetries: 3.0,
WaitProvisioning: 1,
WaitProvisioningTime: 5.0,
WaitProvisioningTimeMax: 10.0,
WaitProvisioningTimeStep: 1.0,
WaitProvisioningRetries: 100,
ForceLocalAccountUpdate: 0,
}
}
func massReencryptOptionsToGo(o C.SealdMassReencryptOptions) sdk.MassReencryptOptions {
return sdk.MassReencryptOptions{
Retries: int(o.Retries),
RetrieveBatchSize: int(o.RetrieveBatchSize),
WaitBetweenRetries: time.Duration(int64(o.WaitBetweenRetries)) * time.Millisecond,
WaitProvisioning: int(o.WaitProvisioning) != 0,
WaitProvisioningTime: time.Duration(int64(o.WaitProvisioningTime)) * time.Millisecond,
WaitProvisioningTimeMax: time.Duration(int64(o.WaitProvisioningTimeMax)) * time.Millisecond,
WaitProvisioningTimeStep: time.Duration(int64(o.WaitProvisioningTimeStep)) * time.Millisecond,
WaitProvisioningRetries: int(o.WaitProvisioningRetries),
ForceLocalAccountUpdate: int(o.ForceLocalAccountUpdate) != 0,
}
}
// Helper SealdDeviceMissingKeys
func deviceMissingKeysFromGo(d *sdk.DeviceMissingKeys) *C.SealdDeviceMissingKeys {
res := (*C.SealdDeviceMissingKeys)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdDeviceMissingKeys{}))))
res.DeviceId = C.CString(d.DeviceId)
return res
}
//export SealdDeviceMissingKeys_Free
func SealdDeviceMissingKeys_Free(d *C.SealdDeviceMissingKeys) {
if d == nil {
return
}
C.free(unsafe.Pointer(d.DeviceId))
C.free(unsafe.Pointer(d))
}
// Helper SealdDeviceMissingKeysArray
type SealdDeviceMissingKeysArray struct {
items []*C.SealdDeviceMissingKeys
}
func deviceMissingKeysArrayToGo(array *C.SealdDeviceMissingKeysArray) *SealdDeviceMissingKeysArray {
return (*SealdDeviceMissingKeysArray)(unsafe.Pointer(array))
}
var sealdDeviceMissingKeysArrayRefMap = sync.Map{}
//export SealdDeviceMissingKeysArray_New
func SealdDeviceMissingKeysArray_New() *C.SealdDeviceMissingKeysArray {
array := &SealdDeviceMissingKeysArray{}
sealdDeviceMissingKeysArrayRefMap.Store(uintptr(unsafe.Pointer(array)), array)
return (*C.SealdDeviceMissingKeysArray)(unsafe.Pointer(array))
}
//export SealdDeviceMissingKeysArray_Free
func SealdDeviceMissingKeysArray_Free(array *C.SealdDeviceMissingKeysArray) {
goArray := deviceMissingKeysArrayToGo(array)
items := goArray.items
goArray.items = nil
for _, c := range items {
SealdDeviceMissingKeys_Free(c)
}
sealdDeviceMissingKeysArrayRefMap.Delete(uintptr(unsafe.Pointer(array)))
}
//export SealdDeviceMissingKeysArray_Add
func SealdDeviceMissingKeysArray_Add(array *C.SealdDeviceMissingKeysArray, d *C.SealdDeviceMissingKeys) {
goArray := deviceMissingKeysArrayToGo(array)
goArray.items = append(goArray.items, d)
}
//export SealdDeviceMissingKeysArray_Get
func SealdDeviceMissingKeysArray_Get(array *C.SealdDeviceMissingKeysArray, i C.int) *C.SealdDeviceMissingKeys {
goArray := deviceMissingKeysArrayToGo(array)
return goArray.items[int(i)]
}
//export SealdDeviceMissingKeysArray_Size
func SealdDeviceMissingKeysArray_Size(array *C.SealdDeviceMissingKeysArray) C.int {
goArray := deviceMissingKeysArrayToGo(array)
return C.int(len(goArray.items))
}
func (array *SealdDeviceMissingKeysArray) getSlice() []*C.SealdDeviceMissingKeys {
return array.items
}
func sliceToDeviceMissingKeysArray(slice []sdk.DeviceMissingKeys) *C.SealdDeviceMissingKeysArray {
ca := SealdDeviceMissingKeysArray_New()
for _, el := range slice {
SealdDeviceMissingKeysArray_Add(ca, deviceMissingKeysFromGo(&el))
}
return ca
}
// Helper SealdActionStatus
//export SealdActionStatus_Free
func SealdActionStatus_Free(as *C.SealdActionStatus) {
if as == nil {
return
}
C.free(unsafe.Pointer(as.Id))
C.free(unsafe.Pointer(as.ErrorCode))
C.free(unsafe.Pointer(as.Result))
C.free(unsafe.Pointer(as))
}
// Helper SealdActionStatusArray
type SealdActionStatusArray struct {
items []*C.SealdActionStatus
}
var sealdActionStatusArrayRefMap = sync.Map{}
//export SealdActionStatusArray_New
func SealdActionStatusArray_New() *C.SealdActionStatusArray {
array := &SealdActionStatusArray{}
sealdActionStatusArrayRefMap.Store(uintptr(unsafe.Pointer(array)), array)
return (*C.SealdActionStatusArray)(unsafe.Pointer(array))
}
func actionStatusArrayToGo(array *C.SealdActionStatusArray) *SealdActionStatusArray {
return (*SealdActionStatusArray)(unsafe.Pointer(array))
}
//export SealdActionStatusArray_Add
func SealdActionStatusArray_Add(array *C.SealdActionStatusArray, as *C.SealdActionStatus) {
goArray := actionStatusArrayToGo(array)
goArray.items = append(goArray.items, as)
}
//export SealdActionStatusArray_Free
func SealdActionStatusArray_Free(array *C.SealdActionStatusArray) {
goArray := actionStatusArrayToGo(array)
items := goArray.items
goArray.items = nil
for _, as := range items {
SealdActionStatus_Free(as)
}
sealdActionStatusArrayRefMap.Delete(uintptr(unsafe.Pointer(array)))
}
//export SealdActionStatusArray_Size
func SealdActionStatusArray_Size(array *C.SealdActionStatusArray) C.int {
goArray := actionStatusArrayToGo(array)
return C.int(len(goArray.items))
}
//export SealdActionStatusArray_Get
func SealdActionStatusArray_Get(array *C.SealdActionStatusArray, i C.int) *C.SealdActionStatus {
goArray := actionStatusArrayToGo(array)
return goArray.items[int(i)]
}
func actionStatusArrayFromAddKey(addKeyResp *sdk.AddKeysMultiStatusResponse) *C.SealdActionStatusArray {
asArray := SealdActionStatusArray_New()
for key, addKeysResp := range addKeyResp.Status {
as := (*C.SealdActionStatus)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdActionStatus{}))))
as.Id = C.CString(key)
as.Success = boolToCInt(addKeysResp.StatusCode == 200)
as.Result = nil // So that it will be a null pointer for the Flutter bindings
if addKeysResp.Error != nil {
as.ErrorCode = C.CString(fmt.Sprintf("%s %s", addKeysResp.Error.Id, addKeysResp.Error.Code))
} else {
as.ErrorCode = nil
}
SealdActionStatusArray_Add(asArray, as)
}
return asArray
}
func actionStatusArrayFromRevoke(revokeMap map[string]string) *C.SealdActionStatusArray {
asArray := SealdActionStatusArray_New()
for uid, status := range revokeMap {
as := (*C.SealdActionStatus)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdActionStatus{}))))
as.Id = C.CString(uid)
as.Result = nil // So that it will be a null pointer for the Flutter bindings
as.Success = boolToCInt(status == "ok")
as.ErrorCode = nil
SealdActionStatusArray_Add(asArray, as)
}
return asArray
}
func actionStatusArrayFromAddTmrAccess(addTmrAccessResp *sdk.AddTmrAccessesMultiStatusResponse) *C.SealdActionStatusArray {
asArray := SealdActionStatusArray_New()
for key, added := range addTmrAccessResp.Status {
as := (*C.SealdActionStatus)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdActionStatus{}))))
as.Id = C.CString(key)
as.Success = boolToCInt(added.Status == 200)
as.Result = C.CString(added.TmrKey.Id)
if added.Error != nil {
as.ErrorCode = C.CString(fmt.Sprintf("%s %s", added.Error.Status, added.Error.Code))
} else {
as.ErrorCode = nil
}
SealdActionStatusArray_Add(asArray, as)
}
return asArray
}
// Helper SealdRevokeResult
//export SealdRevokeResult_Free
func SealdRevokeResult_Free(rr *C.SealdRevokeResult) {
if rr == nil {
return
}
SealdActionStatusArray_Free(rr.Recipients)
SealdActionStatusArray_Free(rr.ProxySessions)
C.free(unsafe.Pointer(rr))
}
func revokeResultFromGo(recipients map[string]string, proxySessions map[string]string) *C.SealdRevokeResult {
res := (*C.SealdRevokeResult)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdRevokeResult{}))))
res.Recipients = actionStatusArrayFromRevoke(recipients)
res.ProxySessions = actionStatusArrayFromRevoke(proxySessions)
return res
}
// Helper SealdEncryptionSessionRetrievalDetails
//export SealdEncryptionSessionRetrievalDetails_Free
func SealdEncryptionSessionRetrievalDetails_Free(details *C.SealdEncryptionSessionRetrievalDetails) {
if details == nil {
return
}
C.free(unsafe.Pointer(details.GroupId))
C.free(unsafe.Pointer(details.ProxySessionId))
C.free(unsafe.Pointer(details))
}
func retrievalDetailsFromGo(d sdk.EncryptionSessionRetrievalDetails) *C.SealdEncryptionSessionRetrievalDetails {
res := (*C.SealdEncryptionSessionRetrievalDetails)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdEncryptionSessionRetrievalDetails{}))))
res.Flow = C.SealdEncryptionSessionRetrievalFlow(d.Flow)
res.GroupId = utils.Ternary(d.GroupId != "", C.CString(d.GroupId), nil)
res.ProxySessionId = utils.Ternary(d.ProxySessionId != "", C.CString(d.ProxySessionId), nil)
res.FromCache = boolToCInt(d.FromCache)
return res
}
// Helper SealdGetSigchainResponse
//export SealdGetSigchainResponse_Free
func SealdGetSigchainResponse_Free(sigchainInfo *C.SealdGetSigchainResponse) {
if sigchainInfo == nil {
return
}
C.free(unsafe.Pointer(sigchainInfo.Hash))
C.free(unsafe.Pointer(sigchainInfo))
}
func getSigchainResponseFromGo(sigchainInfo *sdk.GetSigchainResponse) *C.SealdGetSigchainResponse {
res := (*C.SealdGetSigchainResponse)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdGetSigchainResponse{}))))
res.Hash = C.CString(sigchainInfo.Hash)
res.Position = C.int(sigchainInfo.Position)
return res
}
// Helper SealdCheckSigchainResponse
func checkSigchainResponseFromGo(sigchainInfo *sdk.CheckSigchainResponse) *C.SealdCheckSigchainResponse {
res := (*C.SealdCheckSigchainResponse)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdCheckSigchainResponse{}))))
res.Found = boolToCInt(sigchainInfo.Found)
res.Position = C.int(sigchainInfo.Position)
res.LastPosition = C.int(sigchainInfo.LastPosition)
return res
}
func b64KeyToNative(b64Key *C.char) (*symmetric_key.SymKey, error) {
decodedSymKey, err := utils.Base64DecodeString(C.GoString(b64Key))
if err != nil {
return nil, err
}
overEncryptionKey, err := symmetric_key.Decode(decodedSymKey)
if err != nil {
return nil, err
}
return &overEncryptionKey, nil
}
// Helper SealdTmrAccessesRetrievalFilters
func tmrAccessesRetrievalFiltersToGo(cFilters *C.SealdTmrAccessesRetrievalFilters) *sdk.TmrAccessesRetrievalFilters {
if cFilters == nil {
return nil
}
return &sdk.TmrAccessesRetrievalFilters{
CreatedById: C.GoString(cFilters.CreatedById),
TmrAccessId: C.GoString(cFilters.TmrAccessId),
}
}
//export SealdTmrAccessesRetrievalFilters_Free
func SealdTmrAccessesRetrievalFilters_Free(filters *C.SealdTmrAccessesRetrievalFilters) {
if filters == nil {
return
}
C.free(unsafe.Pointer(filters.CreatedById))
C.free(unsafe.Pointer(filters.TmrAccessId))
}
// Helper SealdTmrAccessesConvertFilters
func tmrAccessesConvertFiltersToGo(cFilters *C.SealdTmrAccessesConvertFilters) *sdk.TmrAccessesConvertFilters {
if cFilters == nil {
return nil
}
return &sdk.TmrAccessesConvertFilters{
SessionId: C.GoString(cFilters.SessionId),
CreatedById: C.GoString(cFilters.CreatedById),
TmrAccessId: C.GoString(cFilters.TmrAccessId),
}
}
//export SealdTmrAccessesConvertFilters_Free
func SealdTmrAccessesConvertFilters_Free(filters *C.SealdTmrAccessesConvertFilters) {
if filters == nil {
return
}
C.free(unsafe.Pointer(filters.SessionId))
C.free(unsafe.Pointer(filters.CreatedById))
C.free(unsafe.Pointer(filters.TmrAccessId))
}
// Helper SealdConvertTmrAccessesResult
//export SealdConvertTmrAccessesResult_Free
func SealdConvertTmrAccessesResult_Free(convAccess *C.SealdConvertTmrAccessesResult) {
if convAccess == nil {
return
}
SealdStringArray_Free(convAccess.Converted)
C.free(unsafe.Pointer(convAccess))
}
func convertTmrAccessesResultFromGo(resp *sdk.ConvertTmrAccessesResponse) *C.SealdConvertTmrAccessesResult {
res := (*C.SealdConvertTmrAccessesResult)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdConvertTmrAccessesResult{}))))
res.Status = C.CString(resp.Status)
res.Converted = sliceToStringArray(resp.Converted)
res.Succeeded = C.int(len(resp.Succeeded))
res.Errored = C.int(len(resp.Errored))
return res
}
// Helper SealdTmrRecipientsWithRightsArray
type SealdTmrRecipientsWithRightsArray struct {
items []*sdk.TmrRecipientWithRights
}
func tmrRecipientsWithRightsArrayToGo(array *C.SealdTmrRecipientsWithRightsArray) *SealdTmrRecipientsWithRightsArray {
return (*SealdTmrRecipientsWithRightsArray)(unsafe.Pointer(array))
}
var SealdTmrRecipientsWithRightsArrayRefMap = sync.Map{}
//export SealdTmrRecipientsWithRightsArray_New
func SealdTmrRecipientsWithRightsArray_New() *C.SealdTmrRecipientsWithRightsArray {
array := &SealdTmrRecipientsWithRightsArray{}
SealdTmrRecipientsWithRightsArrayRefMap.Store(uintptr(unsafe.Pointer(array)), array)
return (*C.SealdTmrRecipientsWithRightsArray)(unsafe.Pointer(array))
}
//export SealdTmrRecipientsWithRightsArray_Free
func SealdTmrRecipientsWithRightsArray_Free(array *C.SealdTmrRecipientsWithRightsArray) {
SealdTmrRecipientsWithRightsArrayRefMap.Delete(uintptr(unsafe.Pointer(array)))
}
//export SealdTmrRecipientsWithRightsArray_Add
func SealdTmrRecipientsWithRightsArray_Add(array *C.SealdTmrRecipientsWithRightsArray, authFactorType *C.char, authFactorValue *C.char, overEncryptionKey *C.uchar, overEncryptionKeyLen C.int, readRight C.int, forwardRight C.int, revokeRight C.int) {
overEncryptionKeyBytes := C.GoBytes(unsafe.Pointer(overEncryptionKey), overEncryptionKeyLen)
goArray := tmrRecipientsWithRightsArrayToGo(array)
goArray.items = append(goArray.items, &sdk.TmrRecipientWithRights{
AuthFactor: &common_models.AuthFactor{
Type: C.GoString(authFactorType),
Value: C.GoString(authFactorValue),
},
OverEncryptionKey: overEncryptionKeyBytes,
Rights: &sdk.RecipientRights{
Read: readRight == 1,
Forward: forwardRight == 1,
Revoke: revokeRight == 1,
},
})
}
//export SealdTmrRecipientsWithRightsArray_AddWithDefaultRights
func SealdTmrRecipientsWithRightsArray_AddWithDefaultRights(array *C.SealdTmrRecipientsWithRightsArray, authFactorType *C.char, authFactorValue *C.char, overEncryptionKey *C.uchar, overEncryptionKeyLen C.int) {
overEncryptionKeyBytes := C.GoBytes(unsafe.Pointer(overEncryptionKey), overEncryptionKeyLen)
goArray := tmrRecipientsWithRightsArrayToGo(array)
goArray.items = append(goArray.items, &sdk.TmrRecipientWithRights{
AuthFactor: &common_models.AuthFactor{
Type: C.GoString(authFactorType),
Value: C.GoString(authFactorValue),
},
OverEncryptionKey: overEncryptionKeyBytes,
Rights: nil,
})
}
//export SealdTmrRecipientsWithRightsArray_Get
func SealdTmrRecipientsWithRightsArray_Get(array *C.SealdTmrRecipientsWithRightsArray, i C.int, authFactorType **C.char, authFactorValue **C.char, overEncryptionKey **C.uchar, overEncryptionKeyLen *C.int, recipientRightRead *C.int, recipientRightForward *C.int, recipientRightRevoke *C.int) {
goArray := tmrRecipientsWithRightsArrayToGo(array)
tmrR := goArray.items[int(i)]
*authFactorType = C.CString(tmrR.AuthFactor.Type)
*authFactorValue = C.CString(tmrR.AuthFactor.Value)
*overEncryptionKey = (*C.uchar)(C.CBytes(tmrR.OverEncryptionKey))
*overEncryptionKeyLen = C.int(len(tmrR.OverEncryptionKey))
*recipientRightRead = boolToCInt(tmrR.Rights.Read)
*recipientRightForward = boolToCInt(tmrR.Rights.Forward)
*recipientRightRevoke = boolToCInt(tmrR.Rights.Revoke)
}
//export SealdTmrRecipientsWithRightsArray_Size
func SealdTmrRecipientsWithRightsArray_Size(array *C.SealdTmrRecipientsWithRightsArray) C.int {
goArray := tmrRecipientsWithRightsArrayToGo(array)
return C.int(len(goArray.items))
}
func (array *SealdTmrRecipientsWithRightsArray) getSlice() []*sdk.TmrRecipientWithRights {
return array.items
}
// Helper SealdEncryptionSessionArray
type SealdEncryptionSessionArray struct {
items []*sdk.EncryptionSession
}
func sealdEncryptionSessionArrayToGo(array *C.SealdEncryptionSessionArray) *SealdEncryptionSessionArray {
return (*SealdEncryptionSessionArray)(unsafe.Pointer(array))
}
var sealdEncryptionSessionArrayRefMap = sync.Map{}
//export SealdEncryptionSessionArray_New
func SealdEncryptionSessionArray_New() *C.SealdEncryptionSessionArray {
array := &SealdEncryptionSessionArray{}
sealdEncryptionSessionArrayRefMap.Store(uintptr(unsafe.Pointer(array)), array)
return (*C.SealdEncryptionSessionArray)(unsafe.Pointer(array))
}
//export SealdEncryptionSessionArray_Free
func SealdEncryptionSessionArray_Free(array *C.SealdEncryptionSessionArray) {
sealdEncryptionSessionArrayRefMap.Delete(uintptr(unsafe.Pointer(array)))
}
//export SealdEncryptionSessionArray_Add
func SealdEncryptionSessionArray_Add(array *C.SealdEncryptionSessionArray, es *C.SealdEncryptionSession) {
goArray := sealdEncryptionSessionArrayToGo(array)
goArray.items = append(goArray.items, encryptionSessionToGo(es))
}
//export SealdEncryptionSessionArray_Get
func SealdEncryptionSessionArray_Get(array *C.SealdEncryptionSessionArray, i C.int) *C.SealdEncryptionSession {
goArray := sealdEncryptionSessionArrayToGo(array)
return goEncryptionSessionToC(goArray.items[int(i)])
}
//export SealdEncryptionSessionArray_Size
func SealdEncryptionSessionArray_Size(array *C.SealdEncryptionSessionArray) C.int {
goArray := sealdEncryptionSessionArrayToGo(array)
return C.int(len(goArray.items))
}
func (array *SealdEncryptionSessionArray) getSlice() []*sdk.EncryptionSession {
return array.items
}
func sliceToSealdEncryptionSessionArray(slice []*sdk.EncryptionSession) *C.SealdEncryptionSessionArray {
array := SealdEncryptionSessionArray_New()
goArray := sealdEncryptionSessionArrayToGo(array)
goArray.items = append([]*sdk.EncryptionSession{}, slice...)
return array
}
func sealdGroupTMRTempKeyFromGo(gTMRTK *sdk.GroupTMRTemporaryKey) *C.SealdGroupTMRTemporaryKey {
res := (*C.SealdGroupTMRTemporaryKey)(C.malloc(C.size_t(unsafe.Sizeof(C.SealdGroupTMRTemporaryKey{}))))
res.Id = C.CString(gTMRTK.Id)
res.GroupId = C.CString(gTMRTK.GroupId)
res.IsAdmin = utils.Ternary(gTMRTK.IsAdmin, C.int(1), C.int(0))
res.CreatedById = C.CString(gTMRTK.CreatedById)
res.Created = C.longlong(gTMRTK.Created.Unix())
res.AuthFactorType = C.CString(gTMRTK.AuthFactorType)
return res
}
//export SealdGroupTMRTemporaryKey_Free
func SealdGroupTMRTemporaryKey_Free(gtmrtk *C.SealdGroupTMRTemporaryKey) {
if gtmrtk == nil {
return
}
C.free(unsafe.Pointer(gtmrtk.Id))
C.free(unsafe.Pointer(gtmrtk.GroupId))
C.free(unsafe.Pointer(gtmrtk.CreatedById))
C.free(unsafe.Pointer(gtmrtk.AuthFactorType))
C.free(unsafe.Pointer(gtmrtk))
}
type SealdGroupTMRTemporaryKeysArray struct {
items []*C.SealdGroupTMRTemporaryKey
}
func sealdGroupTMRTemporaryKeysArrayToGo(array *C.SealdGroupTMRTemporaryKeysArray) *SealdGroupTMRTemporaryKeysArray {
if array == nil {
return nil
}
return (*SealdGroupTMRTemporaryKeysArray)(unsafe.Pointer(array))
}
var sealdGroupTMRTemporaryKeysArrayRefMap = sync.Map{}