Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro authored and alexandrosfilios committed Oct 6, 2023
1 parent ca86b6d commit e9f9474
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
50 changes: 43 additions & 7 deletions platform/fabric/core/generic/msp/idemix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"os"
"path/filepath"

"github.com/IBM/idemix"
im "github.com/IBM/idemix/idemixmsp"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/proto"
m "github.com/hyperledger/fabric-protos-go/msp"
"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/msp"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ const (
ConfigFileSigner = "SignerConfig"
)

func readFile(file string) ([]byte, error) {
func ReadFile(file string) ([]byte, error) {
fileCont, err := os.ReadFile(file)
if err != nil {
return nil, errors.Wrapf(err, "could not read file %s", file)
Expand All @@ -55,8 +55,8 @@ func readFile(file string) ([]byte, error) {
return fileCont, nil
}

func GetLocalMspConfigWithType(dir string, bccspConfig *factory.FactoryOpts, id string) (*m.MSPConfig, error) {
mspConfig, err := msp.GetLocalMspConfigWithType(dir, bccspConfig, id, msp.ProviderTypeToString(msp.IDEMIX))
func GetLocalMspConfigWithType(dir string, id string) (*m.MSPConfig, error) {
mspConfig, err := GetIdemixMspConfigWithType(dir, id)
if err != nil {
// load it using the fabric-ca format
mspConfig2, err2 := GetFabricCAIdemixMspConfig(dir, id)
Expand All @@ -68,16 +68,52 @@ func GetLocalMspConfigWithType(dir string, bccspConfig *factory.FactoryOpts, id
return mspConfig, nil
}

// GetIdemixMspConfigWithType returns the configuration for the Idemix MSP of the specified type
func GetIdemixMspConfigWithType(dir string, ID string) (*m.MSPConfig, error) {
ipkBytes, err := ReadFile(filepath.Join(dir, idemix.IdemixConfigDirMsp, idemix.IdemixConfigFileIssuerPublicKey))
if err != nil {
return nil, errors.Wrapf(err, "failed to read issuer public key file")
}

revocationPkBytes, err := ReadFile(filepath.Join(dir, idemix.IdemixConfigDirMsp, idemix.IdemixConfigFileRevocationPublicKey))
if err != nil {
return nil, errors.Wrapf(err, "failed to read revocation public key file")
}

idemixConfig := &im.IdemixMSPConfig{
Name: ID,
Ipk: ipkBytes,
RevocationPk: revocationPkBytes,
}

signerBytes, err := ReadFile(filepath.Join(dir, idemix.IdemixConfigDirUser, idemix.IdemixConfigFileSigner))
if err == nil {
signerConfig := &im.IdemixMSPSignerConfig{}
err = proto.Unmarshal(signerBytes, signerConfig)
if err != nil {
return nil, err
}
idemixConfig.Signer = signerConfig
}

confBytes, err := proto.Marshal(idemixConfig)
if err != nil {
return nil, err
}

return &m.MSPConfig{Config: confBytes, Type: int32(idemix.IDEMIX)}, nil
}

// GetFabricCAIdemixMspConfig returns the configuration for the Idemix MSP generated by Fabric-CA
func GetFabricCAIdemixMspConfig(dir string, ID string) (*m.MSPConfig, error) {
path := filepath.Join(dir, ConfigFileIssuerPublicKey)
ipkBytes, err := readFile(path)
ipkBytes, err := ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read issuer public key file at [%s]", path)
}

path = filepath.Join(dir, IdemixConfigFileRevocationPublicKey)
revocationPkBytes, err := readFile(path)
revocationPkBytes, err := ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read revocation public key file at [%s]", path)
}
Expand All @@ -89,7 +125,7 @@ func GetFabricCAIdemixMspConfig(dir string, ID string) (*m.MSPConfig, error) {
}

path = filepath.Join(dir, ConfigDirUser, ConfigFileSigner)
signerBytes, err := readFile(path)
signerBytes, err := ReadFile(path)
if err == nil {
// signerBytes is a json structure, convert it to protobuf
si := &SignerConfig{}
Expand Down
4 changes: 2 additions & 2 deletions platform/fabric/core/generic/msp/idemix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func TestIdentityFromFabricCA(t *testing.T) {
sigService := sig2.NewSignService(registry, nil, kvss)
assert.NoError(t, registry.RegisterService(sigService))

config, err := idemix2.GetLocalMspConfigWithType("./testdata/charlie.ExtraId2", nil, "charlie.ExtraId2")
config, err := idemix2.GetLocalMspConfigWithType("./testdata/charlie.ExtraId2", "charlie.ExtraId2")
assert.NoError(t, err)

p, err := idemix2.NewProviderWithSigTypeAncCurve(config, registry, bccsp.Standard, math.BN254)
Expand Down Expand Up @@ -355,7 +355,7 @@ func TestIdentityFromFabricCAWithEidRhNymPolicy(t *testing.T) {
sigService := sig2.NewSignService(registry, nil, kvss)
assert.NoError(t, registry.RegisterService(sigService))

config, err := idemix2.GetLocalMspConfigWithType("./testdata/charlie.ExtraId2", nil, "charlie.ExtraId2")
config, err := idemix2.GetLocalMspConfigWithType("./testdata/charlie.ExtraId2", "charlie.ExtraId2")
assert.NoError(t, err)

p, err := idemix2.NewProviderWithSigTypeAncCurve(config, registry, bccsp.EidNymRhNym, math.BN254)
Expand Down

0 comments on commit e9f9474

Please sign in to comment.