/
sacc.go
321 lines (295 loc) · 11.3 KB
/
sacc.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
var recType = []string{"USER"}
///////////////////////////////////////////////////////////////////
// User object to store the personal information of user
// Note: Fields Should start with Upper case to reflect in payload.
///////////////////////////////////////////////////////////////////
type Users struct {
UserId string
AssetType string //Type = USER
EmailId string
Password string
FirstName string
LastName string
Mobile string
}
type SimpleAsset struct {
}
////////////////////////////////////////////////////////////
// Chain Code kick-off main function
////////////////////////////////////////////////////////////
func main() {
err := shim.Start(new(SimpleAsset))
if err != nil {
fmt.Println("Error Starting Item Asset Handler chaincode: %s", err)
}
}
///////////////////////////////////////////////////////////////////////////
// Init is called during chaincode instantiation to initialize any data.
///////////////////////////////////////////////////////////////////////////
func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("Init() Initialization Complete ")
return shim.Success(nil)
}
/////////////////////////////////////////////////////////////
//Invoke is called per transaction on the chaincode
/////////////////////////////////////////////////////////////
func (t *SimpleAsset) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
fmt.Println("Invoke() : function is : ", function)
fmt.Println("Invoke() : args is : ", args)
if function[0:1] == "i" {
return t.Insert(stub, function, args)
}
if function[0:1] == "q" {
return t.Query(stub, function, args)
}
if function[0:1] == "d" {
return t.Delete(stub, function, args)
}
return shim.Error("Invoke() : Invalid function name - function names begin with either i or q or d")
}
///////////////////////////////////////////////////////////////////////
// Executed when transaction is of type insert or update
///////////////////////////////////////////////////////////////////////
func (t *SimpleAsset) Insert(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response {
if CheckRecType(args[1]) == true {
InsertRequest := InsertFunction(function)
if InsertRequest != nil {
response := InsertRequest(stub, function, args)
return response
}
} else {
errorStr := "Insert() : Invalid recType : " + args[1]
fmt.Println(errorStr)
return shim.Error(errorStr)
}
return shim.Success(nil)
}
///////////////////////////////////////////////////////////////////////
// Executed when transaction is of type Delete
///////////////////////////////////////////////////////////////////////
func (t *SimpleAsset) Delete(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response {
if CheckRecType(args[0]) == true {
DeleteRequest := DeleteFunction(function)
if DeleteRequest != nil {
response := DeleteRequest(stub, function, args)
return response
}
} else {
errorStr := "Delete() : Invalid recType : " + args[0]
fmt.Println(errorStr)
return shim.Error(errorStr)
}
return shim.Success(nil)
}
////////////////////////////////////////////////////////////////////////////
// Executed when transaction is of type query
////////////////////////////////////////////////////////////////////////////
func (t *SimpleAsset) Query(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response {
var response pb.Response
fmt.Println("Query() : Rec Type to be extracted : ", args[0])
if len(args) < 1 {
fmt.Println("Query() : Include at least 1 argument key")
return shim.Error("Query() : Excepting Rec Type and key value for query")
}
QueryRequest := QueryFunction(function)
if QueryRequest != nil {
response = QueryRequest(stub, function, args)
} else {
errorStr := "Query() : Invalid function call : " + function
fmt.Println(errorStr)
return shim.Error(errorStr)
}
if response.Status != shim.OK {
errorStr := "Query() : Object not found"
fmt.Println(errorStr)
return shim.Error(errorStr)
}
return response
}
//////////////////////////////////////////////////////////////////////////////////////
// Higher order function which returns appropriate insert function to use
//////////////////////////////////////////////////////////////////////////////////////
func InsertFunction(fname string) func(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response {
InsertFunc := map[string]func(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response{
"iPostUsers": PostUsers,
}
return InsertFunc[fname]
}
//////////////////////////////////////////////////////////////////////////////////////
// Higher order function which returns appropriate query function to use
//////////////////////////////////////////////////////////////////////////////////////
func QueryFunction(fname string) func(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response {
QueryFunc := map[string]func(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response{
"qGetUsers": GetUsers,
}
return QueryFunc[fname]
}
//////////////////////////////////////////////////////////////////////////////////////
// Higher order function which returns appropriate query function to use
//////////////////////////////////////////////////////////////////////////////////////
func DeleteFunction(fname string) func(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response {
DeleteFunc := map[string]func(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response{
"dDelUsers": DelUsers,
}
return DeleteFunc[fname]
}
////////////////////////////////////////////////////////////////
// Update Object - Create or Replace the object with given data
////////////////////////////////////////////////////////////////
func UpdateObject(stub shim.ChaincodeStubInterface, objectType string, keys []string, objectData []byte) error {
compositeKey, _ := stub.CreateCompositeKey(objectType, keys)
fmt.Println("compositeKey is %s", compositeKey)
fmt.Println("Object Type is %s", objectType)
fmt.Println("keys is ", keys)
// Add Object JSON to world state
err := stub.PutState(compositeKey, objectData)
if err != nil {
fmt.Println("UpdateObject() : Error inserting object into world state database %s", err)
return err
}
return nil
}
///////////////////////////////////////////////////////
// Puts User Object as an Asset in blockchain
///////////////////////////////////////////////////////
func PostUsers(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response {
fmt.Println("PostUsers() Args are ", args)
record, err := CreateUsers(args)
if err != nil {
return shim.Error(err.Error())
}
buff, err := UsersToJSON(record)
if err != nil {
errorStr := "PostUsers() : Failed to create Users object for : " + args[0]
fmt.Println(errorStr)
return shim.Error(errorStr)
}
keys := []string{args[0]}
err = UpdateObject(stub, "USER", keys, buff)
if err != nil {
fmt.Println("PostUsers() : write error while inserting record")
return shim.Error("PostUsers() : write error while inserting record : Error - " + err.Error())
}
return shim.Success(buff)
}
////////////////////////////////////////////////////////
// Creates Users Object from arguments
////////////////////////////////////////////////////////
func CreateUsers(args []string) (Users, error) {
var err error
var aUser Users
fmt.Println("Inside CreateUsers, Args are: ", args)
_, err = strconv.Atoi(args[0])
if err != nil {
return aUser, errors.New("CreateUsers(): User ID should be an integer")
}
aUser = Users{args[0], args[1], args[2], args[3], args[4], args[5], args[6]}
fmt.Println("CreateUsers(): Users : ", aUser)
return aUser, nil
}
//////////////////////////////////////////////////////////
// Converts an User Object to a JSON String
//////////////////////////////////////////////////////////
func UsersToJSON(user Users) ([]byte, error) {
ajson, err := json.Marshal(user)
if err != nil {
fmt.Println("UsersToJSON error: ", err)
return nil, err
}
fmt.Println("UsersToJSON created: ", ajson)
return ajson, nil
}
//////////////////////////////////////////////////////////
// Converts JSON String to Users Object
//////////////////////////////////////////////////////////
func JSONToUsers(user []byte) (Users, error) {
ur := Users{}
err := json.Unmarshal(user, &ur)
if err != nil {
fmt.Println("JSONToUsers error: ", err)
return ur, err
}
fmt.Println("JSONToUsers created: ", ur)
return ur, err
}
/////////////////////////////////////////////////////////////////
// Query Object by type and key
/////////////////////////////////////////////////////////////////
func QueryObject(stub shim.ChaincodeStubInterface, objectType string, keys []string) ([]byte, error) {
compoundKey, _ := stub.CreateCompositeKey(objectType, keys)
fmt.Println("QueryObject() : Compound Key : ", compoundKey)
bytes, err := stub.GetState(compoundKey)
if err != nil {
return nil, err
}
return bytes, nil
}
//////////////////////////////////////////////////////////////////
// Retrieve User Object
//////////////////////////////////////////////////////////////////
func GetUsers(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response {
bytes, err := QueryObject(stub, args[0], []string{args[1]})
if err != nil {
errorStr := "GetUsers() : Failed to query user object for id : " + args[1]
fmt.Println(errorStr)
return shim.Error(errorStr)
}
if err != nil {
errorStr := "GetUsers() : Failed to query complete user object for id : " + args[1]
fmt.Println(errorStr)
return shim.Error(errorStr)
}
fmt.Println("GetUsers() : Response : Successful")
return shim.Success(bytes)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Retrieve the object based on the key and simply delete it
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
func DeleteObject(stub shim.ChaincodeStubInterface, objectType string, keys []string) error {
// Convert keys to compound key
compositeKey, _ := stub.CreateCompositeKey(objectType, keys)
// Remove object from the State Database
err := stub.DelState(compositeKey)
if err != nil {
fmt.Println("DeleteObject() : Error deleting Object into State Database %s", err)
return err
}
fmt.Println("DeleteObject() : ", "Object : ", objectType, " Key : ", compositeKey)
return nil
}
//////////////////////////////////////////////////////////////////
// Retrieve User Object
//////////////////////////////////////////////////////////////////
func DelUsers(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response {
err := DeleteObject(stub, args[0], []string{args[1]})
if err != nil {
errorStr := "DelUsers() : Failed to Delete user object for id : " + args[1]
fmt.Println(errorStr)
return shim.Error(errorStr)
}
fmt.Println("GetUsers() : Response : Successful")
return shim.Success(nil)
}
/////////////////////////////////////////////////////
// Checks whether record type is valid
/////////////////////////////////////////////////////
func CheckRecType(rt string) bool {
for _, val := range recType {
fmt.Println("Val is : ", val)
if val == rt {
return true
}
}
return false
}