2018-02-04 20:54:51 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2018-03-23 20:36:59 +00:00
|
|
|
"encoding/json"
|
2018-03-02 16:24:09 +01:00
|
|
|
"fmt"
|
2018-05-09 08:20:16 +03:00
|
|
|
"strings"
|
2020-05-04 10:35:56 +03:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2021-07-18 15:55:37 +03:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2018-02-04 20:54:51 +01:00
|
|
|
)
|
|
|
|
|
2019-12-03 15:59:21 +03:00
|
|
|
// Uint160Size is the size of Uint160 in bytes.
|
|
|
|
const Uint160Size = 20
|
2018-02-04 20:54:51 +01:00
|
|
|
|
|
|
|
// Uint160 is a 20 byte long unsigned integer.
|
2019-12-03 15:59:21 +03:00
|
|
|
type Uint160 [Uint160Size]uint8
|
2018-02-04 20:54:51 +01:00
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// Uint160DecodeStringBE attempts to decode the given string into a Uint160.
|
2019-11-27 12:20:31 +03:00
|
|
|
func Uint160DecodeStringBE(s string) (Uint160, error) {
|
2018-11-26 18:56:45 +03:00
|
|
|
var u Uint160
|
2019-12-03 15:59:21 +03:00
|
|
|
if len(s) != Uint160Size*2 {
|
|
|
|
return u, fmt.Errorf("expected string size of %d got %d", Uint160Size*2, len(s))
|
2018-03-02 16:24:09 +01:00
|
|
|
}
|
|
|
|
b, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
2019-11-27 12:20:31 +03:00
|
|
|
return Uint160DecodeBytesBE(b)
|
2018-03-02 16:24:09 +01:00
|
|
|
}
|
|
|
|
|
2019-12-03 16:14:44 +03:00
|
|
|
// Uint160DecodeStringLE attempts to decode the given string
|
2022-04-20 21:30:09 +03:00
|
|
|
// in little-endian hex encoding into a Uint160.
|
2019-12-03 16:14:44 +03:00
|
|
|
func Uint160DecodeStringLE(s string) (Uint160, error) {
|
|
|
|
var u Uint160
|
|
|
|
if len(s) != Uint160Size*2 {
|
|
|
|
return u, fmt.Errorf("expected string size of %d got %d", Uint160Size*2, len(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Uint160DecodeBytesLE(b)
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// Uint160DecodeBytesBE attempts to decode the given bytes into a Uint160.
|
2019-11-27 12:20:31 +03:00
|
|
|
func Uint160DecodeBytesBE(b []byte) (u Uint160, err error) {
|
2019-12-03 15:59:21 +03:00
|
|
|
if len(b) != Uint160Size {
|
|
|
|
return u, fmt.Errorf("expected byte size of %d got %d", Uint160Size, len(b))
|
2018-03-02 16:24:09 +01:00
|
|
|
}
|
2019-01-25 14:20:35 +03:00
|
|
|
copy(u[:], b)
|
2018-03-02 16:24:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-03 16:12:26 +03:00
|
|
|
// Uint160DecodeBytesLE attempts to decode the given bytes in little-endian
|
2022-04-20 21:30:09 +03:00
|
|
|
// into a Uint160.
|
2019-12-03 16:12:26 +03:00
|
|
|
func Uint160DecodeBytesLE(b []byte) (u Uint160, err error) {
|
|
|
|
if len(b) != Uint160Size {
|
|
|
|
return u, fmt.Errorf("expected byte size of %d got %d", Uint160Size, len(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range b {
|
|
|
|
u[Uint160Size-i-1] = b[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:20:31 +03:00
|
|
|
// BytesBE returns a big-endian byte representation of u.
|
|
|
|
func (u Uint160) BytesBE() []byte {
|
2018-11-26 18:56:45 +03:00
|
|
|
return u[:]
|
2018-02-04 20:54:51 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:20:31 +03:00
|
|
|
// BytesLE returns a little-endian byte representation of u.
|
|
|
|
func (u Uint160) BytesLE() []byte {
|
2021-07-18 15:55:37 +03:00
|
|
|
return slice.CopyReverse(u.BytesBE())
|
2018-03-25 12:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 20:54:51 +01:00
|
|
|
// String implements the stringer interface.
|
|
|
|
func (u Uint160) String() string {
|
2019-11-27 12:20:31 +03:00
|
|
|
return u.StringBE()
|
2018-02-04 20:54:51 +01:00
|
|
|
}
|
2018-02-07 15:16:50 +01:00
|
|
|
|
2019-11-27 12:20:31 +03:00
|
|
|
// StringBE returns string representations of u with big-endian byte order.
|
|
|
|
func (u Uint160) StringBE() string {
|
|
|
|
return hex.EncodeToString(u.BytesBE())
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringLE returns string representations of u with little-endian byte order.
|
|
|
|
func (u Uint160) StringLE() string {
|
|
|
|
return hex.EncodeToString(u.BytesLE())
|
2019-08-22 15:35:18 +03:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// Reverse returns a reversed representation of u.
|
2019-12-03 16:07:15 +03:00
|
|
|
func (u Uint160) Reverse() (r Uint160) {
|
|
|
|
for i := 0; i < Uint160Size; i++ {
|
|
|
|
r[i] = u[Uint160Size-i-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:41:44 +03:00
|
|
|
// Equals returns true if both Uint160 values are the same.
|
2018-02-07 15:16:50 +01:00
|
|
|
func (u Uint160) Equals(other Uint160) bool {
|
2018-11-26 18:56:45 +03:00
|
|
|
return u == other
|
2018-02-07 15:16:50 +01:00
|
|
|
}
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// Less returns true if this value is less than the given Uint160 value. It's
|
2019-09-30 17:39:42 +03:00
|
|
|
// primarily intended to be used for sorting purposes.
|
|
|
|
func (u Uint160) Less(other Uint160) bool {
|
|
|
|
for k := range u {
|
|
|
|
if u[k] == other[k] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return u[k] < other[k]
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-05-09 08:20:16 +03:00
|
|
|
// UnmarshalJSON implements the json unmarshaller interface.
|
|
|
|
func (u *Uint160) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
var js string
|
|
|
|
if err = json.Unmarshal(data, &js); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-19 16:22:33 +03:00
|
|
|
js = strings.TrimPrefix(js, "0x")
|
2020-03-23 12:46:42 +03:00
|
|
|
*u, err = Uint160DecodeStringLE(js)
|
2018-05-09 08:20:16 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// MarshalJSON implements the json marshaller interface.
|
|
|
|
func (u Uint160) MarshalJSON() ([]byte, error) {
|
2022-06-01 11:26:36 +03:00
|
|
|
r := make([]byte, 3+Uint160Size*2+1)
|
|
|
|
copy(r, `"0x`)
|
|
|
|
r[len(r)-1] = '"'
|
|
|
|
slice.Reverse(u[:]) // u is a copy, so we can mangle it in any way.
|
|
|
|
hex.Encode(r[3:], u[:])
|
|
|
|
return r, nil
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
2020-05-04 10:35:56 +03:00
|
|
|
|
2022-02-11 13:30:01 +03:00
|
|
|
// UnmarshalYAML implements the YAML Unmarshaler interface.
|
|
|
|
func (u *Uint160) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var s string
|
|
|
|
|
|
|
|
err := unmarshal(&s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.TrimPrefix(s, "0x")
|
|
|
|
*u, err = Uint160DecodeStringLE(s)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML implements the YAML marshaller interface.
|
|
|
|
func (u Uint160) MarshalYAML() (interface{}, error) {
|
|
|
|
return "0x" + u.StringLE(), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// EncodeBinary implements the Serializable interface.
|
2020-05-04 10:35:56 +03:00
|
|
|
func (u *Uint160) EncodeBinary(bw *io.BinWriter) {
|
|
|
|
bw.WriteBytes(u[:])
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:30:09 +03:00
|
|
|
// DecodeBinary implements the Serializable interface.
|
2020-05-04 10:35:56 +03:00
|
|
|
func (u *Uint160) DecodeBinary(br *io.BinReader) {
|
|
|
|
br.ReadBytes(u[:])
|
|
|
|
}
|