forked from fractalplatform/fractal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
author.go
164 lines (149 loc) · 3.27 KB
/
author.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
package common
import (
"encoding/json"
"errors"
"io"
"github.com/fractalplatform/fractal/utils/rlp"
)
type AuthorType uint8
const (
AccountNameType AuthorType = iota
PubKeyType
AddressType
)
type (
Author struct {
Owner `json:"owner"`
Weight uint64 `json:"weight"`
}
Owner interface {
String() string
}
)
type AccountAuthor struct {
Account Name
}
type StorageAuthor struct {
Type AuthorType
DataRaw rlp.RawValue
Weight uint64
}
type AuthorJSON struct {
authorType AuthorType
OwnerStr string `json:"owner"`
Weight uint64 `json:"weight"`
}
func NewAuthor(owner Owner, weight uint64) *Author {
return &Author{Owner: owner, Weight: weight}
}
func (a *Author) GetWeight() uint64 {
return a.Weight
}
func (a *Author) EncodeRLP(w io.Writer) error {
storageAuthor, err := a.encode()
if err != nil {
return err
}
return rlp.Encode(w, storageAuthor)
}
func (a *Author) encode() (*StorageAuthor, error) {
switch aTy := a.Owner.(type) {
case Name:
value, err := rlp.EncodeToBytes(&aTy)
if err != nil {
return nil, err
}
return &StorageAuthor{
Type: AccountNameType,
DataRaw: value,
Weight: a.Weight,
}, nil
case PubKey:
value, err := rlp.EncodeToBytes(&aTy)
if err != nil {
return nil, err
}
return &StorageAuthor{
Type: PubKeyType,
DataRaw: value,
Weight: a.Weight,
}, nil
case Address:
value, err := rlp.EncodeToBytes(&aTy)
if err != nil {
return nil, err
}
return &StorageAuthor{
Type: AddressType,
DataRaw: value,
Weight: a.Weight,
}, nil
}
return nil, errors.New("Author encode failed")
}
func (a *Author) DecodeRLP(s *rlp.Stream) error {
storageAuthor := new(StorageAuthor)
err := s.Decode(storageAuthor)
if err != nil {
return err
}
return a.decode(storageAuthor)
}
func (a *Author) decode(sa *StorageAuthor) error {
switch sa.Type {
case AccountNameType:
var name Name
if err := rlp.DecodeBytes(sa.DataRaw, &name); err != nil {
return err
}
a.Owner = name
a.Weight = sa.Weight
return nil
case PubKeyType:
var pubKey PubKey
if err := rlp.DecodeBytes(sa.DataRaw, &pubKey); err != nil {
return err
}
a.Owner = pubKey
a.Weight = sa.Weight
return nil
case AddressType:
var address Address
if err := rlp.DecodeBytes(sa.DataRaw, &address); err != nil {
return err
}
a.Owner = address
a.Weight = sa.Weight
return nil
}
return errors.New("Author decode failed")
}
func (a *Author) MarshalJSON() ([]byte, error) {
switch aTy := a.Owner.(type) {
case Name:
return json.Marshal(&AuthorJSON{authorType: AccountNameType, OwnerStr: aTy.String(), Weight: a.Weight})
case PubKey:
return json.Marshal(&AuthorJSON{authorType: PubKeyType, OwnerStr: aTy.String(), Weight: a.Weight})
case Address:
return json.Marshal(&AuthorJSON{authorType: AddressType, OwnerStr: aTy.String(), Weight: a.Weight})
}
return nil, errors.New("Author marshal failed")
}
func (a *Author) UnmarshalJSON(data []byte) error {
aj := &AuthorJSON{}
if err := json.Unmarshal(data, aj); err != nil {
return err
}
switch aj.authorType {
case AccountNameType:
a.Owner = Name(aj.OwnerStr)
a.Weight = aj.Weight
case PubKeyType:
a.Owner = HexToPubKey(aj.OwnerStr)
a.Weight = aj.Weight
case AddressType:
a.Owner = HexToAddress(aj.OwnerStr)
a.Weight = aj.Weight
}
return nil
}