package main

import (
	"strings"
	"fmt"
	"encoding/json"
	"github.com/hyperledger/fabric/core/chaincode/shim"
)

func splitPath(key string) []string {
	key = strings.Trim(key, "/ ")
	if key == "" {
		return []string{}
	}
	return strings.Split(key, "/")
}

//解析前端数据   证书验证  和 签名验证
func messageToTrans(operation TxType,args []string,stub shim.ChaincodeStubInterface) (*Trans,*Message,  string ,error) {
	if len(args) != 3 {
		return nil,nil, "",fmt.Errorf("put data operation expected more than 3 parameters! ")
	}
	message := &Message{
		UserId: args[0],
		Data: args[1],
		Sign: args[2],
	}
	if message.UserId == "" || message.Sign == "" || message.Data == "" {
		return nil,nil,"" ,fmt.Errorf("Parameter exception, cannot be null!")
	}
	//验证证书和签名
	pubKey ,err := CheckCertSignature(message,operation,stub)
	if err != nil{
		 return nil,nil,"",err
	}
	fmt.Println("1",message.Data)
	var trans Trans
	err = json.Unmarshal([]byte(message.Data), &trans)
	if err != nil {
		return nil,nil,"", fmt.Errorf("参数解析失败"+err.Error())
	}
	return &trans,message,pubKey , nil
}

