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 pathid.gateway.go
147 lines (112 loc) · 5.26 KB
/
id.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
141
142
143
144
145
146
147
/*
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) getIdentity(stub shim.ChaincodeStubInterface, did string, identity *Identity, args interface{}) (string, error) {
log.Infof("[%s][getIdentity] Get Identity", IDGATEWAY)
var idReturn *Identity
var err error
idReq := make(map[string]interface{})
idReq = args.(map[string]interface{})
if idReq["did"] == nil {
log.Errorf("[%s][%s][getIdentity] Problem getting identity: %v", CHANNEL_ENV, IDGATEWAY, "DID input param is undefined")
return "", errors.New("DID input param is undefined")
}
log.Infof("[%s][%s][getIdentity] Get Identity %v", CHANNEL_ENV, IDGATEWAY, idReq["did"].(string))
if idReq["did"].(string) != did {
idReturn, err = cc.getIDRegistry(stub, idReq["did"].(string))
if err != nil {
log.Errorf("[%s][%s][getIdentity] Problem getting identity: %v", CHANNEL_ENV, IDGATEWAY, err.Error())
return "nil", err
}
} else {
idReturn = identity
}
identityReponse := make(map[string]string)
identityReponse["did"] = idReq["did"].(string)
identityReponse["publicKey"] = idReturn.PublicKey
log.Infof("[%s][%s][getIdentity] Get Identity", CHANNEL_ENV, IDGATEWAY)
idBytes, err := json.Marshal(identityReponse)
return string(idBytes), nil
}
func (cc *Chaincode) createIdentity(stub shim.ChaincodeStubInterface, controller string, args interface{}) (string, error) {
idReq := make(map[string]interface{})
idReq = args.(map[string]interface{})
log.Debugf("[%s][%s][createIdentity] Calling to registry", CHANNEL_ENV, IDGATEWAY)
if idReq["did"] == nil {
log.Errorf("[%s][%s][createIdentity] rror creating Identity: %v", CHANNEL_ENV, IDGATEWAY, "DID input param is undefined")
return "", errors.New("DID input param is undefined")
}
identityStore := Identity{PublicKey: idReq["publicKey"].(string), Controller: controller}
_, err := cc.createIDRegistry(stub, idReq["did"].(string), identityStore)
if err != nil {
log.Errorf("[%s][%s][createIdentity] Error creating Identity: %v", CHANNEL_ENV, IDGATEWAY, err.Error())
return "", err
}
return "", nil
}
func (cc *Chaincode) createSelfIdentity(stub shim.ChaincodeStubInterface, args interface{}) (string, error) {
idReq := make(map[string]interface{})
idReq = args.(map[string]interface{})
log.Debugf("[%s][%s][createSelfIdentity] Calling to registry", CHANNEL_ENV, IDGATEWAY)
identityStore := Identity{PublicKey: idReq["publicKey"].(string)}
if idReq["did"] == nil {
log.Errorf("[%s][%s][createSelfIdentity] Error creating Identity: %v", CHANNEL_ENV, IDGATEWAY, "DID input param is undefined")
return "", errors.New("DID input param is undefined")
}
_, err := cc.createIDRegistry(stub, idReq["did"].(string), identityStore)
if err != nil {
log.Errorf("[%s][%s][createSelfIdentity] Error creating Identity: %v", CHANNEL_ENV, IDGATEWAY, err.Error())
return "", err
}
return "The identity has been stored in DLT successfully", nil
}
func (cc *Chaincode) verifyIdentity(stub shim.ChaincodeStubInterface, did string, identity *Identity, args interface{}) (string, error) {
log.Infof("[%s][%s][verifyIdentity]Verifying identity", CHANNEL_ENV, IDGATEWAY)
idReq := make(map[string]interface{})
idReq = args.(map[string]interface{})
log.Infof("[%s][%s][verifyIdentity] Idenitity has access %v", CHANNEL_ENV, IDGATEWAY, identity.Access)
if identity.Access != 4 {
log.Errorf("[%s][%s][verifyIdentity] Error verification unauthorized, the did provided has not access", CHANNEL_ENV, IDGATEWAY)
return "", errors.New(ERRORVerID)
}
if idReq["did"] == nil {
log.Errorf("[%s][%s][verifyIdentity] Error verifying signature: %v", CHANNEL_ENV, IDGATEWAY, "DID input param is undefined")
return "", errors.New("DID input param is undefined")
}
_, err := cc.updateIDRegistry(stub, idReq["did"].(string), did, 1)
if err != nil {
log.Errorf("[%s][%s][verifyIdentity] Error verifying signature: %v", CHANNEL_ENV, IDGATEWAY, err.Error())
return "", errors.New(ERRORVerSign)
}
log.Infof("[%s][%s][verifyIdentity]Idenitity updated", CHANNEL_ENV, IDGATEWAY)
return "The Identity has been verified", nil
}
func (cc *Chaincode) revokeIdentity(stub shim.ChaincodeStubInterface, did string, identity *Identity, args interface{}) (string, error) {
log.Infof("[%s][%s][revokeIdentity]Verifying identity", CHANNEL_ENV, IDGATEWAY)
var err error
idReq := make(map[string]interface{})
idReq = args.(map[string]interface{})
if identity.Access != 4 {
log.Errorf("[%s][%s][revokeIdentity]Error revocation unauthorized, the did provided has not access", CHANNEL_ENV, IDGATEWAY)
return "", errors.New(ERRORRevID)
}
if idReq["did"] == nil {
log.Errorf("[%s][%s][revokeIdentity] Error revoking signature: %v", CHANNEL_ENV, IDGATEWAY, "DID input param is undefined")
return "", errors.New("DID input param is undefined")
}
_, err = cc.revokeIDRegistry(stub, idReq["did"].(string), did)
if err != nil {
log.Errorf("[%s][%s][revokeIdentity] Error revoking signature: %v", CHANNEL_ENV, IDGATEWAY, err.Error())
return "", errors.New(ERRORRevSign)
}
log.Infof("[%s][%s][revokeIdentity]Idenitity revoked", CHANNEL_ENV, IDGATEWAY)
return "Identity revoked successfully", nil
}