/
stack_param.go
269 lines (253 loc) · 5.82 KB
/
stack_param.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
package rpc
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"strconv"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/pkg/errors"
)
// StackParamType represents different types of stack values.
type StackParamType int
// All possible StackParamType values are listed here
const (
Unknown StackParamType = -1
Signature StackParamType = 0x00
Boolean StackParamType = 0x01
Integer StackParamType = 0x02
Hash160 StackParamType = 0x03
Hash256 StackParamType = 0x04
ByteArray StackParamType = 0x05
PublicKey StackParamType = 0x06
String StackParamType = 0x07
Array StackParamType = 0x10
InteropInterface StackParamType = 0xf0
Void StackParamType = 0xff
)
// String implements the stringer interface.
func (t StackParamType) String() string {
switch t {
case Signature:
return "Signature"
case Boolean:
return "Boolean"
case Integer:
return "Integer"
case Hash160:
return "Hash160"
case Hash256:
return "Hash256"
case ByteArray:
return "ByteArray"
case PublicKey:
return "PublicKey"
case String:
return "String"
case Array:
return "Array"
case InteropInterface:
return "InteropInterface"
case Void:
return "Void"
default:
return "Unknown"
}
}
// StackParamTypeFromString converts string into the StackParamType.
func StackParamTypeFromString(s string) (StackParamType, error) {
switch s {
case "Signature":
return Signature, nil
case "Boolean":
return Boolean, nil
case "Integer":
return Integer, nil
case "Hash160":
return Hash160, nil
case "Hash256":
return Hash256, nil
case "ByteArray":
return ByteArray, nil
case "PublicKey":
return PublicKey, nil
case "String":
return String, nil
case "Array":
return Array, nil
case "InteropInterface":
return InteropInterface, nil
case "Void":
return Void, nil
default:
return Unknown, errors.Errorf("unknown stack parameter type: %s", s)
}
}
// UnmarshalJSON sets StackParamType from JSON-encoded data.
func (t *StackParamType) UnmarshalJSON(data []byte) (err error) {
var (
s = string(data)
l = len(s)
)
if l < 2 || s[0] != '"' || s[l-1] != '"' {
*t = Unknown
return errors.Errorf("invalid type: %s", s)
}
*t, err = StackParamTypeFromString(s[1 : l-1])
return
}
// StackParam represent a stack parameter.
type StackParam struct {
Type StackParamType `json:"type"`
Value interface{} `json:"value"`
}
type rawStackParam struct {
Type StackParamType `json:"type"`
Value json.RawMessage `json:"value"`
}
// UnmarshalJSON implements Unmarshaler interface
func (p *StackParam) UnmarshalJSON(data []byte) (err error) {
var (
r rawStackParam
i int64
s string
b []byte
)
if err = json.Unmarshal(data, &r); err != nil {
return
}
switch p.Type = r.Type; r.Type {
case ByteArray:
if err = json.Unmarshal(r.Value, &s); err != nil {
return
}
if b, err = hex.DecodeString(s); err != nil {
return
}
p.Value = b
case String:
if err = json.Unmarshal(r.Value, &s); err != nil {
return
}
p.Value = s
case Integer:
if err = json.Unmarshal(r.Value, &i); err == nil {
p.Value = i
return
}
// sometimes integer comes as string
if err = json.Unmarshal(r.Value, &s); err != nil {
return
}
if i, err = strconv.ParseInt(s, 10, 64); err != nil {
return
}
p.Value = i
case Array:
// https://github.com/neo-project/neo/blob/3d59ecca5a8deb057bdad94b3028a6d5e25ac088/neo/Network/RPC/RpcServer.cs#L67
var rs []StackParam
if err = json.Unmarshal(r.Value, &rs); err != nil {
return
}
p.Value = rs
case Hash160:
var h util.Uint160
if err = json.Unmarshal(r.Value, &h); err != nil {
return
}
p.Value = h
case Hash256:
var h util.Uint256
if err = json.Unmarshal(r.Value, &h); err != nil {
return
}
p.Value = h
default:
return errors.New("not implemented")
}
return
}
// StackParams in an array of StackParam (TODO: drop it?).
type StackParams []StackParam
// TryParseArray converts an array of StackParam into an array of more appropriate things.
func (p StackParams) TryParseArray(vals ...interface{}) error {
var (
err error
i int
par StackParam
)
if len(p) != len(vals) {
return errors.New("receiver array doesn't fit the StackParams length")
}
for i, par = range p {
if err = par.TryParse(vals[i]); err != nil {
return err
}
}
return nil
}
// TryParse converts one StackParam into something more appropriate.
func (p StackParam) TryParse(dest interface{}) error {
var (
err error
ok bool
data []byte
)
switch p.Type {
case ByteArray:
if data, ok = p.Value.([]byte); !ok {
return errors.Errorf("failed to cast %s to []byte", p.Value)
}
switch dest := dest.(type) {
case *util.Uint160:
if *dest, err = util.Uint160DecodeBytes(data); err != nil {
return err
}
return nil
case *[]byte:
*dest = data
return nil
case *util.Uint256:
if *dest, err = util.Uint256DecodeReverseBytes(data); err != nil {
return err
}
return nil
case *int64, *int32, *int16, *int8, *int, *uint64, *uint32, *uint16, *uint8, *uint:
i := bytesToUint64(data)
switch dest := dest.(type) {
case *int64:
*dest = int64(i)
case *int32:
*dest = int32(i)
case *int16:
*dest = int16(i)
case *int8:
*dest = int8(i)
case *int:
*dest = int(i)
case *uint64:
*dest = i
case *uint32:
*dest = uint32(i)
case *uint16:
*dest = uint16(i)
case *uint8:
*dest = uint8(i)
case *uint:
*dest = uint(i)
}
case *string:
*dest = string(data)
return nil
default:
return errors.Errorf("cannot cast stackparam of type %s to type %s", p.Type, dest)
}
default:
return errors.New("cannot define stackparam type")
}
return nil
}
func bytesToUint64(b []byte) uint64 {
data := make([]byte, 8)
copy(data[8-len(b):], util.ArrayReverse(b))
return binary.BigEndian.Uint64(data)
}