I'm new to Hyperledger Fabric. In the current version of Hyperledger Fabric, in chaincode.go I can't find the function called ReadCertAttributes. Is there any way to get attributes?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Starting from Hypeledger Fabric 1.0.0 you can use GetCreator
method of ChaincodeStubInterface
to obtain clients certificate, e.g.:
// 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)
For example you can do something similar to:
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)
}