// Query callback representing the query of a chaincode func (t *AssetManagementChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) { if function != "query" { return nil, errors.New("Invalid query function name. Expecting \"query\"") } var err error if len(args) != 1 { return nil, errors.New("Incorrect number of arguments. Expecting name of an asset to query") } // Who is the owner of the asset? asset := args[0] var columns []shim.Column col1 := shim.Column{Value: &shim.Column_String_{String_: asset}} columns = append(columns, col1) row, err := stub.GetRow("AssetsOwnership", columns) if err != nil { jsonResp := "{\"Error\":\"Failed retrieveing asset " + asset + ". Error " + err.Error() + ". \"}" return nil, errors.New(jsonResp) } jsonResp := "{\"Owner\":\"" + string(row.Columns[1].GetBytes()) + "\"}" fmt.Printf("Query Response:%s\n", jsonResp) return row.Columns[1].GetBytes(), nil }
func (t *AssetManagementChaincode) transfer(stub *shim.ChaincodeStub, args []string) ([]byte, error) { if len(args) != 2 { return nil, errors.New("Incorrect number of arguments. Expecting 2") } asset := args[0] newOwner := []byte(args[1]) // Verify the identity of the caller // Only the owner can transfer one of his assets var columns []shim.Column col1 := shim.Column{Value: &shim.Column_String_{String_: asset}} columns = append(columns, col1) row, err := stub.GetRow("AssetsOwnership", columns) if err != nil { return nil, fmt.Errorf("Failed retrieveing asset [%s]: [%s]", asset, err) } prvOwner := row.Columns[1].GetBytes() myLogger.Debug("Previous owener of [%s] is [% x]", asset, prvOwner) if len(prvOwner) == 0 { return nil, fmt.Errorf("Invalid previous owner. Nil") } // Verify ownership ok, err := t.isCaller(stub, prvOwner) if err != nil { return nil, errors.New("Failed checking asset owner identity") } if !ok { return nil, errors.New("The caller is not the owner of the asset") } // At this point, the proof of ownership is valid, then register transfer err = stub.DeleteRow( "AssetsOwnership", []shim.Column{shim.Column{Value: &shim.Column_String_{String_: asset}}}, ) if err != nil { return nil, errors.New("Failed deliting row.") } _, err = stub.InsertRow( "AssetsOwnership", shim.Row{ Columns: []*shim.Column{ &shim.Column{Value: &shim.Column_String_{String_: asset}}, &shim.Column{Value: &shim.Column_Bytes{Bytes: newOwner}}, }, }) if err != nil { return nil, errors.New("Failed inserting row.") } return nil, nil }
// Query callback representing the query of a chaincode func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) { switch function { case "getRowTableOne": if len(args) < 1 { return nil, errors.New("getRowTableOne failed. Must include 1 key value") } col1Val := args[0] var columns []shim.Column col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}} columns = append(columns, col1) row, err := stub.GetRow("tableOne", columns) if err != nil { return nil, fmt.Errorf("getRowTableOne operation failed. %s", err) } rowString := fmt.Sprintf("%s", row) return []byte(rowString), nil case "getRowTableTwo": if len(args) < 3 { return nil, errors.New("getRowTableTwo failed. Must include 3 key values") } col1Val := args[0] col2Int, err := strconv.ParseInt(args[1], 10, 32) if err != nil { return nil, errors.New("getRowTableTwo failed. arg[1] must be convertable to int32") } col2Val := int32(col2Int) col3Val := args[2] var columns []shim.Column col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}} col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}} col3 := shim.Column{Value: &shim.Column_String_{String_: col3Val}} columns = append(columns, col1) columns = append(columns, col2) columns = append(columns, col3) row, err := stub.GetRow("tableTwo", columns) if err != nil { return nil, fmt.Errorf("getRowTableTwo operation failed. %s", err) } rowString := fmt.Sprintf("%s", row) return []byte(rowString), nil case "getRowTableThree": if len(args) < 1 { return nil, errors.New("getRowTableThree failed. Must include 1 key value") } col1Val := args[0] var columns []shim.Column col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}} columns = append(columns, col1) row, err := stub.GetRow("tableThree", columns) if err != nil { return nil, fmt.Errorf("getRowTableThree operation failed. %s", err) } rowString := fmt.Sprintf("%s", row) return []byte(rowString), nil case "getRowsTableTwo": if len(args) < 1 { return nil, errors.New("getRowsTableTwo failed. Must include at least key values") } var columns []shim.Column col1Val := args[0] col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}} columns = append(columns, col1) if len(args) > 1 { col2Int, err := strconv.ParseInt(args[1], 10, 32) if err != nil { return nil, errors.New("getRowsTableTwo failed. arg[1] must be convertable to int32") } col2Val := int32(col2Int) col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}} columns = append(columns, col2) } rowChannel, err := stub.GetRows("tableTwo", columns) if err != nil { return nil, fmt.Errorf("getRowsTableTwo operation failed. %s", err) } var rows []shim.Row for { select { case row, ok := <-rowChannel: if !ok { rowChannel = nil } else { rows = append(rows, row) } } if rowChannel == nil { break } } jsonRows, err := json.Marshal(rows) if err != nil { return nil, fmt.Errorf("getRowsTableTwo operation failed. Error marshaling JSON: %s", err) } return jsonRows, nil default: return nil, errors.New("Unsupported operation") } }