我是Hyperledger Fabric的新手。在当前版本的Hyperledger Fabric中,在chaincode.go中,我无法找到名为ReadCertAttributes的函数。有没有办法获得属性?
答案 0 :(得分:5)
从Hypeledger Fabric 1.0.0开始,您可以使用GetCreator
的ChaincodeStubInterface
方法获取客户端证书,例如:
// GetCreator returns `SignatureHeader.Creator` (e.g. an identity)
// of the `SignedProposal`. This is the identity of the agent (or user)
// submitting the transaction.
GetCreator() ([]byte, error)
例如,你可以做类似的事情:
func (*smartContract) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
fmt.Println("Invoke")
// GetCreator returns marshaled serialized identity of the client
serializedID, _ := stub.GetCreator()
sId := &msp.SerializedIdentity{}
err := proto.Unmarshal(serializedID, sId)
if err != nil {
return shim.Error(fmt.Sprintf("Could not deserialize a SerializedIdentity, err %s", err))
}
bl, _ := pem.Decode(sId.IdBytes)
if bl == nil {
return shim.Error(fmt.Sprintf("Failed to decode PEM structure"))
}
cert, err := x509.ParseCertificate(bl.Bytes)
if err != nil {
return shim.Error(fmt.Sprintf("Unable to parse certificate %s", err))
}
// Do whatever you need with certificate
return shim.Success(nil)
}