scc-chaincode-demo-go



  • package main
    
    import (
            "bytes"
    	"encoding/json"
    	"fmt"
    	"strconv"
            "crypto/x509"
            "encoding/pem"
    
    	"github.com/hyperledger/fabric/core/chaincode/shim"
    	sc "github.com/hyperledger/fabric/protos/peer"
    )
    
    type SmartContract struct {
    }
    
    type Account struct {
    	Property  float64
    }
    
    func (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
    	return shim.Success(nil)
    }
    
    func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {
            function, args := APIstub.GetFunctionAndParameters()
    	if function == "queryAccount" {
    		return s.queryAccount(APIstub, args)
    	} else if function == "initLedger" {
    		return s.initLedger(APIstub)
    	} else if function == "createAccount" {
    		return s.createAccount(APIstub, args)
    	} else if function == "give" {
    		return s.give(APIstub, args)
    	}
    
    	return shim.Error("Invalid Smart Contract function name.")
    }
    
    func (s *SmartContract) queryAccount(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
    
    	if len(args) != 1 {
    		return shim.Error("Incorrect number of arguments. Expecting 1")
    	}
    
    	accountAsBytes, _ := APIstub.GetState(args[0])
    	return shim.Success(accountAsBytes)
    }
    
    func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {
    	var account = Account{Property: 1000000000.00}
            accountAsBytes, _ := json.Marshal(account)
            APIstub.PutState("7C14F871C33E102858178965B2AB7FF7", accountAsBytes)
    	return shim.Success(nil)
    }
    
    func (s *SmartContract) createAccount(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
    
    	if len(args) != 2 {
    		return shim.Error("Incorrect number of arguments. Expecting 2")
    	}
            checkAccount, _ := APIstub.GetState(args[0])
            if checkAccount != nil {
                    return shim.Error("Account already exists!")
            }
    
            float64, err := strconv.ParseFloat(args[1], 64)
            if err != nil {
                    return shim.Error(err.Error())
            }
    
    	var account = Account{Property: float64}
    
    	accountAsBytes, _ := json.Marshal(account)
    	APIstub.PutState(args[0], accountAsBytes)
    
    	return shim.Success(nil)
    }
    
    func (s *SmartContract) give(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
            
            creatorByte,_:= APIstub.GetCreator()
            certStart := bytes.IndexAny(creatorByte, "-----BEGIN")
            if certStart == -1 {
                    return shim.Error("No certificate found")
            }
            certText := creatorByte[certStart:]
            bl, _ := pem.Decode(certText)
            if bl == nil {
                    return shim.Error("cannot decode pem")
            }
    
            cert, err := x509.ParseCertificate(bl.Bytes)
            if err != nil {
                    return shim.Error(err.Error())
            }
            uname:=cert.Subject.CommonName
    
    	if len(args) != 3 {
    		return shim.Error("Incorrect number of arguments. Expecting 3")
    	}
    
            if args[0] == args[1] {
                    return shim.Error("Cannot transfer to oneself")
            }
    
            if args[0] != uname {
                    return shim.Error("No permission")
            }
    
            float64, err := strconv.ParseFloat(args[2], 64)
            if err != nil {
                    return shim.Error(err.Error())
            }
    
            if float64 <= 0 {
                    return shim.Error("Incorrect amount")
            }
    
            accountFromAsBytes, _ := APIstub.GetState(args[0])
            accountToAsBytes, _ := APIstub.GetState(args[1])
    
            if accountFromAsBytes == nil {
                    return shim.Error("Account not exist!")
            }
    
            if accountToAsBytes == nil {
                    return shim.Error("Account not exist!")
            }
    
            accountFrom := Account{}
            accountTo := Account{}
    
            json.Unmarshal(accountFromAsBytes, &accountFrom)
            json.Unmarshal(accountToAsBytes, &accountTo)
    
            if accountFrom.Property < float64 {
                    return shim.Error("Not enough money")
            }
    
            accountFrom.Property = accountFrom.Property - float64
            accountTo.Property = accountTo.Property + float64
    
            accountFromAsBytes, _ = json.Marshal(accountFrom)
            accountToAsBytes, _ = json.Marshal(accountTo)
            APIstub.PutState(args[0], accountFromAsBytes)
            APIstub.PutState(args[1], accountToAsBytes)
    
    	return shim.Success(nil)
    }
    
    func main() {
    	err := shim.Start(new(SmartContract))
    	if err != nil {
    		fmt.Printf("Error creating new Smart Contract: %s", err)
    	}
    }
    

Log in to reply
 

Popular Topics

|

Looks like your connection to SCC was lost, please wait while we try to reconnect.