/
params.go
399 lines (379 loc) · 11.4 KB
/
params.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package utils
import (
"encoding/hex"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/ontio/ontology/common"
"github.com/ontio/ontology/core/payload"
)
const (
PARAMS_SPLIT = ","
PARAM_TYPE_SPLIT = ":"
PARAM_TYPE_ARRAY = "array"
PARAM_TYPE_BYTE_ARRAY = "bytearray"
PARAM_TYPE_STRING = "string"
PARAM_TYPE_INTEGER = "int"
PARAM_TYPE_BOOLEAN = "bool"
PARAM_TYPE_ADDRESS = "address"
PARAM_LEFT_BRACKET = "["
PARAM_RIGHT_BRACKET = "]"
PARAM_ESC_CHAR = `/`
)
var (
PARAM_TYPE_SPLIT_INC = string([]byte{0})
PARAM_SPLIT_INC = string([]byte{1})
)
//ParseParams return interface{} array of encode params item.
//A param item compose of type and value, type can be: bytearray, string, int, bool
//Param type and param value split with ":", such as int:10
//Param array can be express with "[]", such [int:10,string:foo], param array can be nested, such as [int:10,[int:12,bool:true]]
//A raw params example: string:foo,[int:0,[bool:true,string:bar],bool:false]
//Note that if string contain some special char like :,[,] and so one, please '/' char to escape. For example: string:did/:ed1e25c9dccae0c694ee892231407afa20b76008
func ParseParams(rawParamStr string) ([]interface{}, error) {
rawParams, _, err := parseRawParamsString(rawParamStr)
if err != nil {
return nil, err
}
return parseRawParams(rawParams)
}
func parseRawParamsString(rawParamStr string) ([]interface{}, int, error) {
if len(rawParamStr) == 0 {
return nil, 0, nil
}
rawParamItems := make([]interface{}, 0)
curRawParam := ""
index := 0
totalSize := len(rawParamStr)
isEscMode := false
for i := 0; i < totalSize; i++ {
s := string(rawParamStr[i])
if s == PARAM_ESC_CHAR {
if isEscMode {
isEscMode = false
curRawParam += s
} else {
isEscMode = true
}
continue
} else {
if isEscMode {
isEscMode = false
curRawParam += s
continue
}
}
switch s {
case PARAM_TYPE_SPLIT:
curRawParam += PARAM_TYPE_SPLIT_INC
case PARAMS_SPLIT:
curRawParam = strings.TrimSpace(curRawParam)
if len(curRawParam) > 0 {
rawParamItems = append(rawParamItems, curRawParam)
curRawParam = ""
}
case PARAM_LEFT_BRACKET:
if index == totalSize-1 {
return rawParamItems, 0, nil
}
//clear current param as invalid input
curRawParam = ""
items, size, err := parseRawParamsString(string(rawParamStr[i+1:]))
if err != nil {
return nil, 0, fmt.Errorf("parse params error:%s", err)
}
if len(items) > 0 {
rawParamItems = append(rawParamItems, items)
}
i += size
case PARAM_RIGHT_BRACKET:
curRawParam = strings.TrimSpace(curRawParam)
if len(curRawParam) > 0 {
rawParamItems = append(rawParamItems, curRawParam)
}
return rawParamItems, i + 1, nil
default:
curRawParam += s
}
}
curRawParam = strings.TrimSpace(curRawParam)
if len(curRawParam) != 0 {
rawParamItems = append(rawParamItems, curRawParam)
}
return rawParamItems, totalSize, nil
}
func parseRawParams(rawParams []interface{}) ([]interface{}, error) {
if len(rawParams) == 0 {
return nil, nil
}
params := make([]interface{}, 0)
for _, rawParam := range rawParams {
switch v := rawParam.(type) {
case string:
param, err := parseRawParam(v)
if err != nil {
return nil, err
}
params = append(params, param)
case []interface{}:
res, err := parseRawParams(v)
if err != nil {
return nil, err
}
params = append(params, res)
default:
return nil, fmt.Errorf("unknown param type:%s", reflect.TypeOf(rawParam))
}
}
return params, nil
}
func parseRawParam(rawParam string) (interface{}, error) {
rawParam = strings.TrimSpace(rawParam)
//rawParam = strings.Trim(rawParam, PARAMS_SPLIT)
if len(rawParam) == 0 {
return nil, nil
}
ps := strings.Split(rawParam, PARAM_TYPE_SPLIT_INC)
if len(ps) != 2 {
return nil, fmt.Errorf("invalid param:%s", rawParam)
}
pType := strings.TrimSpace(ps[0])
pValue := strings.TrimSpace(ps[1])
return parseRawParamValue(pType, pValue)
}
func parseRawParamValue(pType string, pValue string) (interface{}, error) {
switch strings.ToLower(pType) {
case PARAM_TYPE_BYTE_ARRAY:
value, err := hex.DecodeString(pValue)
if err != nil {
return nil, fmt.Errorf("parse byte array param:%s error:%s", pValue, err)
}
return value, nil
case PARAM_TYPE_STRING:
return pValue, nil
case PARAM_TYPE_INTEGER:
if pValue == "" {
return nil, fmt.Errorf("invalid integer")
}
value, err := strconv.ParseInt(pValue, 10, 64)
if err != nil {
return nil, fmt.Errorf("parse integer param:%s error:%s", pValue, err)
}
return value, nil
case PARAM_TYPE_BOOLEAN:
switch strings.ToLower(pValue) {
case "true":
return true, nil
case "false":
return false, nil
default:
return nil, fmt.Errorf("parse boolean param:%s failed", pValue)
}
case PARAM_TYPE_ADDRESS:
return common.AddressFromBase58(pValue)
default:
return nil, fmt.Errorf("unspport param type:%s", pType)
}
}
//ParseReturnValue return the value of rawReturnTypeStr type.
//Return type can be: bytearray, string, int, bool.
//Types can be split with "," each other, such as int,string,bool
//Type array can be express with "[]", such [int,string], param array can be nested, such as [int,[int,bool]]
func ParseReturnValue(rawValue interface{}, rawReturnTypeStr string, vmtype payload.VmType) ([]interface{}, error) {
returnTypes, _, err := parseRawParamsString(rawReturnTypeStr)
if err != nil {
return nil, fmt.Errorf("parse raw return types:%s error:%s", rawReturnTypeStr, err)
}
var rawValues []interface{}
rawValues, ok := rawValue.([]interface{})
if !ok {
rawValues = append(rawValues, rawValue)
}
if vmtype == payload.NEOVM_TYPE {
return parseReturnNeoValueArray(rawValues, returnTypes)
} else {
return parasReturnValueWasmArray(rawValues, returnTypes)
}
}
func parasReturnValueWasmArray(rawValues []interface{}, returnTypes []interface{}) ([]interface{}, error) {
values := make([]interface{}, 0)
for i := 0; i < len(rawValues); i++ {
rawValue := rawValues[i]
if i == len(returnTypes) {
return values, nil
}
valueType := returnTypes[i]
var err error
switch v := rawValue.(type) {
case string:
var value interface{}
vType := valueType.(string)
switch strings.ToLower(vType) {
case PARAM_TYPE_BYTE_ARRAY:
value, err = ParseWasmVMContractReturnTypeByteArray(v)
case PARAM_TYPE_STRING:
value, err = ParseWasmVMContractReturnTypeString(v)
case PARAM_TYPE_INTEGER:
value, err = ParseWasmVMContractReturnTypeInteger(v)
case PARAM_TYPE_BOOLEAN:
value, err = ParseWasmVMContractReturnTypeBool(v)
default:
return nil, fmt.Errorf("unknown return type:%s", v)
}
values = append(values, value)
if err != nil {
return nil, fmt.Errorf("parse return value:%s type:byte array error:%s", v, err)
}
case []interface{}:
valueTypes, ok := valueType.([]interface{})
if !ok {
return nil, fmt.Errorf("parse return value:%+v types:%s failed, types doesnot match", v, valueType)
}
values, err := parasReturnValueWasmArray(v, valueTypes)
if err != nil {
return nil, fmt.Errorf("parese return values:%+v types:%s error:%s", values, valueType, err)
}
default:
return nil, fmt.Errorf("unknown return type:%s", reflect.TypeOf(rawValue))
}
}
return values, nil
}
func parseReturnNeoValueArray(rawValues []interface{}, returnTypes []interface{}) ([]interface{}, error) {
values := make([]interface{}, 0)
for i := 0; i < len(rawValues); i++ {
rawValue := rawValues[i]
if i == len(returnTypes) {
return values, nil
}
valueType := returnTypes[i]
var err error
switch v := rawValue.(type) {
case string:
var value interface{}
vType := valueType.(string)
switch strings.ToLower(vType) {
case PARAM_TYPE_BYTE_ARRAY:
value, err = ParseNeoVMContractReturnTypeByteArray(v)
case PARAM_TYPE_STRING:
value, err = ParseNeoVMContractReturnTypeString(v)
case PARAM_TYPE_INTEGER:
value, err = ParseNeoVMContractReturnTypeInteger(v)
case PARAM_TYPE_BOOLEAN:
value, err = ParseNeoVMContractReturnTypeBool(v)
default:
return nil, fmt.Errorf("unknown return type:%s", v)
}
values = append(values, value)
if err != nil {
return nil, fmt.Errorf("parse return value:%s type:byte array error:%s", v, err)
}
case []interface{}:
valueTypes, ok := valueType.([]interface{})
if !ok {
return nil, fmt.Errorf("parse return value:%+v types:%s failed, types doesnot match", v, valueType)
}
values, err := parseReturnNeoValueArray(v, valueTypes)
if err != nil {
return nil, fmt.Errorf("parese return values:%+v types:%s error:%s", values, valueType, err)
}
default:
return nil, fmt.Errorf("unknown return type:%s", reflect.TypeOf(rawValue))
}
}
return values, nil
}
//NeoVMInvokeParam use to express the param to invoke neovm contract.
//Type can be of array, bytearray, string, int and bool
//If type is one of bytearray, string, int and bool, value must be a string
//If Type is array, value must be []*NeoVMInvokeParam
//Example:
//[]interface{}{
// &NeoVMInvokeParam{
// Type: "string",
// Value: "foo",
// },
// &NeoVMInvokeParam{
// Type: "array",
// Value: []interface{}{
// &NeoVMInvokeParam{
// Type: "int",
// Value: "0",
// },
// &NeoVMInvokeParam{
// Type: "bool",
// Value: "true",
// },
// },
// },
//}
type NeoVMInvokeParam struct {
Type string
Value interface{} //string or []*NeoVMInvokeParam
}
//ParseNeoVMInvokeParams parse params to []interface, rawParams is array of NeoVMInvokeParam
func ParseNeoVMInvokeParams(rawParams []interface{}) ([]interface{}, error) {
if len(rawParams) == 0 {
return nil, nil
}
params := make([]interface{}, 0)
for _, rawParam := range rawParams {
rawParamItem, ok := rawParam.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid param %v", rawParam)
}
for k, v := range rawParamItem {
rawParamItem[strings.ToLower(k)] = v
}
pType, ok := rawParamItem["type"]
if !ok {
return nil, fmt.Errorf("invalid param %v", rawParamItem)
}
pt, ok := pType.(string)
if !ok {
return nil, fmt.Errorf("invalid param %v", rawParamItem)
}
pValue, ok := rawParamItem["value"]
if !ok {
return nil, fmt.Errorf("invalid param %v", rawParamItem)
}
switch pv := pValue.(type) {
case string:
pv = strings.TrimSpace(pv)
param, err := parseRawParamValue(pt, pv)
if err != nil {
return nil, fmt.Errorf("Parse Param type:%s value:%s error:%s", pType, pv, err)
}
params = append(params, param)
case []interface{}:
ps, err := ParseNeoVMInvokeParams(pv)
if err != nil {
return nil, err
}
if len(ps) > 0 {
params = append(params, ps)
}
default:
return nil, fmt.Errorf("invalid param %v", rawParamItem)
}
}
return params, nil
}