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.registry.go
159 lines (133 loc) · 5.73 KB
/
service.registry.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
/*
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) createServiceRegistry(stub shim.ChaincodeStubInterface, did string, service Service) (string, error) {
log.Infof("[%s][%s][createIDRegistry] Create Service for did %s", CHANNEL_ENV, ServiceREGISTRY, did)
bytes, err := stub.GetState(did)
if bytes != nil {
log.Errorf("[%s][%s][createServiceRegistry] The service already exists in registry", CHANNEL_ENV, ServiceREGISTRY)
return "", errors.New(ERRORServiceExists)
}
idBytes, err := json.Marshal(service)
if err != nil {
log.Errorf("[%s][%s][createIDRegistry] Error parsing: %v", CHANNEL_ENV, ServiceREGISTRY, err.Error())
return "", errors.New(ERRORParsingService + err.Error())
}
err = stub.PutState(did, idBytes)
if err != nil {
log.Errorf("[%s][%s][createIDRegistry] Error creating service: %v", CHANNEL_ENV, ServiceREGISTRY, err.Error())
return "", errors.New(ERRORCreatingService + err.Error())
}
log.Infof("[%s][%s][createIDRegistry] Service created", CHANNEL_ENV, ServiceREGISTRY)
return "Service created successfully", nil
}
func (cc *Chaincode) getServiceRegistry(stub shim.ChaincodeStubInterface, didService string) (*Service, error) {
log.Infof("[%s][%s][getIDRegistry] Get Service for did %s", CHANNEL_ENV, ServiceREGISTRY, didService)
idStored := Service{}
idBytes, err := stub.GetState(didService)
if len(idBytes) == 0 {
log.Errorf("[%s][%s][getIDRegistry] The service doesn't exist", CHANNEL_ENV, ServiceREGISTRY)
return nil, errors.New(ERRORServiceNotExists)
}
if err != nil {
log.Errorf("[%s][%s][getIDRegistry] Error getting service: %v", CHANNEL_ENV, ServiceREGISTRY, err.Error())
return nil, errors.New(ERRORGetService + err.Error())
}
err = json.Unmarshal(idBytes, &idStored)
log.Infof("[%s][%s][getIDRegistry] The service info is %v", CHANNEL_ENV, ServiceREGISTRY, idStored)
return &idStored, nil
}
// func (cc *Chaincode) getServiceRegistryNameAndAcess(stub shim.ChaincodeStubInterface, did string, didService string) (string, int, error) {
// log.Infof("[%s][%s][getServiceRegistryAcess] Get Service for did %s", ServiceREGISTRY, did)
// service, err := cc.getServiceRegistry(stub, didService)
// if err != nil {
// log.Errorf("[%s][%s][getServiceRegistryAcess] Error getting service: %v", ServiceREGISTRY, err.Error())
// return "", 0, errors.New("Error getting service" + err.Error())
// }
// access := service.Access[did]
// return service.Name, access, nil
// }
func (cc *Chaincode) updateRegistryAccess(stub shim.ChaincodeStubInterface, didController string, didService string, accessPolicy AccessPolicy) (string, error) {
log.Infof("[%s][%s][updateRegistryAccess] Get Service for did %s", CHANNEL_ENV, ServiceREGISTRY, didService)
service, err := cc.getServiceRegistry(stub, didService)
if err != nil {
log.Errorf("[%s][%s][updateRegistryAccess] Error getting service: %v", CHANNEL_ENV, ServiceREGISTRY, err.Error())
return "", errors.New(ERRORGetService + err.Error())
}
if didController != service.Controller {
log.Errorf("[%s][%s][updateRegistryAccess] User has not access to update Registry", CHANNEL_ENV, ServiceREGISTRY)
return "", errors.New(ERRORUserAccess)
}
// service.Access[didAccess] = accessType
service.updateAccess(accessPolicy)
idBytes, err := json.Marshal(service)
if err != nil {
log.Errorf("[%s][%s][updateRegistryAccess] Error parsing: %v", CHANNEL_ENV, ServiceREGISTRY, err.Error())
return "", errors.New(ERRORParsingData + err.Error())
}
err = stub.PutState(didService, idBytes)
if err != nil {
log.Errorf("[%s][%s][updateRegistryAccess] Error updating service: %v", CHANNEL_ENV, ServiceREGISTRY, err.Error())
return "", errors.New(ERRORUpdService + err.Error())
}
return "Registry updated sucessfully", nil
}
func (cc *Chaincode) updateRegistry(stub shim.ChaincodeStubInterface, didController string, didService string, name string, channel string) (string, error) {
log.Infof("[%s][%s][updateRegistry] Get Service for did %s", CHANNEL_ENV, ServiceREGISTRY, didService)
service, err := cc.getServiceRegistry(stub, didService)
if err != nil {
log.Errorf("[%s][%s][updateRegistry] Error getting service: %v", CHANNEL_ENV, ServiceREGISTRY, err.Error())
return "", errors.New(ERRORGetService + err.Error())
}
if didController != service.Controller {
log.Errorf("[%s][%s][updateRegistry] User has not access to update Registry", CHANNEL_ENV, ServiceREGISTRY)
return "", errors.New(ERRORUserAccess)
}
if name != "" {
service.Name = name
}
if channel != "" {
service.Channel = channel
}
idBytes, err := json.Marshal(service)
if err != nil {
log.Errorf("[%s][%s][updateRegistryAccess] Error parsing: %v", CHANNEL_ENV, ServiceREGISTRY, err.Error())
return "", errors.New(ERRORParsingData + err.Error())
}
err = stub.PutState(didService, idBytes)
if err != nil {
log.Errorf("[%s][%s][updateRegistryAccess] Error updating service: %v", CHANNEL_ENV, ServiceREGISTRY, err.Error())
return "", errors.New(ERRORUpdService + err.Error())
}
return "Registry updated sucessfully", nil
}
func (s *Service) updateAccess(newAccess AccessPolicy) {
if newAccess.Policy == "" {
s.Access = AccessPolicy{Policy: PublicPolicy}
return
}
//TODO: Update a bit smarter.
// If
switch newAccess.Policy {
case SameControllerPolicy:
s.Access = newAccess
case FineGrainedPolicy:
// Update threshold if present
s.Access.Threshold = newAccess.Threshold
s.Access.Policy = newAccess.Policy
// Update in service the keys that are present.
for k, v := range newAccess.Registry {
s.Access.Registry[k] = v
}
default:
s.Access = AccessPolicy{Policy: PublicPolicy}
}
}