-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaincode.go
198 lines (176 loc) · 5.79 KB
/
chaincode.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
package main
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
var (
fileName = "chaincode"
)
// Defining the structure of the chaincode
type TechChaincode struct{
}
// Define structure of a User
type User struct{
username string
aadhar string
DOB string
email string
phone_num string
KYC_flag string
}
// ============ INIT ===============
func (t *TechChaincode) Init(stub shim.ChaincodeStubInterface)pb.Response{
// Whatever variable initialisation you want can be done here //
return shim.Success(nil)
}
// =========== INVOKE ==============
func (t *TechChaincode) Invoke(stub shim.ChaincodeStubInterface)pb.Response{
fmt.Println("Entering Invoke")
// IF-ELSE-IF all the functions
function, args := stub.GetFunctionAndParameters()
if function == "CreateUser" {
return t.CreateUser(stub, args)
}else if function == "ChangeAadhar" {
return t.ChangeAadhar(stub, args)
}else if function == "AproveKYC" {
return t.ApproveKYC(stub, args)
}else if function == "QueryFlag" {
return t.QueryFlag(stub, args)
}else if function == "init" {
return t.Init(stub)
}
fmt.Println("invoke did not find func : " + function) //error
return shim.Error("Received unknown function invocation")
// end of all functions
}
/////////////////////////////////////////////////////////
//////// CREATE A USER /////////////
////////////////////////////////////////////////////////
func (t *TechChaincode) CreateUser(stub shim.ChaincodeStubInterface, args []string)pb.Response{
// username ==== aadhar ==== DOB ==== email ==== phone_num //
if len(args) != 5 {
fmt.Println("username ==== aadhar ==== DOB ==== email ==== phone_num")
return shim.Error("Incorrect number of arguments. Expecting 5")
}
// Assigning values to variables
var username = args[0]
var aadhar = args[1]
var DOB = args[2]
var email = args[3]
var phone_num = args[4]
var KYC_flag = "False"
userAsByte, err := stub.GetState(username)
// ======== Check user with username already exists =========
if err != nil {
return shim.Error("Error encountered: " + err.Error())
}else if userAsByte != nil {
fmt.Println(username + " already exists")
return shim.Error(username + " already exists")
}
// ============= create User with a user name- username ==============//
var User = &User{username: username, aadhar: aadhar, DOB: DOB, email: email, phone_num: phone_num, KYC_flag: KYC_flag}
UserJSONasBytes, err := json.Marshal(User)
if err != nil {
return shim.Error(err.Error())
}
// ============ User is being put to blockchain ============//
err = stub.PutState(username, UserJSONasBytes)
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("User Successfully created")
return shim.Success(nil)
}
///////////////////////////////////////////////////////
////// CHANGE AADHAR /////////////
//////////////////////////////////////////////////////
func (t* TechChaincode) ChangeAadhar(stub shim.ChaincodeStubInterface, args []string)pb.Response{
// username ==== aadhar
if len(args) != 2 {
fmt.Println("Give arguments as: username , aadhar number")
return shim.Error("Incorrect number of arguments")
}
// ======== Assigning values ==========
var username = args[0]
var aadhar = args[1]
// Check if User exists
UserAsBytes, err := stub.GetState(username)
if err != nil {
return shim.Error("Failed to get User:" + err.Error())
}else if UserAsBytes == nil {
return shim.Error("User does not exist")
}
var UserChanged = User{}
err = json.Unmarshal(UserAsBytes, &UserChanged)
if err != nil {
return shim.Error(err.Error())
}
UserChanged.aadhar = aadhar
UserAsBytes, err = json.Marshal(UserChanged)
err = stub.PutState(username, UserAsBytes)
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("Aadhar number Succesfully Updated")
return shim.Success(nil)
}
/////////////////////////////////////////////////////////
//////// APPROVE KYC /////////////
////////////////////////////////////////////////////////
func (t* TechChaincode) ApproveKYC(stub shim.ChaincodeStubInterface, args []string)pb.Response{
// username =====
if len(args) != 1 {
fmt.Println("Give arguments as: username ")
return shim.Error("Incorrect number of arguments")
}
// ======== Assigning values ==========
var username = args[0]
// Check if User exists
UserAsBytes, err := stub.GetState(username)
if err != nil {
return shim.Error("Failed to get User:" + err.Error())
}else if UserAsBytes == nil {
return shim.Error("User does not exist")
}
var UserChanged = User{}
err = json.Unmarshal(UserAsBytes, &UserChanged)
if err != nil {
return shim.Error(err.Error())
}
UserChanged.KYC_flag = "True"
UserAsBytes, err = json.Marshal(UserChanged)
err = stub.PutState(username, UserAsBytes)
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("KYC Succesfully Done")
return shim.Success(nil)
}
/////////////////////////////////////////////////////////
//////// QUERY KYC_FLAG /////////////
////////////////////////////////////////////////////////
func (t* TechChaincode) QueryFlag(stub shim.ChaincodeStubInterface, args []string)pb.Response{
// username====
if len(args) != 1{
fmt.Println("Give User Name as first argument")
return shim.Error("Incorrect number of arguments")
}
var username = args[0]
UserAsBytes, err := stub.GetState(username)
if err != nil {
fmt.Println("Invalid username")
return shim.Error(err.Error())
}
return shim.Success(UserAsBytes)
}
///////////////////////////////////////////////////////
////// THIS IS THE MAIN FUNCTION /////////////
//////////////////////////////////////////////////////
func main() {
err := shim.Start(new(TechChaincode))
if err != nil {
fmt.Printf("Error starting Chaincode: %s", err)
}
}