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

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package factory
    17  
    18  import (
    19  	"github.com/hyperledger/fabric/bccsp"
    20  	"github.com/hyperledger/fabric/bccsp/sw"
    21  	"github.com/pkg/errors"
    22  )
    23  
    24  const (
    25  	// SoftwareBasedFactoryName is the name of the factory of the software-based BCCSP implementation
    26  	SoftwareBasedFactoryName = "SW"
    27  )
    28  
    29  // SWFactory is the factory of the software-based BCCSP.
    30  type SWFactory struct{}
    31  
    32  // Name returns the name of this factory
    33  func (f *SWFactory) Name() string {
    34  	return SoftwareBasedFactoryName
    35  }
    36  
    37  // Get returns an instance of BCCSP using Opts.
    38  func (f *SWFactory) Get(config *FactoryOpts) (bccsp.BCCSP, error) {
    39  	// Validate arguments
    40  	if config == nil || config.SwOpts == nil {
    41  		return nil, errors.New("Invalid config. It must not be nil.")
    42  	}
    43  
    44  	swOpts := config.SwOpts
    45  
    46  	var ks bccsp.KeyStore
    47  	switch {
    48  	case swOpts.Ephemeral:
    49  		ks = sw.NewDummyKeyStore()
    50  	case swOpts.FileKeystore != nil:
    51  		fks, err := sw.NewFileBasedKeyStore(nil, swOpts.FileKeystore.KeyStorePath, false)
    52  		if err != nil {
    53  			return nil, errors.Wrapf(err, "Failed to initialize software key store")
    54  		}
    55  		ks = fks
    56  	case swOpts.InmemKeystore != nil:
    57  		ks = sw.NewInMemoryKeyStore()
    58  	default:
    59  		// Default to ephemeral key store
    60  		ks = sw.NewDummyKeyStore()
    61  	}
    62  
    63  	return sw.NewWithParams(swOpts.SecLevel, swOpts.HashFamily, ks)
    64  }
    65  
    66  // SwOpts contains options for the SWFactory
    67  type SwOpts struct {
    68  	// Default algorithms when not specified (Deprecated?)
    69  	SecLevel   int    `mapstructure:"security" json:"security" yaml:"Security"`
    70  	HashFamily string `mapstructure:"hash" json:"hash" yaml:"Hash"`
    71  
    72  	// Keystore Options
    73  	Ephemeral     bool               `mapstructure:"tempkeys,omitempty" json:"tempkeys,omitempty"`
    74  	FileKeystore  *FileKeystoreOpts  `mapstructure:"filekeystore,omitempty" json:"filekeystore,omitempty" yaml:"FileKeyStore"`
    75  	DummyKeystore *DummyKeystoreOpts `mapstructure:"dummykeystore,omitempty" json:"dummykeystore,omitempty"`
    76  	InmemKeystore *InmemKeystoreOpts `mapstructure:"inmemkeystore,omitempty" json:"inmemkeystore,omitempty"`
    77  }
    78  
    79  // Pluggable Keystores, could add JKS, P12, etc..
    80  type FileKeystoreOpts struct {
    81  	KeyStorePath string `mapstructure:"keystore" yaml:"KeyStore"`
    82  }
    83  
    84  type DummyKeystoreOpts struct{}
    85  
    86  // InmemKeystoreOpts - empty, as there is no config for the in-memory keystore
    87  type InmemKeystoreOpts struct{}