chaincode/develop/Get_Class.go
2018-11-11 19:40:26 +01:00

132 lines
4.3 KiB
Go

package main
import (
"encoding/json"
"errors"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
// ============================================================================================================================
// Get Wallet - get a wallet asset from ledger
// ============================================================================================================================
func get_wallet(stub shim.ChaincodeStubInterface, id string) (Wallet, error) {
var wallet Wallet
//getState retreives a key/value from the ledger
walletAsBytes, err := stub.GetState(id)
if err != nil {
return wallet, errors.New("Failed to find wallet - " + id)
}
//un stringify it aka JSON.parse()
json.Unmarshal(walletAsBytes, &wallet)
//test if wallet is actually here or just nil
if wallet.Id != id {
return wallet, errors.New("Wallet does not exist - " + id)
}
return wallet, nil
}
// ============================================================================================================================
// Get Owner - get the owner asset from ledger
// ============================================================================================================================
func get_owner(stub shim.ChaincodeStubInterface, id string) (Owner, error) {
var owner Owner
ownerAsBytes, err := stub.GetState(id)
if err != nil {
return owner, errors.New("Failed to get owner - " + id)
}
json.Unmarshal(ownerAsBytes, &owner)
//test if owner is actually here or just nil
if len(owner.Username) == 0 {
return owner, errors.New("Owner does not exist - " + id + ", '" + owner.Username "'")
}
return owner, nil
}
// ============================================================================================================================
// Get Beneficiary - get the beneficiaries asset from ledger
// ============================================================================================================================
func get_benefeciary(stub shim.ChaincodeStubInterface, id string) (Beneficiary, error) {
var beneficiary Beneficiary
//getState retreives a key/value from the ledger
beneficiaryAsBytes, err := stub.GetState(id)
if err != nil {
return beneficiary, errors.New("Failed to find beneficiary - " + id)
}
json.Unmarshal(walletAsBytes, &beneficiary)
//test if beneficiary is actually here or just nil
if beneficiary.Id != id {
return beneficiary, errors.New("Beneficiary does not exist - " + id)
}
return beneficiary, nil
}
// ============================================================================================================================
// Get Transactions - get the transactions asset from ledger
// ============================================================================================================================
// ============================================================================================================================
// Read - read a generic variable from ledger
//
// Shows Off GetState() - reading a key/value from the ledger
// ============================================================================================================================
func read(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var key, jsonResp string
var err error
fmt.Println("starting read")
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting key of the var to query")
}
// input sanitation
err = sanitize_arguments(args)
if err != nil {
return shim.Error(err.Error())
}
key = args[0]
valAsbytes, err := stub.GetState(key) //get the var from ledger
if err != nil {
jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}"
return shim.Error(jsonResp)
}
fmt.Println("- end read")
return shim.Success(valAsbytes) //send it onward
}
// ========================================================
// Input Sanitation - dumb input checking, look for empty strings
// ========================================================
func sanitize_arguments(strs []string) error{
for i, val:= range strs {
if len(val) <= 0 {
return errors.New("Argument " + strconv.Itoa(i) + " must be a non-empty string")
}
if len(val) > 32 {
return errors.New("Argument " + strconv.Itoa(i) + " must be <= 32 characters")
}
}
return nil
}