func (t *SimpleChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var event string // Indicates whether event has happened. Initially 0
	var eventVal int // State of event
	var err error

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

	// Initialize the chaincode
	event = args[0]
	eventVal, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expecting integer value for event status")
	}
	fmt.Printf("eventVal = %d\n", eventVal)

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

	return nil, nil
}
// Initialize entities to random variables in order to test ledger status consensus
func (t *SimpleChaincode) initRand(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var A, B string    // Entities
	var Aval, Bval int // Asset holdings
	var err error

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

	// Initialize the chaincode
	A = args[0]
	Aval = rand.Intn(100)
	B = args[1]
	Bval = rand.Intn(100)
	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

	// Write the state 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
}
Example #3
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
}
// ============================================================================================================================
// Init - reset all the things
// ============================================================================================================================
func (t *SimpleChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var Aval int
	var err error

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

	// Initialize the chaincode
	Aval, err = strconv.Atoi(args[0])
	if err != nil {
		return nil, errors.New("Expecting integer value for asset holding")
	}

	// Write the state to the ledger
	err = stub.PutState("abc", []byte(strconv.Itoa(Aval))) //making a test var "abc", I find it handy to read/write to it right away to test the network
	if err != nil {
		return nil, err
	}

	var empty []string
	jsonAsBytes, _ := json.Marshal(empty) //marshal an emtpy array of strings to clear the index
	err = stub.PutState(marbleIndexStr, jsonAsBytes)
	if err != nil {
		return nil, err
	}

	return nil, nil
}
Example #5
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
}
Example #6
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
}
Example #7
0
// 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
}
Example #8
0
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
	if function == "getstring" {
		return t.getstring(stub, args)
	} else if function != "query" {
		return nil, errors.New("Invalid query function name. Expecting \"query\"")
	}
	var A string // Entity
	var Aval int // Asset holding
	var err error

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

	A = args[0]
	Aval, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expecting integer value for asset holding")
	}
	fmt.Printf("Aval = %d\n", Aval)

	// Write the state to the ledger - this put is illegal within Run
	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
	if err != nil {
		jsonResp := "{\"Error\":\"Cannot put state within chaincode query\"}"
		return nil, errors.New(jsonResp)
	}

	fmt.Printf("Something is wrong. This query should not have succeeded")
	return nil, nil
}
Example #9
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
}
// ============================================================================================================================
// 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")
}
Example #11
0
func (t *SimpleChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var A string // Entity
	var Aval int // Asset holding
	var err error

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

	// Initialize the chaincode
	A = args[0]
	Aval, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expecting integer value for asset holding")
	}
	fmt.Printf("Aval = %d\n", Aval)

	// Write the state to the ledger - this put is legal within Run
	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
	if err != nil {
		return nil, err
	}

	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
}
// ============================================================================================================================
// Init Car
// ============================================================================================================================
func (t *SimpleChaincode) init_car(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

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

	str := `{
				"data": {
					"vin": "` + args[0] + `",
					"year": "` + args[1] + `"
					"make": "` + args[2] + `",
					"model": "` + args[3] + `",
					"license": "-"
				},
				"users": [{
					"userid": "` + args[4] + `",
					"permissions":["owner"]
				}]
			}`

	// Write the state back to the ledger
	err = stub.PutState(args[0], []byte(str)) //store car with vin# as key
	if err != nil {
		return nil, err
	}
	t.remember_me(stub, args[0])

	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
}
func (t *SimpleChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var sum string // Sum of asset holdings across accounts. Initially 0
	var sumVal int // Sum of holdings
	var err error

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

	// Initialize the chaincode
	sum = args[0]
	sumVal, err = strconv.Atoi(args[1])
	if err != nil {
		return nil, errors.New("Expecting integer value for sum")
	}
	fmt.Printf("sumVal = %d\n", sumVal)

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

	return nil, nil
}
Example #16
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
}
Example #17
0
// 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
}
Example #18
0
func createTableThree(stub *shim.ChaincodeStub) error {
	var columnDefsTableThree []*shim.ColumnDefinition
	columnOneTableThreeDef := shim.ColumnDefinition{Name: "colOneTableThree",
		Type: shim.ColumnDefinition_STRING, Key: true}
	columnTwoTableThreeDef := shim.ColumnDefinition{Name: "colTwoTableThree",
		Type: shim.ColumnDefinition_INT32, Key: false}
	columnThreeTableThreeDef := shim.ColumnDefinition{Name: "colThreeTableThree",
		Type: shim.ColumnDefinition_INT64, Key: false}
	columnFourTableThreeDef := shim.ColumnDefinition{Name: "colFourTableFour",
		Type: shim.ColumnDefinition_UINT32, Key: false}
	columnFiveTableThreeDef := shim.ColumnDefinition{Name: "colFourTableFive",
		Type: shim.ColumnDefinition_UINT64, Key: false}
	columnSixTableThreeDef := shim.ColumnDefinition{Name: "colFourTableSix",
		Type: shim.ColumnDefinition_BYTES, Key: false}
	columnSevenTableThreeDef := shim.ColumnDefinition{Name: "colFourTableSeven",
		Type: shim.ColumnDefinition_BOOL, Key: false}
	columnDefsTableThree = append(columnDefsTableThree, &columnOneTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnTwoTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnThreeTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnFourTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnFiveTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnSixTableThreeDef)
	columnDefsTableThree = append(columnDefsTableThree, &columnSevenTableThreeDef)
	return stub.CreateTable("tableThree", columnDefsTableThree)
}
// 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 sum string             // Sum entity
	var Aval, Bval, sumVal int // value of sum entity - to be computed
	var err error

	// Can query another chaincode within query, but cannot put state or invoke another chaincode (in transaction context)
	if len(args) != 3 {
		return nil, errors.New("Incorrect number of arguments. Expecting 3")
	}

	chaincodeUrl := args[0]     // Expecting "github.com/openblockchain/obc-peer/openchain/example/chaincode/chaincode_example02"
	chaincodeVersion := args[1] // Expecting "0.0.1"
	sum = args[2]

	// Query chaincode_example02
	f := "query"
	queryArgs := []string{"a"}
	response, err := stub.QueryChaincode(chaincodeUrl, chaincodeVersion, f, queryArgs)
	if err != nil {
		errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
		fmt.Printf(errStr)
		return nil, errors.New(errStr)
	}
	Aval, err = strconv.Atoi(string(response))
	if err != nil {
		errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
		fmt.Printf(errStr)
		return nil, errors.New(errStr)
	}

	queryArgs = []string{"b"}
	response, err = stub.QueryChaincode(chaincodeUrl, chaincodeVersion, f, queryArgs)
	if err != nil {
		errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
		fmt.Printf(errStr)
		return nil, errors.New(errStr)
	}
	Bval, err = strconv.Atoi(string(response))
	if err != nil {
		errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
		fmt.Printf(errStr)
		return nil, errors.New(errStr)
	}

	// Compute sum
	sumVal = Aval + Bval

	fmt.Printf("Query chaincode successful. Got sum %d\n", sumVal)
	jsonResp := "{\"Name\":\"" + sum + "\",\"Value\":\"" + strconv.Itoa(sumVal) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return []byte(strconv.Itoa(sumVal)), nil
}
Example #20
0
// Initialize the in the ledger (this needs to be run only once!!!!)
func (t *systemChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var vp int64 = 0

	// Initialize the validity period in the ledger (this needs to be run only once!!!!)
	err := stub.PutState(system_validity_period_key, []byte(strconv.FormatInt(vp, 10)))
	if err != nil {
		return nil, err
	}

	return nil, nil
}
Example #21
0
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
}
func (t *SimpleChaincode) test(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

	// Write the state back to the ledger
	str := "hey there"
	err = stub.PutState("test", []byte(str))
	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
}
Example #24
0
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
}
Example #25
0
func (t *AssetManagementChaincode) isCaller(stub *shim.ChaincodeStub, certificate []byte) (bool, error) {
	// In order to enforce access control, we require that the
	// metadata contains the signature under the signing key corresponding
	// to the verification key inside certificate of
	// the payload of the transaction (namely, function name and args) and
	// the transaction binding (to avoid copying attacks)

	// Verify \sigma=Sign(certificate.sk, tx.Payload||tx.Binding) against certificate.vk
	// \sigma is in the metadata

	sigma, err := stub.GetCallerMetadata()
	if err != nil {
		return false, errors.New("Failed getting metadata")
	}
	payload, err := stub.GetPayload()
	if err != nil {
		return false, errors.New("Failed getting payload")
	}
	binding, err := stub.GetBinding()
	if err != nil {
		return false, errors.New("Failed getting binding")
	}

	myLogger.Debug("passed certificate [% x]", certificate)
	myLogger.Debug("passed sigma [% x]", sigma)
	myLogger.Debug("passed payload [% x]", payload)
	myLogger.Debug("passed binding [% x]", binding)

	return stub.VerifySignature(
		certificate,
		sigma,
		append(payload, binding...),
	)
}
Example #26
0
func createTableOne(stub *shim.ChaincodeStub) error {
	// Create table one
	var columnDefsTableOne []*shim.ColumnDefinition
	columnOneTableOneDef := shim.ColumnDefinition{Name: "colOneTableOne",
		Type: shim.ColumnDefinition_STRING, Key: true}
	columnTwoTableOneDef := shim.ColumnDefinition{Name: "colTwoTableOne",
		Type: shim.ColumnDefinition_INT32, Key: false}
	columnThreeTableOneDef := shim.ColumnDefinition{Name: "colThreeTableOne",
		Type: shim.ColumnDefinition_INT32, Key: false}
	columnDefsTableOne = append(columnDefsTableOne, &columnOneTableOneDef)
	columnDefsTableOne = append(columnDefsTableOne, &columnTwoTableOneDef)
	columnDefsTableOne = append(columnDefsTableOne, &columnThreeTableOneDef)
	return stub.CreateTable("tableOne", columnDefsTableOne)
}
Example #27
0
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
}
Example #28
0
//==============================================================================================================================
//	 create_log - Invokes the function of event_code chaincode with the name 'chaincodeName' to log an
//					event.
//==============================================================================================================================
func (t *Chaincode) create_log(stub *shim.ChaincodeStub, args []string) ([]byte, error) {

	chaincode_name := "398f2e9a632d17bfc54f259f581818448a1f7b654901ce1648a66c36f0003b2ece440382e585d6b7dac8ee010f2b4fae29bda3135cf76d9c3895cacec1ffbeb4"
	chaincode_function := "create_log"
	chaincode_arguments := args

	_, err := stub.InvokeChaincode(chaincode_name, chaincode_function, chaincode_arguments)

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

	return nil, nil
}
Example #29
0
func (t *Vehicle) lease_company_to_private(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")
	}

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

	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")
	}

	if status == 2 && owner == "LeaseCan" && !scrapped {
		owner = args[0]
	} else {
		return nil, errors.New("Permission denied")
	}

	err = stub.PutState("Owner", []byte(owner))

	return nil, nil

}
// ============================================================================================================================
// Init TEST
// ============================================================================================================================
func (t *SimpleChaincode) init_test(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
	var err error

	str := `{"userid": "test", "fullname": "mr test"}`

	// Write the state back to the ledger
	err = stub.PutState("test", []byte(str))
	if err != nil {
		return nil, err
	}
	t.remember_me(stub, "test")

	return nil, nil
}