github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/core/handlers/endorsement/builtin/default_endorsement.go (about)

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