forked from eoscanada/eos-go
/
types.go
290 lines (225 loc) · 5.85 KB
/
types.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
280
281
282
283
284
285
286
287
288
289
290
package eos
import (
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/eoscanada/eos-go/ecc"
)
// For reference:
// https://github.com/mithrilcoin-io/EosCommander/blob/master/app/src/main/java/io/mithrilcoin/eoscommander/data/remote/model/types/EosByteWriter.java
type Name string
type AccountName Name
type PermissionName Name
type ActionName Name
type TableName Name
type ScopeName Name
func AN(in string) AccountName { return AccountName(in) }
func ActN(in string) ActionName { return ActionName(in) }
func PN(in string) PermissionName { return PermissionName(in) }
type CompressionType uint8
const (
CompressionNone = CompressionType(iota)
CompressionZlib
)
func (c CompressionType) String() string {
switch c {
case CompressionNone:
return "none"
case CompressionZlib:
return "zlib"
default:
return ""
}
}
func (c CompressionType) MarshalJSON() ([]byte, error) {
return json.Marshal(c.String())
}
func (c *CompressionType) UnmarshalJSON(data []byte) error {
switch string(data) {
case "zlib":
*c = CompressionZlib
default:
*c = CompressionNone
}
return nil
}
// CurrencyName
type CurrencyName string
// Asset
// NOTE: there's also ExtendedAsset which is a quantity with the attached contract (AccountName)
type Asset struct {
Amount int64
Symbol
}
// NOTE: there's also a new ExtendedSymbol (which includes the contract (as AccountName) on which it is)
type Symbol struct {
Precision uint8
Symbol string
}
// EOSSymbol represents the standard EOS symbol on the chain. It's
// here just to speed up things.
var EOSSymbol = Symbol{Precision: 4, Symbol: "EOS"}
func NewEOSAssetFromString(amount string) (out Asset, err error) {
val, err := strconv.ParseInt(strings.Replace(amount, ".", "", 1), 10, 64)
if err != nil {
return out, err
}
return NewEOSAsset(val), nil
}
func NewEOSAsset(amount int64) Asset {
return Asset{Amount: amount, Symbol: EOSSymbol}
}
// NewAsset parses a string like `1000.0000 EOS` into a properly setup Asset
func NewAsset(in string) (out Asset, err error) {
sec := strings.SplitN(in, " ", 2)
if len(sec) != 2 {
return out, fmt.Errorf("invalid format %q, expected an amount and a currency symbol", in)
}
if len(sec[1]) > 7 {
return out, fmt.Errorf("currency symbol %q too long", sec[1])
}
out.Symbol.Symbol = sec[1]
amount := sec[0]
amountSec := strings.SplitN(amount, ".", 2)
if len(amountSec) == 2 {
out.Symbol.Precision = uint8(len(amountSec[1]))
}
val, err := strconv.ParseInt(strings.Replace(amount, ".", "", 1), 10, 64)
if err != nil {
return out, err
}
out.Amount = val
return
}
func (a *Asset) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
asset, err := NewAsset(s)
if err != nil {
return err
}
*a = asset
return nil
}
type Permission struct {
PermName string `json:"perm_name"`
Parent string `json:"parent"`
RequiredAuth Authority `json:"required_auth"`
}
type PermissionLevel struct {
Actor AccountName `json:"actor"`
Permission PermissionName `json:"permission"`
}
type PermissionLevelWeight struct {
Permission PermissionLevel `json:"permission"`
Weight uint16 `json:"weight"`
}
type Authority struct {
Threshold uint32 `json:"threshold"`
Keys []KeyWeight `json:"keys"`
Accounts []PermissionLevelWeight `json:"accounts"`
}
type KeyWeight struct {
PublicKey ecc.PublicKey `json:"key"`
Weight uint16 `json:"weight"`
}
type Code struct {
AccountName AccountName `json:"account_name"`
CodeHash string `json:"code_hash"`
WAST string `json:"wast"` // TODO: decode into Go ast, see https://github.com/go-interpreter/wagon
ABI ABI `json:"abi"`
}
// JSONTime
type JSONTime struct {
time.Time
}
const JSONTimeFormat = "2006-01-02T15:04:05"
func (t JSONTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", t.Format(JSONTimeFormat))), nil
}
func (t *JSONTime) UnmarshalJSON(data []byte) (err error) {
if string(data) == "null" {
return nil
}
t.Time, err = time.Parse(`"`+JSONTimeFormat+`"`, string(data))
return err
}
// HexBytes
type HexBytes []byte
func (t HexBytes) MarshalJSON() ([]byte, error) {
return json.Marshal(hex.EncodeToString(t))
}
func (t *HexBytes) UnmarshalJSON(data []byte) (err error) {
var s string
err = json.Unmarshal(data, &s)
if err != nil {
return
}
*t, err = hex.DecodeString(s)
return
}
// SHA256Bytes
type SHA256Bytes []byte // should always be 32 bytes
func (t SHA256Bytes) MarshalJSON() ([]byte, error) {
return json.Marshal(hex.EncodeToString(t))
}
func (t *SHA256Bytes) UnmarshalJSON(data []byte) (err error) {
var s string
err = json.Unmarshal(data, &s)
if err != nil {
return
}
*t, err = hex.DecodeString(s)
return
}
type Varuint32 uint32
// Tstamp
type Tstamp struct {
time.Time
}
func (t Tstamp) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("%d", t.UnixNano()))
}
func (t *Tstamp) UnmarshalJSON(data []byte) (err error) {
var unixNano int64
if data[0] == '"' {
var s string
if err = json.Unmarshal(data, &s); err != nil {
return
}
unixNano, err = strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
} else {
unixNano, err = strconv.ParseInt(string(data), 10, 64)
if err != nil {
return err
}
}
*t = Tstamp{time.Unix(0, unixNano)}
return nil
}
type BlockTimestamp struct {
time.Time
}
const BlockTimestampFormat = "2006-01-02T15:04:05"
func (t BlockTimestamp) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", t.Format(BlockTimestampFormat))), nil
}
func (t *BlockTimestamp) UnmarshalJSON(data []byte) (err error) {
if string(data) == "null" {
return nil
}
t.Time, err = time.Parse(`"`+BlockTimestampFormat+`"`, string(data))
if err != nil {
t.Time, err = time.Parse(`"`+BlockTimestampFormat+`Z07:00"`, string(data))
}
return err
}