Beispiel #1
0
/**
 * Should allow us to query the value of the protected strings in the ledger
 * Args:
 * 0 strkey: The key under which the protected string is stored
 */
func (t *SimpleChaincode) getstring(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var strkey string      // The key associated with the string we want to find
	var strvalBytes []byte // The actual value of the string

	if len(args) != 1 {
		return nil, errors.New("Incorrect number of arguments. Expecting 1")
	}

	// Which string do we want?
	strkey = args[0]

	// Get the value of the string from the ledger
	strvalBytes, err := stub.GetState(strkey)
	if err != nil {
		jsonResp := "{\"Error\": \"Failed to get state for " + strkey + "\"}"
		return nil, errors.New(jsonResp)
	}

	if strvalBytes == nil {
		jsonResp := "{\"Error\":\"Nil amount for " + strkey + "\"}"
		return nil, errors.New(jsonResp)
	}

	jsonResp := "{\"StringKey\":\"" + strkey + "\", \"Value\":\"" + string(strvalBytes) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return strvalBytes, nil
}
Beispiel #2
0
func GetAllCPs(stub *shim.ChaincodeStub) ([]CP, error) {

	var allCPs []CP

	// Get list of all the keys
	keysBytes, err := stub.GetState("PaperKeys")
	if err != nil {
		fmt.Println("Error retrieving paper keys")
		return nil, errors.New("Error retrieving paper keys")
	}
	var keys []string
	err = json.Unmarshal(keysBytes, &keys)
	if err != nil {
		fmt.Println("Error unmarshalling paper keys")
		return nil, errors.New("Error unmarshalling paper keys")
	}

	// Get all the cps
	for _, value := range keys {
		cpBytes, err := stub.GetState(value)

		var cp CP
		err = json.Unmarshal(cpBytes, &cp)
		if err != nil {
			fmt.Println("Error retrieving cp " + value)
			return nil, errors.New("Error retrieving cp " + value)
		}

		fmt.Println("Appending CP" + value)
		allCPs = append(allCPs, cp)
	}

	return allCPs, nil
}
func (t *Vehicle) update_registration(stub *shim.ChaincodeStub, args []string) ([]byte, error) {

	regBytes, err := stub.GetState("Reg")

	reg := string(regBytes)

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	statusBytes, err := stub.GetState("Status")
	status, err := strconv.Atoi(string(statusBytes))

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	if status == 1 || status == 2 {
		reg = args[0]
	} else {
		return nil, errors.New("Permission denied")
	}

	err = stub.PutState("Reg", []byte(reg))

	return nil, nil

}
Beispiel #4
0
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
	if function != "query" {
		return nil, errors.New("Invalid query function name. Expecting \"query\"")
	}
	var A string // Entities
	var err error

	if len(args) != 1 {
		return nil, errors.New("Incorrect number of arguments. Expecting name of the person to query")
	}

	A = args[0]

	// Get the state from the ledger
	Avalbytes, err := stub.GetState(A)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
		return nil, errors.New(jsonResp)
	}

	if Avalbytes == nil {
		jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
		return nil, errors.New(jsonResp)
	}

	jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return Avalbytes, nil
}
// ============================================================================================================================
// findMarble4Trade - look for a matching marble that this user owns and return it
// ============================================================================================================================
func findMarble4Trade(stub *shim.ChaincodeStub, user string, color string, size int) (m Marble, err error) {
	var fail Marble
	fmt.Println("- start find marble 4 trade")
	fmt.Println("looking for " + user + ", " + color + ", " + strconv.Itoa(size))

	//get the marble index
	marblesAsBytes, err := stub.GetState(marbleIndexStr)
	if err != nil {
		return fail, errors.New("Failed to get marble index")
	}
	var marbleIndex []string
	json.Unmarshal(marblesAsBytes, &marbleIndex) //un stringify it aka JSON.parse()

	for i := range marbleIndex { //iter through all the marbles
		//fmt.Println("looking @ marble name: " + marbleIndex[i]);

		marbleAsBytes, err := stub.GetState(marbleIndex[i]) //grab this marble
		if err != nil {
			return fail, errors.New("Failed to get marble")
		}
		res := Marble{}
		json.Unmarshal(marbleAsBytes, &res) //un stringify it aka JSON.parse()
		//fmt.Println("looking @ " + res.User + ", " + res.Color + ", " + strconv.Itoa(res.Size));

		//check for user && color && size
		if strings.ToLower(res.User) == strings.ToLower(user) && strings.ToLower(res.Color) == strings.ToLower(color) && res.Size == size {
			fmt.Println("found a marble: " + res.Name)
			fmt.Println("! end find marble 4 trade")
			return res, nil
		}
	}

	fmt.Println("- end find marble 4 trade - error")
	return fail, errors.New("Did not find marble to use in this trade")
}
Beispiel #6
0
func (t *SimpleChaincode) transfer(stub *shim.ChaincodeStub, args []string) ([]byte, error) {

	var v Vehicle
	var err error
	var bytes []byte

	bytes, err = stub.GetState(args[0])

	if err != nil {
		return nil, errors.New("Error retrieving vehicle with v5cID = " + args[0])
	}

	err = json.Unmarshal(bytes, &v)
	if err != nil {
		return nil, errors.New("Corrupt vehicle record")
	}

	v.Owner = args[1]

	bytes, err = json.Marshal(v)

	if err != nil {
		return nil, errors.New("Error creating vehicle record")
	}

	err = stub.PutState(v.V5cID, bytes)

	if err != nil {
		return nil, err
	}

	return nil, nil
}
// ============================================================================================================================
// Set User Permission on Marble
// ============================================================================================================================
func (t *SimpleChaincode) set_user(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

	//   0       1
	// "name", "bob"
	if len(args) < 2 {
		return nil, errors.New("Incorrect number of arguments. Expecting 2")
	}

	fmt.Println("- start set user")
	fmt.Println(args[0] + " - " + args[1])
	marbleAsBytes, err := stub.GetState(args[0])
	if err != nil {
		return nil, errors.New("Failed to get thing")
	}
	res := Marble{}
	json.Unmarshal(marbleAsBytes, &res) //un stringify it aka JSON.parse()
	res.User = args[1]                  //change the user

	jsonAsBytes, _ := json.Marshal(res)
	err = stub.PutState(args[0], jsonAsBytes) //rewrite the marble with id as key
	if err != nil {
		return nil, err
	}

	fmt.Println("- end set user")
	return nil, nil
}
// ============================================================================================================================
// Delete - remove a key/value pair from state
// ============================================================================================================================
func (t *SimpleChaincode) Delete(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	if len(args) != 1 {
		return nil, errors.New("Incorrect number of arguments. Expecting 1")
	}

	name := args[0]
	err := stub.DelState(name) //remove the key from chaincode state
	if err != nil {
		return nil, errors.New("Failed to delete state")
	}

	//get the marble index
	marblesAsBytes, err := stub.GetState(marbleIndexStr)
	if err != nil {
		return nil, errors.New("Failed to get marble index")
	}
	var marbleIndex []string
	json.Unmarshal(marblesAsBytes, &marbleIndex) //un stringify it aka JSON.parse()

	//remove marble from index
	for i, val := range marbleIndex {
		fmt.Println(strconv.Itoa(i) + " - looking at " + val + " for " + name)
		if val == name { //find the correct marble
			fmt.Println("found marble")
			marbleIndex = append(marbleIndex[:i], marbleIndex[i+1:]...) //remove it
			for x := range marbleIndex {                                //debug prints...
				fmt.Println(string(x) + " - " + marbleIndex[x])
			}
			break
		}
	}
	jsonAsBytes, _ := json.Marshal(marbleIndex) //save new index
	err = stub.PutState(marbleIndexStr, jsonAsBytes)
	return nil, nil
}
// ============================================================================================================================
// Set User Permissions
// ============================================================================================================================
func (t *SimpleChaincode) set_user_perms(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

	carAsBytes, err := stub.GetState(args[0])
	if err != nil {
		return nil, errors.New("Failed to get car profile")
	}
	res := Car{}
	json.Unmarshal(carAsBytes, &res)
	fmt.Println(res)

	for i, perm := range res.Users {
		if perm.UserId == args[1] { //find the correct user
			res.Users[i].Permissions[0] = args[2] //set new perm, dsh - to do make this input as array of all perms
			fmt.Println(res.Users[i].Permissions)
			break
		}
	}

	// Write the state back to the ledger
	jsonAsBytes, _ := json.Marshal(res)
	err = stub.PutState(args[0], jsonAsBytes)
	if err != nil {
		return nil, err
	}

	return nil, nil
}
Beispiel #10
0
/**
 * Should allow us to query the value of the protected strings in the ledger
 * Args:
 * stub: The blockchain that holds the string we're want.
 * strkey: The key under which the string is stored.
 */
func (t *SimpleChaincode) getstring(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
    var strkey string
	var strvalBytes []byte  // The actual value of the string
	
	if(len(args) != 1) {
		return nil, errors.New("Incorrect number of arguments. Expecting 1");
	}
	
	// Which string do we want?
	strkey = args[0];
	
	// Get the value of the string from the ledger
	strvalBytes, err := stub.GetState(strkey)
	if err != nil {
		jsonResp := "{\"Error\": \"Failed to get state for " + strkey + "\"}"
		return nil, errors.New(jsonResp)
	}
	
	if strvalBytes == nil {
		jsonResp := "{\"Error\":\"Nil amount for " + strkey + "\"}"
		return nil, errors.New(jsonResp)
	}
    // Add quotes to the string or it will cause an invalid json to be created.
    strvalBytes = []byte(fmt.Sprintf("\"%s\"", string(strvalBytes)))
	
	jsonResp := "{\"StringKey\":\"" + strkey + "\",\"Value\":\"" + string(strvalBytes) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return strvalBytes, nil
}
func (t *Vehicle) update_colour(stub *shim.ChaincodeStub, args []string) ([]byte, error) {

	colourBytes, err := stub.GetState("Colour")

	colour := string(colourBytes)

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	statusBytes, err := stub.GetState("Status")
	status, err := strconv.Atoi(string(statusBytes))

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	if status == 1 || status == 2 {
		colour = args[0]
	} else {
		return nil, errors.New("Permission denied")
	}

	err = stub.PutState("Colour", []byte(colour))

	return nil, nil

}
// Query callback representing the query of a chaincode
func (t *systemChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
	if function != "query" {
		return nil, errors.New("Invalid query function name. Expecting \"query\"")
	}

	if len(args) != 1 {
		return nil, errors.New("Incorrect number of arguments. Expecting 1")
	}

	key := args[0]

	if key != system_validity_period_key {
		return nil, errors.New("Incorrect key. Expecting " + system_validity_period_key)
	}

	// Get the state from the ledger
	vp, err := stub.GetState(key)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get state for " + key + "\"}"
		return nil, errors.New(jsonResp)
	}

	if vp == nil {
		jsonResp := "{\"Error\":\"Nil value for " + key + "\"}"
		return nil, errors.New(jsonResp)
	}

	jsonResp := "{\"Name\":\"" + key + "\",\"Value\":\"" + string(vp) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return []byte(jsonResp), nil
}
func (t *Vehicle) get_attribute(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	state, err := stub.GetState(args[0])

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	jsonResp := "{\"" + args[0] + "\":\"" + string(state) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return []byte(jsonResp), nil
}
// ============================================================================================================================
// Init Marble - create a new marble, store into chaincode state
// ============================================================================================================================
func (t *SimpleChaincode) init_marble(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

	//   0       1       2     3
	// "asdf", "blue", "35", "bob"
	if len(args) != 4 {
		return nil, errors.New("Incorrect number of arguments. Expecting 4")
	}

	fmt.Println("- start init marble")
	if len(args[0]) <= 0 {
		return nil, errors.New("1st argument must be a non-empty string")
	}
	if len(args[1]) <= 0 {
		return nil, errors.New("2nd argument must be a non-empty string")
	}
	if len(args[2]) <= 0 {
		return nil, errors.New("3rd argument must be a non-empty string")
	}
	if len(args[3]) <= 0 {
		return nil, errors.New("4th argument must be a non-empty string")
	}

	size, err := strconv.Atoi(args[2])
	if err != nil {
		return nil, errors.New("3rd argument must be a numeric string")
	}

	color := strings.ToLower(args[1])
	user := strings.ToLower(args[3])

	str := `{"name": "` + args[0] + `", "color": "` + color + `", "size": ` + strconv.Itoa(size) + `, "user": "******"}`
	err = stub.PutState(args[0], []byte(str)) //store marble with id as key
	if err != nil {
		return nil, err
	}

	//get the marble index
	marblesAsBytes, err := stub.GetState(marbleIndexStr)
	if err != nil {
		return nil, errors.New("Failed to get marble index")
	}
	var marbleIndex []string
	json.Unmarshal(marblesAsBytes, &marbleIndex) //un stringify it aka JSON.parse()

	//append
	marbleIndex = append(marbleIndex, args[0]) //add marble name to index list
	fmt.Println("! marble index: ", marbleIndex)
	jsonAsBytes, _ := json.Marshal(marbleIndex)
	err = stub.PutState(marbleIndexStr, jsonAsBytes) //store name of marble

	fmt.Println("- end init marble")
	return nil, nil
}
// Transaction makes payment of X units from A to B
func (t *SimpleChaincode) invoke(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var A, B string    // Entities
	var Aval, Bval int // Asset holdings
	var X int          // Transaction value
	var err error

	if len(args) != 3 {
		return nil, errors.New("Incorrect number of arguments. Expecting 3")
	}

	A = args[0]
	B = args[1]

	// Get the state from the ledger
	// TODO: will be nice to have a GetAllState call to ledger
	Avalbytes, err := stub.GetState(A)
	if err != nil {
		return nil, errors.New("Failed to get state")
	}
	if Avalbytes == nil {
		return nil, errors.New("Entity not found")
	}
	Aval, _ = strconv.Atoi(string(Avalbytes))

	Bvalbytes, err := stub.GetState(B)
	if err != nil {
		return nil, errors.New("Failed to get state")
	}
	if Avalbytes == nil {
		return nil, errors.New("Entity not found")
	}
	Bval, _ = strconv.Atoi(string(Bvalbytes))

	// Perform the execution
	X, err = strconv.Atoi(args[2])
	Aval = Aval - X
	Bval = Bval + X
	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

	// Write the state back to the ledger
	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
	if err != nil {
		return nil, err
	}

	err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
	if err != nil {
		return nil, err
	}

	return nil, nil
}
// ============================================================================================================================
// Get Permissions - get permissions form the car profile
// ============================================================================================================================
func (t *SimpleChaincode) get_permissions(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

	carAsBytes, err := stub.GetState(args[0])
	if err != nil {
		return nil, errors.New("Failed to get car profile")
	}
	res := Car{}
	json.Unmarshal(carAsBytes, &res)
	fmt.Println(res)

	jsonAsBytes, _ := json.Marshal(res.Users)
	return jsonAsBytes, nil
}
// ============================================================================================================================
// Read Names return list of variables in state space
// ============================================================================================================================
func (t *SimpleChaincode) ReadNames(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

	var ben = "_ben_knows"
	var storedNames string

	storedNamesAsBytes, err := stub.GetState(ben)
	if err != nil {
		return nil, errors.New("Failed to get ben")
	}
	storedNames = string(storedNamesAsBytes)
	fmt.Println(storedNames)

	return storedNamesAsBytes, nil
}
func getMortgageStateFull(stub *shim.ChaincodeStub) (map[string]interface{}, error) {
	// Get mortgage state from ledger
	fmt.Printf("Reading mortgage state from shared ledger...\n")
	var state map[string]interface{}
	key := "myMortgageState"
	bytes, err := stub.GetState(key)
	if err != nil || bytes == nil {
		return nil, myError("No mortage state in ledger")
	}
	err = json.Unmarshal(bytes, &state)
	if err != nil {
		return nil, myError("Cannot unmarshal mortage state")
	}
	return state, nil
}
Beispiel #19
0
func GetCompany(companyID string, stub *shim.ChaincodeStub) (Account, error) {
	var company Account
	companyBytes, err := stub.GetState(accountPrefix + companyID)
	if err != nil {
		fmt.Println("Account not found " + companyID)
		return company, errors.New("Account not found " + companyID)
	}

	err = json.Unmarshal(companyBytes, &company)
	if err != nil {
		fmt.Println("Error unmarshalling account " + companyID)
		return company, errors.New("Error unmarshalling account " + companyID)
	}

	return company, nil
}
// ============================================================================================================================
// Remember Me - remember the name of variables we stored in ledger
// ============================================================================================================================
func (t *SimpleChaincode) remember_me(stub *shim.ChaincodeStub, name string) ([]byte, error) { //dsh - to do, should probably not exist here, move to stub
	var err error
	var ben = "_ben_knows"
	var storedNames string
	storeNamesAsBytes, err := stub.GetState(ben)
	if err != nil {
		return nil, errors.New("Failed to get ben")
	}
	storedNames = string(storeNamesAsBytes)
	// Write the state back to the ledger
	err = stub.PutState(ben, []byte(storedNames+","+name)) //dsh - to do, should probably be json
	if err != nil {
		return nil, err
	}
	return nil, nil
}
// ============================================================================================================================
// Read - read a variable from chaincode state
// ============================================================================================================================
func (t *SimpleChaincode) read(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var name, jsonResp string
	var err error

	if len(args) != 1 {
		return nil, errors.New("Incorrect number of arguments. Expecting name of the var to query")
	}

	name = args[0]
	valAsbytes, err := stub.GetState(name) //get the var from chaincode state
	if err != nil {
		jsonResp = "{\"Error\":\"Failed to get state for " + name + "\"}"
		return nil, errors.New(jsonResp)
	}

	return valAsbytes, nil //send it onward
}
Beispiel #22
0
func GetCP(cpid string, stub *shim.ChaincodeStub) (CP, error) {
	var cp CP

	cpBytes, err := stub.GetState(cpid)
	if err != nil {
		fmt.Println("Error retrieving cp " + cpid)
		return cp, errors.New("Error retrieving cp " + cpid)
	}

	err = json.Unmarshal(cpBytes, &cp)
	if err != nil {
		fmt.Println("Error unmarshalling cp " + cpid)
		return cp, errors.New("Error unmarshalling cp " + cpid)
	}

	return cp, nil
}
func (t *SimpleChaincode) Read(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

	if len(args) != 1 {
		return nil, errors.New("Incorrect number of arguments. Expecting name")
	}

	// Get the state from the ledger
	Avalbytes, err := stub.GetState(args[0])
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get state for " + args[0] + "\"}"
		return nil, errors.New(jsonResp)
	}

	strResp := `"` + string(Avalbytes) + `"`

	return []byte(strResp), nil
}
Beispiel #24
0
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
	//need one arg
	if len(args) != 1 {
		return nil, errors.New("Incorrect number of arguments. Expecting 'v5cID'")
	}

	var err error

	bytes, err := stub.GetState(args[0])

	if err != nil {
		return nil, errors.New("Error retrieving vehicle with v5cID = " + args[0])
	}
	fmt.Printf("Found vehicle bytes:%d\n", len(bytes))
	fmt.Printf("Found vehicle:\n%s\n", string(bytes))

	return bytes, nil
}
Beispiel #25
0
//==============================================================================================================================
//	 retrieve_v5c - Gets the state of the data at v5cID in the ledger then converts it from the stored
//					JSON into the Vehicle struct for use in the contract. Returns the Vehcile struct.
//					Returns empty v if it errors.
//==============================================================================================================================
func (t *Chaincode) retrieve_v5c(stub *shim.ChaincodeStub, v5cID string) (Vehicle, error) {

	var v Vehicle

	bytes, err := stub.GetState(v5cID)

	if err != nil {
		return v, errors.New("Error retrieving vehicle with v5cID = " + v5cID)
	}

	err = json.Unmarshal(bytes, &v)

	if err != nil {
		return v, errors.New("Corrupt vehicle record")
	}

	return v, nil
}
Beispiel #26
0
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {

	switch function {

	case "get":
		if len(args) < 1 {
			return nil, errors.New("get operation must include one argument, a key")
		}
		key := args[0]
		value, err := stub.GetState(key)
		if err != nil {
			return nil, fmt.Errorf("get operation failed. Error accessing state: %s", err)
		}
		return value, nil

	case "keys":

		keysIter, err := stub.RangeQueryState("", "")
		if err != nil {
			return nil, fmt.Errorf("keys operation failed. Error accessing state: %s", err)
		}
		defer keysIter.Close()

		var keys []string
		for keysIter.HasNext() {
			key, _, err := keysIter.Next()
			if err != nil {
				return nil, fmt.Errorf("keys operation failed. Error accessing state: %s", err)
			}
			keys = append(keys, key)
		}

		jsonKeys, err := json.Marshal(keys)
		if err != nil {
			return nil, fmt.Errorf("keys operation failed. Error marshaling JSON: %s", err)
		}

		return jsonKeys, nil

	default:
		return nil, errors.New("Unsupported operation")
	}
}
Beispiel #27
0
//==============================================================================================================================
//	get_logs - Takes a users name and returns the logs they are entitled to. If they are the regulator they see all logs
//				 otherwise it calls a function to get the users logs
//==============================================================================================================================
func (t *Chaincode) get_logs(stub *shim.ChaincodeStub, args []string) ([]byte, error) {

	bytes, err := stub.GetState("Vehicle_Log")

	if err != nil {
		return nil, errors.New("Unable to get logs")
	}

	var eh LogsHolder

	err = json.Unmarshal(bytes, &eh)

	if err != nil {
		return nil, errors.New("Corrupt logs record")
	}

	ecert, err := t.get_ecert(stub, args[0])

	if err != nil {
		return nil, err
	}

	role, err := t.check_role(stub, []string{string(ecert)})

	if err != nil {
		return nil, err
	}

	if role == ROLE_AUTHORITY { // Return all logs if authority

		repNull := strings.Replace(string(bytes), "null", "[]", 1) // If the array is blank it has the json value null so we need to make it an empty array

		return []byte(repNull), nil

	} else {

		return t.get_users_logs(stub, eh, args[0])

	}

}
// ============================================================================================================================
// Attach License - edit license field in car profile
// ============================================================================================================================
func (t *SimpleChaincode) attach_license(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

	carAsBytes, err := stub.GetState(args[0])
	if err != nil {
		return nil, errors.New("Failed to get car profile")
	}
	res := Car{}
	json.Unmarshal(carAsBytes, &res)
	fmt.Println(res)

	res.Data.License = args[1]
	jsonAsBytes, _ := json.Marshal(res)
	// Write the state back to the ledger
	err = stub.PutState(args[0], jsonAsBytes)
	if err != nil {
		return nil, err
	}

	return nil, nil
}
func (t *Vehicle) scrap_merchant_to_scrap(stub *shim.ChaincodeStub, args []string) ([]byte, error) {

	statusBytes, err := stub.GetState("Status")
	status, err := strconv.Atoi(string(statusBytes))

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	scrappedBytes, err := stub.GetState("Scrapped")
	scrapped, err := strconv.ParseBool(string(scrappedBytes))

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	ownerBytes, err := stub.GetState("Owner")
	owner := string(ownerBytes)

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	if !scrapped && owner == "Cray Bros (London) Ltd" && status == 4 {
		scrapped = true
	} else {
		return nil, errors.New("Permission denied")
	}

	err = stub.PutState("Scrapped", []byte(strconv.FormatBool(scrapped)))

	return nil, nil
}
func (t *Vehicle) update_model(stub *shim.ChaincodeStub, args []string) ([]byte, error) {

	modelBytes, err := stub.GetState("Model")

	model := string(modelBytes)

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	statusBytes, err := stub.GetState("Status")
	status, err := strconv.Atoi(string(statusBytes))

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	ownerBytes, err := stub.GetState("Owner")
	owner := string(ownerBytes)

	if err != nil {
		return nil, errors.New("Failed to get state")
	}

	if status == 1 && owner == "Toyota" {
		model = args[0]
	} else {
		return nil, errors.New("Permission denied")
	}

	err = stub.PutState("Model", []byte(model))

	return nil, nil
}