-
Notifications
You must be signed in to change notification settings - Fork 0
/
rp.go
384 lines (366 loc) · 13 KB
/
rp.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
/**
* Copyright (c) 2018, 2019 National Digital ID COMPANY LIMITED
*
* This file is part of NDID software.
*
* NDID is the free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or any later
* version.
*
* NDID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Affero GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* along with the NDID source code. If not, see https://www.gnu.org/licenses/agpl.txt.
*
* Please contact info@ndid.co.th for any further questions
*
*/
package did
import (
"encoding/json"
"github.com/golang/protobuf/proto"
"github.com/ndidplatform/smart-contract/abci/code"
"github.com/ndidplatform/smart-contract/abci/utils"
"github.com/ndidplatform/smart-contract/protos/data"
"github.com/tendermint/tendermint/abci/types"
)
func (app *DIDApplication) createRequest(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("CreateRequest, Parameter: %s", param)
var funcParam CreateRequestParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// log chain ID
app.logger.Infof("CreateRequest, Chain ID: %s", app.CurrentChain)
var request data.Request
// set request data
request.RequestId = funcParam.RequestID
key := "Request" + "|" + request.RequestId
requestIDExist := app.HasVersionedStateDB([]byte(key))
if requestIDExist {
return app.ReturnDeliverTxLog(code.DuplicateRequestID, "Duplicate Request ID", "")
}
request.MinIdp = int64(funcParam.MinIdp)
request.MinAal = funcParam.MinAal
request.MinIal = funcParam.MinIal
request.RequestTimeout = int64(funcParam.Timeout)
// request.DataRequestList = funcParam.DataRequestList
request.RequestMessageHash = funcParam.MessageHash
request.Mode = funcParam.Mode
// Check valid mode
allowedMode := app.GetAllowedModeFromStateDB(funcParam.Purpose, false)
validMode := false
for _, mode := range allowedMode {
if mode == request.Mode {
validMode = true
break
}
}
if !validMode {
return app.ReturnDeliverTxLog(code.InvalidMode, "Must be create request on valid mode", "")
}
request.IdpIdList = funcParam.IdPIDList
// Check all IdP in list is active
for _, idp := range request.IdpIdList {
// Get node detail
nodeDetailKey := "NodeID" + "|" + idp
_, nodeDetaiValue := app.GetStateDB([]byte(nodeDetailKey))
if nodeDetaiValue == nil {
return app.ReturnDeliverTxLog(code.NodeIDNotFound, "Node ID not found", "")
}
var node data.NodeDetail
err = proto.Unmarshal([]byte(nodeDetaiValue), &node)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// Check node is active
if !node.Active {
return app.ReturnDeliverTxLog(code.NodeIDInIdPListIsNotActive, "Node ID in IdP list is not active", "")
}
// If node is behind proxy
if node.ProxyNodeId != "" {
proxyNodeID := node.ProxyNodeId
// Get proxy node detail
proxyNodeDetailKey := "NodeID" + "|" + string(proxyNodeID)
_, proxyNodeDetailValue := app.GetStateDB([]byte(proxyNodeDetailKey))
if proxyNodeDetailValue == nil {
return app.ReturnDeliverTxLog(code.NodeIDNotFound, "Node ID not found", "")
}
var proxyNode data.NodeDetail
err = proto.Unmarshal([]byte(proxyNodeDetailValue), &proxyNode)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// Check proxy node is active
if !proxyNode.Active {
return app.ReturnDeliverTxLog(code.NodeIDInIdPListIsNotActive, "Node ID in IdP list is not active", "")
}
}
}
// set data request
request.DataRequestList = make([]*data.DataRequest, 0)
serviceIDInDataRequestList := make(map[string]int)
nodeDetailMap := make(map[string]*data.NodeDetail, 0)
for index := range funcParam.DataRequestList {
var newRow data.DataRequest
newRow.ServiceId = funcParam.DataRequestList[index].ServiceID
// Check for duplicate service ID in data request list
_, exist := serviceIDInDataRequestList[newRow.ServiceId]
if exist {
return app.ReturnDeliverTxLog(code.DuplicateServiceIDInDataRequest, "Duplicate Service ID In Data Request", "")
}
serviceIDInDataRequestList[newRow.ServiceId]++
newRow.RequestParamsHash = funcParam.DataRequestList[index].RequestParamsHash
newRow.MinAs = int64(funcParam.DataRequestList[index].Count)
newRow.AsIdList = funcParam.DataRequestList[index].As
if funcParam.DataRequestList[index].As == nil {
newRow.AsIdList = make([]string, 0)
}
newRow.AnsweredAsIdList = make([]string, 0)
newRow.ReceivedDataFromList = make([]string, 0)
// Check all as in as_list is active
for _, as := range newRow.AsIdList {
var node data.NodeDetail
if nodeDetailMap[as] == nil {
// Get node detail
nodeDetailKey := "NodeID" + "|" + as
_, nodeDetaiValue := app.GetStateDB([]byte(nodeDetailKey))
if nodeDetaiValue == nil {
return app.ReturnDeliverTxLog(code.NodeIDNotFound, "Node ID not found", "")
}
err = proto.Unmarshal([]byte(nodeDetaiValue), &node)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// Save node detail to mapping
nodeDetailMap[as] = &node
} else {
// Get node detail from mapping
node = *nodeDetailMap[as]
}
// Check node is active
if !node.Active {
return app.ReturnDeliverTxLog(code.NodeIDInASListIsNotActive, "Node ID in AS list is not active", "")
}
// If node is behind proxy
if node.ProxyNodeId != "" {
proxyNodeID := node.ProxyNodeId
// Get proxy node detail
proxyNodeDetailKey := "NodeID" + "|" + string(proxyNodeID)
_, proxyNodeDetailValue := app.GetStateDB([]byte(proxyNodeDetailKey))
if proxyNodeDetailValue == nil {
return app.ReturnDeliverTxLog(code.NodeIDNotFound, "Node ID not found", "")
}
var proxyNode data.NodeDetail
err = proto.Unmarshal([]byte(proxyNodeDetailValue), &proxyNode)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// Check proxy node is active
if !proxyNode.Active {
return app.ReturnDeliverTxLog(code.NodeIDInASListIsNotActive, "Node ID in AS list is not active", "")
}
}
}
request.DataRequestList = append(request.DataRequestList, &newRow)
}
// set default value
request.Closed = false
request.TimedOut = false
request.Purpose = ""
request.UseCount = 0
// set Owner
request.Owner = nodeID
// set Can add accossor
ownerRole := app.getRoleFromNodeID(nodeID)
if string(ownerRole) == "IdP" {
request.Purpose = funcParam.Purpose
}
// set default value
request.ResponseList = make([]*data.Response, 0)
// set creation_block_height
request.CreationBlockHeight = app.CurrentBlock
// set chain_id
request.ChainId = app.CurrentChain
value, err := utils.ProtoDeterministicMarshal(&request)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
app.SetVersionedStateDB([]byte(key), []byte(value))
return app.ReturnDeliverTxLog(code.OK, "success", request.RequestId)
}
func (app *DIDApplication) closeRequest(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("CloseRequest, Parameter: %s", param)
var funcParam CloseRequestParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
key := "Request" + "|" + funcParam.RequestID
_, value := app.GetVersionedStateDB([]byte(key), 0)
if value == nil {
return app.ReturnDeliverTxLog(code.RequestIDNotFound, "Request ID not found", "")
}
var request data.Request
err = proto.Unmarshal([]byte(value), &request)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
if request.Closed {
return app.ReturnDeliverTxLog(code.RequestIsClosed, "Can not close a closed request", "")
}
if request.TimedOut {
return app.ReturnDeliverTxLog(code.RequestIsTimedOut, "Can not close a timed out request", "")
}
for _, valid := range funcParam.ResponseValidList {
for index := range request.ResponseList {
if valid.IdpID == request.ResponseList[index].IdpId {
if valid.ValidIal != nil {
if *valid.ValidIal {
request.ResponseList[index].ValidIal = "true"
} else {
request.ResponseList[index].ValidIal = "false"
}
}
if valid.ValidSignature != nil {
if *valid.ValidSignature {
request.ResponseList[index].ValidSignature = "true"
} else {
request.ResponseList[index].ValidSignature = "false"
}
}
}
}
}
request.Closed = true
value, err = utils.ProtoDeterministicMarshal(&request)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
app.SetVersionedStateDB([]byte(key), []byte(value))
return app.ReturnDeliverTxLog(code.OK, "success", funcParam.RequestID)
}
func (app *DIDApplication) timeOutRequest(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("TimeOutRequest, Parameter: %s", param)
var funcParam TimeOutRequestParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
key := "Request" + "|" + funcParam.RequestID
_, value := app.GetVersionedStateDB([]byte(key), 0)
if value == nil {
return app.ReturnDeliverTxLog(code.RequestIDNotFound, "Request ID not found", "")
}
var request data.Request
err = proto.Unmarshal([]byte(value), &request)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
if request.TimedOut {
return app.ReturnDeliverTxLog(code.RequestIsTimedOut, "Can not set time out a timed out request", "")
}
if request.Closed {
return app.ReturnDeliverTxLog(code.RequestIsClosed, "Can not set time out a closed request", "")
}
for _, valid := range funcParam.ResponseValidList {
for index := range request.ResponseList {
if valid.IdpID == request.ResponseList[index].IdpId {
if valid.ValidIal != nil {
if *valid.ValidIal {
request.ResponseList[index].ValidIal = "true"
} else {
request.ResponseList[index].ValidIal = "false"
}
}
if valid.ValidSignature != nil {
if *valid.ValidSignature {
request.ResponseList[index].ValidSignature = "true"
} else {
request.ResponseList[index].ValidSignature = "false"
}
}
}
}
}
request.TimedOut = true
value, err = utils.ProtoDeterministicMarshal(&request)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
app.SetVersionedStateDB([]byte(key), []byte(value))
return app.ReturnDeliverTxLog(code.OK, "success", funcParam.RequestID)
}
func (app *DIDApplication) setDataReceived(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("SetDataReceived, Parameter: %s", param)
var funcParam SetDataReceivedParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
key := "Request" + "|" + funcParam.RequestID
_, value := app.GetVersionedStateDB([]byte(key), 0)
if value == nil {
return app.ReturnDeliverTxLog(code.RequestIDNotFound, "Request ID not found", "")
}
var request data.Request
err = proto.Unmarshal([]byte(value), &request)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// Check IsClosed
if request.Closed {
return app.ReturnDeliverTxLog(code.RequestIsClosed, "Request is closed", "")
}
// Check IsTimedOut
if request.TimedOut {
return app.ReturnDeliverTxLog(code.RequestIsTimedOut, "Request is timed out", "")
}
// Check as_id is exist in as_id_list
exist := false
for _, dataRequest := range request.DataRequestList {
if dataRequest.ServiceId == funcParam.ServiceID {
for _, as := range dataRequest.AnsweredAsIdList {
if as == funcParam.AsID {
exist = true
break
}
}
}
}
if exist == false {
return app.ReturnDeliverTxLog(code.AsIDDoesNotExistInASList, "AS ID does not exist in answered AS list", "")
}
// Check Duplicate AS ID
duplicate := false
for _, dataRequest := range request.DataRequestList {
if dataRequest.ServiceId == funcParam.ServiceID {
for _, as := range dataRequest.ReceivedDataFromList {
if as == funcParam.AsID {
duplicate = true
break
}
}
}
}
if duplicate == true {
return app.ReturnDeliverTxLog(code.DuplicateASInDataRequest, "Duplicate AS ID in data request", "")
}
// Update received_data_from_list in request
for index, dataRequest := range request.DataRequestList {
if dataRequest.ServiceId == funcParam.ServiceID {
request.DataRequestList[index].ReceivedDataFromList = append(dataRequest.ReceivedDataFromList, funcParam.AsID)
}
}
value, err = utils.ProtoDeterministicMarshal(&request)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
app.SetVersionedStateDB([]byte(key), []byte(value))
return app.ReturnDeliverTxLog(code.OK, "success", funcParam.RequestID)
}