-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (84 loc) · 2.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
"github.com/kmilodenisglez/github.template-chaincode.go/contracts/contractnameone"
"io/ioutil"
"log"
"os"
"strconv"
)
type serverConfig struct {
CCID string
Address string
}
func main() {
// See chaincode.env.example
config := serverConfig{
CCID: os.Getenv("CHAINCODE_ID"),
Address: os.Getenv("CHAINCODE_SERVER_ADDRESS"),
}
assetChaincode, err := contractapi.NewChaincode(&contractnameone.SmartContract{})
if err != nil {
log.Panicf("error create asset-transfer-basic chaincode: %s", err)
}
server := &shim.ChaincodeServer{
CCID: config.CCID,
Address: config.Address,
CC: assetChaincode,
TLSProps: getTLSProperties(),
}
if err := server.Start(); err != nil {
log.Panicf("error starting asset-transfer-basic chaincode: %s", err)
}
}
func getTLSProperties() shim.TLSProperties {
// Check if chaincode is TLS enabled
tlsDisabledStr := getEnvOrDefault("CHAINCODE_TLS_DISABLED", "true")
key := getEnvOrDefault("CHAINCODE_TLS_KEY", "")
cert := getEnvOrDefault("CHAINCODE_TLS_CERT", "")
clientCACert := getEnvOrDefault("CHAINCODE_CLIENT_CA_CERT", "")
// convert tlsDisabledStr to boolean
tlsDisabled := getBoolOrDefault(tlsDisabledStr, false)
var keyBytes, certBytes, clientCACertBytes []byte
var err error
if !tlsDisabled {
keyBytes, err = ioutil.ReadFile(key)
if err != nil {
log.Panicf("error while reading the crypto file: %s", err)
}
certBytes, err = ioutil.ReadFile(cert)
if err != nil {
log.Panicf("error while reading the crypto file: %s", err)
}
}
// Did not request for the peer cert verification
if clientCACert != "" {
clientCACertBytes, err = ioutil.ReadFile(clientCACert)
if err != nil {
log.Panicf("error while reading the crypto file: %s", err)
}
}
return shim.TLSProperties{
Disabled: tlsDisabled,
Key: keyBytes,
Cert: certBytes,
ClientCACerts: clientCACertBytes,
}
}
func getEnvOrDefault(env, defaultVal string) string {
value, ok := os.LookupEnv(env)
if !ok {
value = defaultVal
}
return value
}
// Note that the method returns default value if the string
// cannot be parsed!
func getBoolOrDefault(value string, defaultVal bool) bool {
parsed, err := strconv.ParseBool(value)
if err != nil {
return defaultVal
}
return parsed
}