-
Notifications
You must be signed in to change notification settings - Fork 77
/
oracle_types.go
86 lines (77 loc) · 1.89 KB
/
oracle_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
package native
import (
"crypto/elliptic"
"errors"
"math/big"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// IDList is a list of oracle request IDs.
type IDList []uint64
// NodeList represents list or oracle nodes.
type NodeList keys.PublicKeys
// ToStackItem implements stackitem.Convertible. It never returns an error.
func (l IDList) ToStackItem() (stackitem.Item, error) {
arr := make([]stackitem.Item, len(l))
for i := range l {
arr[i] = stackitem.NewBigInteger(new(big.Int).SetUint64(l[i]))
}
return stackitem.NewArray(arr), nil
}
// FromStackItem implements stackitem.Convertible.
func (l *IDList) FromStackItem(it stackitem.Item) error {
arr, ok := it.Value().([]stackitem.Item)
if !ok {
return errors.New("not an array")
}
*l = make(IDList, len(arr))
for i := range arr {
bi, err := arr[i].TryInteger()
if err != nil {
return err
}
(*l)[i] = bi.Uint64()
}
return nil
}
// Remove removes id from list.
func (l *IDList) Remove(id uint64) bool {
for i := range *l {
if id == (*l)[i] {
if i < len(*l) {
copy((*l)[i:], (*l)[i+1:])
}
*l = (*l)[:len(*l)-1]
return true
}
}
return false
}
// ToStackItem implements stackitem.Convertible. It never returns an error.
func (l NodeList) ToStackItem() (stackitem.Item, error) {
arr := make([]stackitem.Item, len(l))
for i := range l {
arr[i] = stackitem.NewByteArray(l[i].Bytes())
}
return stackitem.NewArray(arr), nil
}
// FromStackItem implements stackitem.Convertible.
func (l *NodeList) FromStackItem(it stackitem.Item) error {
arr, ok := it.Value().([]stackitem.Item)
if !ok {
return errors.New("not an array")
}
*l = make(NodeList, len(arr))
for i := range arr {
bs, err := arr[i].TryBytes()
if err != nil {
return err
}
pub, err := keys.NewPublicKeyFromBytes(bs, elliptic.P256())
if err != nil {
return err
}
(*l)[i] = pub
}
return nil
}