package main import ( "fmt" "strconv" "github.com/hyperledger/fabric/core/chaincode/shim" pb "github.com/hyperledger/fabric/protos/peer" ) // SimpleChaincode example simple Chaincode implementation type SimpleChaincode struct { } // ============================================================================================================================ // Asset Definitions - The ledger will store wallets and owners // ============================================================================================================================ // ----- Wallets ----- // type Wallet struct { ObjectType string `json:"docType"` Id string `json:"id"` Balance int `json:"balance"` Owner OwnerRelation `json:"owner"` } // ----- Beneficiaries ----- // type Beneficiary struct { ObjectType string `json:"docType"` Id string `json:"id"` IdBenef string `json:"idbenef` NameBenef string `json:"namebenef"` FirstnameBenef string `json:"firstnamebenef"` Owner OwnerRelation `json:"owner"` } // ----- Owners ----- // type Owner struct { ObjectType string `json:"docType"` Id string `json:"id"` Name string `json:"name"` Firstname string `json:"firstname"` Mail string `json:"mail"` Address string `json:"address"` City string `json:"city"` Postalcode int `json:"postal"` Password string `json:"password"` Association string `json:"association"` //disabled owners will not be visible to the application Enabled bool `json:"enabled"` } type OwnerRelation struct { Id string `json:"id"` Name string `json:"name"` Firstname string `json:"firstname"` } // ============================================================================================================================ // Main // ============================================================================================================================ func main() { err := shim.Start(new(SimpleChaincode)) if err != nil { fmt.Printf("Error starting Simple chaincode - %s", err) } } // ============================================================================================================================ // Init - initialize the chaincode // // Returns - shim.Success or error // ============================================================================================================================ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { fmt.Println("Wallet Is Starting Up") funcName, args := stub.GetFunctionAndParameters() var number int var err error txId := stub.GetTxID() fmt.Println("Init() is running") fmt.Println("Transaction ID:", txId) fmt.Println(" GetFunctionAndParameters() function:", funcName) fmt.Println(" GetFunctionAndParameters() args count:", len(args)) fmt.Println(" GetFunctionAndParameters() args found:", args) // expecting 1 arg for instantiate or upgrade if len(args) == 1 { fmt.Println(" GetFunctionAndParameters() arg[0] length", len(args[0])) // expecting arg[0] to be length 0 for upgrade if len(args[0]) == 0 { fmt.Println("args[0] is empty...") } else { fmt.Println(" Great news everyone, args[0] is not empty") // convert numeric string to integer number, err = strconv.Atoi(args[0]) if err != nil { return shim.Error("Expecting a numeric string argument to Init() for instantiate") } // this is a very simple test. let's write to the ledger and error out on any errors // it's handy to read this right away to verify network is healthy if it wrote the correct value err = stub.PutState("selftest", []byte(strconv.Itoa(number))) if err != nil { return shim.Error(err.Error()) //self-test fail } } } // showing the alternative argument shim function alt := stub.GetStringArgs() fmt.Println(" GetStringArgs() args count:", len(alt)) fmt.Println(" GetStringArgs() args found:", alt) // store compatible wallet application version err = stub.PutState("wallets_ui", []byte("4.0.1")) if err != nil { return shim.Error(err.Error()) } fmt.Println("Ready for action") //self-test pass return shim.Success(nil) } // ============================================================================================================================ // Invoke - Our entry point for Invocations // ============================================================================================================================ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { function, args := stub.GetFunctionAndParameters() fmt.Println(" ") fmt.Println("starting invoke, for - " + function) // Handle different functions if function == "init" { //initialize the chaincode state, used as reset return t.Init(stub) } else if function == "read" { //generic read ledger return read(stub, args) } else if function == "write" { //generic writes to ledger return write(stub, args) } else if function == "delete_wallet" { //deletes a wallet from state return delete_wallet(stub, args) } else if function == "delete_owner" { //deletes an owner from state return delete_owner(stub, args) } else if function == "delete_beneficiary" { //deletes a beneficiary from state return delete_beneficiary(stub, args) } else if function == "init_wallet" { //create a new wallet return init_wallet(stub, args) } else if function == "init_owner"{ //create a new wallet owner return init_owner(stub, args) } else if function == "init_beneficiary" { //create a new beneficiary return init_beneficiary(stub, args) } else if function == "set_owner" { //change owner of a wallet return set_owner(stub, args) } else if function == "set_beneficiary" { //change owner of a wallet return set_beneficiary(stub, args) } else if function == "disable_owner"{ //disable a wallet owner from appearing on the UI return disable_owner(stub, args) } else if function == "transfer"{ //transfer X Units From a User to an other return transfer(stub, args) } else if function == "get_wallet"{ //get wallets return get_wallet(stub, args) } else if function == "get_owner"{ //get owners return get_owner(stub, args) } else if function == "get_beneficiary"{ //get beneficiaries return get_beneficiary(stub, args) } else if function == "get_transaction"{ //get transactions return get_transaction(stub, args) // error out fmt.Println("Received unknown invoke function name - " + function) return shim.Error("Received unknown invoke function name - '" + function + "'") } // ============================================================================================================================ // Query - legacy function // ============================================================================================================================ func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) pb.Response { return shim.Error("Unknown supported call - Query()") }