2019-12-25 12:22:02 +00:00
|
|
|
package address
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-25 12:50:52 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/base58"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-03-17 11:53:21 +00:00
|
|
|
)
|
|
|
|
|
2020-06-16 11:08:47 +00:00
|
|
|
const (
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEO2Prefix is the first byte of an address for NEO2.
|
2020-06-16 11:08:47 +00:00
|
|
|
NEO2Prefix byte = 0x17
|
2022-04-20 18:30:09 +00:00
|
|
|
// NEO3Prefix is the first byte of an address for NEO3.
|
2020-06-16 11:08:47 +00:00
|
|
|
NEO3Prefix byte = 0x35
|
|
|
|
)
|
|
|
|
|
2019-12-25 12:42:18 +00:00
|
|
|
// Prefix is the byte used to prepend to addresses when encoding them, it can
|
2020-06-16 10:47:29 +00:00
|
|
|
// be changed and defaults to 53 (0x35), the standard NEO prefix.
|
|
|
|
var Prefix = NEO3Prefix
|
2019-12-25 12:42:18 +00:00
|
|
|
|
2019-12-25 14:34:18 +00:00
|
|
|
// Uint160ToString returns the "NEO address" from the given Uint160.
|
|
|
|
func Uint160ToString(u util.Uint160) string {
|
2022-04-20 18:30:09 +00:00
|
|
|
// Don't forget to prepend the Address version 0x17 (23) A
|
2019-12-25 12:42:18 +00:00
|
|
|
b := append([]byte{Prefix}, u.BytesBE()...)
|
2019-12-25 12:05:54 +00:00
|
|
|
return base58.CheckEncode(b)
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 14:34:18 +00:00
|
|
|
// StringToUint160 attempts to decode the given NEO address string
|
2022-04-20 18:30:09 +00:00
|
|
|
// into a Uint160.
|
2019-12-25 14:34:18 +00:00
|
|
|
func StringToUint160(s string) (u util.Uint160, err error) {
|
2019-12-25 12:05:54 +00:00
|
|
|
b, err := base58.CheckDecode(s)
|
2018-03-17 11:53:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
2019-12-25 12:50:52 +00:00
|
|
|
if b[0] != Prefix {
|
|
|
|
return u, errors.New("wrong address prefix")
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
return util.Uint160DecodeBytesBE(b[1:21])
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|