-
Notifications
You must be signed in to change notification settings - Fork 5
/
wallet_accessmatrix.go
87 lines (70 loc) · 1.84 KB
/
wallet_accessmatrix.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
package mock
import (
"errors"
"github.com/hyperledger/fabric-chaincode-go/shim"
)
// Right represents a right in the access matrix
type Right struct {
Channel string
Chaincode string
Role string
Operation string
Address string
}
// IsValid checks if the right is valid
func (r Right) IsValid() error {
if len(r.Channel) == 0 {
return errors.New("right is broken, channel is not set")
}
if len(r.Chaincode) == 0 {
return errors.New("right is broken, chaincode is not set")
}
if len(r.Role) == 0 {
return errors.New("right is broken, role is not set")
}
if len(r.Address) == 0 {
return errors.New("right is broken, address is not set")
}
return nil
}
type operation string
const (
// AddRights adds a right to the access matrix
AddRights operation = "addRights"
// RemoveRights removes a right from the access matrix
RemoveRights operation = "removeRights"
)
// AddAccountRight adds a right to the access matrix
func (w *Wallet) AddAccountRight(right *Right) error {
return w.modifyRight(AddRights, right)
}
// RemoveAccountRight removes a right from the access matrix
func (w *Wallet) RemoveAccountRight(right *Right) error {
return w.modifyRight(RemoveRights, right)
}
func (w *Wallet) modifyRight(opFn operation, right *Right) error {
if right == nil {
return errors.New("right is not set")
}
validationErr := right.IsValid()
if validationErr != nil {
return validationErr
}
params := [][]byte{
[]byte(opFn),
[]byte(right.Channel),
[]byte(right.Chaincode),
[]byte(right.Role),
[]byte(right.Operation),
[]byte(right.Address),
}
const acl = "acl"
aclstub := w.ledger.GetStub(acl)
aclstub.TxID = txIDGen()
aclstub.MockPeerChaincodeWithChannel(acl, aclstub, acl)
rsp := aclstub.InvokeChaincode(acl, params, acl)
if rsp.GetStatus() != shim.OK {
return errors.New(rsp.GetMessage())
}
return nil
}