forked from eoscanada/eos-go
/
actions.go
101 lines (87 loc) · 2.64 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
package eos
import (
"bytes"
"encoding/json"
)
// 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:"bytes"`
}
// SetABI represents the hard-coded `setabi` action.
type SetABI struct {
Account AccountName `json:"account"`
ABI ABI `json:"abi"`
}
// NewAccount represents the hard-coded `newaccount` action.
type NewAccount struct {
Creator AccountName `json:"creator"`
Name AccountName `json:"name"`
Owner Authority `json:"owner"`
Active Authority `json:"active"`
Recovery Authority `json:"recovery"`
}
// Action
type Action struct {
Account AccountName `json:"account"`
Name ActionName `json:"name"`
Authorization []PermissionLevel `json:"authorization,omitempty"`
ActionData
}
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,
}
}
// 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,omitempty"`
Data HexBytes `json:"data,omitempty"`
}
type jsonActionFromServer struct {
Account AccountName `json:"account"`
Name ActionName `json:"name"`
Authorization []PermissionLevel `json:"authorization,omitempty"`
Data interface{} `json:"data,omitempty"`
HexData HexBytes `json:"hex_data,omitempty"`
}
func (a *Action) MarshalJSON() ([]byte, error) {
if a.toServer {
var err error
buf := new(bytes.Buffer)
encoder := NewEncoder(buf)
encoder.Encode(a.ActionData.Data)
if err != nil {
return nil, err
}
data := buf.Bytes()
return json.Marshal(&jsonActionToServer{
Account: a.Account,
Name: a.Name,
Authorization: a.Authorization,
Data: HexBytes(data),
})
}
return json.Marshal(&jsonActionFromServer{
Account: a.Account,
Name: a.Name,
Authorization: a.Authorization,
HexData: a.HexData,
Data: a.Data,
})
}