//Invoke Transaction makes increment counter func (t *AuthorizableCounterChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) { if function != "increment" { return nil, errors.New("Invalid invoke function name. Expecting \"increment\"") } val, err := stub.ReadCertAttribute("position") fmt.Printf("Position => %v error %v \n", string(val), err) isOk, _ := stub.VerifyAttribute("position", []byte("Software Engineer")) // Here the ABAC API is called to verify the attribute, just if the value is verified the counter will be incremented. if isOk { counter, err := stub.GetState("counter") if err != nil { return nil, err } var cInt int cInt, err = strconv.Atoi(string(counter)) if err != nil { return nil, err } cInt = cInt + 1 counter = []byte(strconv.Itoa(cInt)) stub.PutState("counter", counter) } return nil, nil }
// isAuthorized checks if the transaction invoker has the appropriate role // stub: chaincodestub // requiredRole: required role; this function will return true if invoker has this role func (t *certHandler) isAuthorized(stub *shim.ChaincodeStub, requiredRole string) (bool, error) { //read transaction invoker's role, and verify that is the same as the required role passed in return stub.VerifyAttribute(role, []byte(requiredRole)) }