2020-06-16 07:56:06 +00:00
|
|
|
/*
|
|
|
|
Package binary provides binary serialization routines.
|
|
|
|
*/
|
|
|
|
package binary
|
|
|
|
|
2021-02-05 16:02:09 +00:00
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
|
|
|
|
)
|
|
|
|
|
2020-06-16 07:56:06 +00:00
|
|
|
// Serialize serializes any given item into a byte slice. It works for all
|
|
|
|
// regular VM types (not ones from interop package) and allows to save them in
|
|
|
|
// storage or pass into Notify and then Deserialize them on the next run or in
|
|
|
|
// the external event receiver. It uses `System.Binary.Serialize` syscall.
|
|
|
|
func Serialize(item interface{}) []byte {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall1("System.Binary.Serialize", item).([]byte)
|
2020-06-16 07:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize unpacks previously serialized value from a byte slice, it's the
|
|
|
|
// opposite of Serialize. It uses `System.Binary.Deserialize` syscall.
|
|
|
|
func Deserialize(b []byte) interface{} {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall1("System.Binary.Deserialize", b)
|
2020-06-16 07:56:06 +00:00
|
|
|
}
|
2020-07-15 08:49:24 +00:00
|
|
|
|
|
|
|
// Base64Encode encodes given byte slice into a base64 string and returns byte
|
|
|
|
// representation of this string. It uses `System.Binary.Base64Encode` interop.
|
2020-09-08 10:48:33 +00:00
|
|
|
func Base64Encode(b []byte) string {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall1("System.Binary.Base64Encode", b).(string)
|
2020-07-15 08:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Base64Decode decodes given base64 string represented as a byte slice into
|
|
|
|
// byte slice. It uses `System.Binary.Base64Decode` interop.
|
|
|
|
func Base64Decode(b []byte) []byte {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall1("System.Binary.Base64Decode", b).([]byte)
|
2020-07-15 08:49:24 +00:00
|
|
|
}
|
2020-08-23 14:19:56 +00:00
|
|
|
|
|
|
|
// Base58Encode encodes given byte slice into a base58 string and returns byte
|
|
|
|
// representation of this string. It uses `System.Binary.Base58Encode` syscall.
|
2020-09-08 10:48:33 +00:00
|
|
|
func Base58Encode(b []byte) string {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall1("System.Binary.Base58Encode", b).(string)
|
2020-08-23 14:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Base58Decode decodes given base58 string represented as a byte slice into
|
|
|
|
// a new byte slice. It uses `System.Binary.Base58Decode` syscall.
|
|
|
|
func Base58Decode(b []byte) []byte {
|
2021-02-25 12:11:52 +00:00
|
|
|
return neogointernal.Syscall1("System.Binary.Base58Decode", b).([]byte)
|
2020-08-23 14:19:56 +00:00
|
|
|
}
|
2020-11-10 09:39:52 +00:00
|
|
|
|
|
|
|
// Itoa converts num in a given base to string. Base should be either 10 or 16.
|
|
|
|
// It uses `System.Binary.Itoa` syscall.
|
|
|
|
func Itoa(num int, base int) string {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall2("System.Binary.Itoa", num, base).(string)
|
2020-11-10 09:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Atoi converts string to a number in a given base. Base should be either 10 or 16.
|
|
|
|
// It uses `System.Binary.Atoi` syscall.
|
|
|
|
func Atoi(s string, base int) int {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall2("System.Binary.Atoi", s, base).(int)
|
2020-11-10 09:39:52 +00:00
|
|
|
}
|