go - 如何将 prpBytes(描述​​中的链接 src)(ProposalResponsePayload protobuf 消息)反序列化为原始对象

标签 go protocol-buffers hyperledger-fabric hyperledger

我对 plugin.go 中的方法有疑问,发现 here在 Hyperledger 结构库中。

// Endorse signs the given payload(ProposalResponsePayload bytes), and optionally mutates it.
// Returns:
// The Endorsement: A signature over the payload, and an identity that is used to verify the signature
// The payload that was given as input (could be modified within this function)
// Or error on failure
func (e *DefaultEndorsement) Endorse(prpBytes []byte, sp *peer.SignedProposal) (*peer.Endorsement, []byte, error) {
    signer, err := e.SigningIdentityForRequest(sp)
    if err != nil {
        return nil, nil, errors.New(fmt.Sprintf("failed fetching signing identity: %v", err))
    }
    // serialize the signing identity
    identityBytes, err := signer.Serialize()
    if err != nil {
        return nil, nil, errors.New(fmt.Sprintf("could not serialize the signing identity: %v", err))
    }

    // sign the concatenation of the proposal response and the serialized endorser identity with this endorser's key
    signature, err := signer.Sign(append(prpBytes, identityBytes...))
    if err != nil {
        return nil, nil, errors.New(fmt.Sprintf("could not sign the proposal response payload: %v", err))
    }
    endorsement := &peer.Endorsement{Signature: signature, Endorser: identityBytes}
    return endorsement, prpBytes, nil
}

如何将参数 prpBytes 反序列化为原始对象?

prpBytes 是类型 ProposalResponsePayload protobuf 消息

最佳答案

像这样:

import 
  (
      "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"

  )

prp := &peer.ProposalResponsePayload{}
proto.Unmarshal(prpBytes, prp)

chaincodeAction := &peer.ChaincodeAction{}
proto.Unmarshal(prp.Extension, chaincodeAction)

fmt.Println(chaincodeAction.Response)
txRWSet := &rwsetutil.TxRwSet{}
txRWSet.FromProtoBytes(chaincodeAction.Results)
for _, ns := range txRWSet.NsRwSets {
    fmt.Println("chaincode:", ns.NameSpace)
    for _, rws := range ns.KvRwSet.Reads {
        fmt.Println("read key", rws.Key)
    }

    for _, rws := range ns.KvRwSet.Writes {
        fmt.Println("wrote", string(rws.Value), "to key", rws.Key)
    }
}

关于go - 如何将 prpBytes(描述​​中的链接 src)(ProposalResponsePayload protobuf 消息)反序列化为原始对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54339002/

相关文章:

go - 有没有办法验证/解析其中包含 go-template 变量的 YAML 文件?

go - 按名称设置 protobuf 属性

go - 如何访问 map[interface{}] interface{} 中的键

database - 我可以用哪种格式存储 golang 的 time.Time 在 scylladb 中?

go - 通过其字符串名称类型转换为特定的结构类型

c++ - protobuf 3 中的原始类型

go - 如何使用 Go 的类型别名让自己的模型与 protobufs 一起工作?

hyperledger-fabric - 列出 super 账本结构中的所有对等点、部分 channel

docker - IBM Blockchain Platform在VS代码中创建Fabric环境时出错

hyperledger - Hyperledger Fabric 中的对等 channel 创建失败