Skip to content

Commit 8c97f46

Browse files
committed
Use protobufs to serialize identities
This change-set replaces the ASN.1 based marshalling of identities with protobuf. Change-Id: I81e1e4e99f18a8d355dfb36551922a0277da9d75 Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent 0eadb03 commit 8c97f46

6 files changed

+102
-21
lines changed

msp/identities.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import (
2323

2424
"encoding/pem"
2525

26-
"encoding/asn1"
27-
2826
"errors"
2927

28+
"github.com/golang/protobuf/proto"
3029
"github.com/hyperledger/fabric/core/crypto/bccsp"
3130
"github.com/hyperledger/fabric/core/crypto/bccsp/signer"
3231
)
@@ -115,8 +114,8 @@ func (id *identity) Serialize() ([]byte, error) {
115114
}
116115

117116
// We serialize identities by prepending the MSPID and appending the ASN.1 DER content of the cert
118-
sId := SerializedIdentity{Mspid: id.id.Mspid, IdBytes: pemBytes}
119-
idBytes, err := asn1.Marshal(sId)
117+
sId := &SerializedIdentity{Mspid: id.id.Mspid, IdBytes: pemBytes}
118+
idBytes, err := proto.Marshal(sId)
120119
if err != nil {
121120
return nil, fmt.Errorf("Could not marshal a SerializedIdentity structure for identity %s, err %s", id.id, err)
122121
}

msp/identities.pb.go

+63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

msp/identities.proto

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
syntax = "proto3";
18+
19+
option go_package = "github.com/hyperledger/fabric/msp";
20+
21+
package msp;
22+
23+
// This struct represents an Identity
24+
// (with its MSP identifier) to be used
25+
// to serialize it and deserialize it
26+
message SerializedIdentity {
27+
// The identifier of the associated membership service provider
28+
string Mspid = 1;
29+
30+
// the Identity, serialized according to the rules of its MPS
31+
bytes IdBytes = 2;
32+
}

msp/msp.go

-11
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,3 @@ const (
223223
FABRIC ProviderType = iota // MSP is of FABRIC type
224224
OTHER // MSP is of OTHER TYPE
225225
)
226-
227-
// This struct represents an Identity
228-
// (with its MSP identifier) to be used
229-
// to serialize it and deserialize it
230-
type SerializedIdentity struct {
231-
// The identifier of the associated membership service provider
232-
Mspid string
233-
234-
// the Identity, serialized according to the rules of its MPS
235-
IdBytes []byte
236-
}

msp/mspimpl.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import (
2525

2626
"encoding/json"
2727

28-
"encoding/asn1"
29-
28+
"github.com/golang/protobuf/proto"
3029
"github.com/hyperledger/fabric/core/crypto/bccsp"
3130
"github.com/hyperledger/fabric/core/crypto/bccsp/signer"
3231
"github.com/hyperledger/fabric/core/crypto/bccsp/sw"
@@ -263,7 +262,7 @@ func (msp *bccspmsp) DeserializeIdentity(serializedID []byte) (Identity, error)
263262

264263
// We first deserialize to a SerializedIdentity to get the MSP ID
265264
sId := &SerializedIdentity{}
266-
_, err := asn1.Unmarshal(serializedID, sId)
265+
err := proto.Unmarshal(serializedID, sId)
267266
if err != nil {
268267
return nil, fmt.Errorf("Could not deserialize a SerializedIdentity, err %s", err)
269268
}

msp/mspmgrimpl.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ package msp
1919
import (
2020
"fmt"
2121

22-
"encoding/asn1"
23-
22+
"github.com/golang/protobuf/proto"
2423
"github.com/hyperledger/fabric/protos/msp"
2524
"github.com/op/go-logging"
2625
)
@@ -106,7 +105,7 @@ func (mgr *mspManagerImpl) GetMSPs() (map[string]MSP, error) {
106105
func (mgr *mspManagerImpl) DeserializeIdentity(serializedID []byte) (Identity, error) {
107106
// We first deserialize to a SerializedIdentity to get the MSP ID
108107
sId := &SerializedIdentity{}
109-
_, err := asn1.Unmarshal(serializedID, sId)
108+
err := proto.Unmarshal(serializedID, sId)
110109
if err != nil {
111110
return nil, fmt.Errorf("Could not deserialize a SerializedIdentity, err %s", err)
112111
}

0 commit comments

Comments
 (0)