-
Notifications
You must be signed in to change notification settings - Fork 91
/
fpcclient.go
117 lines (96 loc) · 2.95 KB
/
fpcclient.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package pkg
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
fpc "github.com/hyperledger/fabric-private-chaincode/client_sdk/go/pkg/gateway"
cfg "github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/gateway"
"github.com/hyperledger/fabric/common/flogging"
"github.com/pkg/errors"
)
var logger = flogging.MustGetLogger("sdk-test")
type Client struct {
contract fpc.Contract
}
func NewClient(config *Config) *Client {
return &Client{contract: newContract(config)}
}
func findSigningCert(mspConfigPath string) (string, error) {
p := filepath.Join(mspConfigPath, "signcerts")
files, err := ioutil.ReadDir(p)
if err != nil {
return "", errors.Wrapf(err, "error while searching pem in %s", mspConfigPath)
}
// return first pem we find
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".pem") {
return filepath.Join(p, f.Name()), nil
}
}
return "", errors.Errorf("cannot find pem in %s", mspConfigPath)
}
func populateWallet(wallet *gateway.Wallet, config *Config) error {
logger.Debugf("============ Populating wallet ============")
certPath, err := findSigningCert(config.CorePeerMSPConfigPath)
if err != nil {
return err
}
// read the certificate pem
cert, err := ioutil.ReadFile(filepath.Clean(certPath))
if err != nil {
return err
}
keyDir := filepath.Join(config.CorePeerMSPConfigPath, "keystore")
// there's a single file in this dir containing the private key
files, err := ioutil.ReadDir(keyDir)
if err != nil {
return err
}
if len(files) != 1 {
return fmt.Errorf("keystore folder should have contain one file")
}
keyPath := filepath.Join(keyDir, files[0].Name())
key, err := ioutil.ReadFile(filepath.Clean(keyPath))
if err != nil {
return err
}
identity := gateway.NewX509Identity(config.CorePeerLocalMSPID, string(cert), string(key))
return wallet.Put("appUser", identity)
}
func newContract(config *Config) fpc.Contract {
wallet := gateway.NewInMemoryWallet()
err := populateWallet(wallet, config)
if err != nil {
logger.Fatalf("Failed to populate wallet contents: %v", err)
}
gw, err := gateway.Connect(
gateway.WithConfig(cfg.FromFile(filepath.Clean(config.GatewayConfigPath))),
gateway.WithIdentity(wallet, "appUser"),
)
if err != nil {
logger.Fatalf("Failed to connect to gateway: %v", err)
}
defer gw.Close()
network, err := gw.GetNetwork(config.ChannelId)
if err != nil {
logger.Fatalf("Failed to get network: %v", err)
}
// Get FPC Contract
contract := fpc.GetContract(network, config.ChaincodeId)
return contract
}
func (c *Client) Call(function string, args ...string) string {
logger.Infof("--> Invoke FPC chaincode with %s", args)
result, err := c.contract.SubmitTransaction(function, args...)
if err != nil {
logger.Fatalf("Failed to Submit transaction: %v", err)
}
logger.Infof("--> Result: %s", string(result))
return string(result)
}