How to retrieve user information from recent versi

2020-07-27 02:05发布

问题:

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)
}