|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package common |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "strconv" |
| 13 | + |
| 14 | + pkgFpc "github.com/hyperledger/fabric-private-chaincode/samples/application/ccapi/fpcUtils" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + defaultFpcConfig *pkgFpc.Config |
| 19 | +) |
| 20 | + |
| 21 | +func InitFpcConfig() { |
| 22 | + |
| 23 | + getStrEnv := func(key string) string { |
| 24 | + val := os.Getenv(key) |
| 25 | + if val == "" { |
| 26 | + panic(fmt.Sprintf("%s not set", key)) |
| 27 | + } |
| 28 | + return val |
| 29 | + } |
| 30 | + |
| 31 | + getBoolEnv := func(key string) bool { |
| 32 | + val := getStrEnv(key) |
| 33 | + ret, err := strconv.ParseBool(val) |
| 34 | + if err != nil { |
| 35 | + if val == "" { |
| 36 | + panic(fmt.Sprintf("invalid bool value for %s", key)) |
| 37 | + } |
| 38 | + } |
| 39 | + return ret |
| 40 | + } |
| 41 | + |
| 42 | + defaultFpcConfig = &pkgFpc.Config{ |
| 43 | + CorePeerAddress: getStrEnv("CORE_PEER_ADDRESS"), |
| 44 | + CorePeerId: getStrEnv("CORE_PEER_ID"), |
| 45 | + CorePeerOrgName: getStrEnv("CORE_PEER_ORG_NAME"), |
| 46 | + CorePeerLocalMSPID: getStrEnv("CORE_PEER_LOCALMSPID"), |
| 47 | + CorePeerMSPConfigPath: getStrEnv("CORE_PEER_MSPCONFIGPATH"), |
| 48 | + CorePeerTLSCertFile: getStrEnv("CORE_PEER_TLS_CERT_FILE"), |
| 49 | + CorePeerTLSEnabled: getBoolEnv("CORE_PEER_TLS_ENABLED"), |
| 50 | + CorePeerTLSKeyFile: getStrEnv("CORE_PEER_TLS_KEY_FILE"), |
| 51 | + CorePeerTLSRootCertFile: getStrEnv("CORE_PEER_TLS_ROOTCERT_FILE"), |
| 52 | + OrdererCA: getStrEnv("ORDERER_CA"), |
| 53 | + ChaincodeId: getStrEnv("CCNAME"), |
| 54 | + ChannelId: getStrEnv("CHANNEL"), |
| 55 | + GatewayConfigPath: getStrEnv("GATEWAY_CONFIG"), |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +func NewDefaultFpcClient() *pkgFpc.Client { |
| 61 | + return pkgFpc.NewClient(defaultFpcConfig) |
| 62 | +} |
| 63 | + |
| 64 | +func NewFpcClient(channelName string, chaincodeName string) *pkgFpc.Client { |
| 65 | + fpcConfig := defaultFpcConfig |
| 66 | + fpcConfig.ChannelId = channelName |
| 67 | + fpcConfig.ChaincodeId = chaincodeName |
| 68 | + return pkgFpc.NewClient(fpcConfig) |
| 69 | +} |
0 commit comments