mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-20 00:25:09 +00:00
Add function to private data hash to sample private data chaincode
Signed-off-by: NIKHIL E GUPTA <negupta@us.ibm.com>
This commit is contained in:
parent
23df2d620a
commit
6c876534bc
1 changed files with 33 additions and 1 deletions
|
|
@ -27,7 +27,10 @@ SPDX-License-Identifier: Apache-2.0
|
|||
// peer chaincode query -C mychannel -n marblesp -c '{"Args":["readMarble","marble1"]}'
|
||||
// peer chaincode query -C mychannel -n marblesp -c '{"Args":["readMarblePrivateDetails","marble1"]}'
|
||||
// peer chaincode query -C mychannel -n marblesp -c '{"Args":["getMarblesByRange","marble1","marble4"]}'
|
||||
//
|
||||
|
||||
// Query a marble's public data hash
|
||||
// peer chaincode query -C mychannel -n marblesp -c '{"Args":["getMarbleHash","collectionMarbles","marble1"]}'
|
||||
|
||||
// Rich Query (Only supported if CouchDB is used as state database):
|
||||
// peer chaincode query -C mychannel -n marblesp -c '{"Args":["queryMarblesByOwner","tom"]}'
|
||||
// peer chaincode query -C mychannel -n marblesp -c '{"Args":["queryMarbles","{\"selector\":{\"owner\":\"tom\"}}"]}'
|
||||
|
|
@ -166,6 +169,9 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
|
|||
case "getMarblesByRange":
|
||||
//get marbles based on range query
|
||||
return t.getMarblesByRange(stub, args)
|
||||
case "getMarbleHash":
|
||||
//verify a marble using the public hash
|
||||
return t.getMarbleHash(stub, args)
|
||||
default:
|
||||
//error
|
||||
fmt.Println("invoke did not find func: " + function)
|
||||
|
|
@ -649,3 +655,29 @@ func getQueryResultForQueryString(stub shim.ChaincodeStubInterface, queryString
|
|||
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
// ===============================================
|
||||
// getMarbleHash - use the public data hash to verify a private marble
|
||||
// Result is the hash on the public ledger of a marble stored a private data collection
|
||||
// ===============================================
|
||||
func (t *SimpleChaincode) getMarbleHash(stub shim.ChaincodeStubInterface, args []string) pb.Response {
|
||||
var name, collection string
|
||||
var err error
|
||||
|
||||
if len(args) != 2 {
|
||||
return shim.Error("Incorrect number of arguments. Expecting the collection and the name of the marble to query")
|
||||
}
|
||||
|
||||
collection = args[0]
|
||||
name = args[1]
|
||||
|
||||
// GetPrivateDataHash can use any collection deployed with the chaincode as input
|
||||
hashAsbytes, err := stub.GetPrivateDataHash(collection, name)
|
||||
if err != nil {
|
||||
return shim.Error("Failed to get public data hash for marble:" + err.Error())
|
||||
} else if hashAsbytes == nil {
|
||||
return shim.Error("Marble does not exist: " + name)
|
||||
}
|
||||
|
||||
return shim.Success(hashAsbytes)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue