github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/build-blockchain-insurance-app-master/web/chaincode/src/bcins/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "encoding/json" 7 8 "github.com/hyperledger/fabric/core/chaincode/shim" 9 pb "github.com/hyperledger/fabric/protos/peer" 10 ) 11 12 const prefixContractType = "contract_type" 13 const prefixContract = "contract" 14 const prefixClaim = "claim" 15 const prefixUser = "user" 16 const prefixRepairOrder = "repair_order" 17 18 var logger = shim.NewLogger("main") 19 20 type SmartContract struct { 21 } 22 23 var bcFunctions = map[string]func(shim.ChaincodeStubInterface, []string) pb.Response{ 24 // Insurance Peer 25 "contract_type_ls": listContractTypes, 26 "contract_type_create": createContractType, 27 "contract_type_set_active": setActiveContractType, 28 "contract_ls": listContracts, 29 "claim_ls": listClaims, 30 "claim_file": fileClaim, 31 "claim_process": processClaim, 32 "user_authenticate": authUser, 33 "user_get_info": getUser, 34 // Shop Peer 35 "contract_create": createContract, 36 "user_create": createUser, 37 // Repair Shop Peer 38 "repair_order_ls": listRepairOrders, 39 "repair_order_complete": completeRepairOrder, 40 // Police Peer 41 "theft_claim_ls": listTheftClaims, 42 "theft_claim_process": processTheftClaim, 43 } 44 45 // Init callback representing the invocation of a chaincode 46 func (t *SmartContract) Init(stub shim.ChaincodeStubInterface) pb.Response { 47 _, args := stub.GetFunctionAndParameters() 48 49 if len(args) == 1 { 50 var contractTypes []struct { 51 UUID string `json:"uuid"` 52 *contractType 53 } 54 err := json.Unmarshal([]byte(args[0]), &contractTypes) 55 if err != nil { 56 return shim.Error(err.Error()) 57 } 58 for _, ct := range contractTypes { 59 contractTypeKey, err := stub.CreateCompositeKey(prefixContractType, []string{ct.UUID}) 60 if err != nil { 61 return shim.Error(err.Error()) 62 } 63 contractTypeAsBytes, err := json.Marshal(ct.contractType) 64 if err != nil { 65 return shim.Error(err.Error()) 66 } 67 err = stub.PutState(contractTypeKey, contractTypeAsBytes) 68 if err != nil { 69 return shim.Error(err.Error()) 70 } 71 } 72 } 73 return shim.Success(nil) 74 } 75 76 // Invoke Function accept blockchain code invocations. 77 func (t *SmartContract) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 78 function, args := stub.GetFunctionAndParameters() 79 80 if function == "init" { 81 return t.Init(stub) 82 } 83 bcFunc := bcFunctions[function] 84 if bcFunc == nil { 85 return shim.Error("Invalid invoke function.") 86 } 87 return bcFunc(stub, args) 88 } 89 90 func main() { 91 logger.SetLevel(shim.LogInfo) 92 93 err := shim.Start(new(SmartContract)) 94 if err != nil { 95 fmt.Printf("Error starting Simple chaincode: %s", err) 96 } 97 }