Skip to content

Commit

Permalink
fixed the dependency of identity file while signing update
Browse files Browse the repository at this point in the history
Signed-off-by: adityajoshi12 <adityaprakashjoshi1@gmail.com>
  • Loading branch information
adityajoshi12 committed Apr 8, 2024
1 parent f0f56f1 commit 72e6350
Showing 1 changed file with 61 additions and 44 deletions.
105 changes: 61 additions & 44 deletions kubectl-hlf/cmd/channel/signupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package channel
import (
"bytes"
"fmt"
mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"os"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
Expand All @@ -18,7 +20,6 @@ import (
"gopkg.in/yaml.v3"

"io"
"io/ioutil"
)

type signUpdateChannelCmd struct {
Expand Down Expand Up @@ -58,55 +59,71 @@ func (c *signUpdateChannelCmd) run(out io.Writer) error {
if err != nil {
return err
}
updateEnvelopeBytes, err := ioutil.ReadFile(c.file)
updateEnvelopeBytes, err := os.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

var signatureBytes []byte

// use identity file if provided
if c.identity != "" {
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 := os.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
}
} else {
mspClient, err := mspclient.New(org1AdminClientContext, mspclient.WithOrg(c.mspID))
if err != nil {
return err
}
usr, err := mspClient.GetSigningIdentity(c.userName)
signature, err := resClient.CreateConfigSignatureFromReader(usr, configUpdateReader)
signatureBytes, err = proto.Marshal(signature)
}

if c.output != "" {
err = ioutil.WriteFile(c.output, signatureBytes, 0644)
err = os.WriteFile(c.output, signatureBytes, 0644)
if err != nil {
return err
}
Expand Down

0 comments on commit 72e6350

Please sign in to comment.