-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.go
More file actions
195 lines (164 loc) · 3.59 KB
/
auth.go
File metadata and controls
195 lines (164 loc) · 3.59 KB
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
package chain
type KeyWeight struct {
Key PublicKey
Weight uint16
}
type PermissionLevel struct {
Actor Name
Permission Name
}
type PermissionLevelWeight struct {
Permission PermissionLevel
Weight uint16
}
type WaitWeight struct {
WaitSec uint32
Weight uint16
}
type Authority struct {
Threshold uint32
Keys []KeyWeight
Accounts []PermissionLevelWeight
Waits []WaitWeight
}
func NewPermissionLevel(actor Name, permission Name) *PermissionLevel {
return &PermissionLevel{actor, permission}
}
func (t *PermissionLevel) Pack(enc *Encoder) int {
size := enc.GetSize()
enc.PackUint64(t.Actor.N)
enc.PackUint64(t.Permission.N)
return enc.GetSize() - size
}
func (t *PermissionLevel) Unpack(data []byte) int {
dec := NewDecoder(data)
t.Actor = dec.UnpackName()
t.Permission = dec.UnpackName()
return dec.Pos()
}
func (t *PermissionLevel) Size() int {
size := 0
size += 8 //Actor
size += 8 //Permission
return size
}
func (t *PermissionLevelWeight) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
t.Permission.Pack(enc)
enc.PackUint16(t.Weight)
return enc.GetSize() - oldSize
}
func (t *PermissionLevelWeight) Unpack(data []byte) int {
dec := NewDecoder(data)
dec.UnpackI(&t.Permission)
t.Weight = dec.UnpackUint16()
return dec.Pos()
}
func (t *PermissionLevelWeight) Size() int {
size := 0
size += t.Permission.Size() //Permission
size += 2 //Weight
return size
}
func (t *WaitWeight) Pack(enc *Encoder) int {
size := enc.GetSize()
enc.PackUint32(t.WaitSec)
enc.PackUint16(t.Weight)
return enc.GetSize() - size
}
func (t *WaitWeight) Unpack(data []byte) int {
dec := NewDecoder(data)
t.WaitSec = dec.UnpackUint32()
t.Weight = dec.UnpackUint16()
return dec.Pos()
}
func (t *WaitWeight) Size() int {
size := 0
size += 4 //WaitSec
size += 2 //Weight
return size
}
func (t *Authority) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.PackUint32(t.Threshold)
{
enc.PackLength(len(t.Keys))
for i := range t.Keys {
t.Keys[i].Pack(enc)
}
}
{
enc.PackLength(len(t.Accounts))
for i := range t.Accounts {
t.Accounts[i].Pack(enc)
}
}
{
enc.PackLength(len(t.Waits))
for i := range t.Waits {
t.Waits[i].Pack(enc)
}
}
return enc.GetSize() - oldSize
}
func (t *Authority) Unpack(data []byte) int {
dec := NewDecoder(data)
t.Threshold = dec.UnpackUint32()
{
length := dec.UnpackLength()
t.Keys = make([]KeyWeight, length)
for i := 0; i < length; i++ {
dec.UnpackI(&t.Keys[i])
}
}
{
length := dec.UnpackLength()
t.Accounts = make([]PermissionLevelWeight, length)
for i := 0; i < length; i++ {
dec.UnpackI(&t.Accounts[i])
}
}
{
length := dec.UnpackLength()
t.Waits = make([]WaitWeight, length)
for i := 0; i < length; i++ {
dec.UnpackI(&t.Waits[i])
}
}
return dec.Pos()
}
func (t *Authority) Size() int {
size := 0
size += 4 //Threshold
size += PackedVarUint32Length(uint32(len(t.Keys)))
for i := range t.Keys {
size += t.Keys[i].Size()
}
size += PackedVarUint32Length(uint32(len(t.Accounts)))
for i := range t.Accounts {
size += t.Accounts[i].Size()
}
size += PackedVarUint32Length(uint32(len(t.Waits)))
for i := range t.Waits {
size += t.Waits[i].Size()
}
return size
}
func (t *KeyWeight) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
t.Key.Pack(enc)
enc.PackUint16(t.Weight)
return enc.GetSize() - oldSize
}
func (t *KeyWeight) Unpack(data []byte) int {
dec := NewDecoder(data)
dec.UnpackI(&t.Key)
t.Weight = dec.UnpackUint16()
return dec.Pos()
}
func (t *KeyWeight) Size() int {
size := 0
size += t.Key.Size() //Key
size += 2 //Weight
return size
}