This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathservice.gateway.go
140 lines (108 loc) · 5.31 KB
/
service.gateway.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
/*
Copyright 2020 Telefónica Digital España. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
log "TrustID/fabric-chaincode/log"
"encoding/json"
"errors"
"github.com/hyperledger/fabric-chaincode-go/shim"
)
func (cc *Chaincode) createServiceIdentity(stub shim.ChaincodeStubInterface, did string, args interface{}) (string, error) {
var err error
service := make(map[string]interface{})
service = args.(map[string]interface{})
log.Debugf("[%s][%s][createServiceIdentity] Calling to registry", CHANNEL_ENV, ServiceGATEWAY)
log.Debugf("[%s][%s][createServiceIdentity] ****The service store is %v", CHANNEL_ENV, ServiceGATEWAY, args)
if service["name"] == nil {
log.Errorf("[%s][%s][getIdencreateServiceIdentitytity] Error creating service in registry: %v", CHANNEL_ENV, ServiceGATEWAY, "Name input param is undefined")
return "", errors.New("Name input param is undefined")
}
if service["channel"] == nil {
log.Errorf("[%s][%s][createServiceIdentity] Error creating service in registry: %v", CHANNEL_ENV, ServiceGATEWAY, "Channel input param is undefined")
return "", errors.New("Channel input param is undefined")
}
if service["did"] == nil {
log.Errorf("[%s][%s][createServiceIdentity] Error creating service in registry: %v", CHANNEL_ENV, ServiceGATEWAY, "DID input param is undefined")
return "", errors.New("DID input param is undefined")
}
serviceStore := Service{Name: service["name"].(string), Controller: did, Channel: service["channel"].(string)}
access := AccessPolicy{}
accessBt, _ := json.Marshal(service["accessPolicy"])
json.Unmarshal(accessBt, &access)
serviceStore.updateAccess(access)
res, err := cc.createServiceRegistry(stub, service["did"].(string), serviceStore)
if err != nil {
log.Errorf("[%s][%s][createServiceIdentity] Error creating service in registry: %v", CHANNEL_ENV, ServiceGATEWAY, err.Error())
return "", err
}
log.Infof("[%s][%s][createServiceIdentity] Everything went ok", CHANNEL_ENV, ServiceGATEWAY)
return res, nil
}
func (cc *Chaincode) updateServiceAccess(stub shim.ChaincodeStubInterface, did string, args interface{}) (string, error) {
log.Infof("[%s][%s][updateServiceAccess] Entry in updateServiceAccess", CHANNEL_ENV, ServiceGATEWAY)
service := make(map[string]interface{})
service = args.(map[string]interface{})
if service["did"] == nil {
log.Errorf("[%s][%s][updateServiceAccess] Error creating service in registry: %v", CHANNEL_ENV, ServiceGATEWAY, "DID input param is undefined")
return "", errors.New("DID input param is undefined")
}
// m := make(map[string]interface{}) // parse access to interact
access := AccessPolicy{}
accessBt, _ := json.Marshal(service["access"])
json.Unmarshal(accessBt, &access)
// m = service["access"].(AccessPolicy)
result, err := cc.updateRegistryAccess(stub, did, service["did"].(string), access)
if err != nil {
log.Errorf("[%s][%s][updateServiceAccess] Error updating registry access: %v", CHANNEL_ENV, ServiceGATEWAY, err.Error())
log.Errorf("[%s][%s][updateServiceAccess] Return error", CHANNEL_ENV, ServiceGATEWAY)
return "", err
}
log.Infof("[%s][%s][updateServiceAccess] Update registry Ok", CHANNEL_ENV, ServiceGATEWAY)
return result, nil
}
func (cc *Chaincode) updateService(stub shim.ChaincodeStubInterface, did string, args interface{}) (string, error) {
log.Infof("[%s][%s][updateService] Entry in updateService", CHANNEL_ENV, ServiceGATEWAY)
service := make(map[string]interface{})
service = args.(map[string]interface{})
if service["did"] == nil {
log.Errorf("[%s][%s][updateService] Error updating service : %v", CHANNEL_ENV, ServiceGATEWAY, "DID input param is undefined")
return "", errors.New("DID input param is undefined")
}
// m := make(map[string]interface{}) // parse access to interact
channel := ""
name := ""
if service["channel"] != nil {
channel = service["channel"].(string)
}
if service["name"] != nil {
name = service["name"].(string)
}
result, err := cc.updateRegistry(stub, did, service["did"].(string), name, channel)
if err != nil {
log.Errorf("[%s][%s][updateService] Error updating registry access: %v", CHANNEL_ENV, ServiceGATEWAY, err.Error())
log.Errorf("[%s][%s][updateService] Return error", CHANNEL_ENV, ServiceGATEWAY)
return "", err
}
log.Infof("[%s][%s][updateService] Update registry Ok", CHANNEL_ENV, ServiceGATEWAY)
return result, nil
}
func (cc *Chaincode) getServiceIdentity(stub shim.ChaincodeStubInterface, args interface{}) (string, error) {
var err error
service := make(map[string]interface{})
service = args.(map[string]interface{})
if service["did"] == nil {
log.Errorf("[%s][%s][getServiceIdentity] Error creating service in registry: %v", CHANNEL_ENV, ServiceGATEWAY, "DID input param is undefined")
return "", errors.New("DID input param is undefined")
}
result, err := cc.getServiceRegistry(stub, service["did"].(string))
if err != nil {
log.Errorf("[%s][%s][getServiceIdentity] Error getting registry access: %v", CHANNEL_ENV, ServiceGATEWAY, err.Error())
log.Errorf("[%s][%s][getServiceIdentity] Return error", CHANNEL_ENV, ServiceGATEWAY)
return "", err
}
log.Infof("[%s][%s][getServiceIdentity]Service to return Name: %s, Controller: %s, Access: %v", CHANNEL_ENV, ServiceGATEWAY, result.Name, result.Controller, result.Access)
serviceBytes, err := json.Marshal(*result)
return string(serviceBytes), nil
}