Documentation ¶
Index ¶
- Constants
- Variables
- func CheckIntegerSize(value *big.Int) error
- func DeserializeConvertible(data []byte, conv Convertible) error
- func EncodeBinary(item Item, w *io.BinWriter)
- func EncodeBinaryProtected(item Item, w *io.BinWriter)
- func IsValidMapKey(key Item) error
- func Serialize(item Item) ([]byte, error)
- func SerializeConvertible(conv Convertible) ([]byte, error)
- func ToJSON(item Item) ([]byte, error)
- func ToJSONWithTypes(item Item) ([]byte, error)
- func ToString(item Item) (string, error)
- type Array
- func (i *Array) Append(item Item)
- func (i *Array) Clear()
- func (i *Array) Convert(typ Type) (Item, error)
- func (r *Array) DecRC() int
- func (i *Array) Dup() Item
- func (i *Array) Equals(s Item) bool
- func (r *Array) IncRC() int
- func (i *Array) Len() int
- func (i *Array) MarshalJSON() ([]byte, error)
- func (i *Array) Remove(pos int)
- func (i *Array) String() string
- func (i *Array) TryBool() (bool, error)
- func (i *Array) TryBytes() ([]byte, error)
- func (i *Array) TryInteger() (*big.Int, error)
- func (i *Array) Type() Type
- func (i *Array) Value() interface{}
- type BigInteger
- func (i *BigInteger) Big() *big.Int
- func (i *BigInteger) Bytes() []byte
- func (i *BigInteger) Convert(typ Type) (Item, error)
- func (i *BigInteger) Dup() Item
- func (i *BigInteger) Equals(s Item) bool
- func (i *BigInteger) MarshalJSON() ([]byte, error)
- func (i *BigInteger) String() string
- func (i *BigInteger) TryBool() (bool, error)
- func (i *BigInteger) TryBytes() ([]byte, error)
- func (i *BigInteger) TryInteger() (*big.Int, error)
- func (i *BigInteger) Type() Type
- func (i *BigInteger) Value() interface{}
- type Bool
- func (i Bool) Bytes() []byte
- func (i Bool) Convert(typ Type) (Item, error)
- func (i Bool) Dup() Item
- func (i Bool) Equals(s Item) bool
- func (i Bool) MarshalJSON() ([]byte, error)
- func (i Bool) String() string
- func (i Bool) TryBool() (bool, error)
- func (i Bool) TryBytes() ([]byte, error)
- func (i Bool) TryInteger() (*big.Int, error)
- func (i Bool) Type() Type
- func (i Bool) Value() interface{}
- type Buffer
- func (i *Buffer) Convert(typ Type) (Item, error)
- func (i *Buffer) Dup() Item
- func (i *Buffer) Equals(s Item) bool
- func (i *Buffer) Len() int
- func (i *Buffer) MarshalJSON() ([]byte, error)
- func (i *Buffer) String() string
- func (i *Buffer) TryBool() (bool, error)
- func (i *Buffer) TryBytes() ([]byte, error)
- func (i *Buffer) TryInteger() (*big.Int, error)
- func (i *Buffer) Type() Type
- func (i *Buffer) Value() interface{}
- type ByteArray
- func (i *ByteArray) Convert(typ Type) (Item, error)
- func (i *ByteArray) Dup() Item
- func (i *ByteArray) Equals(s Item) bool
- func (i *ByteArray) MarshalJSON() ([]byte, error)
- func (i *ByteArray) String() string
- func (i *ByteArray) TryBool() (bool, error)
- func (i ByteArray) TryBytes() ([]byte, error)
- func (i ByteArray) TryInteger() (*big.Int, error)
- func (i *ByteArray) Type() Type
- func (i *ByteArray) Value() interface{}
- type Convertible
- type Interop
- func (i *Interop) Convert(typ Type) (Item, error)
- func (i *Interop) Dup() Item
- func (i *Interop) Equals(s Item) bool
- func (i *Interop) MarshalJSON() ([]byte, error)
- func (i *Interop) String() string
- func (i *Interop) TryBool() (bool, error)
- func (i *Interop) TryBytes() ([]byte, error)
- func (i *Interop) TryInteger() (*big.Int, error)
- func (i *Interop) Type() Type
- func (i *Interop) Value() interface{}
- type Item
- type Map
- func (i *Map) Add(key, value Item)
- func (i *Map) Clear()
- func (i *Map) Convert(typ Type) (Item, error)
- func (r *Map) DecRC() int
- func (i *Map) Drop(index int)
- func (i *Map) Dup() Item
- func (i *Map) Equals(s Item) bool
- func (i *Map) Has(key Item) bool
- func (r *Map) IncRC() int
- func (i *Map) Index(key Item) int
- func (i *Map) Len() int
- func (i *Map) String() string
- func (i *Map) TryBool() (bool, error)
- func (i *Map) TryBytes() ([]byte, error)
- func (i *Map) TryInteger() (*big.Int, error)
- func (i *Map) Type() Type
- func (i *Map) Value() interface{}
- type MapElement
- type Null
- func (i Null) Convert(typ Type) (Item, error)
- func (i Null) Dup() Item
- func (i Null) Equals(s Item) bool
- func (i Null) String() string
- func (i Null) TryBool() (bool, error)
- func (i Null) TryBytes() ([]byte, error)
- func (i Null) TryInteger() (*big.Int, error)
- func (i Null) Type() Type
- func (i Null) Value() interface{}
- type Pointer
- func (p *Pointer) Convert(typ Type) (Item, error)
- func (p *Pointer) Dup() Item
- func (p *Pointer) Equals(s Item) bool
- func (p *Pointer) Position() int
- func (p *Pointer) ScriptHash() util.Uint160
- func (p *Pointer) String() string
- func (p *Pointer) TryBool() (bool, error)
- func (p *Pointer) TryBytes() ([]byte, error)
- func (p *Pointer) TryInteger() (*big.Int, error)
- func (p *Pointer) Type() Type
- func (p *Pointer) Value() interface{}
- type Struct
- func (i *Struct) Append(item Item)
- func (i *Struct) Clear()
- func (i *Struct) Clone() (*Struct, error)
- func (i *Struct) Convert(typ Type) (Item, error)
- func (r *Struct) DecRC() int
- func (i *Struct) Dup() Item
- func (i *Struct) Equals(s Item) bool
- func (r *Struct) IncRC() int
- func (i *Struct) Len() int
- func (i *Struct) Remove(pos int)
- func (i *Struct) String() string
- func (i *Struct) TryBool() (bool, error)
- func (i *Struct) TryBytes() ([]byte, error)
- func (i *Struct) TryInteger() (*big.Int, error)
- func (i *Struct) Type() Type
- func (i *Struct) Value() interface{}
- type Type
Constants ¶
const ( // MaxBigIntegerSizeBits is the maximum size of BigInt item in bits. MaxBigIntegerSizeBits = 32 * 8 // MaxSize is the maximum item size allowed in the VM. MaxSize = 1024 * 1024 // MaxComparableNumOfItems is the maximum number of items that can be compared for structs. MaxComparableNumOfItems = MaxDeserialized // MaxClonableNumOfItems is the maximum number of items that can be cloned in structs. MaxClonableNumOfItems = MaxDeserialized // MaxByteArrayComparableSize is the maximum allowed length of ByteArray for Equals method. // It is set to be the maximum uint16 value. MaxByteArrayComparableSize = math.MaxUint16 // MaxKeySize is the maximum size of map key. MaxKeySize = 64 )
const MaxAllowedInteger = 2<<53 - 1
MaxAllowedInteger is the maximum integer allowed to be encoded.
const MaxDeserialized = 2048
MaxDeserialized is the maximum number one deserialized item can contain (including itself).
const MaxJSONDepth = 10
MaxJSONDepth is the maximum allowed nesting level of encoded/decoded JSON.
Variables ¶
var ( // ErrInvalidConversion is returned on attempt to make an incorrect // conversion between item types. ErrInvalidConversion = errors.New("invalid conversion") // ErrTooBig is returned when item exceeds some size constraints like // maximum allowed integer value of number of elements in array. It // can also be returned by serialization functions if resulting // value exceeds MaxSize. ErrTooBig = errors.New("too big") )
var ErrInvalidType = errors.New("invalid type")
ErrInvalidType is returned on attempts to deserialize some unknown item type.
var ErrInvalidValue = errors.New("invalid value")
ErrInvalidValue is returned when item value doesn't fit some constraints during serialization or deserialization.
var ErrRecursive = errors.New("recursive item")
ErrRecursive is returned on attempts to serialize some recursive stack item (like array including an item with reference to the same array).
var ErrTooDeep = errors.New("too deep")
ErrTooDeep is returned when JSON encoder/decoder goes beyond MaxJSONDepth in its processing.
var ErrUnserializable = errors.New("unserializable")
ErrUnserializable is returned on attempt to serialize some item that can't be serialized (like Interop item or Pointer).
Functions ¶
func CheckIntegerSize ¶ added in v0.98.2
CheckIntegerSize checks that value size doesn't exceed VM limit for Interer.
func DeserializeConvertible ¶ added in v0.96.0
func DeserializeConvertible(data []byte, conv Convertible) error
DeserializeConvertible deserializes Convertible from a slice of bytes.
func EncodeBinary ¶ added in v0.95.4
EncodeBinary encodes given Item into the given BinWriter. It's similar to io.Serializable's EncodeBinary, but works with Item interface.
func EncodeBinaryProtected ¶ added in v0.95.4
EncodeBinaryProtected encodes given Item into the given BinWriter. It's similar to EncodeBinary but allows to encode interop items (only type, value is lost) and doesn't return any errors in w, instead if error (like recursive array) is encountered it just writes special InvalidT type of element to w.
func IsValidMapKey ¶
IsValidMapKey checks whether it's possible to use given Item as a Map key.
func SerializeConvertible ¶ added in v0.96.0
func SerializeConvertible(conv Convertible) ([]byte, error)
SerializeConvertible serializes Convertible into a slice of bytes.
func ToJSON ¶
ToJSON encodes Item to JSON. It behaves as following:
ByteArray -> base64 string BigInteger -> number Bool -> bool Null -> null Array, Struct -> array Map -> map with keys as UTF-8 bytes
func ToJSONWithTypes ¶ added in v0.91.0
ToJSONWithTypes serializes any stackitem to JSON in a lossless way.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
Array represents a new Array object.
func (*Array) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Array) Remove ¶
Remove removes element at `pos` index from Array value. It will panics on bad index.
func (*Array) TryInteger ¶
TryInteger implements Item interface.
type BigInteger ¶
BigInteger represents a big integer on the stack.
func NewBigInteger ¶
func NewBigInteger(value *big.Int) *BigInteger
NewBigInteger returns an new BigInteger object.
func (*BigInteger) Big ¶ added in v0.97.2
func (i *BigInteger) Big() *big.Int
Big casts i to the big.Int type.
func (*BigInteger) Bytes ¶
func (i *BigInteger) Bytes() []byte
Bytes converts i to a slice of bytes.
func (*BigInteger) Convert ¶
func (i *BigInteger) Convert(typ Type) (Item, error)
Convert implements Item interface.
func (*BigInteger) Equals ¶
func (i *BigInteger) Equals(s Item) bool
Equals implements Item interface.
func (*BigInteger) MarshalJSON ¶
func (i *BigInteger) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (*BigInteger) String ¶
func (i *BigInteger) String() string
func (*BigInteger) TryBool ¶ added in v0.92.0
func (i *BigInteger) TryBool() (bool, error)
TryBool implements Item interface.
func (*BigInteger) TryBytes ¶
func (i *BigInteger) TryBytes() ([]byte, error)
TryBytes implements Item interface.
func (*BigInteger) TryInteger ¶
func (i *BigInteger) TryInteger() (*big.Int, error)
TryInteger implements Item interface.
func (*BigInteger) Value ¶
func (i *BigInteger) Value() interface{}
Value implements Item interface.
type Bool ¶
type Bool bool
Bool represents a boolean Item.
func (Bool) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Bool) TryInteger ¶
TryInteger implements Item interface.
type Buffer ¶
type Buffer []byte
Buffer represents represents Buffer stack item.
func (*Buffer) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Buffer) TryInteger ¶
TryInteger implements Item interface.
type ByteArray ¶
type ByteArray []byte
ByteArray represents a byte array on the stack.
func NewByteArray ¶
NewByteArray returns an new ByteArray object.
func (*ByteArray) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (ByteArray) TryInteger ¶
TryInteger implements Item interface.
type Convertible ¶ added in v0.96.0
Convertible is something that can be converted to/from Item.
type Interop ¶
type Interop struct {
// contains filtered or unexported fields
}
Interop represents interop data on the stack.
func NewInterop ¶
func NewInterop(value interface{}) *Interop
NewInterop returns new Interop object.
func (*Interop) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Interop) TryInteger ¶
TryInteger implements Item interface.
type Item ¶
type Item interface { fmt.Stringer Value() interface{} // Dup duplicates current Item. Dup() Item // TryBool converts Item to a boolean value. TryBool() (bool, error) // TryBytes converts Item to a byte slice. If the underlying type is a // byte slice, it's returned as is without copying. TryBytes() ([]byte, error) // TryInteger converts Item to an integer. TryInteger() (*big.Int, error) // Equals checks if 2 StackItems are equal. Equals(s Item) bool // Type returns stack item type. Type() Type // Convert converts Item to another type. Convert(Type) (Item, error) }
Item represents the "real" value that is pushed on the stack.
func DecodeBinary ¶ added in v0.95.4
DecodeBinary decodes previously serialized Item from the given reader. It's similar to the io.Serializable's DecodeBinary(), but implemented as a function because Item itself is an interface. Caveat: always check reader's error value before using the returned Item.
func DecodeBinaryProtected ¶ added in v0.95.4
DecodeBinaryProtected is similar to DecodeBinary but allows Interop and Invalid values to be present (making it symmetric to EncodeBinaryProtected).
func DeepCopy ¶ added in v0.91.0
DeepCopy returns new deep copy of the provided item. Values of Interop items are not deeply copied. It does preserve duplicates only for non-primitive types.
func Deserialize ¶ added in v0.95.4
Deserialize decodes Item from the given byte slice.
func FromJSON ¶
FromJSON decodes Item from JSON. It behaves as following:
string -> ByteArray from base64 number -> BigInteger bool -> Bool null -> Null array -> Array map -> Map, keys are UTF-8
func FromJSONWithTypes ¶ added in v0.91.0
FromJSONWithTypes deserializes an item from typed-json representation.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map represents Map object. It's ordered, so we use slice representation which should be fine for maps with less than 32 or so elements. Given that our VM has quite low limit of overall stack items, it should be good enough, but it can be extended with a real map for fast random access in the future if need be.
func NewMapWithValue ¶
func NewMapWithValue(value []MapElement) *Map
NewMapWithValue returns new Map object filled with specified value.
func (*Map) TryInteger ¶
TryInteger implements Item interface.
type MapElement ¶
MapElement is a key-value pair of StackItems.
type Null ¶
type Null struct{}
Null represents null on the stack.
func (Null) Dup ¶
Dup implements Item interface. There is no need to perform a real copy here, as Null has no internal state.
func (Null) TryInteger ¶
TryInteger implements Item interface.
type Pointer ¶
type Pointer struct {
// contains filtered or unexported fields
}
Pointer represents VM-level instruction pointer.
func NewPointer ¶
NewPointer returns new pointer on the specified position.
func NewPointerWithHash ¶ added in v0.92.0
NewPointerWithHash returns new pointer on the specified position of the specified script. It differs from NewPointer in that the script hash is being passed explicitly to save on hash calculcation. This hash is then being used for pointer comparisons.
func (*Pointer) ScriptHash ¶
ScriptHash returns pointer item hash.
func (*Pointer) TryInteger ¶
TryInteger implements Item interface.
type Struct ¶
type Struct struct {
// contains filtered or unexported fields
}
Struct represents a struct on the stack.
func (*Struct) Clone ¶
Clone returns a Struct with all Struct fields copied by value. Array fields are still copied by reference.
func (*Struct) Remove ¶
Remove removes element at `pos` index from Struct value. It will panics on bad index.
func (*Struct) TryInteger ¶
TryInteger implements Item interface.
type Type ¶
type Type byte
Type represents type of the stack item.
const ( AnyT Type = 0x00 PointerT Type = 0x10 BooleanT Type = 0x20 IntegerT Type = 0x21 ByteArrayT Type = 0x28 BufferT Type = 0x30 ArrayT Type = 0x40 StructT Type = 0x41 MapT Type = 0x48 InteropT Type = 0x60 InvalidT Type = 0xFF )
This block defines all known stack item types.
func FromString ¶ added in v0.91.0
FromString returns stackitem type from string.