Как десериализовать prpBytes (ссылка src в описании) (сообщение ProposalResponsePayload protobuf) на оригинальный объект - PullRequest
0 голосов
/ 24 января 2019

У меня есть вопрос относительно метода в plugin.go, который найден здесь в библиотеке фабрики 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 message

1 Ответ

0 голосов
/ 26 января 2019

Примерно так:

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)
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...