package main
import (
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
"fmt"
"bytes"
"encoding/pem"
"crypto/x509"
"encoding/json"
"time"
)
type Finance struct {
}
type Compact struct {
Timestamp int64 `json:"timestamp"`
Uid string `json:"uid"`
LoanAmount string `json:"loanAmount"`
ApplyDate string `json:"applyDate"`
CompactStartDate string `json:"compactStartDate"`
CompactEndDate string `json:"compactEndDate"`
RealEndDate string `json:"realEndDate"`
}
func Loan(stub shim.ChaincodeStubInterface, args []string, name string) {
fmt.Println("in Loan ...")
var compact Compact
compact.Uid = args[0]
compact.LoanAmount = args[1]
compact.ApplyDate = args[2]
compact.CompactStartDate = args[3]
compact.CompactEndDate = args[4]
compact.Timestamp = time.Now().Unix()
compactJsonBytes, err := json.Marshal(&compact)
fmt.Println(string(compactJsonBytes))
if err != nil {
fmt.Println("json serialize compact fail, compact id = ",args[5])
}
key, err2 := stub.CreateCompositeKey("compact", []string{name, args[5]})
if err2 != nil {
fmt.Printf("failed to CreateCompositeKey, %s", err)
}
fmt.Println("key:",key)
err3 := stub.PutState(key, compactJsonBytes)
fmt.Println("put state err:", err3)
if err3 != nil {
fmt.Printf("failed to putstate, compact id = " + args[5])
}
}
func (t *Finance) Init(stub shim.ChaincodeStubInterface) peer.Response{
fmt.Println("Init ...")
return shim.Success(nil)
}
func (t *Finance) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
fmt.Println("Invoke ...")
fn, args := stub.GetFunctionAndParameters()
switch fn {
case "loan":
return loan(stub, args)
case "get":
return get(stub, args)
default:
return shim.Error("func name wrong")
}
}
func get(stub shim.ChaincodeStubInterface, args []string) peer.Response {
fmt.Println("in get ...")
fmt.Println(args[0])
value, err := stub.GetState(args[0])
if err != nil {
fmt.Errorf("failed to get asset %s", args[0])
}
fmt.Println("get val: ", string(value))
return shim.Success(nil)
}
func loan(stub shim.ChaincodeStubInterface, args []string) peer.Response {
fmt.Println("loan ...")
name,err := GetCreatorName(stub)
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("name:", name)
key := "compact" + name + args[5]
fmt.Println("key:",key)
var compact Compact
compact.Uid = args[0]
compact.LoanAmount = args[1]
compact.ApplyDate = args[2]
compact.CompactStartDate = args[3]
compact.CompactEndDate = args[4]
compact.Timestamp = time.Now().Unix()
compactJsonBytes, err := json.Marshal(&compact)
stub.PutState(key, compactJsonBytes)
return shim.Success([]byte("loan ok"))
}
func GetCreatorName(stub shim.ChaincodeStubInterface) (string, error){
name, err := Getcreator(stub)
if err != nil {
return "", err
}
return name, err
}
func Getcreator(stub shim.ChaincodeStubInterface) (string, error) {
creator, _ := stub.GetCreator()
certStart := bytes.IndexAny(creator, "-----BEGIN CERTIFICATE-----")
certText := creator[certStart+9:]
fmt.Println(string(certText))
block, err := pem.Decode(certText)
if err != nil {
fmt.Errorf("pem.Decode err: %s", err)
}
ucert, err2 := x509.ParseCertificate(block.Bytes)
if err2 != nil {
fmt.Errorf("Error received on ParseCertificate", err2)
}
uname:=ucert.Subject.CommonName
fmt.Println(uname)
return uname, nil
}
func main() {
err := shim.Start(new(Finance))
if err != nil {
fmt.Printf("Chaincode 啟動(dòng)失敽采唷:%s", err)
}
}
測(cè)試:
peer chaincode invoke -C myc -n finance -c '{"Args":["loan", "O8yTkeBYU3J9otEdOpr9fNR4bE1sHgQ2", "200000", "20180806080812", "20180807080812", "20190807080812", "000011"]}'
peer chaincode invoke -C myc -n finance -c '{"Args":["get", "compactpeer0.org1.example.com000011"]}'