Guest User

Untitled

a guest
Jan 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.  "github.com/hyperledger/fabric/core/chaincode/shim"
  5.   sc "github.com/hyperledger/fabric/protos/peer"
  6. )
  7.  
  8. type SmartContract struct {
  9. // Here we can store specific smartcontract data
  10. }
  11.  
  12. type Asset struct {
  13. Name string `json:"name"`
  14. }
  15.  
  16. /*
  17.  * The Invoke method is called as a result of an application request to run the Smart Contract
  18.  * The calling application program has also specified the particular smart contract function to be called, with arguments
  19.  */
  20. func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {
  21. function, args := APIstub.GetFunctionAndParameters()
  22. switch function {
  23.   case "getAsset":
  24.   return s.getAsset(APIstub, args)
  25.   }
  26.  
  27. return shim.Error("Invalid Smart Contract function name.")
  28. }
  29.  
  30. func (s *SmartContract) getAsset(
  31.  APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  32.  
  33. if len(args) != 1 {
  34.   return shim.Error("Incorrect number of arguments. Expecting 1")
  35.   }
  36.  
  37. assetAsBytes, err := APIstub.GetState(args[0])
  38.  
  39. if err != nil || len(assetAsBytes) == 0 {
  40.   return shim.Error(fmt.Sprintf("No asset with ID: %s", args[0]))
  41.   }
  42.  
  43. return shim.Success(s.getObjectAsBytes(assetLocalID, assetAsBytes))
  44. }
  45.  
  46. func (s *SmartContract) getObjectAsBytes(
  47. identificator string, objectAsBytes []byte) []byte {
  48.  
  49. var buffer bytes.Buffer
  50. buffer.WriteString("{\"Key\":")
  51.   buffer.WriteString("\"")
  52.   buffer.WriteString(identificator)
  53.   buffer.WriteString("\"")
  54.   buffer.WriteString(", \"Record\":")
  55.   buffer.WriteString(string(objectAsBytes))
  56.   buffer.WriteString("}")
  57.  
  58. return buffer.Bytes()
  59. }
Add Comment
Please, Sign In to add comment