-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkyc.go
256 lines (169 loc) · 8.79 KB
/
kyc.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
package main
import (
"errors"
"fmt"
"encoding/json"
"github.com/hyperledger/fabric/core/chaincode/shim"
"strings"
)
var logger = shim.NewLogger("CLDChaincode")
//==============================================================================================================================
// Chaincode - A blank struct for use with Shim (A HyperLedger included go file used for get/put state
// and other HyperLedger functions)
//==============================================================================================================================
type SimpleChaincode struct {
}
//ASSET
type KYCInfo struct {
KYC_Id string `json:"kyc_id"`
// Kyc_Type string `json:"kyc_type"`
// Cust_Id string `json:"cust_id"`
}
//==============================================================================================================================
// User_and_eCert - Struct for storing the JSON of a user and their ecert
//==============================================================================================================================
type Kyc_Holder struct {
KYCs []string `json:"kycs"`
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil { fmt.Printf("Error starting Chaincode: %s", err) }
}
//==============================================================================================================================
// Init Function - Called when the user deploys the chaincode
//==============================================================================================================================
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
//Args
// 0
// peer_address
var kycs Kyc_Holder
bytes, err := json.Marshal(kycs)
if err != nil { return nil, errors.New("Error creating KYC Holder record") }
err = stub.PutState("Deployed successfully", bytes)
return nil, nil
}
//==============================================================================================================================
// Router Functions
//==============================================================================================================================
// Invoke - Called on chaincode invoke. Takes a function name passed and calls that function. Converts some
// initial arguments passed to other things for use in the called function e.g. name -> ecert
//==============================================================================================================================
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
logger.Debug("Inside Invoke")
if function == "create_kyc" {
logger.Debug("Inside Invoke: calling create kyc")
fmt.Printf("calling create_kyc!!!!!!!!!!!!!!!!!!!!!!!!!!");
return t.create_kyc(stub, args[0])
} else if function == "ping" {
return t.ping(stub)
} else {
return nil, errors.New("Function of the name "+ function +" doesn't exist.")
}
}
func (t *SimpleChaincode) ping(stub shim.ChaincodeStubInterface) ([]byte, error) {
return []byte("Hello, world!"), nil
}
//=================================================================================================================================
// Create Function
//=================================================================================================================================
// Create Vehicle - Creates the initial JSON for the kyc and then saves it to the ledger.
//=================================================================================================================================
func (t *SimpleChaincode) create_kyc(stub shim.ChaincodeStubInterface, k string) ([]byte, error) {
var v KYCInfo
logger.Debug("Inside create KYC")
kyc_id := "\"KYC_Id\":\""+k+"\""
//kyc_type := "\"KYC_Id\":\""+k[2]+"\", "
//cust_id :="\"Cust_Id\":\""+k[0]+ "\""
fmt.Printf("Inside create_kyc!!!!!!!!!!!!!!!!!!!!!!!!!!");
kyc_json := "{" +kyc_id+ "}" // Concatenates the variables to create the total JSON object
//logger.Debug("kyc_json: ", kyc_json)
err := json.Unmarshal([]byte(kyc_json), &v) // Convert the JSON defined above into a vehicle object for go
if err != nil { return nil, errors.New("Invalid JSON object") }
record, err := stub.GetState(v.KYC_Id) // If not an error then a record exists so cant create a new car with this V5cID as it must be unique
if record != nil { return nil, errors.New("Vehicle already exists") }
_, err = t.save_changes(stub, v)
if err != nil { fmt.Printf("CREATE_VEHICLE: Error saving changes: %s", err); return nil, errors.New("Error saving changes") }
bytes, err := stub.GetState("kycs")
if err != nil { return nil, errors.New("Unable to get KYCs") }
var kycs Kyc_Holder
err = json.Unmarshal(bytes, &kycs)
// if err != nil { return nil, errors.New("Corrupt KYC record") }
kycs.KYCs = append(kycs.KYCs, kyc_id)
bytes, err = json.Marshal(kycs)
if err != nil { fmt.Print("Error creating Kyc_Holder record") }
err = stub.PutState("kycs", bytes)
if err != nil { return nil, errors.New("Unable to put the state") }
return nil, nil
}
func (t *SimpleChaincode) save_changes(stub shim.ChaincodeStubInterface, v KYCInfo) (bool, error) {
bytes, err := json.Marshal(v)
if err != nil { fmt.Printf("SAVE_CHANGES: Error converting vehicle record: %s", err); return false, errors.New("Error converting vehicle record") }
err = stub.PutState(v.KYC_Id, bytes)
if err != nil { fmt.Printf("SAVE_CHANGES: Error storing vehicle record: %s", err); return false, errors.New("Error storing vehicle record") }
return true, nil
}
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
logger.Debug("function: ", function)
logger.Debug("customer: ", args[0])
if function == "get_kyc_details" {
if len(args) != 1 { fmt.Printf("Incorrect number of arguments passed"); return nil, errors.New("QUERY: Incorrect number of arguments passed") }
//if err != nil { fmt.Printf("QUERY: Error retrieving cust_id: %s", err); return nil, errors.New("QUERY: Error retrieving cust_id "+err.Error()) }
return t.get_kyc_details(stub,args[0] )
//) else if function == "get_ecert" {
//return t.get_ecert(stub, args[0])
} else if function == "ping" {
return t.ping(stub)
}
return nil, errors.New("Received unknown function invocation " + function)
}
func (t *SimpleChaincode) get_kyc_details(stub shim.ChaincodeStubInterface, cust_id string) ([]byte, error) {
bytes, err := stub.GetState("kycs")
if err != nil { return nil, errors.New("Unable to get kycs") }
var kycs Kyc_Holder
err = json.Unmarshal(bytes, &kycs)
result := "["
var temp []byte
var v KYCInfo
for _, id := range kycs.KYCs {
id = "{" +id +"}"
err:=json.Unmarshal([]byte(id),&v)
bytes, err := stub.GetState(v.KYC_Id);
fmt.Printf("%sKYC_ID------" +id);
if err != nil { fmt.Printf("RETRIEVE_V5C: Failed to invoke vehicle_code: %s", err); return bytes, errors.New("RETRIEVE_V5C: Error retrieving vehicle with v5cID = " + id) }
//return bytes,nil
err = json.Unmarshal(bytes, &v);
if err != nil { fmt.Printf("RETRIEVE_V5C: Corrupt vehicle record "+string(bytes)+": %s", err); return bytes, errors.New("RETRIEVE_V5C: Corrupt vehicle record------"+id+string(bytes)) }
//return v, nil
//==========
if err != nil {return nil, errors.New("Failed to retrieve V5C"+id)}
temp, err = t.get_kyc(stub, v,cust_id)
if err == nil {
result += string(temp) + ","
}
}
if len(result) == 1 {
result = "[]"
} else {
result = result[:len(result)-1] + "]"
}
return []byte(result), nil
}
func (t *SimpleChaincode) retrieve_v5c(stub shim.ChaincodeStubInterface, kyc_id string) (KYCInfo, error) {
var v KYCInfo
bytes, err := stub.GetState(kyc_id);
fmt.Printf("KYC_ID------" +kyc_id);
if err != nil { fmt.Printf("RETRIEVE_V5C: Failed to invoke vehicle_code: %s", err); return v, errors.New("RETRIEVE_V5C: Error retrieving vehicle with v5cID = " + kyc_id) }
err = json.Unmarshal(bytes, &v);
if err != nil { fmt.Printf("RETRIEVE_V5C: Corrupt vehicle record "+string(bytes)+": %s", err); return v, errors.New("RETRIEVE_V5C: Corrupt vehicle record"+string(bytes)) }
return v, nil
}
func (t *SimpleChaincode) get_kyc(stub shim.ChaincodeStubInterface, v KYCInfo, cust_id string) ([]byte, error) {
bytes, err := json.Marshal(v)
if err != nil { return nil, errors.New("GET_VEHICLE_DETAILS: Invalid vehicle object") }
//return bytes, nil
if strings.Contains(v.KYC_Id,cust_id) {
return bytes, nil
} else {
return nil, errors.New("Permission Denied. get_vehicle_details")
}
}