-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change-set does the following: - Introduce an MSP factory that allows the creation of MSP instances based on passed options. An option specifies the MSP's type to be created and its version. This is to address backwards incompatibility by proving the ability to change the MSP version to be used. Change-Id: I7dcfdd783260d98896487147e50cd0e8ea62971c Signed-off-by: Angelo De Caro <[email protected]>
- Loading branch information
Showing
12 changed files
with
138 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package msp | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type MSPVersion int | ||
|
||
const ( | ||
MSPv1_0 = iota | ||
MSPv1_1 | ||
) | ||
|
||
// NewOpts represent | ||
type NewOpts interface { | ||
// GetVersion returns the MSP's version to be instantiated | ||
GetVersion() MSPVersion | ||
} | ||
|
||
// NewBaseOpts is the default base type for all MSP instantiation Opts | ||
type NewBaseOpts struct { | ||
Version MSPVersion | ||
} | ||
|
||
func (o *NewBaseOpts) GetVersion() MSPVersion { | ||
return o.Version | ||
} | ||
|
||
// BCCSPNewOpts contains the options to instantiate a new BCCSP-based (X509) MSP | ||
type BCCSPNewOpts struct { | ||
NewBaseOpts | ||
} | ||
|
||
// IdemixNewOpts contains the options to instantiate a new Idemix-based MSP | ||
type IdemixNewOpts struct { | ||
NewBaseOpts | ||
} | ||
|
||
// New create a new MSP instance depending on the passed Opts | ||
func New(opts NewOpts) (MSP, error) { | ||
switch opts.(type) { | ||
case *BCCSPNewOpts: | ||
switch opts.GetVersion() { | ||
case MSPv1_0: | ||
return newBccspMsp() | ||
default: | ||
return nil, errors.Errorf("Invalid *BCCSPNewOpts. Version not recognized [%v]", opts.GetVersion()) | ||
} | ||
case *IdemixNewOpts: | ||
switch opts.GetVersion() { | ||
case MSPv1_0: | ||
return newIdemixMsp() | ||
default: | ||
return nil, errors.Errorf("Invalid *IdemixNewOpts. Version not recognized [%v]", opts.GetVersion()) | ||
} | ||
default: | ||
return nil, errors.Errorf("Invalid msp.NewOpts instance. It must be either *BCCSPNewOpts or *IdemixNewOpts. It was [%v]", opts) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package msp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewInvalidOpts(t *testing.T) { | ||
i, err := New(nil) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "Invalid msp.NewOpts instance. It must be either *BCCSPNewOpts or *IdemixNewOpts. It was [<nil>]") | ||
assert.Nil(t, i) | ||
|
||
i, err = New(&BCCSPNewOpts{NewBaseOpts{Version: -1}}) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "Invalid *BCCSPNewOpts. Version not recognized [-1]") | ||
assert.Nil(t, i) | ||
|
||
i, err = New(&IdemixNewOpts{NewBaseOpts{Version: -1}}) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "Invalid *IdemixNewOpts. Version not recognized [-1]") | ||
assert.Nil(t, i) | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
i, err := New(&BCCSPNewOpts{NewBaseOpts{Version: MSPv1_0}}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, i) | ||
|
||
i, err = New(&IdemixNewOpts{NewBaseOpts{Version: MSPv1_0}}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters