github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric/bccsp/factory/nopkcs11.go (about)

     1  //go:build !pkcs11
     2  // +build !pkcs11
     3  
     4  /*
     5  Copyright IBM Corp. All Rights Reserved.
     6  
     7  SPDX-License-Identifier: Apache-2.0
     8  */
     9  
    10  package factory
    11  
    12  import (
    13  	"github.com/hellobchain/third_party/hyperledger/fabric/bccsp"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  const pkcs11Enabled = false
    18  
    19  // FactoryOpts holds configuration information used to initialize factory implementations
    20  type FactoryOpts struct {
    21  	ProviderName string      `mapstructure:"default" json:"default" yaml:"Default"`
    22  	SwOpts       *SwOpts     `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"`
    23  	PluginOpts   *PluginOpts `mapstructure:"PLUGIN,omitempty" json:"PLUGIN,omitempty" yaml:"PluginOpts"`
    24  }
    25  
    26  // InitFactories must be called before using factory interfaces
    27  // It is acceptable to call with config = nil, in which case
    28  // some defaults will get used
    29  // Error is returned only if defaultBCCSP cannot be found
    30  func InitFactories(config *FactoryOpts) error {
    31  	factoriesInitOnce.Do(func() {
    32  		factoriesInitError = initFactories(config)
    33  	})
    34  
    35  	return factoriesInitError
    36  }
    37  
    38  func initFactories(config *FactoryOpts) error {
    39  	// Take some precautions on default opts
    40  	if config == nil {
    41  		config = GetDefaultOpts()
    42  	}
    43  
    44  	if config.ProviderName == "" {
    45  		config.ProviderName = "SW"
    46  	}
    47  
    48  	if config.SwOpts == nil {
    49  		config.SwOpts = GetDefaultOpts().SwOpts
    50  	}
    51  
    52  	// Initialize factories map
    53  	bccspMap = make(map[string]bccsp.BCCSP)
    54  
    55  	// Software-Based BCCSP
    56  	if config.ProviderName == "SW" && config.SwOpts != nil {
    57  		f := &SWFactory{}
    58  		err := initBCCSP(f, config)
    59  		if err != nil {
    60  			return errors.Wrapf(err, "Failed initializing BCCSP")
    61  		}
    62  	}
    63  
    64  	// BCCSP Plugin
    65  	if config.ProviderName == "PLUGIN" && config.PluginOpts != nil {
    66  		f := &PluginFactory{}
    67  		err := initBCCSP(f, config)
    68  		if err != nil {
    69  			return errors.Wrapf(err, "Failed initializing PLUGIN.BCCSP")
    70  		}
    71  	}
    72  
    73  	var ok bool
    74  	defaultBCCSP, ok = bccspMap[config.ProviderName]
    75  	if !ok {
    76  		return errors.Errorf("Could not find default `%s` BCCSP", config.ProviderName)
    77  	}
    78  	return nil
    79  }
    80  
    81  // GetBCCSPFromOpts returns a BCCSP created according to the options passed in input.
    82  func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) {
    83  	var f BCCSPFactory
    84  	switch config.ProviderName {
    85  	case "SW":
    86  		f = &SWFactory{}
    87  	case "PLUGIN":
    88  		f = &PluginFactory{}
    89  	default:
    90  		return nil, errors.Errorf("Could not find BCCSP, no '%s' provider", config.ProviderName)
    91  	}
    92  
    93  	csp, err := f.Get(config)
    94  	if err != nil {
    95  		return nil, errors.Wrapf(err, "Could not initialize BCCSP %s", f.Name())
    96  	}
    97  	return csp, nil
    98  }