Skip to content

Commit 8236a01

Browse files
committed
load fabric CA generated identity properly
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent bdeac16 commit 8236a01

File tree

1 file changed

+88
-6
lines changed
  • token/core/identity/msp/idemix

1 file changed

+88
-6
lines changed

token/core/identity/msp/idemix/lm.go

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ SPDX-License-Identifier: Apache-2.0
77
package idemix
88

99
import (
10+
"encoding/json"
1011
"os"
1112
"path/filepath"
1213
"sync"
1314

15+
"github.com/IBM/idemix/idemixmsp"
16+
1417
"github.com/hyperledger/fabric-protos-go/msp"
1518

1619
"github.com/IBM/idemix"
@@ -33,6 +36,10 @@ import (
3336

3437
var logger = flogging.MustGetLogger("token-sdk.msp.idemix")
3538

39+
const (
40+
SignerConfigFull = "SignerConfigFull"
41+
)
42+
3643
type PublicParametersWithIdemixSupport interface {
3744
IdemixCurve() math3.CurveID
3845
}
@@ -233,7 +240,7 @@ func (lm *LocalMembership) registerProvider(identity config.Identity, curveID ma
233240
if err != nil {
234241
logger.Debugf("failed reading idemix msp configuration from [%s]: [%s], try adding 'msp'...", identity.Path, err)
235242
// Try with "msp"
236-
conf, err = idemix2.GetLocalMspConfigWithType(filepath.Join(identity.Path, "msp"), nil, lm.mspID)
243+
conf, err = GetLocalMspConfigWithType(filepath.Join(identity.Path, "msp"), lm.mspID, lm.ignoreVerifyOnlyWallet)
237244
if err != nil {
238245
return errors.Wrapf(err, "failed reading idemix msp configuration from [%s] and with 'msp'", identity.Path)
239246
}
@@ -378,19 +385,32 @@ func (lm *LocalMembership) loadFromKVS() error {
378385
return nil
379386
}
380387

388+
func GetLocalMspConfigWithType(dir string, id string, ignoreVerifyOnlyWallet bool) (*msp.MSPConfig, error) {
389+
mspConfig, err := GetIdemixMspConfigWithType(dir, id, ignoreVerifyOnlyWallet)
390+
if err != nil {
391+
// load it using the fabric-ca format
392+
mspConfig2, err2 := GetFabricCAIdemixMspConfig(dir, id)
393+
if err2 != nil {
394+
return nil, errors.Wrapf(err2, "cannot get idemix msp config from [%s]: [%s]", dir, err)
395+
}
396+
mspConfig = mspConfig2
397+
}
398+
return mspConfig, nil
399+
}
400+
381401
// GetIdemixMspConfigWithType returns the configuration for the Idemix MSP of the specified type
382402
func GetIdemixMspConfigWithType(dir string, ID string, ignoreVerifyOnlyWallet bool) (*msp.MSPConfig, error) {
383-
ipkBytes, err := os.ReadFile(filepath.Join(dir, idemix.IdemixConfigDirMsp, idemix.IdemixConfigFileIssuerPublicKey))
403+
ipkBytes, err := ReadFile(filepath.Join(dir, idemix.IdemixConfigDirMsp, idemix.IdemixConfigFileIssuerPublicKey))
384404
if err != nil {
385405
return nil, errors.Wrapf(err, "failed to read issuer public key file")
386406
}
387407

388-
revocationPkBytes, err := os.ReadFile(filepath.Join(dir, idemix.IdemixConfigDirMsp, idemix.IdemixConfigFileRevocationPublicKey))
408+
revocationPkBytes, err := ReadFile(filepath.Join(dir, idemix.IdemixConfigDirMsp, idemix.IdemixConfigFileRevocationPublicKey))
389409
if err != nil {
390410
return nil, errors.Wrapf(err, "failed to read revocation public key file")
391411
}
392412

393-
idemixConfig := &msp.IdemixMSPConfig{
413+
idemixConfig := &idemixmsp.IdemixMSPConfig{
394414
Name: ID,
395415
Ipk: ipkBytes,
396416
RevocationPk: revocationPkBytes,
@@ -400,7 +420,7 @@ func GetIdemixMspConfigWithType(dir string, ID string, ignoreVerifyOnlyWallet bo
400420
if ignoreVerifyOnlyWallet {
401421
logger.Debugf("check the existence of SignerConfigFull")
402422
// check if `SignerConfigFull` exists, if yes, use that file
403-
path := filepath.Join(dir, idemix.IdemixConfigDirUser, "SignerConfigFull")
423+
path := filepath.Join(dir, idemix.IdemixConfigDirUser, SignerConfigFull)
404424
_, err := os.Stat(path)
405425
if err == nil {
406426
logger.Debugf("SignerConfigFull found, use it")
@@ -409,7 +429,7 @@ func GetIdemixMspConfigWithType(dir string, ID string, ignoreVerifyOnlyWallet bo
409429
}
410430
signerBytes, err := os.ReadFile(signerConfigPath)
411431
if err == nil {
412-
signerConfig := &msp.IdemixMSPSignerConfig{}
432+
signerConfig := &idemixmsp.IdemixMSPSignerConfig{}
413433
err = proto.Unmarshal(signerBytes, signerConfig)
414434
if err != nil {
415435
return nil, err
@@ -424,3 +444,65 @@ func GetIdemixMspConfigWithType(dir string, ID string, ignoreVerifyOnlyWallet bo
424444

425445
return &msp.MSPConfig{Config: confBytes, Type: int32(idemix.IDEMIX)}, nil
426446
}
447+
448+
// GetFabricCAIdemixMspConfig returns the configuration for the Idemix MSP generated by Fabric-CA
449+
func GetFabricCAIdemixMspConfig(dir string, ID string) (*msp.MSPConfig, error) {
450+
path := filepath.Join(dir, idemix2.ConfigFileIssuerPublicKey)
451+
ipkBytes, err := ReadFile(path)
452+
if err != nil {
453+
return nil, errors.Wrapf(err, "failed to read issuer public key file at [%s]", path)
454+
}
455+
456+
path = filepath.Join(dir, idemix2.IdemixConfigFileRevocationPublicKey)
457+
revocationPkBytes, err := ReadFile(path)
458+
if err != nil {
459+
return nil, errors.Wrapf(err, "failed to read revocation public key file at [%s]", path)
460+
}
461+
462+
idemixConfig := &idemixmsp.IdemixMSPConfig{
463+
Name: ID,
464+
Ipk: ipkBytes,
465+
RevocationPk: revocationPkBytes,
466+
}
467+
468+
path = filepath.Join(dir, idemix2.ConfigDirUser, idemix2.ConfigFileSigner)
469+
signerBytes, err := ReadFile(path)
470+
if err == nil {
471+
// signerBytes is a json structure, convert it to protobuf
472+
si := &idemix2.SignerConfig{}
473+
if err := json.Unmarshal(signerBytes, si); err != nil {
474+
return nil, errors.Wrapf(err, "failed to json unmarshal signer config read at [%s]", path)
475+
}
476+
477+
signerConfig := &idemixmsp.IdemixMSPSignerConfig{
478+
Cred: si.Cred,
479+
Sk: si.Sk,
480+
OrganizationalUnitIdentifier: si.OrganizationalUnitIdentifier,
481+
Role: int32(si.Role),
482+
EnrollmentId: si.EnrollmentID,
483+
CredentialRevocationInformation: si.CredentialRevocationInformation,
484+
RevocationHandle: si.RevocationHandle,
485+
}
486+
idemixConfig.Signer = signerConfig
487+
} else {
488+
if !os.IsNotExist(errors.Cause(err)) {
489+
return nil, errors.Wrapf(err, "failed to read the content of signer config at [%s]", path)
490+
}
491+
}
492+
493+
confBytes, err := proto.Marshal(idemixConfig)
494+
if err != nil {
495+
return nil, err
496+
}
497+
498+
return &msp.MSPConfig{Config: confBytes, Type: int32(idemix.IDEMIX)}, nil
499+
}
500+
501+
func ReadFile(file string) ([]byte, error) {
502+
fileCont, err := os.ReadFile(file)
503+
if err != nil {
504+
return nil, errors.Wrapf(err, "could not read file %s", file)
505+
}
506+
507+
return fileCont, nil
508+
}

0 commit comments

Comments
 (0)