forked from FISCO-BCOS/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_manager.go
619 lines (524 loc) · 18 KB
/
auth_manager.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package commandline
import (
"encoding/hex"
"fmt"
"math"
"math/big"
"strconv"
"github.com/FISCO-BCOS/go-sdk/precompiled/auth"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
)
var getDeployAuthType = &cobra.Command{
Use: "getDeployAuthType",
Short: " get global deploy auth type",
Long: `get global deploy auth type.
For example:
[getDeployAuthType]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#getdeployauthtype`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("getDeployAuthType failed, err:%v\n", err)
return
}
result, err := authManagerService.GetDeployAuthType()
if err != nil {
fmt.Printf("getDeployAuthType failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
var checkDeployAuth = &cobra.Command{
Use: "checkDeployAuth",
Short: "[accountAddress] check the account whether this account can deploy contract.",
Long: `check the account whether this account can deploy contract.
Arguments:
[checkDeployAuth]: string
For example:
[checkDeployAuth] [0x112fb844934c794a9e425dd6b4e57eff1b519f17]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#checkdeployauth`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
accountAddress := args[0]
if !IsValidAccount(accountAddress) {
fmt.Printf("the format of accountAddress %v is unvalid\n", accountAddress)
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("checkDeployAuth failed, err:%v\n", err)
return
}
result, err := authManagerService.CheckDeployAuth(common.HexToAddress(accountAddress))
if err != nil {
fmt.Printf("checkDeployAuth failed, err: %v\n", err)
return
}
fmt.Println(*result)
},
}
var checkMethodAuth = &cobra.Command{
Use: "checkMethodAuth",
Short: "[contractAddr][funcSelector][account] check the contract interface func whether this account can call.",
Long: `check the contract interface func whether this account can call.
Arguments:
[contractAddr]: the contractAddress
[funcSelector]: interface func selector of contract, 4 bytes
[account]: the account to check
For example:
[checkMethodAuth] [0x112fb844934c794a9e425dd6b4e57eff1b519f17][c53057b4][0x112fb844934c794a9e425dd6b4e57eff1b519f17]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#checkmethodauth`,
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
contractAddr := args[0]
if !IsValidAccount(contractAddr) {
fmt.Printf("the format of contractAddr %v is unvalid\n", contractAddr)
return
}
funcSelectorStr := args[1]
funcByte, err := hex.DecodeString(funcSelectorStr)
if err != nil {
fmt.Printf("the format of funcSelector %v is unvalid , hex decode err\n", funcSelectorStr)
return
}
if len(funcByte) != 4 {
fmt.Printf("the format of funcSelector %v is unvalid\n", funcSelectorStr)
return
}
var funcs [4]byte
funcs[0] = funcByte[0]
funcs[1] = funcByte[1]
funcs[2] = funcByte[2]
funcs[3] = funcByte[3]
accountAddress := args[2]
if !IsValidAccount(accountAddress) {
fmt.Printf("the format of accountAddress %v is unvalid\n", accountAddress)
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("checkMethodAuth failed, err:%v\n", err)
return
}
result, err := authManagerService.CheckMethodAuth(common.HexToAddress(contractAddr), funcs, common.HexToAddress(accountAddress))
if err != nil {
fmt.Printf("checkMethodAuth failed, err: %v\n", err)
return
}
fmt.Println(*result)
},
}
var getAdmin = &cobra.Command{
Use: "getAdmin",
Short: "[contractAddr] get a specific contract admin.",
Long: `get a specific contract admin.
Arguments:
[contractAddr]: string
For example:
[getAdmin] [0x112fb844934c794a9e425dd6b4e57eff1b519f17]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#getadmin`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
contractAddr := args[0]
if !IsValidAccount(contractAddr) {
fmt.Printf("the format of contractAddr %v is unvalid\n", contractAddr)
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("getAdmin failed, err:%v\n", err)
return
}
result, err := authManagerService.GetAdmin(common.HexToAddress(contractAddr))
if err != nil {
fmt.Printf("getAdmin failed, err: %v\n", err)
return
}
fmt.Println(*result)
},
}
var resetAdmin = &cobra.Command{
Use: "resetAdmin",
Short: "[newAdmin][contractAddr] submit a proposal of resetting contract admin, only governor can call it.",
Long: `submit a proposal of resetting contract admin, only governor can call it.
Arguments:
[newAdmin]: new admin address
[contractAddr]: the address of contract which will propose to reset admin
For example:
[resetAdmin] [0x112fb844934c794a9e425dd6b4e57eff1b519f17][0x112fb844934c794a9e425dd6b4e57eff1b519f17]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#resetadmin`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
newAdmin := args[0]
if !IsValidAccount(newAdmin) {
fmt.Printf("the format of newAdmin %v is unvalid\n", newAdmin)
return
}
contractAddr := args[1]
if !IsValidAccount(contractAddr) {
fmt.Printf("the format of contractAddr %v is unvalid\n", contractAddr)
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("resetAdmin failed, err:%v\n", err)
return
}
result, err := authManagerService.ResetAdmin(common.HexToAddress(newAdmin), common.HexToAddress(contractAddr))
if err != nil {
fmt.Printf("resetAdmin failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
var updateGovernor = &cobra.Command{
Use: "updateGovernor",
Short: "[accountAddress][weight] apply for update governor, only governor can call it.",
Long: `apply for update governor, only governor can call it.
Arguments:
[accountAddress]: string
[weight]: uint32
For example:
[updateGovernor] [0x112fb844934c794a9e425dd6b4e57eff1b519f17] [1]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#updategovernor`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
accountAddress := args[0]
if !IsValidAccount(accountAddress) {
fmt.Printf("the format of accountAddress %v is unvalid\n", accountAddress)
return
}
weight, err := strconv.ParseInt(args[1], 0, 0)
if err != nil {
fmt.Println("weight should be integer")
return
}
if weight < 0 || weight > math.MaxInt32 {
fmt.Println("weight failed, must be >= 0")
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("updateGovernor failed, err:%v\n", err)
return
}
result, err := authManagerService.UpdateGovernor(common.HexToAddress(accountAddress), uint32(weight))
if err != nil {
fmt.Printf("updateGovernor failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
var setRate = &cobra.Command{
Use: "setRate",
Short: "[participatesRate][winRate] apply set participate rate and win rate. only governor can call it.",
Long: `apply set participate rate and win rate. only governor can call it.
Arguments:
[participatesRate]: uint8
[winRate]: uint8
For example:
[setRate] [1] [1]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#setrate`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
participatesRateStr, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("participatesRate should be integer")
return
}
if participatesRateStr < 0 || participatesRateStr > 100 {
fmt.Println("participatesRate failed, must be must be in the range of [0, 100]")
return
}
participatesRate := uint8(participatesRateStr)
winRate, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println("winRate should be integer")
return
}
if winRate < 0 || winRate > 100 {
fmt.Println("winRate failed, must be must be in the range of [0, 100]")
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("setRate failed, err:%v\n", err)
return
}
result, err := authManagerService.SetRate(participatesRate, uint8(winRate))
if err != nil {
fmt.Printf("setRate failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
var setDeployAuthType = &cobra.Command{
Use: "setDeployAuthType",
Short: "[deployAuthType] submit a proposal of setting deploy contract auth type, only governor can call it.",
Long: `submit a proposal of setting deploy contract auth type, only governor can call it.
Arguments:
[deployAuthType]: uint8 1-whitelist; 2-blacklist
For example:
[setDeployAuthType] [1]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#setdeployauthtype`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
num, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("deployAuthType should be integer")
return
}
if num != 1 && num != 2 {
fmt.Println("deployAuthType failed, must be [1-whitelist or 2-blacklist] ")
return
}
deployAuthType := uint8(num)
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("setDeployAuthType failed, err:%v\n", err)
return
}
result, err := authManagerService.SetDeployAuthType(deployAuthType)
if err != nil {
fmt.Printf("setDeployAuthType failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
var modifyDeployAuth = &cobra.Command{
Use: "modifyDeployAuth",
Short: "[accountAddress][openFlag] submit a proposal of adding deploy contract auth for account, only governor can call it.",
Long: `submit a proposal of adding deploy contract auth for account, only governor can call it.
Arguments:
[accountAddress]: account address string
[openFlag]: true-open; false-close
For example:
[modifyDeployAuth] [0x112fb844934c794a9e425dd6b4e57eff1b519f17][true]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#modifydeployauth`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
accountAddress := args[0]
if !IsValidAccount(accountAddress) {
fmt.Printf("the format of accountAddress %v is unvalid\n", accountAddress)
return
}
openFlag, err := strconv.ParseBool(args[1])
if err != nil {
fmt.Printf("openFlag failed, must be [true-open; false-close] %s", args[1])
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("modifyDeployAuth failed, err:%v\n", err)
return
}
result, err := authManagerService.ModifyDeployAuth(common.HexToAddress(accountAddress), openFlag)
if err != nil {
fmt.Printf("modifyDeployAuth failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
var revokeProposal = &cobra.Command{
Use: "revokeProposal",
Short: "[proposalId] revoke proposal, only governor can call it.",
Long: `revoke proposal, only governor can call it.
Arguments:
[proposalId]: proposal id
For example:
[revokeProposal] [1]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#revokeproposal`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
proposalId, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
fmt.Println("proposalId should be integer")
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("revokeProposal failed, err:%v\n", err)
return
}
result, err := authManagerService.RevokeProposal(*big.NewInt(proposalId))
if err != nil {
fmt.Printf("revokeProposal failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
var voteProposal = &cobra.Command{
Use: "voteProposal",
Short: "[proposalId][agree] unified vote, only governor can call it.",
Long: `unified vote, only governor can call it.
Arguments:
[proposalId]: proposal id
[agree]: true or false
For example:
[voteProposal] [1]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#voteproposal`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
proposalId, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
fmt.Println("proposalId should be integer")
return
}
agree, err := strconv.ParseBool(args[1])
if err != nil {
fmt.Printf("agree failed, must be [true or false] %s", args[1])
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("voteProposal failed, err:%v\n", err)
return
}
result, err := authManagerService.VoteProposal(*big.NewInt(proposalId), agree)
if err != nil {
fmt.Printf("voteProposal failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
var setMethodAuthType = &cobra.Command{
Use: "setMethodAuthType",
Short: "[contractAddr][funcSelector][authType] set a specific contract's method auth type, only contract admin can call it.",
Long: `set a specific contract's method auth type, only contract admin can call it.
Arguments:
[contractAddr]: the contract address to set auth
[funcSelector]: interface func selector of contract, 4 bytes
[authType]: uint8 1-whitelist; 2-blacklist
For example:
[setMethodAuthType] [0x][0x9cc3ca0f][1]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#setmethodauthtype`,
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
accountAddress := args[0]
if !IsValidAccount(accountAddress) {
fmt.Printf("the format of accountAddress %v is unvalid\n", accountAddress)
return
}
funcSelectorStr := args[1]
funcByte, err := hex.DecodeString(funcSelectorStr)
if err != nil {
fmt.Printf("the format of funcSelector %v is unvalid , hex decode err\n", funcSelectorStr)
return
}
if len(funcByte) != 4 {
fmt.Printf("the format of funcSelector %v is unvalid\n", funcSelectorStr)
return
}
var funcs [4]byte
funcs[0] = funcByte[0]
funcs[1] = funcByte[1]
funcs[2] = funcByte[2]
funcs[3] = funcByte[3]
num, err := strconv.Atoi(args[2])
if err != nil {
fmt.Println("deployAuthType should be integer")
return
}
if num != 1 && num != 2 {
fmt.Println("deployAuthType failed, must be [1-whitelist or 2-blacklist] ")
return
}
authType := uint8(num)
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("setMethodAuthType failed, err:%v\n", err)
return
}
result, err := authManagerService.SetMethodAuthType(common.HexToAddress(accountAddress), funcs, authType)
if err != nil {
fmt.Printf("setMethodAuthType failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
var setMethodAuth = &cobra.Command{
Use: "setMethodAuth",
Short: "[contractAddr][funcSelector][account][isOpen] set a specific contract's method ACL, only contract admin can call it",
Long: `set a specific contract's method ACL, only contract admin can call it.
Arguments:
[contractAddr]: the contract address to set auth
[funcSelector]: interface func selector of contract, 4 bytes
[account]: account
[isOpen]: true , false
For example:
[setMethodAuth] [0x][0x9cc3ca0f][0x][false]
For more information please refer:
https://fisco-bcos-doc.readthedocs.io/zh_CN/latest/docs/develop/sdk/java_sdk/rpc_api.html#setmethodauth`,
Args: cobra.ExactArgs(4),
Run: func(cmd *cobra.Command, args []string) {
contractAddr := args[0]
if !IsValidAccount(contractAddr) {
fmt.Printf("the format of contractAddr %v is unvalid\n", contractAddr)
return
}
funcSelectorStr := args[1]
funcByte, err := hex.DecodeString(funcSelectorStr)
if err != nil {
fmt.Printf("the format of funcSelector %v is unvalid , hex decode err\n", funcSelectorStr)
return
}
if len(funcByte) != 4 {
fmt.Printf("the format of funcSelector %v is unvalid\n", funcSelectorStr)
return
}
var funcs [4]byte
funcs[0] = funcByte[0]
funcs[1] = funcByte[1]
funcs[2] = funcByte[2]
funcs[3] = funcByte[3]
accountAddr := args[2]
if !IsValidAccount(accountAddr) {
fmt.Printf("the format of accountAddr %v is unvalid\n", accountAddr)
return
}
isOpen, err := strconv.ParseBool(args[3])
if err != nil {
fmt.Printf("isOpen failed, must be [true or false] %s", args[1])
return
}
authManagerService, err := auth.NewAuthManagerService(RPC)
if err != nil {
fmt.Printf("setMethodAuth failed, err:%v\n", err)
return
}
result, err := authManagerService.SetMethodAuth(common.HexToAddress(contractAddr), funcs, common.HexToAddress(accountAddr), isOpen)
if err != nil {
fmt.Printf("setMethodAuth failed, err: %v\n", err)
return
}
fmt.Println(result)
},
}
func init() {
rootCmd.AddCommand( /*getCommitteeInfo, getProposalInfo, */ getDeployAuthType, checkDeployAuth, checkMethodAuth, getAdmin)
rootCmd.AddCommand(updateGovernor, setRate, setDeployAuthType, modifyDeployAuth, resetAdmin, revokeProposal, voteProposal)
rootCmd.AddCommand(setMethodAuthType, setMethodAuth)
}