Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.0 #194

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

V2.0 #194

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added finished/chaincode_finished
Binary file not shown.
73 changes: 63 additions & 10 deletions finished/chaincode_finished.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"errors"
"fmt"

"github.com/hyperledger/fabric/core/chaincode/shim"
)

Expand All @@ -32,6 +31,7 @@ func main() {
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}

}

// Init resets all the things
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
}

Binary file added start/chaincode_start
Binary file not shown.
53 changes: 49 additions & 4 deletions start/chaincode_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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

}