-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcvChain.go
426 lines (333 loc) · 11.4 KB
/
cvChain.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// +build !experimental
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"encoding/json"
"fmt"
//"strconv"
//"time"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/core/chaincode/shim/ext/entities"
"github.com/hyperledger/fabric/bccsp/factory"
//"github.com/pkg/errors"
"regexp"
)
const DECKEY = "DECKEY"
const VERKEY = "VERKEY"
const ENCKEY = "ENCKEY"
const SIGKEY = "SIGKEY"
const IV = "IV"
// EncCC example simple Chaincode implementation of a chaincode that uses encryption/signatures
type EncCC struct {
bccspInst bccsp.BCCSP
}
type Input struct {
YearValue string
SchoolOrCompanyValue string
IdentityValue string
}
func getStateAndDecrypt(stub shim.ChaincodeStubInterface, ent entities.Encrypter, key string,year string) ([]byte, error) {
// at first we retrieve the ciphertext from the ledger
ciphertext, err := stub.GetHistoryForKey(key)
if err != nil {
return nil, err
}
defer ciphertext.Close()
var keys []string
for ciphertext.HasNext() {
response, iterErr := ciphertext.Next()
if iterErr != nil {
return nil,err
}
var returnObject Input
//var decObject Input
fmt.Println("undecValue:"+string(response.Value))
decObject,_ := ent.Decrypt(response.Value)
json.Unmarshal(decObject,&returnObject)
fmt.Println(returnObject.YearValue)
if(returnObject.YearValue == year){
fmt.Println("query:"+returnObject.SchoolOrCompanyValue)
returnJson,_ := json.Marshal(returnObject.SchoolOrCompanyValue)
//returnJson := returnObject.SchoolOrCompanyValue
return returnJson,err
}
keys = append(keys, string(response.Value))
}
for key, txID := range keys {
fmt.Printf("key %d contains %s\n", key, txID)
}
return nil,err
}
// encryptAndPutState encrypts the supplied value using the
// supplied entity and puts it to the ledger associated to
// the supplied KVS key
func encryptAndPutState(stub shim.ChaincodeStubInterface, ent entities.Encrypter, key string, value []byte) error {
// at first we use the supplied entity to encrypt the value
ciphertext, err := ent.Encrypt(value)
if err != nil {
return err
}
return stub.PutState(key, ciphertext)
}
// getStateDecryptAndVerify retrieves the value associated to key,
// decrypts it with the supplied entity, verifies the signature
// over it and returns the result of the decryption in case of
// success
func getStateDecryptAndVerify(stub shim.ChaincodeStubInterface, ent entities.EncrypterSignerEntity, key string) ([]byte, error) {
// here we retrieve and decrypt the state associated to key
val, err := getStateAndDecrypt(stub, ent, key,"")
if err != nil {
return nil, err
}
// we unmarshal a SignedMessage from the decrypted state
msg := &entities.SignedMessage{}
err = msg.FromBytes(val)
if err != nil {
return nil, err
}
// we verify the signature
ok, err := msg.Verify(ent)
if err != nil {
return nil, err
} else if !ok {
return nil, nil
}
return msg.Payload, nil
}
// signEncryptAndPutState signs the supplied value, encrypts
// the supplied value together with its signature using the
// supplied entity and puts it to the ledger associated to
// the supplied KVS key
func signEncryptAndPutState(stub shim.ChaincodeStubInterface, ent entities.EncrypterSignerEntity, key string, value []byte) error {
// here we create a SignedMessage, set its payload
// to value and the ID of the entity and
// sign it with the entity
msg := &entities.SignedMessage{Payload: value, ID: []byte(ent.ID())}
err := msg.Sign(ent)
if err != nil {
return err
}
// here we serialize the SignedMessage
b, err := msg.ToBytes()
if err != nil {
return err
}
// here we encrypt the serialized version associated to args[0]
return encryptAndPutState(stub, ent, key, b)
}
type keyValuePair struct {
Key string `json:"key"`
Value string `json:"value"`
}
// getStateByRangeAndDecrypt retrieves a range of KVS pairs from the
// ledger and decrypts each value with the supplied entity; it returns
// a json-marshalled slice of keyValuePair
func getStateByRangeAndDecrypt(stub shim.ChaincodeStubInterface, ent entities.Encrypter, startKey, endKey string) ([]byte, error) {
// we call get state by range to go through the entire range
iterator, err := stub.GetStateByRange(startKey, endKey)
if err != nil {
return nil, err
}
defer iterator.Close()
// we decrypt each entry - the assumption is that they have all been encrypted with the same key
keyvalueset := []keyValuePair{}
for iterator.HasNext() {
el, err := iterator.Next()
if err != nil {
return nil, err
}
v, err := ent.Decrypt(el.Value)
if err != nil {
return nil, err
}
keyvalueset = append(keyvalueset, keyValuePair{el.Key, string(v)})
}
bytes, err := json.Marshal(keyvalueset)
if err != nil {
return nil, err
}
return bytes, nil
}
// Encrypter exposes how to write state to the ledger after having
// encrypted it with an AES 256 bit key that has been provided to the chaincode through the
// transient field
func (t *EncCC) Encrypter(stub shim.ChaincodeStubInterface, args []string, encKey, IV []byte) []byte {
// create the encrypter entity - we give it an ID, the bccsp instance, the key and (optionally) the IV
ent, err := entities.NewAES256EncrypterEntity("ID", t.bccspInst, encKey, IV)
if err != nil {
return []byte("entities.NewAES256EncrypterEntity failed, err %s")
}
key := args[0]
encryptGroup := Input{
YearValue : args[1] ,
SchoolOrCompanyValue : args[2] ,
IdentityValue : args[3],
}
cleartextValue,_ := json.Marshal(encryptGroup)
// here, we encrypt cleartextValue and assign it to key
err = encryptAndPutState(stub, ent, key, cleartextValue)
if err != nil {
return nil
}
return nil
}
// Decrypter exposes how to read from the ledger and decrypt using an AES 256
// bit key that has been provided to the chaincode through the transient field.
func (t *EncCC) Decrypter(stub shim.ChaincodeStubInterface, args []string, decKey, IV []byte) []byte{
// create the encrypter entity - we give it an ID, the bccsp instance, the key and (optionally) the IV
ent, err := entities.NewAES256EncrypterEntity("ID", t.bccspInst, decKey, IV)
if err != nil {
return []byte("entities.NewAES256EncrypterEntity failed, err %s")
}
key := args[0]
year := args[1]
// here we decrypt the state associated to key
cleartextValue, err := getStateAndDecrypt(stub, ent, key,year)
if err != nil {
return []byte("getStateAndDecrypt failed, err %+v")
}
fmt.Println("cleartextValue:"+string(cleartextValue))
reg := regexp.MustCompile("\"")
temp := []byte("")
//返回str中第一个匹配reg的字符串
data := reg.ReplaceAll(cleartextValue,temp)
// here we return the decrypted value as a result
return data
}
// EncrypterSigner exposes how to write state to the ledger after having received keys for
// encrypting (AES 256 bit key) and signing (X9.62/SECG curve over a 256 bit prime field) that has been provided to the chaincode through the
// transient field
// This chaincode implements a simple map that is stored in the state.
// The following operations are available.
// Invoke operations
// put - requires two arguments, a key and value
// remove - requires a key
// get - requires one argument, a key, and returns a value
// keys - requires no arguments, returns all keys
// SimpleChaincode example simple Chaincode implementation
// EncCC example simple Chaincode implementation of a chaincode that uses encryption/signatures
// Init does nothing for this cc
func (t *EncCC) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}
// Invoke has two functions
// put - takes two arguments, a key and value, and stores them in the state
// remove - takes one argument, a key, and removes if from the state
func (t *EncCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
tMap, _ := stub.GetTransient()
switch function {
case "addRecord":
fmt.Println("addRecord")
if len(args) < 4 {
return shim.Error("put operation must include four arguments")
}
//定义结构体变量
//var inputOject input
//获取参数
//inputOject.IDKey = args[0]
/*inputOject.yearValue = args[1]
inputOject.schoolOrCompanyValue = args[2]
inputOject.identityValue = args[3]
*/
group := Input {
//IDKey : args[0] ,
YearValue : args[1] ,
SchoolOrCompanyValue : args[2] ,
IdentityValue : args[3],
}
//输出结果
fmt.Println("arg0"+args[0])
fmt.Println("arg1"+args[1])
fmt.Println("arg2"+args[2])
fmt.Println("arg3"+args[3])
//序列化
Value,err := json.Marshal(group)
fmt.Println(string(Value))
fmt.Println(Value)
if err = stub.PutState(args[0], Value); err != nil {
fmt.Printf("Error putting state %s", err)
return shim.Error("put operation failed. Error updating state: %s")
}
indexName := args[0]
compositeKey, err := stub.CreateCompositeKey(indexName, []string{args[1]})
fmt.Println(compositeKey)
if err != nil {
return shim.Error(err.Error())
}
valueByte := Value
if err := stub.PutState(compositeKey, valueByte); err != nil {
fmt.Printf("Error putting state with compositeKey %s", err)
return shim.Error("put operation failed. Error updating state with compositeKey: %s")
}
return shim.Success(nil)
case "getRecord":
key := args[0]
keysIter, err := stub.GetHistoryForKey(key)
if err != nil {
return shim.Error("query operation failed. Error accessing state: %s")
}
defer keysIter.Close()
var keys []string
for keysIter.HasNext() {
response, iterErr := keysIter.Next()
if iterErr != nil {
return shim.Error("query operation failed. Error accessing state: %s")
}
var returnObject Input
json.Unmarshal(response.Value,&returnObject)
fmt.Println(returnObject.YearValue)
if(returnObject.YearValue == args[1]){
fmt.Println("query:"+returnObject.SchoolOrCompanyValue)
returnJson,_ := json.Marshal(returnObject.SchoolOrCompanyValue)
reg := regexp.MustCompile("\"")
temp := []byte("")
//返回str中第一个匹配reg的字符串
data2 := reg.ReplaceAll(returnJson,temp)
return shim.Success(data2)
}
keys = append(keys, string(response.Value))
}
for key, txID := range keys {
fmt.Printf("key %d contains %s\n", key, txID)
}
//jsonKeys, err := json.Marshal(keys)
if err != nil {
return shim.Error("query operation failed. Error marshaling JSON: %s")
}
return shim.Error("")
case "encRecord":
// make sure there's a key in transient - the assumption is that
// it's associated to the string "ENCKEY"
if _, in := tMap[ENCKEY]; !in {
return shim.Error("Expected transient encryption key %s")
}
return shim.Success(t.Encrypter(stub, args[0:], tMap[ENCKEY], tMap[IV]))
case "decRecord":
// make sure there's a key in transient - the assumption is that
// it's associated to the string "DECKEY"
if _, in := tMap[DECKEY]; !in {
return shim.Error("Expected transient decryption key %s")
}
returnByte := t.Decrypter(stub, args[0:], tMap[DECKEY], tMap[IV])
reg := regexp.MustCompile("\"")
temp := []byte("")
//返回str中第一个匹配reg的字符串
data3 := reg.ReplaceAll(returnByte,temp)
return shim.Success(data3)
default:
return shim.Error("Unsupported operation")
}
}
func main() {
factory.InitFactories(nil)
err := shim.Start(&EncCC{factory.GetDefault()})
if err != nil {
fmt.Printf("Error starting chaincode: %s", err)
}
}