|
| 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 | + "errors" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/hyperledger/fabric/core/chaincode/shim" |
| 24 | + "github.com/hyperledger/fabric/core/chaincode/shim/crypto/attr" |
| 25 | +) |
| 26 | + |
| 27 | +// Attributes2State demonstrates how to read attributes from TCerts. |
| 28 | +type Attributes2State struct { |
| 29 | +} |
| 30 | + |
| 31 | +func (t *Attributes2State) setStateToAttributes(stub *shim.ChaincodeStub, args []string) error { |
| 32 | + attrHandler, err := attr.NewAttributesHandlerImpl(stub) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + for _, att := range args { |
| 37 | + fmt.Println("Writing attribute " + att) |
| 38 | + attVal, err := attrHandler.GetValue(att) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + err = stub.PutState(att, attVal) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + } |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +// Init intializes the chaincode by reading the transaction attributes and storing |
| 51 | +// the attrbute values in the state |
| 52 | +func (t *Attributes2State) Init(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) { |
| 53 | + err := t.setStateToAttributes(stub, args) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + return nil, nil |
| 58 | +} |
| 59 | + |
| 60 | +// Invoke takes two arguements, a key and value, and stores these in the state |
| 61 | +func (t *Attributes2State) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) { |
| 62 | + if function == "delete" { |
| 63 | + return nil, t.delete(stub, args) |
| 64 | + } |
| 65 | + |
| 66 | + if function != "submit" { |
| 67 | + return nil, errors.New("Invalid invoke function name. Expecting either \"delete\" or \"submit\"") |
| 68 | + } |
| 69 | + err := t.setStateToAttributes(stub, args) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + return nil, nil |
| 74 | +} |
| 75 | + |
| 76 | +// delete Deletes an entity from the state, returning error if the entity was not found in the state. |
| 77 | +func (t *Attributes2State) delete(stub *shim.ChaincodeStub, args []string) error { |
| 78 | + if len(args) != 1 { |
| 79 | + return errors.New("Incorrect number of arguments. Expecting only 1 (attributeName)") |
| 80 | + } |
| 81 | + |
| 82 | + attributeName := args[0] |
| 83 | + fmt.Printf("Deleting attribute %v", attributeName) |
| 84 | + valBytes, err := stub.GetState(attributeName) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + if valBytes == nil { |
| 90 | + return errors.New("Attribute '" + attributeName + "' not found.") |
| 91 | + } |
| 92 | + |
| 93 | + isOk, err := stub.VerifyAttribute(attributeName, valBytes) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + if isOk { |
| 98 | + // Delete the key from the state in ledger |
| 99 | + err = stub.DelState(attributeName) |
| 100 | + if err != nil { |
| 101 | + return errors.New("Failed to delete state") |
| 102 | + } |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +// Query callback representing the query of a chaincode |
| 108 | +func (t *Attributes2State) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) { |
| 109 | + if function != "read" { |
| 110 | + return nil, errors.New("Invalid query function name. Expecting \"read\"") |
| 111 | + } |
| 112 | + var attributeName string // Name of the attributeName to query. |
| 113 | + var err error |
| 114 | + |
| 115 | + if len(args) != 1 { |
| 116 | + return nil, errors.New("Incorrect number of arguments. Expecting only 1 (attributeName)") |
| 117 | + } |
| 118 | + |
| 119 | + attributeName = args[0] |
| 120 | + fmt.Printf("Reading attribute %v", attributeName) |
| 121 | + |
| 122 | + // Get the state from the ledger |
| 123 | + Avalbytes, err := stub.GetState(attributeName) |
| 124 | + if err != nil { |
| 125 | + jsonResp := "{\"Error\":\"Failed to get state for " + attributeName + "\"}" |
| 126 | + fmt.Printf("Query Response:%s\n", jsonResp) |
| 127 | + return nil, errors.New(jsonResp) |
| 128 | + } |
| 129 | + |
| 130 | + if Avalbytes == nil { |
| 131 | + jsonResp := "{\"Error\":\"Nil amount for " + attributeName + "\"}" |
| 132 | + fmt.Printf("Query Response:%s\n", jsonResp) |
| 133 | + return nil, errors.New(jsonResp) |
| 134 | + } |
| 135 | + |
| 136 | + jsonResp := "{\"Name\":\"" + attributeName + "\",\"Amount\":\"" + string(Avalbytes) + "\"}" |
| 137 | + fmt.Printf("Query Response:%s\n", jsonResp) |
| 138 | + return []byte(jsonResp), nil |
| 139 | +} |
| 140 | + |
| 141 | +func main() { |
| 142 | + err := shim.Start(new(Attributes2State)) |
| 143 | + if err != nil { |
| 144 | + fmt.Printf("Error running Attributes2State chaincode: %s", err) |
| 145 | + } |
| 146 | +} |
0 commit comments