github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/examples/chaincode/go/attributes_to_state/attributes_to_state.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 22 "github.com/hyperledger/fabric/accesscontrol/crypto/attr" 23 "github.com/hyperledger/fabric/accesscontrol/impl" 24 "github.com/hyperledger/fabric/core/chaincode/shim" 25 pb "github.com/hyperledger/fabric/protos/peer" 26 ) 27 28 // Attributes2State demonstrates how to read attributes from TCerts. 29 type Attributes2State struct { 30 } 31 32 func (t *Attributes2State) setStateToAttributes(stub shim.ChaincodeStubInterface, args []string) pb.Response { 33 attrHandler, err := attr.NewAttributesHandlerImpl(stub) 34 if err != nil { 35 return shim.Error(err.Error()) 36 } 37 for _, att := range args { 38 fmt.Println("Writing attribute " + att) 39 attVal, err := attrHandler.GetValue(att) 40 if err != nil { 41 return shim.Error(err.Error()) 42 } 43 err = stub.PutState(att, attVal) 44 if err != nil { 45 return shim.Error(err.Error()) 46 } 47 } 48 return shim.Success(nil) 49 } 50 51 // Init intializes the chaincode by reading the transaction attributes and storing 52 // the attrbute values in the state 53 func (t *Attributes2State) Init(stub shim.ChaincodeStubInterface) pb.Response { 54 _, args := stub.GetFunctionAndParameters() 55 return t.setStateToAttributes(stub, args) 56 } 57 58 func (t *Attributes2State) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 59 function, args := stub.GetFunctionAndParameters() 60 if function == "delete" { 61 return t.delete(stub, args) 62 } else if function == "submit" { 63 return t.setStateToAttributes(stub, args) 64 } else if function == "read" { 65 return t.query(stub, args) 66 } 67 68 return shim.Error("Invalid invoke function name. Expecting either \"delete\" or \"submit\" or \"read\"") 69 } 70 71 // delete Deletes an entity from the state, returning error if the entity was not found in the state. 72 func (t *Attributes2State) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response { 73 if len(args) != 1 { 74 return shim.Error("Incorrect number of arguments. Expecting only 1 (attributeName)") 75 } 76 77 attributeName := args[0] 78 fmt.Printf("Deleting attribute %v", attributeName) 79 valBytes, err := stub.GetState(attributeName) 80 if err != nil { 81 return shim.Error(err.Error()) 82 } 83 84 if valBytes == nil { 85 return shim.Error("Attribute '" + attributeName + "' not found.") 86 } 87 88 isOk, err := impl.NewAccessControlShim(stub).VerifyAttribute(attributeName, valBytes) 89 if err != nil { 90 return shim.Error(err.Error()) 91 } 92 if isOk { 93 // Delete the key from the state in ledger 94 err = stub.DelState(attributeName) 95 if err != nil { 96 return shim.Error("Failed to delete state") 97 } 98 } 99 return shim.Success(nil) 100 } 101 102 func (t *Attributes2State) query(stub shim.ChaincodeStubInterface, args []string) pb.Response { 103 var attributeName string // Name of the attributeName to query. 104 var err error 105 106 if len(args) != 1 { 107 return shim.Error("Incorrect number of arguments. Expecting only 1 (attributeName)") 108 } 109 110 attributeName = args[0] 111 fmt.Printf("Reading attribute %v", attributeName) 112 113 // Get the state from the ledger 114 Avalbytes, err := stub.GetState(attributeName) 115 if err != nil { 116 jsonResp := "{\"Error\":\"Failed to get state for " + attributeName + "\"}" 117 fmt.Printf("Query Response:%s\n", jsonResp) 118 return shim.Error(jsonResp) 119 } 120 121 if Avalbytes == nil { 122 jsonResp := "{\"Error\":\"Nil amount for " + attributeName + "\"}" 123 fmt.Printf("Query Response:%s\n", jsonResp) 124 return shim.Error(jsonResp) 125 } 126 127 jsonResp := "{\"Name\":\"" + attributeName + "\",\"Amount\":\"" + string(Avalbytes) + "\"}" 128 fmt.Printf("Query Response:%s\n", jsonResp) 129 return shim.Success([]byte(jsonResp)) 130 } 131 132 func main() { 133 err := shim.Start(new(Attributes2State)) 134 if err != nil { 135 fmt.Printf("Error running Attributes2State chaincode: %s", err) 136 } 137 }