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

2023-12-11

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


从 Hyperledger 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)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从最新版本的 Hyperledger Fabric 检索用户信息? 的相关文章

随机推荐