@@ -15,8 +15,7 @@ import (
15
15
16
16
"github.com/golang/protobuf/proto"
17
17
cb "github.com/hyperledger/fabric-protos-go/common"
18
- "github.com/hyperledger/fabric-protos-go/msp"
19
- "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protoutil"
18
+ mb "github.com/hyperledger/fabric-protos-go/msp"
20
19
)
21
20
22
21
// AcceptAllPolicy always evaluates to true
@@ -32,26 +31,18 @@ var RejectAllPolicy *cb.SignaturePolicyEnvelope
32
31
var MarshaledRejectAllPolicy []byte
33
32
34
33
func init () {
35
- var err error
36
-
37
34
AcceptAllPolicy = Envelope (NOutOf (0 , []* cb.SignaturePolicy {}), [][]byte {})
38
- MarshaledAcceptAllPolicy , err = proto .Marshal (AcceptAllPolicy )
39
- if err != nil {
40
- panic ("Error marshaling trueEnvelope" )
41
- }
35
+ MarshaledAcceptAllPolicy = protoMarshalOrPanic (AcceptAllPolicy )
42
36
43
37
RejectAllPolicy = Envelope (NOutOf (1 , []* cb.SignaturePolicy {}), [][]byte {})
44
- MarshaledRejectAllPolicy , err = proto .Marshal (RejectAllPolicy )
45
- if err != nil {
46
- panic ("Error marshaling falseEnvelope" )
47
- }
38
+ MarshaledRejectAllPolicy = protoMarshalOrPanic (RejectAllPolicy )
48
39
}
49
40
50
41
// Envelope builds an envelope message embedding a SignaturePolicy
51
42
func Envelope (policy * cb.SignaturePolicy , identities [][]byte ) * cb.SignaturePolicyEnvelope {
52
- ids := make ([]* msp .MSPPrincipal , len (identities ))
43
+ ids := make ([]* mb .MSPPrincipal , len (identities ))
53
44
for i := range ids {
54
- ids [i ] = & msp .MSPPrincipal {PrincipalClassification : msp .MSPPrincipal_IDENTITY , Principal : identities [i ]}
45
+ ids [i ] = & mb .MSPPrincipal {PrincipalClassification : mb .MSPPrincipal_IDENTITY , Principal : identities [i ]}
55
46
}
56
47
57
48
return & cb.SignaturePolicyEnvelope {
@@ -73,34 +64,34 @@ func SignedBy(index int32) *cb.SignaturePolicy {
73
64
// SignedByMspMember creates a SignaturePolicyEnvelope
74
65
// requiring 1 signature from any member of the specified MSP
75
66
func SignedByMspMember (mspId string ) * cb.SignaturePolicyEnvelope {
76
- return signedByFabricEntity (mspId , msp .MSPRole_MEMBER )
67
+ return signedByFabricEntity (mspId , mb .MSPRole_MEMBER )
77
68
}
78
69
79
70
// SignedByMspClient creates a SignaturePolicyEnvelope
80
71
// requiring 1 signature from any client of the specified MSP
81
72
func SignedByMspClient (mspId string ) * cb.SignaturePolicyEnvelope {
82
- return signedByFabricEntity (mspId , msp .MSPRole_CLIENT )
73
+ return signedByFabricEntity (mspId , mb .MSPRole_CLIENT )
83
74
}
84
75
85
76
// SignedByMspPeer creates a SignaturePolicyEnvelope
86
77
// requiring 1 signature from any peer of the specified MSP
87
78
func SignedByMspPeer (mspId string ) * cb.SignaturePolicyEnvelope {
88
- return signedByFabricEntity (mspId , msp .MSPRole_PEER )
79
+ return signedByFabricEntity (mspId , mb .MSPRole_PEER )
89
80
}
90
81
91
82
// SignedByFabricEntity creates a SignaturePolicyEnvelope
92
83
// requiring 1 signature from any fabric entity, having the passed role, of the specified MSP
93
- func signedByFabricEntity (mspId string , role msp .MSPRole_MSPRoleType ) * cb.SignaturePolicyEnvelope {
84
+ func signedByFabricEntity (mspId string , role mb .MSPRole_MSPRoleType ) * cb.SignaturePolicyEnvelope {
94
85
// specify the principal: it's a member of the msp we just found
95
- principal := & msp .MSPPrincipal {
96
- PrincipalClassification : msp .MSPPrincipal_ROLE ,
97
- Principal : protoutil . MarshalOrPanic ( & msp .MSPRole {Role : role , MspIdentifier : mspId })}
86
+ principal := & mb .MSPPrincipal {
87
+ PrincipalClassification : mb .MSPPrincipal_ROLE ,
88
+ Principal : protoMarshalOrPanic ( & mb .MSPRole {Role : role , MspIdentifier : mspId })}
98
89
99
90
// create the policy: it requires exactly 1 signature from the first (and only) principal
100
91
p := & cb.SignaturePolicyEnvelope {
101
92
Version : 0 ,
102
93
Rule : NOutOf (1 , []* cb.SignaturePolicy {SignedBy (0 )}),
103
- Identities : []* msp .MSPPrincipal {principal },
94
+ Identities : []* mb .MSPPrincipal {principal },
104
95
}
105
96
106
97
return p
@@ -110,35 +101,36 @@ func signedByFabricEntity(mspId string, role msp.MSPRole_MSPRoleType) *cb.Signat
110
101
// requiring 1 signature from any admin of the specified MSP
111
102
func SignedByMspAdmin (mspId string ) * cb.SignaturePolicyEnvelope {
112
103
// specify the principal: it's a member of the msp we just found
113
- principal := & msp .MSPPrincipal {
114
- PrincipalClassification : msp .MSPPrincipal_ROLE ,
115
- Principal : protoutil . MarshalOrPanic ( & msp .MSPRole {Role : msp .MSPRole_ADMIN , MspIdentifier : mspId })}
104
+ principal := & mb .MSPPrincipal {
105
+ PrincipalClassification : mb .MSPPrincipal_ROLE ,
106
+ Principal : protoMarshalOrPanic ( & mb .MSPRole {Role : mb .MSPRole_ADMIN , MspIdentifier : mspId })}
116
107
117
108
// create the policy: it requires exactly 1 signature from the first (and only) principal
118
109
p := & cb.SignaturePolicyEnvelope {
119
110
Version : 0 ,
120
111
Rule : NOutOf (1 , []* cb.SignaturePolicy {SignedBy (0 )}),
121
- Identities : []* msp .MSPPrincipal {principal },
112
+ Identities : []* mb .MSPPrincipal {principal },
122
113
}
123
114
124
115
return p
125
116
}
126
117
127
118
//wrapper for generating "any of a given role" type policies
128
- func signedByAnyOfGivenRole (role msp .MSPRole_MSPRoleType , ids []string ) * cb.SignaturePolicyEnvelope {
119
+ func signedByAnyOfGivenRole (role mb .MSPRole_MSPRoleType , ids []string ) * cb.SignaturePolicyEnvelope {
129
120
return SignedByNOutOfGivenRole (1 , role , ids )
130
121
}
131
122
132
- func SignedByNOutOfGivenRole (n int32 , role msp .MSPRole_MSPRoleType , ids []string ) * cb.SignaturePolicyEnvelope {
123
+ func SignedByNOutOfGivenRole (n int32 , role mb .MSPRole_MSPRoleType , ids []string ) * cb.SignaturePolicyEnvelope {
133
124
// we create an array of principals, one principal
134
125
// per application MSP defined on this chain
135
126
sort .Strings (ids )
136
- principals := make ([]* msp .MSPPrincipal , len (ids ))
127
+ principals := make ([]* mb .MSPPrincipal , len (ids ))
137
128
sigspolicy := make ([]* cb.SignaturePolicy , len (ids ))
129
+
138
130
for i , id := range ids {
139
- principals [i ] = & msp .MSPPrincipal {
140
- PrincipalClassification : msp .MSPPrincipal_ROLE ,
141
- Principal : protoutil . MarshalOrPanic ( & msp .MSPRole {Role : role , MspIdentifier : id })}
131
+ principals [i ] = & mb .MSPPrincipal {
132
+ PrincipalClassification : mb .MSPPrincipal_ROLE ,
133
+ Principal : protoMarshalOrPanic ( & mb .MSPRole {Role : role , MspIdentifier : id })}
142
134
sigspolicy [i ] = SignedBy (int32 (i ))
143
135
}
144
136
@@ -156,28 +148,28 @@ func SignedByNOutOfGivenRole(n int32, role msp.MSPRole_MSPRoleType, ids []string
156
148
// signature from a member of any of the orgs whose ids are
157
149
// listed in the supplied string array
158
150
func SignedByAnyMember (ids []string ) * cb.SignaturePolicyEnvelope {
159
- return signedByAnyOfGivenRole (msp .MSPRole_MEMBER , ids )
151
+ return signedByAnyOfGivenRole (mb .MSPRole_MEMBER , ids )
160
152
}
161
153
162
154
// SignedByAnyClient returns a policy that requires one valid
163
155
// signature from a client of any of the orgs whose ids are
164
156
// listed in the supplied string array
165
157
func SignedByAnyClient (ids []string ) * cb.SignaturePolicyEnvelope {
166
- return signedByAnyOfGivenRole (msp .MSPRole_CLIENT , ids )
158
+ return signedByAnyOfGivenRole (mb .MSPRole_CLIENT , ids )
167
159
}
168
160
169
161
// SignedByAnyPeer returns a policy that requires one valid
170
162
// signature from an orderer of any of the orgs whose ids are
171
163
// listed in the supplied string array
172
164
func SignedByAnyPeer (ids []string ) * cb.SignaturePolicyEnvelope {
173
- return signedByAnyOfGivenRole (msp .MSPRole_PEER , ids )
165
+ return signedByAnyOfGivenRole (mb .MSPRole_PEER , ids )
174
166
}
175
167
176
168
// SignedByAnyAdmin returns a policy that requires one valid
177
169
// signature from a admin of any of the orgs whose ids are
178
170
// listed in the supplied string array
179
171
func SignedByAnyAdmin (ids []string ) * cb.SignaturePolicyEnvelope {
180
- return signedByAnyOfGivenRole (msp .MSPRole_ADMIN , ids )
172
+ return signedByAnyOfGivenRole (mb .MSPRole_ADMIN , ids )
181
173
}
182
174
183
175
// And is a convenience method which utilizes NOutOf to produce And equivalent behavior
@@ -201,3 +193,14 @@ func NOutOf(n int32, policies []*cb.SignaturePolicy) *cb.SignaturePolicy {
201
193
},
202
194
}
203
195
}
196
+
197
+ // protoMarshalOrPanic serializes a protobuf message and panics if this
198
+ // operation fails
199
+ func protoMarshalOrPanic (pb proto.Message ) []byte {
200
+ data , err := proto .Marshal (pb )
201
+ if err != nil {
202
+ panic (err )
203
+ }
204
+
205
+ return data
206
+ }
0 commit comments