github.com/true-sqn/fabric@v2.1.1+incompatible/bccsp/factory/nopkcs11.go (about)

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