forked from jmzkChain/jmzk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevt_evtlink.cpp
191 lines (171 loc) · 4.33 KB
/
evt_evtlink.cpp
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
/**
* @file
* @copyright defined in evt/LICENSE.txt
*/
#include <libevt/evt_evtlink.h>
#include "evt_impl.hpp"
#include <string.h>
#include <stddef.h>
#include <evt/chain/contracts/evt_link.hpp>
#include <fc/crypto/private_key.hpp>
using evt::chain::contracts::evt_link;
using fc::crypto::private_key;
extern "C" {
evt_link_t*
evt_link_new() {
auto linkp = new evt_link();
return (evt_link_t*)linkp;
}
void
evt_link_free(evt_link_t* linkp) {
delete (evt_link*)(linkp);
}
int
evt_link_tostring(evt_link_t* linkp, char** str) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
auto _str = ((evt_link*)(linkp))->to_string();
*str = strdup(_str);
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_parse_from_evtli(const char* str, evt_link_t* linkp) {
if (str == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
*((evt_link*)(linkp)) = evt_link::parse_from_evtli(std::string(str));
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_get_header(evt_link_t* linkp, uint16_t* header/* out */) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
*header = ((evt_link*)(linkp))->get_header();
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_set_header(evt_link_t* linkp, uint16_t header) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
((evt_link*)(linkp))->set_header(header);
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_get_segment_int(evt_link_t* linkp, uint8_t key, uint32_t* intv) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
auto _segment = ((evt_link*)(linkp))->get_segment(key);
if(_segment.intv) {
*intv = *_segment.intv;
}
else {
return EVT_INVALID_ARGUMENT;
}
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_get_segment_str(evt_link_t* linkp, uint8_t key, char** strv) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
auto _segment = ((evt_link*)(linkp))->get_segment(key);
if(_segment.strv) {
*strv = strdup(*_segment.strv);
}
else {
return EVT_INVALID_ARGUMENT;
}
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_add_segment_int(evt_link_t* linkp, uint8_t key, uint32_t intv) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
auto _segment = evt_link::segment(key, intv);
((evt_link*)(linkp))->add_segment(_segment);
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_add_segment_str(evt_link_t* linkp, uint8_t key, const char* strv) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
auto _segment = evt_link::segment(key, std::string(strv));
((evt_link*)(linkp))->add_segment(_segment);
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_clear_signatures(evt_link_t* linkp) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
((evt_link*)(linkp))->clear_signatures();
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_get_signatures(evt_link_t* linkp, evt_signature_t*** signs, uint32_t* len) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
try {
auto _signs = ((evt_link*)(linkp))->get_signatures();
auto size = _signs.size();
auto psigns = (evt_signature_t**)malloc(sizeof(evt_signature_t*) * size);
int i = 0;
for(auto it = _signs.begin(); it != _signs.end(); it++, i++) {
psigns[i] = get_evt_data(*it);
}
*signs = psigns;
*len = size;
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
int
evt_link_sign(evt_link_t* linkp, evt_private_key_t* priv_key) {
if (linkp == nullptr) {
return EVT_INVALID_ARGUMENT;
}
auto pk = private_key();
if (extract_data(priv_key, pk) != EVT_OK) {
return EVT_INVALID_PRIVATE_KEY;
}
try {
((evt_link*)(linkp))->sign(pk);
}
CATCH_AND_RETURN(EVT_INTERNAL_ERROR)
return EVT_OK;
}
}