@@ -15,8 +15,7 @@ import (
1515
1616 "github.com/golang/protobuf/proto"
1717 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"
2019)
2120
2221// AcceptAllPolicy always evaluates to true
@@ -32,26 +31,18 @@ var RejectAllPolicy *cb.SignaturePolicyEnvelope
3231var MarshaledRejectAllPolicy []byte
3332
3433func init () {
35- var err error
36-
3734 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 )
4236
4337 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 )
4839}
4940
5041// Envelope builds an envelope message embedding a SignaturePolicy
5142func Envelope (policy * cb.SignaturePolicy , identities [][]byte ) * cb.SignaturePolicyEnvelope {
52- ids := make ([]* msp .MSPPrincipal , len (identities ))
43+ ids := make ([]* mb .MSPPrincipal , len (identities ))
5344 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 ]}
5546 }
5647
5748 return & cb.SignaturePolicyEnvelope {
@@ -73,34 +64,34 @@ func SignedBy(index int32) *cb.SignaturePolicy {
7364// SignedByMspMember creates a SignaturePolicyEnvelope
7465// requiring 1 signature from any member of the specified MSP
7566func SignedByMspMember (mspId string ) * cb.SignaturePolicyEnvelope {
76- return signedByFabricEntity (mspId , msp .MSPRole_MEMBER )
67+ return signedByFabricEntity (mspId , mb .MSPRole_MEMBER )
7768}
7869
7970// SignedByMspClient creates a SignaturePolicyEnvelope
8071// requiring 1 signature from any client of the specified MSP
8172func SignedByMspClient (mspId string ) * cb.SignaturePolicyEnvelope {
82- return signedByFabricEntity (mspId , msp .MSPRole_CLIENT )
73+ return signedByFabricEntity (mspId , mb .MSPRole_CLIENT )
8374}
8475
8576// SignedByMspPeer creates a SignaturePolicyEnvelope
8677// requiring 1 signature from any peer of the specified MSP
8778func SignedByMspPeer (mspId string ) * cb.SignaturePolicyEnvelope {
88- return signedByFabricEntity (mspId , msp .MSPRole_PEER )
79+ return signedByFabricEntity (mspId , mb .MSPRole_PEER )
8980}
9081
9182// SignedByFabricEntity creates a SignaturePolicyEnvelope
9283// 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 {
9485 // 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 })}
9889
9990 // create the policy: it requires exactly 1 signature from the first (and only) principal
10091 p := & cb.SignaturePolicyEnvelope {
10192 Version : 0 ,
10293 Rule : NOutOf (1 , []* cb.SignaturePolicy {SignedBy (0 )}),
103- Identities : []* msp .MSPPrincipal {principal },
94+ Identities : []* mb .MSPPrincipal {principal },
10495 }
10596
10697 return p
@@ -110,35 +101,36 @@ func signedByFabricEntity(mspId string, role msp.MSPRole_MSPRoleType) *cb.Signat
110101// requiring 1 signature from any admin of the specified MSP
111102func SignedByMspAdmin (mspId string ) * cb.SignaturePolicyEnvelope {
112103 // 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 })}
116107
117108 // create the policy: it requires exactly 1 signature from the first (and only) principal
118109 p := & cb.SignaturePolicyEnvelope {
119110 Version : 0 ,
120111 Rule : NOutOf (1 , []* cb.SignaturePolicy {SignedBy (0 )}),
121- Identities : []* msp .MSPPrincipal {principal },
112+ Identities : []* mb .MSPPrincipal {principal },
122113 }
123114
124115 return p
125116}
126117
127118//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 {
129120 return SignedByNOutOfGivenRole (1 , role , ids )
130121}
131122
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 {
133124 // we create an array of principals, one principal
134125 // per application MSP defined on this chain
135126 sort .Strings (ids )
136- principals := make ([]* msp .MSPPrincipal , len (ids ))
127+ principals := make ([]* mb .MSPPrincipal , len (ids ))
137128 sigspolicy := make ([]* cb.SignaturePolicy , len (ids ))
129+
138130 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 })}
142134 sigspolicy [i ] = SignedBy (int32 (i ))
143135 }
144136
@@ -156,28 +148,28 @@ func SignedByNOutOfGivenRole(n int32, role msp.MSPRole_MSPRoleType, ids []string
156148// signature from a member of any of the orgs whose ids are
157149// listed in the supplied string array
158150func SignedByAnyMember (ids []string ) * cb.SignaturePolicyEnvelope {
159- return signedByAnyOfGivenRole (msp .MSPRole_MEMBER , ids )
151+ return signedByAnyOfGivenRole (mb .MSPRole_MEMBER , ids )
160152}
161153
162154// SignedByAnyClient returns a policy that requires one valid
163155// signature from a client of any of the orgs whose ids are
164156// listed in the supplied string array
165157func SignedByAnyClient (ids []string ) * cb.SignaturePolicyEnvelope {
166- return signedByAnyOfGivenRole (msp .MSPRole_CLIENT , ids )
158+ return signedByAnyOfGivenRole (mb .MSPRole_CLIENT , ids )
167159}
168160
169161// SignedByAnyPeer returns a policy that requires one valid
170162// signature from an orderer of any of the orgs whose ids are
171163// listed in the supplied string array
172164func SignedByAnyPeer (ids []string ) * cb.SignaturePolicyEnvelope {
173- return signedByAnyOfGivenRole (msp .MSPRole_PEER , ids )
165+ return signedByAnyOfGivenRole (mb .MSPRole_PEER , ids )
174166}
175167
176168// SignedByAnyAdmin returns a policy that requires one valid
177169// signature from a admin of any of the orgs whose ids are
178170// listed in the supplied string array
179171func SignedByAnyAdmin (ids []string ) * cb.SignaturePolicyEnvelope {
180- return signedByAnyOfGivenRole (msp .MSPRole_ADMIN , ids )
172+ return signedByAnyOfGivenRole (mb .MSPRole_ADMIN , ids )
181173}
182174
183175// 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 {
201193 },
202194 }
203195}
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