From e9f9474da968acd366f0731f5f76eec9bb984f7c Mon Sep 17 00:00:00 2001 From: Angelo De Caro Date: Fri, 1 Sep 2023 15:46:57 +0200 Subject: [PATCH] minor refactoring Signed-off-by: Angelo De Caro --- .../fabric/core/generic/msp/idemix/config.go | 50 ++++++++++++++++--- .../core/generic/msp/idemix/provider_test.go | 4 +- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/platform/fabric/core/generic/msp/idemix/config.go b/platform/fabric/core/generic/msp/idemix/config.go index f778499bb..614acc8f5 100644 --- a/platform/fabric/core/generic/msp/idemix/config.go +++ b/platform/fabric/core/generic/msp/idemix/config.go @@ -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" ) @@ -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) @@ -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) @@ -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) } @@ -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{} diff --git a/platform/fabric/core/generic/msp/idemix/provider_test.go b/platform/fabric/core/generic/msp/idemix/provider_test.go index 4ec5a1f3c..88bdae53a 100644 --- a/platform/fabric/core/generic/msp/idemix/provider_test.go +++ b/platform/fabric/core/generic/msp/idemix/provider_test.go @@ -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) @@ -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)