-
Notifications
You must be signed in to change notification settings - Fork 79
/
helper.go
162 lines (152 loc) · 5.21 KB
/
helper.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
package client
import (
"crypto/elliptic"
"errors"
"fmt"
nns "github.com/nspcc-dev/neo-go/examples/nft-nd-nns"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// getInvocationError returns an error in case of bad VM state or empty stack.
func getInvocationError(result *result.Invoke) error {
if result.State != "HALT" {
return fmt.Errorf("invocation failed: %s", result.FaultException)
}
if len(result.Stack) == 0 {
return errors.New("result stack is empty")
}
return nil
}
// topBoolFromStack returns the top boolean value from stack.
func topBoolFromStack(st []stackitem.Item) (bool, error) {
index := len(st) - 1 // top stack element is last in the array
result, ok := st[index].Value().(bool)
if !ok {
return false, fmt.Errorf("invalid stack item type: %s", st[index].Type())
}
return result, nil
}
// topIntFromStack returns the top integer value from stack.
func topIntFromStack(st []stackitem.Item) (int64, error) {
index := len(st) - 1 // top stack element is last in the array
bi, err := st[index].TryInteger()
if err != nil {
return 0, err
}
return bi.Int64(), nil
}
// topPublicKeysFromStack returns the top array of public keys from stack.
func topPublicKeysFromStack(st []stackitem.Item) (keys.PublicKeys, error) {
index := len(st) - 1 // top stack element is last in the array
var (
pks keys.PublicKeys
err error
)
items, ok := st[index].Value().([]stackitem.Item)
if !ok {
return nil, fmt.Errorf("invalid stack item type: %s", st[index].Type())
}
pks = make(keys.PublicKeys, len(items))
for i, item := range items {
val, ok := item.Value().([]byte)
if !ok {
return nil, fmt.Errorf("invalid array element #%d: %s", i, item.Type())
}
pks[i], err = keys.NewPublicKeyFromBytes(val, elliptic.P256())
if err != nil {
return nil, err
}
}
return pks, nil
}
// top string from stack returns the top string from stack.
func topStringFromStack(st []stackitem.Item) (string, error) {
index := len(st) - 1 // top stack element is last in the array
bs, err := st[index].TryBytes()
if err != nil {
return "", err
}
return string(bs), nil
}
// topUint160FromStack returns the top util.Uint160 from stack.
func topUint160FromStack(st []stackitem.Item) (util.Uint160, error) {
index := len(st) - 1 // top stack element is last in the array
bs, err := st[index].TryBytes()
if err != nil {
return util.Uint160{}, err
}
return util.Uint160DecodeBytesBE(bs)
}
// topMapFromStack returns the top stackitem.Map from stack.
func topMapFromStack(st []stackitem.Item) (*stackitem.Map, error) {
index := len(st) - 1 // top stack element is last in the array
if t := st[index].Type(); t != stackitem.MapT {
return nil, fmt.Errorf("invalid return stackitem type: %s", t.String())
}
return st[index].(*stackitem.Map), nil
}
// topIterableFromStack returns top list of elements of `resultItemType` type from stack.
func topIterableFromStack(st []stackitem.Item, resultItemType interface{}) ([]interface{}, error) {
index := len(st) - 1 // top stack element is last in the array
if t := st[index].Type(); t != stackitem.InteropT {
return nil, fmt.Errorf("invalid return stackitem type: %s (InteropInterface expected)", t.String())
}
iter, ok := st[index].Value().(result.Iterator)
if !ok {
return nil, fmt.Errorf("failed to deserialize iterable from interop stackitem: invalid value type (Array expected)")
}
result := make([]interface{}, len(iter.Values))
for i := range iter.Values {
switch resultItemType.(type) {
case string:
bytes, err := iter.Values[i].TryBytes()
if err != nil {
return nil, fmt.Errorf("failed to deserialize string from stackitem #%d: %w", i, err)
}
result[i] = string(bytes)
case util.Uint160:
bytes, err := iter.Values[i].TryBytes()
if err != nil {
return nil, fmt.Errorf("failed to deserialize uint160 from stackitem #%d: %w", i, err)
}
result[i], err = util.Uint160DecodeBytesBE(bytes)
if err != nil {
return nil, fmt.Errorf("failed to decode uint160 from stackitem #%d: %w", i, err)
}
case nns.RecordState:
rs, ok := iter.Values[i].Value().([]stackitem.Item)
if !ok {
return nil, fmt.Errorf("failed to decode RecordState from stackitem #%d: not a struct", i)
}
if len(rs) != 3 {
return nil, fmt.Errorf("failed to decode RecordState from stackitem #%d: wrong number of elements", i)
}
name, err := rs[0].TryBytes()
if err != nil {
return nil, fmt.Errorf("failed to deocde RecordState from stackitem #%d: %w", i, err)
}
typ, err := rs[1].TryInteger()
if err != nil {
return nil, fmt.Errorf("failed to deocde RecordState from stackitem #%d: %w", i, err)
}
data, err := rs[2].TryBytes()
if err != nil {
return nil, fmt.Errorf("failed to deocde RecordState from stackitem #%d: %w", i, err)
}
u64Typ := typ.Uint64()
if !typ.IsUint64() || u64Typ > 255 {
return nil, fmt.Errorf("failed to deocde RecordState from stackitem #%d: bad type", i)
}
result[i] = nns.RecordState{
Name: string(name),
Type: nns.RecordType(u64Typ),
Data: string(data),
}
default:
return nil, errors.New("unsupported iterable type")
}
}
return result, nil
}