/
account_key_role_based.go
279 lines (236 loc) · 7.17 KB
/
account_key_role_based.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
// Copyright 2019 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package accountkey
import (
"crypto/ecdsa"
"encoding/json"
"errors"
"io"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/kerrors"
"github.com/klaytn/klaytn/rlp"
)
type RoleType int
const (
RoleTransaction RoleType = iota
RoleAccountUpdate
RoleFeePayer
// TODO-Klaytn-Accounts: more roles can be listed here.
RoleLast
)
var (
errKeyLengthZero = errors.New("key length is zero")
errKeyShouldNotBeNilOrCompositeType = errors.New("key should not be nil or a composite type")
)
// AccountKeyRoleBased represents a role-based key.
// The roles are defined like below:
// RoleTransaction - this key is used to verify transactions transferring values.
// RoleAccountUpdate - this key is used to update keys in the account when using TxTypeAccountUpdate.
// RoleFeePayer - this key is used to pay tx fee when using fee-delegated transactions.
// If an account has a key of this role and wants to pay tx fee,
// fee-delegated transactions should be signed by this key.
//
// If RoleAccountUpdate or RoleFeePayer is not set, RoleTransaction will be used instead by default.
type AccountKeyRoleBased []AccountKey
func NewAccountKeyRoleBased() *AccountKeyRoleBased {
return &AccountKeyRoleBased{}
}
func NewAccountKeyRoleBasedWithValues(keys []AccountKey) *AccountKeyRoleBased {
return (*AccountKeyRoleBased)(&keys)
}
func (a *AccountKeyRoleBased) Type() AccountKeyType {
return AccountKeyTypeRoleBased
}
func (a *AccountKeyRoleBased) IsCompositeType() bool {
return true
}
func (a *AccountKeyRoleBased) DeepCopy() AccountKey {
n := make(AccountKeyRoleBased, len(*a))
for i, k := range *a {
n[i] = k.DeepCopy()
}
return &n
}
func (a *AccountKeyRoleBased) Equal(b AccountKey) bool {
tb, ok := b.(*AccountKeyRoleBased)
if !ok {
return false
}
if len(*a) != len(*tb) {
return false
}
for i, tbi := range *tb {
if (*a)[i].Equal(tbi) == false {
return false
}
}
return true
}
func (a *AccountKeyRoleBased) EncodeRLP(w io.Writer) error {
enc := make([][]byte, len(*a))
for i, k := range *a {
enc[i], _ = rlp.EncodeToBytes(NewAccountKeySerializerWithAccountKey(k))
}
return rlp.Encode(w, enc)
}
func (a *AccountKeyRoleBased) DecodeRLP(s *rlp.Stream) error {
enc := [][]byte{}
if err := s.Decode(&enc); err != nil {
return err
}
keys := make([]AccountKey, len(enc))
for i, b := range enc {
serializer := NewAccountKeySerializer()
if err := rlp.DecodeBytes(b, &serializer); err != nil {
return err
}
keys[i] = serializer.key
}
*a = (AccountKeyRoleBased)(keys)
return nil
}
func (a *AccountKeyRoleBased) MarshalJSON() ([]byte, error) {
serializers := make([]*AccountKeySerializer, len(*a))
for i, k := range *a {
serializers[i] = NewAccountKeySerializerWithAccountKey(k)
}
return json.Marshal(serializers)
}
func (a *AccountKeyRoleBased) UnmarshalJSON(b []byte) error {
var serializers []*AccountKeySerializer
if err := json.Unmarshal(b, &serializers); err != nil {
return err
}
*a = make(AccountKeyRoleBased, len(serializers))
for i, s := range serializers {
(*a)[i] = s.key
}
return nil
}
func (a *AccountKeyRoleBased) Validate(r RoleType, recoveredKeys []*ecdsa.PublicKey, from common.Address) bool {
if len(*a) > int(r) {
return (*a)[r].Validate(r, recoveredKeys, from)
}
return a.getDefaultKey().Validate(r, recoveredKeys, from)
}
func (a *AccountKeyRoleBased) getDefaultKey() AccountKey {
return (*a)[RoleTransaction]
}
func (a *AccountKeyRoleBased) String() string {
serializer := NewAccountKeySerializerWithAccountKey(a)
b, _ := json.Marshal(serializer)
return string(b)
}
func (a *AccountKeyRoleBased) AccountCreationGas(currentBlockNumber uint64) (uint64, error) {
gas := uint64(0)
for _, k := range *a {
gasK, err := k.AccountCreationGas(currentBlockNumber)
if err != nil {
return 0, err
}
gas += gasK
}
return gas, nil
}
func (a *AccountKeyRoleBased) SigValidationGas(currentBlockNumber uint64, r RoleType) (uint64, error) {
var key AccountKey
// Set the key used to sign for validation.
if len(*a) > int(r) {
key = (*a)[r]
} else {
key = a.getDefaultKey()
}
gas, err := key.SigValidationGas(currentBlockNumber, r)
if err != nil {
return 0, err
}
return gas, nil
}
func (a *AccountKeyRoleBased) CheckInstallable(currentBlockNumber uint64) error {
// A zero-role key is not allowed.
if len(*a) == 0 {
return kerrors.ErrZeroLength
}
// Do not allow undefined roles.
if len(*a) > (int)(RoleLast) {
return kerrors.ErrLengthTooLong
}
for i := 0; i < len(*a); i++ {
// A composite key is not allowed.
if (*a)[i].IsCompositeType() {
return kerrors.ErrNestedCompositeType
}
// If any key in the role cannot be initialized, return an error.
if err := (*a)[i].CheckInstallable(currentBlockNumber); err != nil {
return err
}
}
return nil
}
func (a *AccountKeyRoleBased) CheckUpdatable(newKey AccountKey, currentBlockNumber uint64) error {
if newKey, ok := newKey.(*AccountKeyRoleBased); ok {
lenOldKey := len(*a)
lenNewKey := len(*newKey)
// If no key is to be replaced, it is regarded as a fail.
if lenNewKey == 0 {
return kerrors.ErrZeroLength
}
// Do not allow undefined roles.
if lenNewKey > (int)(RoleLast) {
return kerrors.ErrLengthTooLong
}
for i := 0; i < lenNewKey; i++ {
switch {
// A composite key is not allowed.
case (*newKey)[i].IsCompositeType():
return kerrors.ErrNestedCompositeType
// If newKey is longer than oldKey, init the new attributes.
case i >= lenOldKey:
if err := (*newKey)[i].CheckInstallable(currentBlockNumber); err != nil {
return err
}
// Do nothing for AccountKeyTypeNil
case (*newKey)[i].Type() == AccountKeyTypeNil:
// Check whether the newKey is replacable or not
default:
if err := CheckReplacable((*a)[i], (*newKey)[i], currentBlockNumber); err != nil {
return err
}
}
}
return nil
}
// Update is not possible if the type is different.
return kerrors.ErrDifferentAccountKeyType
}
func (a *AccountKeyRoleBased) Update(newKey AccountKey, currentBlockNumber uint64) error {
if err := a.CheckUpdatable(newKey, currentBlockNumber); err != nil {
return err
}
newRoleKey, _ := newKey.(*AccountKeyRoleBased)
lenNewKey := len(*newRoleKey)
lenOldKey := len(*a)
if lenOldKey < lenNewKey {
*a = append(*a, (*newRoleKey)[lenOldKey:]...)
}
for i := 0; i < lenNewKey; i++ {
if (*newRoleKey)[i].Type() == AccountKeyTypeNil {
continue
}
(*a)[i] = (*newRoleKey)[i]
}
return nil
}