github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/handlers/endorsement/builtin/default_endorsement.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package builtin 8 9 import ( 10 endorsement "github.com/hechain20/hechain/core/handlers/endorsement/api" 11 identities "github.com/hechain20/hechain/core/handlers/endorsement/api/identities" 12 "github.com/hyperledger/fabric-protos-go/peer" 13 "github.com/pkg/errors" 14 ) 15 16 // DefaultEndorsementFactory returns an endorsement plugin factory which returns plugins 17 // that behave as the default endorsement system chaincode 18 type DefaultEndorsementFactory struct{} 19 20 // New returns an endorsement plugin that behaves as the default endorsement system chaincode 21 func (*DefaultEndorsementFactory) New() endorsement.Plugin { 22 return &DefaultEndorsement{} 23 } 24 25 // DefaultEndorsement is an endorsement plugin that behaves as the default endorsement system chaincode 26 type DefaultEndorsement struct { 27 identities.SigningIdentityFetcher 28 } 29 30 // Endorse signs the given payload(ProposalResponsePayload bytes), and optionally mutates it. 31 // Returns: 32 // The Endorsement: A signature over the payload, and an identity that is used to verify the signature 33 // The payload that was given as input (could be modified within this function) 34 // Or error on failure 35 func (e *DefaultEndorsement) Endorse(prpBytes []byte, sp *peer.SignedProposal) (*peer.Endorsement, []byte, error) { 36 signer, err := e.SigningIdentityForRequest(sp) 37 if err != nil { 38 return nil, nil, errors.Wrap(err, "failed fetching signing identity") 39 } 40 // serialize the signing identity 41 identityBytes, err := signer.Serialize() 42 if err != nil { 43 return nil, nil, errors.Wrapf(err, "could not serialize the signing identity") 44 } 45 46 // sign the concatenation of the proposal response and the serialized endorser identity with this endorser's key 47 signature, err := signer.Sign(append(prpBytes, identityBytes...)) 48 if err != nil { 49 return nil, nil, errors.Wrapf(err, "could not sign the proposal response payload") 50 } 51 endorsement := &peer.Endorsement{Signature: signature, Endorser: identityBytes} 52 return endorsement, prpBytes, nil 53 } 54 55 // Init injects dependencies into the instance of the Plugin 56 func (e *DefaultEndorsement) Init(dependencies ...endorsement.Dependency) error { 57 for _, dep := range dependencies { 58 sIDFetcher, isSigningIdentityFetcher := dep.(identities.SigningIdentityFetcher) 59 if !isSigningIdentityFetcher { 60 continue 61 } 62 e.SigningIdentityFetcher = sIDFetcher 63 return nil 64 } 65 return errors.New("could not find SigningIdentityFetcher in dependencies") 66 }