-
Notifications
You must be signed in to change notification settings - Fork 92
/
signupdate.go
146 lines (138 loc) · 3.82 KB
/
signupdate.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package channel
import (
"bytes"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite/bccsp/sw"
"github.com/hyperledger/fabric-sdk-go/pkg/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
mspimpl "github.com/hyperledger/fabric-sdk-go/pkg/msp"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
)
type signUpdateChannelCmd struct {
configPath string
channelName string
userName string
file string
mspID string
signatures []string
identity string
output string
}
func (c *signUpdateChannelCmd) validate() error {
return nil
}
type identity struct {
Cert Pem `json:"cert"`
Key Pem `json:"key"`
}
type Pem struct {
Pem string
}
func (c *signUpdateChannelCmd) run(out io.Writer) error {
configBackend := config.FromFile(c.configPath)
sdk, err := fabsdk.New(configBackend)
if err != nil {
return err
}
org1AdminClientContext := sdk.Context(
fabsdk.WithUser(c.userName),
fabsdk.WithOrg(c.mspID),
)
resClient, err := resmgmt.New(org1AdminClientContext)
if err != nil {
return err
}
updateEnvelopeBytes, err := ioutil.ReadFile(c.file)
if err != nil {
return err
}
configUpdateReader := bytes.NewReader(updateEnvelopeBytes)
sdkConfig, err := sdk.Config()
if err != nil {
return err
}
cryptoConfig := cryptosuite.ConfigFromBackend(sdkConfig)
cryptoSuite, err := sw.GetSuiteByConfig(cryptoConfig)
if err != nil {
return err
}
userStore := mspimpl.NewMemoryUserStore()
endpointConfig, err := fab.ConfigFromBackend(sdkConfig)
if err != nil {
return err
}
identityManager, err := mspimpl.NewIdentityManager(c.mspID, userStore, cryptoSuite, endpointConfig)
if err != nil {
return err
}
identityBytes, err := ioutil.ReadFile(c.identity)
if err != nil {
return err
}
id := &identity{}
err = yaml.Unmarshal(identityBytes, id)
if err != nil {
return err
}
signingIdentity, err := identityManager.CreateSigningIdentity(
msp.WithPrivateKey([]byte(id.Key.Pem)),
msp.WithCert([]byte(id.Cert.Pem)),
)
if err != nil {
return err
}
signature, err := resClient.CreateConfigSignatureFromReader(signingIdentity, configUpdateReader)
if err != nil {
return err
}
signatureBytes, err := proto.Marshal(signature)
if err != nil {
return err
}
if c.output != "" {
err = ioutil.WriteFile(c.output, signatureBytes, 0644)
if err != nil {
return err
}
} else {
_, err = fmt.Fprint(out, signatureBytes)
if err != nil {
return err
}
}
return nil
}
func newSignUpdateChannelCMD(stdOut io.Writer, stdErr io.Writer) *cobra.Command {
c := &signUpdateChannelCmd{}
cmd := &cobra.Command{
Use: "signupdate",
RunE: func(cmd *cobra.Command, args []string) error {
if err := c.validate(); err != nil {
return err
}
return c.run(stdOut)
},
}
persistentFlags := cmd.PersistentFlags()
persistentFlags.StringVarP(&c.mspID, "mspid", "", "", "MSP ID of the organization")
persistentFlags.StringVarP(&c.channelName, "channel", "", "", "Channel name")
persistentFlags.StringVarP(&c.configPath, "config", "", "", "Configuration file for the SDK")
persistentFlags.StringVarP(&c.identity, "identity", "", "", "Identity file")
persistentFlags.StringVarP(&c.file, "file", "f", "", "Config update file")
persistentFlags.StringVarP(&c.output, "output", "o", "", "Output signature")
persistentFlags.StringVarP(&c.userName, "user", "", "", "User name for the transaction")
cmd.MarkPersistentFlagRequired("mspid")
cmd.MarkPersistentFlagRequired("channel")
cmd.MarkPersistentFlagRequired("config")
cmd.MarkPersistentFlagRequired("user")
cmd.MarkPersistentFlagRequired("file")
return cmd
}