2018-02-04 19:54:51 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2018-03-23 20:36:59 +00:00
|
|
|
"encoding/json"
|
2018-03-02 15:24:09 +00:00
|
|
|
"fmt"
|
2018-05-09 05:20:16 +00:00
|
|
|
"strings"
|
2018-02-04 19:54:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const uint160Size = 20
|
|
|
|
|
|
|
|
// Uint160 is a 20 byte long unsigned integer.
|
|
|
|
type Uint160 [uint160Size]uint8
|
|
|
|
|
2019-11-27 09:20:31 +00:00
|
|
|
// Uint160DecodeStringBE attempts to decode the given string into an Uint160.
|
|
|
|
func Uint160DecodeStringBE(s string) (Uint160, error) {
|
2018-11-26 15:56:45 +00:00
|
|
|
var u Uint160
|
2018-03-02 15:24:09 +00:00
|
|
|
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
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
return Uint160DecodeBytesBE(b)
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 09:20:31 +00:00
|
|
|
// Uint160DecodeBytesBE attempts to decode the given bytes into an Uint160.
|
|
|
|
func Uint160DecodeBytesBE(b []byte) (u Uint160, err error) {
|
2018-03-02 15:24:09 +00:00
|
|
|
if len(b) != uint160Size {
|
|
|
|
return u, fmt.Errorf("expected byte size of %d got %d", uint160Size, len(b))
|
|
|
|
}
|
2019-01-25 11:20:35 +00:00
|
|
|
copy(u[:], b)
|
2018-03-02 15:24:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:20:31 +00:00
|
|
|
// BytesBE returns a big-endian byte representation of u.
|
|
|
|
func (u Uint160) BytesBE() []byte {
|
2018-11-26 15:56:45 +00:00
|
|
|
return u[:]
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 09:20:31 +00:00
|
|
|
// BytesLE returns a little-endian byte representation of u.
|
|
|
|
func (u Uint160) BytesLE() []byte {
|
|
|
|
return ArrayReverse(u.BytesBE())
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// String implements the stringer interface.
|
|
|
|
func (u Uint160) String() string {
|
2019-11-27 09:20:31 +00:00
|
|
|
return u.StringBE()
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
2018-02-07 14:16:50 +00:00
|
|
|
|
2019-11-27 09:20:31 +00: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 12:35:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 14:16:50 +00:00
|
|
|
// Equals returns true if both Uint256 values are the same.
|
|
|
|
func (u Uint160) Equals(other Uint160) bool {
|
2018-11-26 15:56:45 +00:00
|
|
|
return u == other
|
2018-02-07 14:16:50 +00:00
|
|
|
}
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2019-09-30 14:39:42 +00:00
|
|
|
// Less returns true if this value is less than given Uint160 value. It's
|
|
|
|
// 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 05:20:16 +00: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 13:22:33 +00:00
|
|
|
js = strings.TrimPrefix(js, "0x")
|
2019-11-27 09:20:31 +00:00
|
|
|
*u, err = Uint160DecodeStringBE(js)
|
2018-05-09 05:20:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// MarshalJSON implements the json marshaller interface.
|
|
|
|
func (u Uint160) MarshalJSON() ([]byte, error) {
|
2018-11-26 15:56:45 +00:00
|
|
|
return []byte(`"0x` + u.String() + `"`), nil
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|