/
statebasedimpl.go
143 lines (128 loc) · 3.65 KB
/
statebasedimpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright the Hyperledger Fabric contributors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package statebased
import (
"fmt"
"sort"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/msp"
)
// stateEP implements the KeyEndorsementPolicy
type stateEP struct {
orgs map[string]msp.MSPRole_MSPRoleType
}
// NewStateEP constructs a state-based endorsement policy from a given
// serialized EP byte array. If the byte array is empty, a new EP is created.
func NewStateEP(policy []byte) (KeyEndorsementPolicy, error) {
s := &stateEP{orgs: make(map[string]msp.MSPRole_MSPRoleType)}
if policy != nil {
spe := &common.SignaturePolicyEnvelope{}
if err := proto.Unmarshal(policy, spe); err != nil {
return nil, fmt.Errorf("Error unmarshaling to SignaturePolicy: %s", err)
}
err := s.setMSPIDsFromSP(spe)
if err != nil {
return nil, err
}
}
return s, nil
}
// Policy returns the endorsement policy as bytes
func (s *stateEP) Policy() ([]byte, error) {
spe, err := s.policyFromMSPIDs()
if err != nil {
return nil, err
}
spBytes, err := proto.Marshal(spe)
if err != nil {
return nil, err
}
return spBytes, nil
}
// AddOrgs adds the specified channel orgs to the existing key-level EP
func (s *stateEP) AddOrgs(role RoleType, neworgs ...string) error {
var mspRole msp.MSPRole_MSPRoleType
switch role {
case RoleTypeMember:
mspRole = msp.MSPRole_MEMBER
case RoleTypePeer:
mspRole = msp.MSPRole_PEER
default:
return &RoleTypeDoesNotExistError{RoleType: role}
}
// add new orgs
for _, addorg := range neworgs {
s.orgs[addorg] = mspRole
}
return nil
}
// DelOrgs delete the specified channel orgs from the existing key-level EP
func (s *stateEP) DelOrgs(delorgs ...string) {
for _, delorg := range delorgs {
delete(s.orgs, delorg)
}
}
// ListOrgs returns an array of channel orgs that are required to endorse chnages
func (s *stateEP) ListOrgs() []string {
orgNames := make([]string, 0, len(s.orgs))
for mspid := range s.orgs {
orgNames = append(orgNames, mspid)
}
return orgNames
}
func (s *stateEP) setMSPIDsFromSP(sp *common.SignaturePolicyEnvelope) error {
// iterate over the identities in this envelope
for _, identity := range sp.Identities {
// this imlementation only supports the ROLE type
if identity.PrincipalClassification == msp.MSPPrincipal_ROLE {
msprole := &msp.MSPRole{}
err := proto.Unmarshal(identity.Principal, msprole)
if err != nil {
return fmt.Errorf("error unmarshaling msp principal: %s", err)
}
s.orgs[msprole.GetMspIdentifier()] = msprole.GetRole()
}
}
return nil
}
func (s *stateEP) policyFromMSPIDs() (*common.SignaturePolicyEnvelope, error) {
mspids := s.ListOrgs()
sort.Strings(mspids)
principals := make([]*msp.MSPPrincipal, len(mspids))
sigspolicy := make([]*common.SignaturePolicy, len(mspids))
for i, id := range mspids {
principal, err := proto.Marshal(
&msp.MSPRole{
Role: s.orgs[id],
MspIdentifier: id,
},
)
if err != nil {
return nil, err
}
principals[i] = &msp.MSPPrincipal{
PrincipalClassification: msp.MSPPrincipal_ROLE,
Principal: principal,
}
sigspolicy[i] = &common.SignaturePolicy{
Type: &common.SignaturePolicy_SignedBy{
SignedBy: int32(i),
},
}
}
// create the policy: it requires exactly 1 signature from all of the principals
p := &common.SignaturePolicyEnvelope{
Version: 0,
Rule: &common.SignaturePolicy{
Type: &common.SignaturePolicy_NOutOf_{
NOutOf: &common.SignaturePolicy_NOutOf{
N: int32(len(mspids)),
Rules: sigspolicy,
},
},
},
Identities: principals,
}
return p, nil
}