forked from eoscanada/eosc
/
systemUpdateauth.go
153 lines (128 loc) · 3.42 KB
/
systemUpdateauth.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
// Copyright © 2018 EOS Canada <info@eoscanada.com>
package cmd
import (
"context"
"fmt"
"sort"
"github.com/sebastianmontero/eos-go"
"github.com/sebastianmontero/eos-go/ecc"
"github.com/sebastianmontero/eos-go/system"
"github.com/spf13/cobra"
)
var systemUpdateauthCmd = &cobra.Command{
Use: `updateauth [account] [permission_name] [parent permission or ""] [authority]`,
Short: "Set or update a permission on an account. See --help for more details.",
Long: `Set or update a permission on an account.
The [authority] field can be either a *public key* or a path to a YAML
file.
If you specify a public key, a simple 'authority' structure is built,
with a threshold of 1, and a single key.
Otherwise, it should be a path to a YAML file. Here is a sample YAML
authority file:
---
threshold: 3
keys:
- key: EOS6MRyAjQq8ud7hVNYcfn................tHuGYqET5GDW5CV
weight: 1
accounts:
- permission:
actor: accountname
permission: namedperm
weight: 1
waits:
- wait_sec: 300
weight: 1
---
`,
Args: cobra.ExactArgs(4),
Run: func(cmd *cobra.Command, args []string) {
account := toAccount(args[0], "account")
permissionName := toName(args[1], "permission_name")
var parent eos.Name
if args[2] != "" {
parent = toName(args[2], "parent permission")
}
authParam := args[3]
var auth eos.Authority
authKey, err := ecc.NewPublicKey(authParam)
if err == nil {
auth = eos.Authority{
Threshold: 1,
Keys: []eos.KeyWeight{
{PublicKey: authKey, Weight: 1},
},
}
} else {
err := loadYAMLOrJSONFile(authParam, &auth)
errorCheck("authority file invalid", err)
}
sortAuth(auth)
err = ValidateAuth(auth)
errorCheck("authority file invalid", err)
api := getAPI()
var updateAuthActionPermission = "active"
if parent == "" {
updateAuthActionPermission = "owner"
}
pushEOSCActions(
context.Background(),
api,
system.NewUpdateAuth(
account,
eos.PermissionName(permissionName),
eos.PermissionName(parent),
auth,
eos.PermissionName(updateAuthActionPermission),
),
)
},
}
func init() {
systemCmd.AddCommand(systemUpdateauthCmd)
}
func ValidateAuth(auth eos.Authority) error {
for idx, account := range auth.Accounts {
if len(account.Permission.Permission) == 0 {
return fmt.Errorf("account #%d missing permission", idx)
}
if len(account.Permission.Actor) == 0 {
return fmt.Errorf("account #%d missing actor", idx)
}
if account.Weight == 0 {
return fmt.Errorf("account #%d missing weight", idx)
}
}
for idx, key := range auth.Keys {
if len(key.PublicKey.Content) == 0 {
return fmt.Errorf("key #%d missing publicKey", idx)
}
if key.Weight == 0 {
return fmt.Errorf("key #%d missing weight", idx)
}
}
for idx, wait := range auth.Waits {
if wait.WaitSec == 0 {
return fmt.Errorf("wait #%d cannot be 0", idx)
}
if wait.Weight == 0 {
return fmt.Errorf("wait #%d is missing weight", idx)
}
}
return nil
}
func sortAuth(auth eos.Authority) {
sort.Slice(auth.Keys, func(i, j int) bool {
return auth.Keys[i].PublicKey.String() < auth.Keys[j].PublicKey.String()
})
sort.Slice(auth.Accounts, func(i, j int) bool {
perm1 := auth.Accounts[i].Permission
perm2 := auth.Accounts[j].Permission
if perm1.Actor == perm2.Actor {
return perm1.Permission < perm2.Permission
}
return perm1.Actor < perm2.Actor
})
sort.Slice(auth.Waits, func(i, j int) bool {
return auth.Waits[i].WaitSec < auth.Waits[j].WaitSec
})
}