forked from eoscanada/eos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
176 lines (149 loc) · 3.97 KB
/
actions.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
package eos
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"reflect"
)
// See: libraries/chain/include/eosio/chain/contracts/types.hpp:203
// See: build/contracts/eosio.system/eosio.system.abi
// SetCode represents the hard-coded `setcode` action.
type SetCode struct {
Account AccountName `json:"account"`
VMType byte `json:"vmtype"`
VMVersion byte `json:"vmversion"`
Code HexBytes `json:"code"`
}
// SetABI represents the hard-coded `setabi` action.
type SetABI struct {
Account AccountName `json:"account"`
ABI ABI `json:"abi"`
}
// Action
type Action struct {
Account AccountName `json:"account"`
Name ActionName `json:"name"`
Authorization []PermissionLevel `json:"authorization,omitempty"`
ActionData
}
func (a Action) Digest() Checksum256 {
toEat := jsonActionToServer{
Account: a.Account,
Name: a.Name,
Authorization: a.Authorization,
Data: a.ActionData.HexData,
}
bin, err := MarshalBinary(toEat)
if err != nil {
panic("this should never panic, we know it marshals properly all the time")
}
h := sha256.New()
_, _ = h.Write(bin)
return h.Sum(nil)
}
type ActionData struct {
HexData HexBytes `json:"hex_data,omitempty"`
Data interface{} `json:"data,omitempty" eos:"-"`
abi []byte // TBD: we could use the ABI to decode in obj
toServer bool
}
func NewActionData(obj interface{}) ActionData {
return ActionData{
HexData: []byte{},
Data: obj,
toServer: true,
}
}
func NewActionDataFromHexData(data []byte) ActionData {
return ActionData{
HexData: data,
Data: nil,
toServer: true,
}
}
func (a *ActionData) SetToServer(toServer bool) {
// FIXME: let's clarify this design, make it simpler..
// toServer doesn't speak of the intent..
a.toServer = toServer
}
// jsonActionToServer represents what /v1/chain/push_transaction
// expects, which isn't allllways the same everywhere.
type jsonActionToServer struct {
Account AccountName `json:"account"`
Name ActionName `json:"name"`
Authorization []PermissionLevel `json:"authorization"`
Data HexBytes `json:"data,omitempty"`
}
type jsonActionFromServer struct {
Account AccountName `json:"account"`
Name ActionName `json:"name"`
Authorization []PermissionLevel `json:"authorization"`
Data interface{} `json:"data,omitempty"`
HexData HexBytes `json:"hex_data,omitempty"`
}
func (a *Action) MarshalJSON() ([]byte, error) {
auths := a.Authorization
if auths == nil {
auths = make([]PermissionLevel, 0)
}
if a.toServer {
data, err := a.ActionData.EncodeActionData()
if err != nil {
return nil, err
}
return json.Marshal(&jsonActionToServer{
Account: a.Account,
Name: a.Name,
Authorization: auths,
Data: data,
})
}
return json.Marshal(&jsonActionFromServer{
Account: a.Account,
Name: a.Name,
Authorization: auths,
HexData: a.HexData,
Data: a.Data,
})
}
func (data *ActionData) EncodeActionData() ([]byte, error) {
if data.Data == nil {
return data.HexData, nil
}
buf := new(bytes.Buffer)
encoder := NewEncoder(buf)
if err := encoder.Encode(data.Data); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (a *Action) MapToRegisteredAction() error {
src, ok := a.ActionData.Data.(map[string]interface{})
if !ok {
return nil
}
actionMap := RegisteredActions[a.Account]
var decodeInto reflect.Type
if actionMap != nil {
objType := actionMap[a.Name]
if objType != nil {
decodeInto = objType
}
}
if decodeInto == nil {
return nil
}
obj := reflect.New(decodeInto)
objIface := obj.Interface()
cnt, err := json.Marshal(src)
if err != nil {
return fmt.Errorf("marshaling data: %s", err)
}
err = json.Unmarshal(cnt, objIface)
if err != nil {
return fmt.Errorf("json unmarshal into registered actions: %s", err)
}
a.ActionData.Data = objIface
return nil
}