示例#1
0
// delete Deletes an entity from the state, returning error if the entity was not found in the state.
func (t *Attributes2State) delete(stub shim.ChaincodeStubInterface, args []string) error {
	if len(args) != 1 {
		return errors.New("Incorrect number of arguments. Expecting only 1 (attributeName)")
	}

	attributeName := args[0]
	fmt.Printf("Deleting attribute %v", attributeName)
	valBytes, err := stub.GetState(attributeName)
	if err != nil {
		return err
	}

	if valBytes == nil {
		return errors.New("Attribute '" + attributeName + "' not found.")
	}

	isOk, err := stub.VerifyAttribute(attributeName, valBytes)
	if err != nil {
		return err
	}
	if isOk {
		// Delete the key from the state in ledger
		err = stub.DelState(attributeName)
		if err != nil {
			return errors.New("Failed to delete state")
		}
	}
	return nil
}
//Invoke makes increment counter
func (t *AuthorizableCounterChaincode) increment(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
	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

}
示例#3
0
// 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.ChaincodeStubInterface, 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))
}