如何从Hyperledger Fabric的最新版本中检索用户信息? [英] How to retrieve user information from recent version of Hyperledger Fabric?

查看:247
本文介绍了如何从Hyperledger Fabric的最新版本中检索用户信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

从Hypeledger Fabric 1.0.0开始,您可以使用

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

这篇关于如何从Hyperledger Fabric的最新版本中检索用户信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆