-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
125 lines (112 loc) · 2.87 KB
/
parser.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
package main
/*
#include <stdlib.h>
#ifndef RESPONSE_H
#define RESPONSE_H
#include "response.h"
#endif
*/
import "C"
import (
"crypto/ecdsa"
"crypto/elliptic"
"encoding/hex"
"fmt"
"github.com/AxLabs/neofs-api-shared-lib/response"
"github.com/google/uuid"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
v2container "github.com/nspcc-dev/neofs-api-go/v2/container"
"github.com/nspcc-dev/neofs-sdk-go/container"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/user"
"math/big"
)
func toGoString(cString *C.char) string {
return C.GoString(cString)
}
func responseToC(p *response.PointerResponse) C.pointerResponse {
return C.pointerResponse{
C.CString(p.GetTypeString()),
C.int(len(p.GetData())),
(*C.char)(C.CBytes(p.GetData())),
}
}
func stringResponseToC(p *response.StringResponse) C.response {
return C.response{
C.CString(p.GetTypeString()),
C.CString(p.GetValue()),
}
}
func GetECDSAPrivKey(key *C.char) *ecdsa.PrivateKey {
keyStr := C.GoString(key)
bytes, _ := hex.DecodeString(keyStr)
k := new(big.Int)
k.SetBytes(bytes)
priv := new(ecdsa.PrivateKey)
curve := elliptic.P256()
priv.PublicKey.Curve = curve
priv.D = k
priv.PublicKey.X, priv.PublicKey.Y = curve.ScalarBaseMult(k.Bytes())
return priv
}
func UserIDFromPublicKey(publicKey *C.char) (*user.ID, error) {
pubKey, err := keys.NewPublicKeyFromString(C.GoString(publicKey))
if err != nil {
return nil, err
}
var id user.ID
uint160, err := address.StringToUint160(pubKey.Address())
if err != nil {
return nil, err
}
id.SetScriptHash(uint160)
return &id, nil
}
func uuidToGo(clientID *C.char) (*uuid.UUID, error) {
id, err := uuid.Parse(C.GoString(clientID))
if err != nil {
return nil, fmt.Errorf("could not parse provided client id. id was " + C.GoString(clientID))
}
return &id, nil
}
func getContainerFromC(v2Container *C.char) (*container.Container, error) {
v2cnr := new(v2container.Container)
err := v2cnr.UnmarshalJSON([]byte(C.GoString(v2Container)))
if err != nil {
return nil, err
}
//v2cnr.SetHomomorphicHashingState()
var cnr container.Container
err = cnr.ReadFromV2(*v2cnr)
if err != nil {
return nil, err
}
return &cnr, nil
}
func getContainerIDFromC(containerID *C.char) (*cid.ID, error) {
id := new(cid.ID)
err := id.DecodeString(C.GoString(containerID))
if err != nil {
return nil, err
}
return id, nil
}
func getObjectIDFromC(objectID *C.char) (*oid.ID, error) {
id := new(oid.ID)
err := id.DecodeString(C.GoString(objectID))
if err != nil {
return nil, err
}
return id, nil
}
//func getSessionTokenFromC(sessionToken *C.char) (*session.Container, error) {
// token := new(session.Container)
//
// err := token.Unmarshal([]byte(C.GoString(sessionToken)))
// if err != nil {
// return nil, err
// }
// return token, nil
//}