-
Notifications
You must be signed in to change notification settings - Fork 109
/
config.go
202 lines (176 loc) · 5.47 KB
/
config.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
mixTy "github.com/33cn/plugin/plugin/dapp/mix/types"
"github.com/pkg/errors"
)
// IsSuperManager is supper manager or not
func isSuperManager(cfg *types.Chain33Config, addr string) bool {
confMix := types.ConfSub(cfg, mixTy.MixX)
for _, m := range confMix.GStrList("mixApprs") {
if addr == m {
return true
}
}
return false
}
// need super manager
func (a *action) Config(config *mixTy.MixConfigAction) (*types.Receipt, error) {
cfg := a.api.GetConfig()
switch config.Ty {
case mixTy.MixConfigType_Verify:
//必须是超级管理员才能配置
if !isSuperManager(cfg, a.fromaddr) {
return nil, errors.Wrapf(types.ErrNotAllow, "not super manager,%s", a.fromaddr)
}
return a.ConfigAddVerifyKey(config.GetVerifyKey())
case mixTy.MixConfigType_Auth:
//必须是超级管理员才能配置
if !isSuperManager(cfg, a.fromaddr) {
return nil, errors.Wrapf(types.ErrNotAllow, "not super manager,%s", a.fromaddr)
}
if config.Action == mixTy.MixConfigAct_Add {
return a.ConfigAddAuthPubKey(config.GetAuthKey())
} else {
return a.ConfigDeleteAuthPubKey(config.GetAuthKey())
}
case mixTy.MixConfigType_Payment:
//个人配置,个人负责,可重配
return a.ConfigPaymentPubKey(config.GetNoteAccountKey())
}
return nil, errors.Wrapf(types.ErrNotFound, "ty=%d", config.Ty)
}
func makeConfigVerifyKeyReceipt(data *mixTy.ZkVerifyKeys, ty int32) *types.Receipt {
key := getVerifyKeysKey(ty)
return &types.Receipt{
Ty: types.ExecOk,
KV: []*types.KeyValue{
{Key: key, Value: types.Encode(data)},
},
Logs: []*types.ReceiptLog{
{Ty: mixTy.TyLogMixConfigVk, Log: types.Encode(data)},
},
}
}
func getVerifyKeys(db dbm.KV, ty int32) (*mixTy.ZkVerifyKeys, error) {
key := getVerifyKeysKey(ty)
v, err := db.Get(key)
if err != nil {
return nil, errors.Wrapf(err, "get db verify key")
}
var keys mixTy.ZkVerifyKeys
err = types.Decode(v, &keys)
if err != nil {
return nil, errors.Wrapf(err, "decode db verify key")
}
return &keys, nil
}
func (a *action) ConfigAddVerifyKey(newKey *mixTy.MixZkVerifyKey) (*types.Receipt, error) {
keys, err := getVerifyKeys(a.db, int32(newKey.Type))
if isNotFound(errors.Cause(err)) {
keys := &mixTy.ZkVerifyKeys{}
keys.Data = append(keys.Data, newKey)
return makeConfigVerifyKeyReceipt(keys, int32(newKey.Type)), nil
}
if err != nil {
return nil, errors.Wrapf(err, "AddVerifyKey,ty=%d", newKey.Type)
}
//逆序保存keys,保证新的key先遍历到
keys.Data = []*mixTy.MixZkVerifyKey{newKey, keys.Data[0]}
return makeConfigVerifyKeyReceipt(keys, int32(newKey.Type)), nil
}
func makeConfigAuthKeyReceipt(data *mixTy.AuthKeys) *types.Receipt {
key := getAuthPubKeysKey()
return &types.Receipt{
Ty: types.ExecOk,
KV: []*types.KeyValue{
{Key: key, Value: types.Encode(data)},
},
Logs: []*types.ReceiptLog{
{Ty: mixTy.TyLogMixConfigAuth, Log: types.Encode(data)},
},
}
}
func (a *action) getAuthKeys() (*mixTy.AuthKeys, error) {
key := getAuthPubKeysKey()
v, err := a.db.Get(key)
if err != nil {
return nil, errors.Wrapf(err, "get db")
}
var keys mixTy.AuthKeys
err = types.Decode(v, &keys)
if err != nil {
return nil, errors.Wrapf(err, "decode db key")
}
return &keys, nil
}
func (a *action) ConfigAddAuthPubKey(key string) (*types.Receipt, error) {
keys, err := a.getAuthKeys()
if isNotFound(errors.Cause(err)) {
keys := &mixTy.AuthKeys{}
keys.Keys = append(keys.Keys, key)
return makeConfigAuthKeyReceipt(keys), nil
}
if err != nil {
return nil, err
}
keys.Keys = append(keys.Keys, key)
return makeConfigAuthKeyReceipt(keys), nil
}
func (a *action) ConfigDeleteAuthPubKey(key string) (*types.Receipt, error) {
keys, err := a.getAuthKeys()
if err != nil {
return nil, err
}
var newKeys mixTy.AuthKeys
for _, v := range keys.Keys {
if key == v {
continue
}
newKeys.Keys = append(newKeys.Keys, v)
}
return makeConfigAuthKeyReceipt(&newKeys), nil
}
func makeConfigPaymentKeyReceipt(data *mixTy.NoteAccountKey) *types.Receipt {
key := calcReceivingKey(data.Addr)
return &types.Receipt{
Ty: types.ExecOk,
KV: []*types.KeyValue{
{Key: key, Value: types.Encode(data)},
},
Logs: []*types.ReceiptLog{
{Ty: mixTy.TyLogMixConfigPaymentKey, Log: types.Encode(data)},
},
}
}
func GetPaymentPubKey(db dbm.KV, addr string) (*mixTy.NoteAccountKey, error) {
key := calcReceivingKey(addr)
v, err := db.Get(key)
if err != nil {
return nil, errors.Wrapf(err, "get db")
}
var keys mixTy.NoteAccountKey
err = types.Decode(v, &keys)
if err != nil {
return nil, errors.Wrapf(err, "decode db key")
}
return &keys, nil
}
func (a *action) ConfigPaymentPubKey(paykey *mixTy.NoteAccountKey) (*types.Receipt, error) {
if paykey == nil || len(paykey.NoteReceiveAddr) == 0 || len(paykey.SecretReceiveKey) == 0 || len(paykey.Addr) == 0 {
return nil, errors.Wrapf(types.ErrInvalidParam, "pubkey=%v", paykey)
}
//检查用户使用对应的addr的key,但不能确保key就是对应addr
if paykey.Addr != a.fromaddr {
return nil, errors.Wrapf(types.ErrInvalidParam, "register addr=%s not match with sign=%s", paykey.Addr, a.fromaddr)
}
//直接覆盖
return makeConfigPaymentKeyReceipt(&mixTy.NoteAccountKey{
Addr: a.fromaddr,
NoteReceiveAddr: paykey.NoteReceiveAddr,
SecretReceiveKey: paykey.SecretReceiveKey}), nil
}