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 (r *Array) IsReadOnly() bool
- func (i *Array) Len() int
- func (r *Array) MarkAsReadOnly()
- 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 Immutable
- 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
- func DecodeBinary(r *io.BinReader) Item
- func DecodeBinaryProtected(r *io.BinReader) Item
- func DeepCopy(item Item, asImmutable bool) Item
- func Deserialize(data []byte) (Item, error)
- func FromJSON(data []byte, maxCount int) (Item, error)
- func FromJSONWithTypes(data []byte) (Item, error)
- func Make(v interface{}) 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 (r *Map) IsReadOnly() bool
- func (i *Map) Len() int
- func (r *Map) MarkAsReadOnly()
- 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 SerializationContext
- 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 (r *Struct) IsReadOnly() bool
- func (i *Struct) Len() int
- func (r *Struct) MarkAsReadOnly()
- 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 a 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 a ByteArray for Equals method. // It is set to be the maximum uint16 value + 1. MaxByteArrayComparableSize = math.MaxUint16 + 1 // MaxKeySize is the maximum size of a 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 an encoded/decoded JSON.
Variables ¶
var ( // ErrInvalidConversion is returned upon an attempt to make an incorrect // conversion between item types. ErrInvalidConversion = errors.New("invalid conversion") // ErrTooBig is returned when an item exceeds some size constraints, like // the maximum allowed integer value of the number of elements in an array. It // can also be returned by serialization functions if the resulting // value exceeds MaxSize. ErrTooBig = errors.New("too big") // ErrReadOnly is returned on attempt to modify immutable stack item. ErrReadOnly = errors.New("item is read-only") )
var ErrInvalidType = errors.New("invalid type")
ErrInvalidType is returned upon attempts to deserialize some unknown item type.
var ErrInvalidValue = errors.New("invalid value")
ErrInvalidValue is returned when an item value doesn't fit some constraints during serialization or deserialization.
var ErrRecursive = errors.New("recursive item")
ErrRecursive is returned upon an attempt to serialize some recursive stack item (like an array including an item with the 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 upon an 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 the value size doesn't exceed the 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 the 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 the 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 the w. Instead, if an error (like recursive array) is encountered, it just writes the special InvalidT type of an element to the w.
func IsValidMapKey ¶
IsValidMapKey checks whether it's possible to use the 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) IsReadOnly ¶ added in v0.99.0
func (r *Array) IsReadOnly() bool
IsReadOnly implements Immutable interface.
func (*Array) MarkAsReadOnly ¶ added in v0.99.0
func (r *Array) MarkAsReadOnly()
MarkAsReadOnly implements immutable interface.
func (*Array) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Array) Remove ¶
Remove removes the element at `pos` index from Array value. It will panics on bad index.
func (*Array) TryInteger ¶
TryInteger implements the 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 the Item interface.
func (*BigInteger) Equals ¶
func (i *BigInteger) Equals(s Item) bool
Equals implements the 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 the Item interface.
func (*BigInteger) TryBytes ¶
func (i *BigInteger) TryBytes() ([]byte, error)
TryBytes implements the Item interface.
func (*BigInteger) TryInteger ¶
func (i *BigInteger) TryInteger() (*big.Int, error)
TryInteger implements the Item interface.
func (*BigInteger) Value ¶
func (i *BigInteger) Value() interface{}
Value implements the 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 the Item interface.
type Buffer ¶
type Buffer []byte
Buffer represents represents a Buffer stack item.
func (*Buffer) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Buffer) TryInteger ¶
TryInteger implements the 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 the Item interface.
type Convertible ¶ added in v0.96.0
Convertible is something that can be converted to/from Item.
type Immutable ¶ added in v0.99.0
type Immutable interface { IsReadOnly() bool MarkAsReadOnly() }
Immutable is an interface supported by compound types (Array, Map, Struct).
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 a new Interop object.
func (*Interop) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Interop) TryInteger ¶
TryInteger implements the 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 the 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 a 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 the Item from the given byte slice.
func FromJSON ¶
FromJSON decodes an 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 a 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 needed.
func NewMapWithValue ¶
func NewMapWithValue(value []MapElement) *Map
NewMapWithValue returns a new Map object filled with the specified value.
func (*Map) IsReadOnly ¶ added in v0.99.0
func (r *Map) IsReadOnly() bool
IsReadOnly implements Immutable interface.
func (*Map) MarkAsReadOnly ¶ added in v0.99.0
func (r *Map) MarkAsReadOnly()
MarkAsReadOnly implements immutable interface.
func (*Map) TryInteger ¶
TryInteger implements the 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 the Item interface. There is no need to perform a real copy here since Null has no internal state.
func (Null) TryInteger ¶
TryInteger implements the Item interface.
type Pointer ¶
type Pointer struct {
// contains filtered or unexported fields
}
Pointer represents a VM-level instruction pointer.
func NewPointer ¶
NewPointer returns a new pointer on the specified position.
func NewPointerWithHash ¶ added in v0.92.0
NewPointerWithHash returns a 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 calculation. This hash is then being used for pointer comparisons.
func (*Pointer) ScriptHash ¶
ScriptHash returns the pointer item hash.
func (*Pointer) TryInteger ¶
TryInteger implements the Item interface.
type SerializationContext ¶ added in v0.99.0
type SerializationContext struct {
// contains filtered or unexported fields
}
SerializationContext is a serialization context.
func NewSerializationContext ¶ added in v0.99.0
func NewSerializationContext() *SerializationContext
NewSerializationContext returns reusable stack item serialization context.
func (*SerializationContext) Serialize ¶ added in v0.99.0
func (w *SerializationContext) Serialize(item Item, protected bool) ([]byte, error)
Serialize returns flat slice of bytes with the given item. The process can be protected from bad elements if appropriate flag is given (otherwise an error is returned on encountering any of them). The buffer returned is only valid until the call to Serialize.
type Struct ¶
type Struct struct {
// contains filtered or unexported fields
}
Struct represents a struct on the stack.
func (*Struct) Clear ¶
func (i *Struct) Clear()
Clear removes all elements from the Struct item value.
func (*Struct) Clone ¶
Clone returns a Struct with all Struct fields copied by the value. Array fields are still copied by reference.
func (*Struct) IsReadOnly ¶ added in v0.99.0
func (r *Struct) IsReadOnly() bool
IsReadOnly implements Immutable interface.
func (*Struct) MarkAsReadOnly ¶ added in v0.99.0
func (r *Struct) MarkAsReadOnly()
MarkAsReadOnly implements immutable interface.
func (*Struct) Remove ¶
Remove removes the element at `pos` index from the Struct value. It will panic if a bad index given.
func (*Struct) TryInteger ¶
TryInteger implements the Item interface.
type Type ¶
type Type byte
Type represents a 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 the string.