github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/bccsp/factory/nopkcs11.go (about) 1 // +build nopkcs11 2 3 /* 4 Copyright IBM Corp. 2017 All Rights Reserved. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 package factory 19 20 import ( 21 "fmt" 22 23 "github.com/hyperledger/fabric/bccsp" 24 ) 25 26 type FactoryOpts struct { 27 ProviderName string `mapstructure:"default" json:"default" yaml:"Default"` 28 SwOpts *SwOpts `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"` 29 } 30 31 // InitFactories must be called before using factory interfaces 32 // It is acceptable to call with config = nil, in which case 33 // some defaults will get used 34 // Error is returned only if defaultBCCSP cannot be found 35 func InitFactories(config *FactoryOpts) error { 36 factoriesInitOnce.Do(func() { 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 // Initialize factories map 51 bccspMap = make(map[string]bccsp.BCCSP) 52 53 // Software-Based BCCSP 54 if config.SwOpts != nil { 55 f := &SWFactory{} 56 err := initBCCSP(f, config) 57 if err != nil { 58 factoriesInitError = fmt.Errorf("[%s]", err) 59 } 60 } 61 62 var ok bool 63 defaultBCCSP, ok = bccspMap[config.ProviderName] 64 if !ok { 65 factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName) 66 } 67 }) 68 69 return factoriesInitError 70 } 71 72 // GetBCCSPFromOpts returns a BCCSP created according to the options passed in input. 73 func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) { 74 var f BCCSPFactory 75 switch config.ProviderName { 76 case "SW": 77 f = &SWFactory{} 78 default: 79 return nil, fmt.Errorf("Could not find BCCSP, no '%s' provider", config.ProviderName) 80 } 81 82 csp, err := f.Get(config) 83 if err != nil { 84 return nil, fmt.Errorf("Could not initialize BCCSP %s [%s]", f.Name(), err) 85 } 86 return csp, nil 87 }