Last active
April 11, 2019 15:10
-
-
Save aschmidt75/cb1d1aeb0efcff5c76c461f1f4b951d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (cci *PreciousCargoChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { | |
| function, args := stub.GetFunctionAndParameters() | |
| if function == "registerParticipant" { | |
| if len(args) != 1 { | |
| return shim.Error("expecting JSON input as first param") | |
| } | |
| // (1) parse input | |
| arg := struct { | |
| Name string `json:"name"` | |
| Address string `json:"address"` | |
| }{} | |
| err := json.Unmarshal([]byte(args[0]), &arg) | |
| if err != nil { | |
| return shim.Error("invalid JSON") | |
| } | |
| // (2) Create an ID for the new participant | |
| idStr, err := newID(stub, "IndividualParticipant") | |
| if err != nil { | |
| return shim.Error("internal error generating index key") | |
| } | |
| // (3) create data item for world state update | |
| p := IndividualParticipant{ | |
| Participant: Participant{ | |
| ID: ID{ | |
| ID: idStr, | |
| }, | |
| Name: arg.Name, | |
| }, | |
| Address: arg.Address, | |
| } | |
| ck, err := stub.CreateCompositeKey("co.preciousgoodsshipping.IndividualParticipant#", []string{idStr}) | |
| if err != nil { | |
| return shim.Error("error creating key") | |
| } | |
| // (4) marshal data to json and ... | |
| data, err := json.Marshal(p) | |
| if err != nil { | |
| return shim.Error("internal JSON marshal error (1)") | |
| } | |
| // (5) ... save to world state | |
| err = stub.PutState(ck, []byte(data)) | |
| if err != nil { | |
| return shim.Error("internal error writing world state") | |
| } | |
| // return ID of new participant | |
| return shim.Success([]byte(idStr)) | |
| } | |
| return shim.Error("Invalid transaction name.") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment