diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..ca6584298 Binary files /dev/null and b/.DS_Store differ diff --git a/finished/chaincode_finished b/finished/chaincode_finished new file mode 100755 index 000000000..fb32c440f Binary files /dev/null and b/finished/chaincode_finished differ diff --git a/finished/chaincode_finished.go b/finished/chaincode_finished.go index e9615d43a..e362fb96e 100644 --- a/finished/chaincode_finished.go +++ b/finished/chaincode_finished.go @@ -19,7 +19,6 @@ package main import ( "errors" "fmt" - "github.com/hyperledger/fabric/core/chaincode/shim" ) @@ -32,6 +31,7 @@ func main() { if err != nil { fmt.Printf("Error starting Simple chaincode: %s", err) } + } // Init resets all the things @@ -57,7 +57,7 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function stri return t.Init(stub, "init", args) } else if function == "write" { return t.write(stub, args) - } + } fmt.Println("invoke did not find func: " + function) return nil, errors.New("Received unknown function invocation: " + function) @@ -70,31 +70,63 @@ func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function strin // Handle different functions if function == "read" { //read a variable return t.read(stub, args) + } else if function == "verifyUser" { + return t.verifyUser(stub, args) } fmt.Println("query did not find func: " + function) return nil, errors.New("Received unknown function query: " + function) } -// write - invoke function to write key/value pair -func (t *SimpleChaincode) write(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { - var key, value string - var err error - fmt.Println("running write()") - +// write - invoke function to write key/value pair +func (t *SimpleChaincode) write(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { + var key, value string + fmt.Println("running write()") + if len(args) != 2 { return nil, errors.New("Incorrect number of arguments. Expecting 2. name of the key and value to set") } - key = args[0] //rename for funsies + key = args[0] //rename for fun value = args[1] - err = stub.PutState(key, []byte(value)) //write the variable into the chaincode state + err := stub.PutState(key, []byte(value)) //write the variable into the chaincode state if err != nil { return nil, err } return nil, nil } +func (t *SimpleChaincode) verifyUser(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { + var keyGuess string + var valGuess string + var returnMessage string + var err error + fmt.Println("running read") + + if len(args) != 2 { + return nil, errors.New("Incorrect number of arguments. Expected 2") + } + + keyGuess = args[0] + valGuess = args[1] + + valActual, err := stub.GetState(keyGuess) + if err != nil { + returnMessage = "Username Incorrect. Login Failed" + return []byte(returnMessage), nil + } + + if testEqualSlice([]byte(valGuess), valActual) { + returnMessage = "Login Succesful" + return []byte(returnMessage), nil + } else { + returnMessage = "Password Incorrect. Login Failed" + return []byte(returnMessage), nil + } +} + + + // read - query function to read key/value pair func (t *SimpleChaincode) read(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { var key, jsonResp string @@ -113,3 +145,24 @@ func (t *SimpleChaincode) read(stub shim.ChaincodeStubInterface, args []string) return valAsbytes, nil } + +func testEqualSlice (a []byte, b []byte) bool { + + if a == nil && b == nil { + return true; + } else if a == nil || b == nil { + return false; + } + + if len(a) != len(b) { + return false + } + + for i := range a { + if a[i] != b[i] { + return false + } + } + return true +} + diff --git a/start/chaincode_start b/start/chaincode_start new file mode 100755 index 000000000..f68e039a0 Binary files /dev/null and b/start/chaincode_start differ diff --git a/start/chaincode_start.go b/start/chaincode_start.go index c44519f29..c53ccd845 100644 --- a/start/chaincode_start.go +++ b/start/chaincode_start.go @@ -42,7 +42,10 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string if len(args) != 1 { return nil, errors.New("Incorrect number of arguments. Expecting 1") } - + err := stub.PutState("hello_world", []byte(args[0])) + if err != nil { + return nil, err + } return nil, nil } @@ -53,22 +56,64 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function stri // Handle different functions if function == "init" { //initialize the chaincode state, used as reset return t.Init(stub, "init", args) + } else if function == "write" { + return t.write(stub, args) } + fmt.Println("invoke did not find func: " + function) //error return nil, errors.New("Received unknown function invocation: " + function) } +func (t *SimpleChaincode) write(stub shim.ChaincodeStubInterface, args []string)([]byte, error) { + var key, value string + var err error + + fmt.Println("Running write func") + + if len(args) != 2 { + return nil, errors.New("Wrong number of arguemnts. Expecting 2") + } + + key = args[0] + value = args[1] + err = stub.PutState(key, []byte(value)) //write the variable into chaincode state + if err != nil { + return nil, err + } + return nil, nil +} + // Query is our entry point for queries func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { fmt.Println("query is running " + function) // Handle different functions - if function == "dummy_query" { //read a variable - fmt.Println("hi there " + function) //error - return nil, nil; + if function == "read" { //read a variable + return t.read(stub, args) } fmt.Println("query did not find func: " + function) //error return nil, errors.New("Received unknown function query: " + function) } + +func (t *SimpleChaincode) read(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) { + var key, jsonResp string + var err error + fmt.Println("Running read func") + + if len(args) != 1 { + return nil, errors.New("Incorrect number of arguments. Expecting name of key to query") + } + + key = args[0] + valAsBytes , err := stub.GetState(key) + + if err != nil { + jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}" + return nil, errors.New(jsonResp) + } + + return valAsBytes, nil + +}