github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/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  	"errors"
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/bccsp"
    23  	"github.com/hyperledger/fabric/bccsp/sw"
    24  )
    25  
    26  const (
    27  	// SoftwareBasedFactoryName is the name of the factory of the software-based BCCSP implementation
    28  	SoftwareBasedFactoryName = "SW"
    29  )
    30  
    31  // SWFactory is the factory of the software-based BCCSP.
    32  type SWFactory struct{}
    33  
    34  // Name returns the name of this factory
    35  func (f *SWFactory) Name() string {
    36  	return SoftwareBasedFactoryName
    37  }
    38  
    39  // Get returns an instance of BCCSP using Opts.
    40  func (f *SWFactory) Get(config *FactoryOpts) (bccsp.BCCSP, error) {
    41  	// Validate arguments
    42  	if config == nil || config.SwOpts == nil {
    43  		return nil, errors.New("Invalid config. It must not be nil.")
    44  	}
    45  
    46  	swOpts := config.SwOpts
    47  
    48  	var ks bccsp.KeyStore
    49  	if swOpts.Ephemeral == true {
    50  		ks = sw.NewDummyKeyStore()
    51  	} else if swOpts.FileKeystore != nil {
    52  		fks, err := sw.NewFileBasedKeyStore(nil, swOpts.FileKeystore.KeyStorePath, false)
    53  		if err != nil {
    54  			return nil, fmt.Errorf("Failed to initialize software key store: %s", err)
    55  		}
    56  		ks = fks
    57  	} else {
    58  		// Default to DummyKeystore
    59  		ks = sw.NewDummyKeyStore()
    60  	}
    61  
    62  	return sw.New(swOpts.SecLevel, swOpts.HashFamily, ks)
    63  }
    64  
    65  // SwOpts contains options for the SWFactory
    66  type SwOpts struct {
    67  	// Default algorithms when not specified (Deprecated?)
    68  	SecLevel   int    `mapstructure:"security" json:"security" yaml:"Security"`
    69  	HashFamily string `mapstructure:"hash" json:"hash" yaml:"Hash"`
    70  
    71  	// Keystore Options
    72  	Ephemeral     bool               `mapstructure:"tempkeys,omitempty" json:"tempkeys,omitempty"`
    73  	FileKeystore  *FileKeystoreOpts  `mapstructure:"filekeystore,omitempty" json:"filekeystore,omitempty" yaml:"FileKeyStore"`
    74  	DummyKeystore *DummyKeystoreOpts `mapstructure:"dummykeystore,omitempty" json:"dummykeystore,omitempty"`
    75  }
    76  
    77  // Pluggable Keystores, could add JKS, P12, etc..
    78  type FileKeystoreOpts struct {
    79  	KeyStorePath string `mapstructure:"keystore" yaml:"KeyStore"`
    80  }
    81  
    82  type DummyKeystoreOpts struct{}