github.com/yimialmonte/fabric@v2.1.1+incompatible/core/handlers/endorsement/plugin/plugin.go (about)

     1  /*
     2  Copyright IBM Corp. 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  	"github.com/hyperledger/fabric-protos-go/peer"
    14  	. "github.com/hyperledger/fabric/core/handlers/endorsement/api"
    15  	. "github.com/hyperledger/fabric/core/handlers/endorsement/api/identities"
    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  
    27  // New returns an endorsement plugin that behaves as the default endorsement system chaincode
    28  func (*DefaultEndorsementFactory) New() Plugin {
    29  	return &DefaultEndorsement{}
    30  }
    31  
    32  // DefaultEndorsement is an endorsement plugin that behaves as the default endorsement system chaincode
    33  type DefaultEndorsement struct {
    34  	SigningIdentityFetcher
    35  }
    36  
    37  // Endorse signs the given payload(ProposalResponsePayload bytes), and optionally mutates it.
    38  // Returns:
    39  // The Endorsement: A signature over the payload, and an identity that is used to verify the signature
    40  // The payload that was given as input (could be modified within this function)
    41  // Or error on failure
    42  func (e *DefaultEndorsement) Endorse(prpBytes []byte, sp *peer.SignedProposal) (*peer.Endorsement, []byte, error) {
    43  	signer, err := e.SigningIdentityForRequest(sp)
    44  	if err != nil {
    45  		return nil, nil, errors.New(fmt.Sprintf("failed fetching signing identity: %v", err))
    46  	}
    47  	// serialize the signing identity
    48  	identityBytes, err := signer.Serialize()
    49  	if err != nil {
    50  		return nil, nil, errors.New(fmt.Sprintf("could not serialize the signing identity: %v", err))
    51  	}
    52  
    53  	// sign the concatenation of the proposal response and the serialized endorser identity with this endorser's key
    54  	signature, err := signer.Sign(append(prpBytes, identityBytes...))
    55  	if err != nil {
    56  		return nil, nil, errors.New(fmt.Sprintf("could not sign the proposal response payload: %v", err))
    57  	}
    58  	endorsement := &peer.Endorsement{Signature: signature, Endorser: identityBytes}
    59  	return endorsement, prpBytes, nil
    60  }
    61  
    62  // Init injects dependencies into the instance of the Plugin
    63  func (e *DefaultEndorsement) Init(dependencies ...Dependency) error {
    64  	for _, dep := range dependencies {
    65  		sIDFetcher, isSigningIdentityFetcher := dep.(SigningIdentityFetcher)
    66  		if !isSigningIdentityFetcher {
    67  			continue
    68  		}
    69  		e.SigningIdentityFetcher = sIDFetcher
    70  		return nil
    71  	}
    72  	return errors.New("could not find SigningIdentityFetcher in dependencies")
    73  }
    74  
    75  // NewPluginFactory is the function ran by the plugin infrastructure to create an endorsement plugin factory.
    76  func NewPluginFactory() PluginFactory {
    77  	return &DefaultEndorsementFactory{}
    78  }