-
Notifications
You must be signed in to change notification settings - Fork 22
/
request.go
792 lines (731 loc) · 26.5 KB
/
request.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
/**
* 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 app
import (
"encoding/json"
"github.com/tendermint/tendermint/abci/types"
"google.golang.org/protobuf/proto"
"github.com/ndidplatform/smart-contract/v7/abci/code"
"github.com/ndidplatform/smart-contract/v7/abci/utils"
data "github.com/ndidplatform/smart-contract/v7/protos/data"
)
type IdPResponse struct {
Ial *float64 `json:"ial,omitempty"`
Aal *float64 `json:"aal,omitempty"`
Status *string `json:"status,omitempty"`
Signature *string `json:"signature,omitempty"`
IdpID string `json:"idp_id"`
ValidIal *bool `json:"valid_ial"`
ValidSignature *bool `json:"valid_signature"`
ErrorCode *int32 `json:"error_code,omitempty"`
}
type ASResponse struct {
AsID string `json:"as_id"`
Signed *bool `json:"signed,omitempty"`
ReceivedData *bool `json:"received_data,omitempty"`
ErrorCode *int32 `json:"error_code,omitempty"`
}
type DataRequest struct {
ServiceID string `json:"service_id"`
As []string `json:"as_id_list"`
Count int `json:"min_as"`
RequestParamsHash string `json:"request_params_hash"`
ResponseList []ASResponse `json:"response_list"`
}
type CreateRequestParam struct {
RequestID string `json:"request_id"`
MinIdp int `json:"min_idp"`
MinAal float64 `json:"min_aal"`
MinIal float64 `json:"min_ial"`
Timeout int `json:"request_timeout"`
IdPIDList []string `json:"idp_id_list"`
DataRequestList []DataRequest `json:"data_request_list"`
MessageHash string `json:"request_message_hash"`
Purpose string `json:"purpose"`
Mode int32 `json:"mode"`
RequestType *string `json:"request_type"`
}
func (app *ABCIApplication) 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(), "")
}
// get RP node detail
nodeDetailKey := nodeIDKeyPrefix + keySeparator + nodeID
nodeDetaiValue, err := app.state.Get([]byte(nodeDetailKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if nodeDetaiValue == nil {
return app.ReturnDeliverTxLog(code.NodeIDNotFound, "Node ID not found", "")
}
var requesterNodeDetail data.NodeDetail
err = proto.Unmarshal([]byte(nodeDetaiValue), &requesterNodeDetail)
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
request.Mode = funcParam.Mode
if requesterNodeDetail.Role == "IdP" {
// IdP must not be able to create request with mode 1 or 2
if request.Mode == 1 {
return app.ReturnDeliverTxLog(code.IdPCreateRequestMode1And2NotAllowed, "IdP cannot create request with mode 1 or 2", "")
}
// IdP must not be able to create request with data request to AS
if len(funcParam.DataRequestList) > 0 {
return app.ReturnDeliverTxLog(code.IdPCreateRequestWithDataRequestNotAllowed, "IdP cannot create request with data request", "")
}
}
key := requestKeyPrefix + keySeparator + request.RequestId
requestIDExist, err := app.state.HasVersioned([]byte(key), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
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
// 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 {
// Check idp is in the rp whitelist
if requesterNodeDetail.UseWhitelist && !contains(idp, requesterNodeDetail.Whitelist) {
return app.ReturnDeliverTxLog(code.NodeNotInWhitelist, "IdP is not in RP whitelist", "")
}
// Get node detail
nodeDetailKey := nodeIDKeyPrefix + keySeparator + idp
nodeDetaiValue, err := app.state.Get([]byte(nodeDetailKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
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", "")
}
// Check rp is in the idp whitelist
if node.UseWhitelist && !contains(nodeID, node.Whitelist) {
return app.ReturnDeliverTxLog(code.NodeNotInWhitelist, "RP is not in IdP whitelist", "")
}
// If node is behind proxy
if node.ProxyNodeId != "" {
proxyNodeID := node.ProxyNodeId
// Get proxy node detail
proxyNodeDetailKey := nodeIDKeyPrefix + keySeparator + string(proxyNodeID)
proxyNodeDetailValue, err := app.state.Get([]byte(proxyNodeDetailKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
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.ResponseList = make([]*data.ASResponse, 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 := nodeIDKeyPrefix + keySeparator + as
nodeDetaiValue, err := app.state.Get([]byte(nodeDetailKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
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 := nodeIDKeyPrefix + keySeparator + string(proxyNodeID)
proxyNodeDetailValue, err := app.state.Get([]byte(proxyNodeDetailKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
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
if funcParam.RequestType != nil {
key := requestTypeKeyPrefix + keySeparator + *funcParam.RequestType
requestTypeExists, err := app.state.Has([]byte(key), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if !requestTypeExists {
return app.ReturnDeliverTxLog(code.RequestTypeDoesNotExist, err.Error(), "")
}
request.RequestType = *funcParam.RequestType
}
// set Owner
request.Owner = nodeID
// set Can add accossor
if requesterNodeDetail.Role == "IdP" {
request.Purpose = funcParam.Purpose
}
// set default value
request.ResponseList = make([]*data.Response, 0)
// set creation_block_height
request.CreationBlockHeight = app.state.CurrentBlockHeight
// set chain_id
request.ChainId = app.CurrentChain
value, err := utils.ProtoDeterministicMarshal(&request)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
err = app.state.SetVersioned([]byte(key), []byte(value))
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
return app.ReturnDeliverTxLog(code.OK, "success", request.RequestId)
}
type ResponseValid struct {
IdpID string `json:"idp_id"`
ValidIal *bool `json:"valid_ial"`
ValidSignature *bool `json:"valid_signature"`
}
type CloseRequestParam struct {
RequestID string `json:"request_id"`
ResponseValidList []ResponseValid `json:"response_valid_list"`
}
func (app *ABCIApplication) 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 := requestKeyPrefix + keySeparator + funcParam.RequestID
value, err := app.state.GetVersioned([]byte(key), 0, false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
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(), "")
}
err = app.state.SetVersioned([]byte(key), []byte(value))
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
return app.ReturnDeliverTxLog(code.OK, "success", funcParam.RequestID)
}
type TimeOutRequestParam struct {
RequestID string `json:"request_id"`
ResponseValidList []ResponseValid `json:"response_valid_list"`
}
func (app *ABCIApplication) 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 := requestKeyPrefix + keySeparator + funcParam.RequestID
value, err := app.state.GetVersioned([]byte(key), 0, false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
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(), "")
}
err = app.state.SetVersioned([]byte(key), []byte(value))
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
return app.ReturnDeliverTxLog(code.OK, "success", funcParam.RequestID)
}
type SetDataReceivedParam struct {
RequestID string `json:"request_id"`
ServiceID string `json:"service_id"`
AsID string `json:"as_id"`
}
func (app *ABCIApplication) 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 := requestKeyPrefix + keySeparator + funcParam.RequestID
value, err := app.state.GetVersioned([]byte(key), 0, false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
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", "")
}
var targetAsResponse *data.ASResponse
for _, dataRequest := range request.DataRequestList {
if dataRequest.ServiceId == funcParam.ServiceID {
for _, asResponse := range dataRequest.ResponseList {
if asResponse.AsId == funcParam.AsID {
targetAsResponse = asResponse
break
}
}
}
}
// Check as_id is exist in as_id_list
if targetAsResponse == nil {
return app.ReturnDeliverTxLog(code.AsIDDoesNotExistInASList, "AS ID does not exist in answered AS list", "")
}
// Check Duplicate
if targetAsResponse.ReceivedData {
return app.ReturnDeliverTxLog(code.DuplicateASInDataRequest, "Duplicate AS ID in data request", "")
}
// Update targetAsResponse status
targetAsResponse.ReceivedData = true
value, err = utils.ProtoDeterministicMarshal(&request)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
err = app.state.SetVersioned([]byte(key), []byte(value))
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
return app.ReturnDeliverTxLog(code.OK, "success", funcParam.RequestID)
}
type GetRequestParam struct {
RequestID string `json:"request_id"`
}
type GetRequestResult struct {
IsClosed bool `json:"closed"`
IsTimedOut bool `json:"timed_out"`
MessageHash string `json:"request_message_hash"`
Mode int32 `json:"mode"`
}
func (app *ABCIApplication) getRequest(param string, height int64) types.ResponseQuery {
app.logger.Infof("GetRequest, Parameter: %s", param)
var funcParam GetRequestParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
key := requestKeyPrefix + keySeparator + funcParam.RequestID
value, err := app.state.GetVersioned([]byte(key), height, true)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
if value == nil {
valueJSON := []byte("{}")
return app.ReturnQuery(valueJSON, "not found", app.state.Height)
}
var request data.Request
err = proto.Unmarshal([]byte(value), &request)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
var res GetRequestResult
res.IsClosed = request.Closed
res.IsTimedOut = request.TimedOut
res.MessageHash = request.RequestMessageHash
res.Mode = request.Mode
valueJSON, err := json.Marshal(res)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
return app.ReturnQuery(valueJSON, "success", app.state.Height)
}
type GetRequestDetailResult struct {
RequestID string `json:"request_id"`
MinIdp int `json:"min_idp"`
MinAal float64 `json:"min_aal"`
MinIal float64 `json:"min_ial"`
Timeout int `json:"request_timeout"`
IdPIDList []string `json:"idp_id_list"`
DataRequestList []DataRequest `json:"data_request_list"`
MessageHash string `json:"request_message_hash"`
Responses []IdPResponse `json:"response_list"`
IsClosed bool `json:"closed"`
IsTimedOut bool `json:"timed_out"`
Purpose string `json:"purpose"`
Mode int32 `json:"mode"`
RequestType *string `json:"request_type"`
RequesterNodeID string `json:"requester_node_id"`
CreationBlockHeight int64 `json:"creation_block_height"`
CreationChainID string `json:"creation_chain_id"`
}
func (app *ABCIApplication) getRequestDetail(param string, height int64, committedState bool) types.ResponseQuery {
app.logger.Infof("GetRequestDetail, Parameter: %s", param)
var funcParam GetRequestParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
key := requestKeyPrefix + keySeparator + funcParam.RequestID
var value []byte
value, err = app.state.GetVersioned([]byte(key), height, committedState)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
if value == nil {
valueJSON := []byte("{}")
return app.ReturnQuery(valueJSON, "not found", app.state.Height)
}
var result GetRequestDetailResult
var request data.Request
err = proto.Unmarshal([]byte(value), &request)
if err != nil {
value = []byte("")
return app.ReturnQuery(value, err.Error(), app.state.Height)
}
result.RequestID = request.RequestId
result.MinIdp = int(request.MinIdp)
result.MinAal = float64(request.MinAal)
result.MinIal = float64(request.MinIal)
result.Timeout = int(request.RequestTimeout)
result.IdPIDList = request.IdpIdList
result.DataRequestList = make([]DataRequest, 0)
for _, dataRequest := range request.DataRequestList {
newRow := DataRequest{
ServiceID: dataRequest.ServiceId,
As: dataRequest.AsIdList,
Count: int(dataRequest.MinAs),
ResponseList: make([]ASResponse, 0, len(dataRequest.ResponseList)),
RequestParamsHash: dataRequest.RequestParamsHash,
}
for _, asResponse := range dataRequest.ResponseList {
if asResponse.ErrorCode == 0 {
newRow.ResponseList = append(newRow.ResponseList, ASResponse{
AsID: asResponse.AsId,
Signed: &asResponse.Signed,
ReceivedData: &asResponse.ReceivedData,
})
} else {
newRow.ResponseList = append(newRow.ResponseList, ASResponse{
AsID: asResponse.AsId,
ErrorCode: &asResponse.ErrorCode,
})
}
}
result.DataRequestList = append(result.DataRequestList, newRow)
}
result.MessageHash = request.RequestMessageHash
result.Responses = make([]IdPResponse, 0)
for _, response := range request.ResponseList {
var newRow IdPResponse
if response.ErrorCode == 0 {
var validIal *bool
if response.ValidIal != "" {
tValue := response.ValidIal == "true"
validIal = &tValue
}
var validSignature *bool
if response.ValidSignature != "" {
tValue := response.ValidSignature == "true"
validSignature = &tValue
}
ial := float64(response.Ial)
aal := float64(response.Aal)
newRow = IdPResponse{
IdpID: response.IdpId,
Ial: &ial,
Aal: &aal,
Status: &response.Status,
Signature: &response.Signature,
ValidIal: validIal,
ValidSignature: validSignature,
}
} else {
newRow = IdPResponse{
IdpID: response.IdpId,
ErrorCode: &response.ErrorCode,
}
}
result.Responses = append(result.Responses, newRow)
}
result.IsClosed = request.Closed
result.IsTimedOut = request.TimedOut
result.Mode = request.Mode
// Set purpose
result.Purpose = request.Purpose
// make nil to array len 0
if result.IdPIDList == nil {
result.IdPIDList = make([]string, 0)
}
if request.RequestType != "" {
result.RequestType = &request.RequestType
}
// Set requester_node_id
result.RequesterNodeID = request.Owner
// Set creation_block_height
result.CreationBlockHeight = request.CreationBlockHeight
// Set creation_chain_id
result.CreationChainID = request.ChainId
resultJSON, err := json.Marshal(result)
if err != nil {
value = []byte("")
return app.ReturnQuery(value, err.Error(), app.state.Height)
}
return app.ReturnQuery(resultJSON, "success", app.state.Height)
}
type GetDataSignatureParam struct {
NodeID string `json:"node_id"`
ServiceID string `json:"service_id"`
RequestID string `json:"request_id"`
}
type GetDataSignatureResult struct {
Signature string `json:"signature"`
}
func (app *ABCIApplication) getDataSignature(param string) types.ResponseQuery {
app.logger.Infof("GetDataSignature, Parameter: %s", param)
var funcParam GetDataSignatureParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
signDataKey := dataSignatureKeyPrefix + keySeparator + funcParam.NodeID + keySeparator + funcParam.ServiceID + keySeparator + funcParam.RequestID
signDataValue, err := app.state.Get([]byte(signDataKey), true)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
if signDataValue == nil {
return app.ReturnQuery([]byte("{}"), "not found", app.state.Height)
}
var result GetDataSignatureResult
result.Signature = string(signDataValue)
returnValue, err := json.Marshal(result)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
return app.ReturnQuery(returnValue, "success", app.state.Height)
}
type GetAllowedModeListParam struct {
Purpose string `json:"purpose"`
}
type GetAllowedModeListResult struct {
AllowedModeList []int32 `json:"allowed_mode_list"`
}
func (app *ABCIApplication) GetAllowedModeList(param string) types.ResponseQuery {
app.logger.Infof("GetAllowedModeList, Parameter: %s", param)
var funcParam GetAllowedModeListParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
var result GetAllowedModeListResult
result.AllowedModeList = app.GetAllowedModeFromStateDB(funcParam.Purpose, true)
returnValue, err := json.Marshal(result)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
return app.ReturnQuery(returnValue, "success", app.state.Height)
}
func (app *ABCIApplication) GetAllowedModeFromStateDB(purpose string, committedState bool) (result []int32) {
allowedModeKey := "AllowedModeList" + keySeparator + purpose
var allowedModeList data.AllowedModeList
allowedModeValue, err := app.state.Get([]byte(allowedModeKey), committedState)
if err != nil {
return nil
}
if allowedModeValue == nil {
// return default value
if !modeFunctionMap[purpose] {
result = append(result, 1)
}
result = append(result, 2)
result = append(result, 3)
return result
}
err = proto.Unmarshal(allowedModeValue, &allowedModeList)
if err != nil {
return result
}
result = allowedModeList.Mode
return result
}