// deleteAccountRecord deletes the record row associated with an account ID on the chaincode state table // stub: chaincodestub // accountID: account ID (record matching this account ID will be deleted after calling this method) func (t *depositoryHandler) deleteAccountRecord(stub shim.ChaincodeStubInterface, accountID string) error { myLogger.Debugf("insert accountID= %v", accountID) //delete record matching account ID passed in err := stub.DeleteRow( "AssetsOwnership", []shim.Column{shim.Column{Value: &shim.Column_String_{String_: accountID}}}, ) if err != nil { myLogger.Errorf("system error %v", err) return errors.New("error in deleting account record") } return nil }
func (t *AssetManagementChaincode) transfer(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { myLogger.Debug("Transfer...") if len(args) != 2 { return nil, errors.New("Incorrect number of arguments. Expecting 2") } asset := args[0] newOwner, err := base64.StdEncoding.DecodeString(args[1]) if err != nil { return nil, fmt.Errorf("Failed decoding owner") } // 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 retrieving asset [%s]: [%s]", asset, err) } prvOwner := row.Columns[1].GetBytes() myLogger.Debugf("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.") } myLogger.Debug("New owner of [%s] is [% x]", asset, newOwner) myLogger.Debug("Transfer...done") return nil, nil }
func (t *AssetManagementChaincode) transfer(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { if len(args) != 2 { return nil, errors.New("Incorrect number of arguments. Expecting 2") } asset := args[0] newOwner, err := base64.StdEncoding.DecodeString(args[1]) if err != nil { fmt.Printf("Error decoding [%v] \n", err) return nil, errors.New("Failed decoding owner") } // 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 retrieving asset [%s]: [%s]", asset, err) } prvOwner := row.Columns[1].GetBytes() myLogger.Debugf("Previous owener of [%s] is [% x]", asset, prvOwner) if len(prvOwner) == 0 { return nil, fmt.Errorf("Invalid previous owner. Nil") } // Verify ownership callerAccount, err := stub.ReadCertAttribute("account") if err != nil { return nil, fmt.Errorf("Failed fetching caller account. Error was [%v]", err) } if bytes.Compare(prvOwner, callerAccount) != 0 { return nil, fmt.Errorf("Failed verifying caller ownership.") } newOwnerAccount, err := attr.GetValueFrom("account", newOwner) if err != nil { return nil, fmt.Errorf("Failed fetching new owner account. Error was [%v]", err) } // 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: newOwnerAccount}}, }, }) if err != nil { return nil, errors.New("Failed inserting row.") } return nil, nil }
// Invoke callback representing the invocation of a chaincode // This chaincode will manage two accounts A and B and will transfer X units from A to B upon invoke func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { switch function { case "insertRowTableOne": if len(args) < 3 { return nil, errors.New("insertTableOne failed. Must include 3 column values") } col1Val := args[0] col2Int, err := strconv.ParseInt(args[1], 10, 32) if err != nil { return nil, errors.New("insertTableOne failed. arg[1] must be convertable to int32") } col2Val := int32(col2Int) col3Int, err := strconv.ParseInt(args[2], 10, 32) if err != nil { return nil, errors.New("insertTableOne failed. arg[2] must be convertable to int32") } col3Val := int32(col3Int) 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_Int32{Int32: col3Val}} columns = append(columns, &col1) columns = append(columns, &col2) columns = append(columns, &col3) row := shim.Row{Columns: columns} ok, err := stub.InsertRow("tableOne", row) if err != nil { return nil, fmt.Errorf("insertTableOne operation failed. %s", err) } if !ok { return nil, errors.New("insertTableOne operation failed. Row with given key already exists") } case "insertRowTableTwo": if len(args) < 4 { return nil, errors.New("insertRowTableTwo failed. Must include 4 column values") } col1Val := args[0] col2Int, err := strconv.ParseInt(args[1], 10, 32) if err != nil { return nil, errors.New("insertRowTableTwo failed. arg[1] must be convertable to int32") } col2Val := int32(col2Int) col3Int, err := strconv.ParseInt(args[2], 10, 32) if err != nil { return nil, errors.New("insertRowTableTwo failed. arg[2] must be convertable to int32") } col3Val := int32(col3Int) col4Val := args[3] 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_Int32{Int32: col3Val}} col4 := shim.Column{Value: &shim.Column_String_{String_: col4Val}} columns = append(columns, &col1) columns = append(columns, &col2) columns = append(columns, &col3) columns = append(columns, &col4) row := shim.Row{Columns: columns} ok, err := stub.InsertRow("tableTwo", row) if err != nil { return nil, fmt.Errorf("insertRowTableTwo operation failed. %s", err) } if !ok { return nil, errors.New("insertRowTableTwo operation failed. Row with given key already exists") } case "insertRowTableThree": if len(args) < 7 { return nil, errors.New("insertRowTableThree failed. Must include 7 column values") } col1Val := args[0] col2Int, err := strconv.ParseInt(args[1], 10, 32) if err != nil { return nil, errors.New("insertRowTableThree failed. arg[1] must be convertable to int32") } col2Val := int32(col2Int) col3Val, err := strconv.ParseInt(args[2], 10, 64) if err != nil { return nil, errors.New("insertRowTableThree failed. arg[2] must be convertable to int64") } col4Uint, err := strconv.ParseUint(args[3], 10, 32) if err != nil { return nil, errors.New("insertRowTableThree failed. arg[3] must be convertable to uint32") } col4Val := uint32(col4Uint) col5Val, err := strconv.ParseUint(args[4], 10, 64) if err != nil { return nil, errors.New("insertRowTableThree failed. arg[4] must be convertable to uint64") } col6Val := []byte(args[5]) col7Val, err := strconv.ParseBool(args[6]) if err != nil { return nil, errors.New("insertRowTableThree failed. arg[6] must be convertable to bool") } 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_Int64{Int64: col3Val}} col4 := shim.Column{Value: &shim.Column_Uint32{Uint32: col4Val}} col5 := shim.Column{Value: &shim.Column_Uint64{Uint64: col5Val}} col6 := shim.Column{Value: &shim.Column_Bytes{Bytes: col6Val}} col7 := shim.Column{Value: &shim.Column_Bool{Bool: col7Val}} columns = append(columns, &col1) columns = append(columns, &col2) columns = append(columns, &col3) columns = append(columns, &col4) columns = append(columns, &col5) columns = append(columns, &col6) columns = append(columns, &col7) row := shim.Row{Columns: columns} ok, err := stub.InsertRow("tableThree", row) if err != nil { return nil, fmt.Errorf("insertRowTableThree operation failed. %s", err) } if !ok { return nil, errors.New("insertRowTableThree operation failed. Row with given key already exists") } case "insertRowTableFour": if len(args) < 1 { return nil, errors.New("insertRowTableFour failed. Must include 1 column value1") } col1Val := args[0] var columns []*shim.Column col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}} columns = append(columns, &col1) row := shim.Row{Columns: columns} ok, err := stub.InsertRow("tableFour", row) if err != nil { return nil, fmt.Errorf("insertRowTableFour operation failed. %s", err) } if !ok { return nil, errors.New("insertRowTableFour operation failed. Row with given key already exists") } case "deleteRowTableOne": if len(args) < 1 { return nil, errors.New("deleteRowTableOne 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) err := stub.DeleteRow("tableOne", columns) if err != nil { return nil, fmt.Errorf("deleteRowTableOne operation failed. %s", err) } case "replaceRowTableOne": if len(args) < 3 { return nil, errors.New("replaceRowTableOne failed. Must include 3 column values") } col1Val := args[0] col2Int, err := strconv.ParseInt(args[1], 10, 32) if err != nil { return nil, errors.New("replaceRowTableOne failed. arg[1] must be convertable to int32") } col2Val := int32(col2Int) col3Int, err := strconv.ParseInt(args[2], 10, 32) if err != nil { return nil, errors.New("replaceRowTableOne failed. arg[2] must be convertable to int32") } col3Val := int32(col3Int) 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_Int32{Int32: col3Val}} columns = append(columns, &col1) columns = append(columns, &col2) columns = append(columns, &col3) row := shim.Row{Columns: columns} ok, err := stub.ReplaceRow("tableOne", row) if err != nil { return nil, fmt.Errorf("replaceRowTableOne operation failed. %s", err) } if !ok { return nil, errors.New("replaceRowTableOne operation failed. Row with given key does not exist") } case "deleteAndRecreateTableOne": err := stub.DeleteTable("tableOne") if err != nil { return nil, fmt.Errorf("deleteAndRecreateTableOne operation failed. Error deleting table. %s", err) } err = createTableOne(stub) if err != nil { return nil, fmt.Errorf("deleteAndRecreateTableOne operation failed. Error creating table. %s", err) } return nil, nil default: return nil, errors.New("Unsupported operation") } return nil, nil }