2021-03-19 09:11:48 +00:00
|
|
|
/*
|
2022-04-20 18:30:09 +00:00
|
|
|
Package std provides an interface to StdLib native contract.
|
2021-03-19 09:11:48 +00:00
|
|
|
It implements various useful conversion functions.
|
|
|
|
*/
|
2021-03-03 15:59:10 +00:00
|
|
|
package std
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-12-08 19:33:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
|
2021-03-03 15:59:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hash represents StdLib contract hash.
|
|
|
|
const Hash = "\xc0\xef\x39\xce\xe0\xe4\xe9\x25\xc6\xc2\xa0\x6a\x79\xe1\x44\x0d\xd8\x6f\xce\xac"
|
|
|
|
|
|
|
|
// Serialize calls `serialize` method of StdLib native contract and serializes
|
|
|
|
// any given item into a byte slice. It works for all regular VM types (not ones
|
2022-04-20 18:30:09 +00:00
|
|
|
// from interop package) and allows to save them in the storage or pass them into Notify
|
2021-03-03 15:59:10 +00:00
|
|
|
// and then Deserialize them on the next run or in the external event receiver.
|
2023-04-03 10:34:24 +00:00
|
|
|
func Serialize(item any) []byte {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "serialize", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
item).([]byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize calls `deserialize` method of StdLib native contract and unpacks
|
2022-04-20 18:30:09 +00:00
|
|
|
// a previously serialized value from a byte slice, it's the opposite of Serialize.
|
2023-04-03 10:34:24 +00:00
|
|
|
func Deserialize(b []byte) any {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "deserialize", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
b)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// JSONSerialize serializes a value to json. It uses `jsonSerialize` method of StdLib native
|
2021-03-03 15:59:10 +00:00
|
|
|
// contract.
|
|
|
|
// Serialization format is the following:
|
|
|
|
// []byte -> base64 string
|
|
|
|
// bool -> json boolean
|
|
|
|
// nil -> Null
|
|
|
|
// string -> base64 encoded sequence of underlying bytes
|
|
|
|
// (u)int* -> integer, only value in -2^53..2^53 are allowed
|
|
|
|
// []interface{} -> json array
|
2023-04-03 10:34:24 +00:00
|
|
|
// []any -> json array
|
2021-03-03 15:59:10 +00:00
|
|
|
// map[type1]type2 -> json object with string keys marshaled as strings (not base64).
|
2023-04-03 10:34:24 +00:00
|
|
|
func JSONSerialize(item any) []byte {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "jsonSerialize", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
item).([]byte)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// JSONDeserialize deserializes a value from json. It uses `jsonDeserialize` method of StdLib
|
2021-03-03 15:59:10 +00:00
|
|
|
// native contract.
|
|
|
|
// It performs deserialization as follows:
|
2022-08-08 10:23:21 +00:00
|
|
|
//
|
|
|
|
// strings -> []byte (string) from base64
|
|
|
|
// integers -> (u)int* types
|
|
|
|
// null -> interface{}(nil)
|
|
|
|
// arrays -> []interface{}
|
|
|
|
// maps -> map[string]interface{}
|
2023-04-03 10:34:24 +00:00
|
|
|
func JSONDeserialize(data []byte) any {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "jsonDeserialize", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base64Encode calls `base64Encode` method of StdLib native contract and encodes
|
2022-04-20 18:30:09 +00:00
|
|
|
// the given byte slice into a base64 string and returns byte representation of this
|
2021-03-03 15:59:10 +00:00
|
|
|
// string.
|
|
|
|
func Base64Encode(b []byte) string {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "base64Encode", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
b).(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base64Decode calls `base64Decode` method of StdLib native contract and decodes
|
2022-04-20 18:30:09 +00:00
|
|
|
// the given base64 string represented as a byte slice into byte slice.
|
2021-03-03 15:59:10 +00:00
|
|
|
func Base64Decode(b []byte) []byte {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "base64Decode", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
b).([]byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base58Encode calls `base58Encode` method of StdLib native contract and encodes
|
2022-04-20 18:30:09 +00:00
|
|
|
// the given byte slice into a base58 string and returns byte representation of this
|
2021-03-03 15:59:10 +00:00
|
|
|
// string.
|
|
|
|
func Base58Encode(b []byte) string {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "base58Encode", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
b).(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base58Decode calls `base58Decode` method of StdLib native contract and decodes
|
2022-04-20 18:30:09 +00:00
|
|
|
// the given base58 string represented as a byte slice into a new byte slice.
|
2021-03-03 15:59:10 +00:00
|
|
|
func Base58Decode(b []byte) []byte {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "base58Decode", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
b).([]byte)
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:06:37 +00:00
|
|
|
// Base58CheckEncode calls `base58CheckEncode` method of StdLib native contract and encodes
|
2022-04-20 18:30:09 +00:00
|
|
|
// the given byte slice into a base58 string with checksum and returns byte representation of this
|
2021-05-25 15:06:37 +00:00
|
|
|
// string.
|
|
|
|
func Base58CheckEncode(b []byte) string {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "base58CheckEncode", int(contract.NoneFlag),
|
2021-05-25 15:06:37 +00:00
|
|
|
b).(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base58CheckDecode calls `base58CheckDecode` method of StdLib native contract and decodes
|
2022-04-20 18:30:09 +00:00
|
|
|
// thr given base58 string with a checksum represented as a byte slice into a new byte slice.
|
2021-05-25 15:06:37 +00:00
|
|
|
func Base58CheckDecode(b []byte) []byte {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "base58CheckDecode", int(contract.NoneFlag),
|
2021-05-25 15:06:37 +00:00
|
|
|
b).([]byte)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Itoa converts num in the given base to a string. Base should be either 10 or 16.
|
2021-03-03 15:59:10 +00:00
|
|
|
// It uses `itoa` method of StdLib native contract.
|
|
|
|
func Itoa(num int, base int) string {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "itoa", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
num, base).(string)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Itoa10 converts num in base 10 to a string.
|
2021-04-30 08:22:00 +00:00
|
|
|
// It uses `itoa` method of StdLib native contract.
|
|
|
|
func Itoa10(num int) string {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "itoa", int(contract.NoneFlag),
|
2021-04-30 08:22:00 +00:00
|
|
|
num).(string)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Atoi converts a string to a number in the given base. Base should be either 10 or 16.
|
2021-03-03 15:59:10 +00:00
|
|
|
// It uses `atoi` method of StdLib native contract.
|
|
|
|
func Atoi(s string, base int) int {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "atoi", int(contract.NoneFlag),
|
2021-03-03 15:59:10 +00:00
|
|
|
s, base).(int)
|
|
|
|
}
|
2021-04-30 08:22:00 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Atoi10 converts a string to a number in base 10.
|
2021-04-30 08:22:00 +00:00
|
|
|
// It uses `atoi` method of StdLib native contract.
|
|
|
|
func Atoi10(s string) int {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "atoi", int(contract.NoneFlag),
|
2021-04-30 08:22:00 +00:00
|
|
|
s).(int)
|
|
|
|
}
|
2021-04-30 12:02:44 +00:00
|
|
|
|
|
|
|
// MemoryCompare is similar to bytes.Compare:
|
|
|
|
// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
|
|
|
|
// It uses `memoryCompare` method of StdLib native contract.
|
|
|
|
func MemoryCompare(s1, s2 []byte) int {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "memoryCompare", int(contract.NoneFlag),
|
2021-04-30 12:02:44 +00:00
|
|
|
s1, s2).(int)
|
|
|
|
}
|
2021-04-30 12:33:07 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MemorySearch returns the index of the first occurrence of the val in the mem.
|
2021-04-30 12:33:07 +00:00
|
|
|
// If not found, -1 is returned. It uses `memorySearch` method of StdLib native contract.
|
|
|
|
func MemorySearch(mem, pattern []byte) int {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "memorySearch", int(contract.NoneFlag),
|
2021-04-30 12:33:07 +00:00
|
|
|
mem, pattern).(int)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MemorySearchIndex returns the index of the first occurrence of the val in the mem starting from the start.
|
2021-04-30 12:33:07 +00:00
|
|
|
// If not found, -1 is returned. It uses `memorySearch` method of StdLib native contract.
|
|
|
|
func MemorySearchIndex(mem, pattern []byte, start int) int {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "memorySearch", int(contract.NoneFlag),
|
2021-04-30 12:33:07 +00:00
|
|
|
mem, pattern, start).(int)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MemorySearchLastIndex returns the index of the last occurrence of the val in the mem ending before start.
|
2021-04-30 12:33:07 +00:00
|
|
|
// If not found, -1 is returned. It uses `memorySearch` method of StdLib native contract.
|
|
|
|
func MemorySearchLastIndex(mem, pattern []byte, start int) int {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "memorySearch", int(contract.NoneFlag),
|
2021-04-30 12:33:07 +00:00
|
|
|
mem, pattern, start, true).(int)
|
|
|
|
}
|
2021-04-30 14:00:29 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// StringSplit splits s by occurrences of the sep.
|
2021-04-30 14:00:29 +00:00
|
|
|
// It uses `stringSplit` method of StdLib native contract.
|
|
|
|
func StringSplit(s, sep string) []string {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "stringSplit", int(contract.NoneFlag),
|
2021-04-30 14:00:29 +00:00
|
|
|
s, sep).([]string)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// StringSplitNonEmpty splits s by occurrences of the sep and returns a list of non-empty items.
|
2021-04-30 14:00:29 +00:00
|
|
|
// It uses `stringSplit` method of StdLib native contract.
|
|
|
|
func StringSplitNonEmpty(s, sep string) []string {
|
2021-12-08 19:33:03 +00:00
|
|
|
return neogointernal.CallWithToken(Hash, "stringSplit", int(contract.NoneFlag),
|
2021-04-30 14:00:29 +00:00
|
|
|
s, sep, true).([]string)
|
|
|
|
}
|