stdlib

class StdLib

Bases: object

A class used to represent StdLib native contract.

Check out Neo’s Documentation to learn more about the StdLib class.

hash: UInt160
classmethod serialize(item: Any) bytes

Serializes the given value into its bytes representation.

>>> StdLib.serialize('42')
b'(\x0242'
>>> StdLib.serialize(42)
b'!\x01*'
>>> StdLib.serialize([2, 3, 5, 7])
b'@\x04!\x01\x02!\x01\x03!\x01\x05!\x01\x07'
>>> StdLib.serialize({1: 1, 2: 1, 3: 2})
b'H\x03!\x01\x01!\x01\x01!\x01\x02!\x01\x01!\x01\x03!\x01\x02'
Parameters:

item (Any) – value to be serialized

Returns:

the serialized value

Return type:

bytes

Raises:

Exception – raised if the item’s type is not serializable.

classmethod deserialize(data: bytes) Any

Deserializes the given bytes value.

>>> StdLib.deserialize(b'(\x0242')
'42'
>>> StdLib.deserialize(b'!\x01*')
42
>>> StdLib.deserialize(b'@\x04!\x01\x02!\x01\x03!\x01\x05!\x01\x07')
[2, 3, 5, 7]
>>> StdLib.deserialize(b'H\x03!\x01\x01!\x01\x01!\x01\x02!\x01\x01!\x01\x03!\x01\x02')
{1: 1, 2: 1, 3: 2}
Parameters:

data (bytes) – serialized value

Returns:

the deserialized result

Return type:

Any

Raises:

Exception – raised when the date doesn’t represent a serialized value.

classmethod json_serialize(item: Any) str

Serializes an item into a json.

>>> StdLib.json_serialize({'one': 1, 'two': 2, 'three': 3})
'{"one":1,"two":2,"three":3}'
Parameters:

item (Any) – The item that will be serialized

Returns:

The serialized item

Return type:

str

Raises:

Exception – raised if the item is an integer value out of the Neo’s accepted range, is a dictionary with a bytearray key, or isn’t serializable.

classmethod json_deserialize(json: str) Any

Deserializes a json into some valid type.

>>> StdLib.json_deserialize('{"one":1,"two":2,"three":3}')
{'one': 1, 'three': 3, 'two': 2}
Parameters:

json (str) – A json that will be deserialized

Returns:

The deserialized json

Return type:

Any

Raises:

Exception – raised if jsons deserialization is not valid.

classmethod base64_decode(key: str) bytes

Decodes a string value encoded with base64.

>>> StdLib.base64_decode("dW5pdCB0ZXN0")
b"unit test"
Parameters:

key (str) – string value to be decoded

Returns:

the decoded string

Return type:

bytes

classmethod base64_encode(key: bytes) str

Encodes a bytes value using base64.

>>> StdLib.base64_encode(b'unit test')
b"dW5pdCB0ZXN0"
Parameters:

key (bytes) – bytes value to be encoded

Returns:

the encoded string

Return type:

str

classmethod base58_decode(key: str) bytes

Decodes a string value encoded with base58.

>>> StdLib.base58_decode('2VhL46g69A1mu')
b"unit test"
Parameters:

key (str) – string value to be decoded

Returns:

the decoded bytes

Return type:

bytes

classmethod base58_encode(key: bytes) str

Encodes a bytes value using base58.

>>> StdLib.base58_encode(b'unit test')
b"2VhL46g69A1mu"
Parameters:

key (bytes) – bytes value to be encoded

Returns:

the encoded string

Return type:

str

classmethod base58_check_decode(key: str) bytes

Converts the specified str, which encodes binary data as base-58 digits, to an equivalent bytes value. The encoded str contains the checksum of the binary data.

>>> StdLib.base58_check_decode('AnJcKqvgBwKxsjX75o')
b"unit test"
Parameters:

key (str) – string value to be decoded

Returns:

the decoded bytes

Return type:

bytes

classmethod base58_check_encode(key: bytes) str

Converts a bytes value to its equivalent str representation that is encoded with base-58 digits. The encoded str contains the checksum of the binary data.

>>> StdLib.base58_check_encode(b'unit test')
b"AnJcKqvgBwKxsjX75o"
Parameters:

key (bytes) – bytes value to be encoded

Returns:

the encoded string

Return type:

str

classmethod itoa(value: int, base: int = 10) str

Converts the specific type of value to a decimal or hexadecimal string. The default is decimal.

>>> StdLib.itoa(10)
'10'
>>> StdLib.itoa(123)
'123'
>>> StdLib.itoa(-1, 16)
'f'
>>> StdLib.itoa(15, 16)
'0f'
Parameters:
  • value (int) – the int value

  • base (int) – the value’s base

Returns:

the converted string

Return type:

int

classmethod atoi(value: str, base: int = 10) int

Converts a character string to a specific base value, decimal or hexadecimal. The default is decimal.

>>> StdLib.atoi('10')
10
>>> StdLib.atoi('123')
123
>>> StdLib.atoi('1f', 16)
31
>>> StdLib.atoi('ff', 16)
-1
Parameters:
  • value (str) – the int value as a string

  • base (int) – the value base

Returns:

the equivalent value

Return type:

int

Raises:

Exception – raised when base isn’t 10 or 16.

classmethod memory_compare(mem1: Union[bytes, str], mem2: Union[bytes, str]) int

Compares a memory with another one.

>>> StdLib.memory_compare('abc', 'abc')
0
>>> StdLib.memory_compare('ABC', 'abc')
-1
>>> StdLib.memory_compare('abc', 'ABC')
1
Parameters:
  • mem1 (bytes or str) – a memory to be compared to another one

  • mem2 (bytes or str) – a memory that will be compared with another one

Returns:

-1 if mem1 precedes mem2, 0 if mem1 and mem2 are equal, 1 if mem1 follows mem2

Return type:

int

Searches for a given value in a given memory.

>>> StdLib.memory_search('abcde', 'a', 0)
0
>>> StdLib.memory_search('abcde', 'e', 0)
4
Parameters:
  • mem (bytes or str) – the memory

  • value (bytes or str) – the value

  • start (int) – the index the search should start from

  • backward (bool) – whether it should invert the memory

Returns:

the index of the value in the memory. Returns -1 if it’s not found

Return type:

int