如何从Hyperledger Fabric的最新版本中检索用户信息?

use*_*508 2 hyperledger-fabric

我是Hyperledger Fabric的新手。在当前版本的Hyperledger Fabric中,在chaincode.go中找不到名为ReadCertAttributes的函数。有什么方法可以获取属性?

Art*_*ger 8

从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)
Run Code Online (Sandbox Code Playgroud)

例如,您可以执行类似的操作:

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)
}
Run Code Online (Sandbox Code Playgroud)