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