github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/encoding/address/address.go (about)

     1  package address
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/nspcc-dev/neo-go/pkg/encoding/base58"
     7  	"github.com/nspcc-dev/neo-go/pkg/util"
     8  )
     9  
    10  const (
    11  	// NEO2Prefix is the first byte of an address for NEO2.
    12  	NEO2Prefix byte = 0x17
    13  	// NEO3Prefix is the first byte of an address for NEO3.
    14  	NEO3Prefix byte = 0x35
    15  )
    16  
    17  // Prefix is the byte used to prepend to addresses when encoding them, it can
    18  // be changed and defaults to 53 (0x35), the standard NEO prefix.
    19  var Prefix = NEO3Prefix
    20  
    21  // Uint160ToString returns the "NEO address" from the given Uint160.
    22  func Uint160ToString(u util.Uint160) string {
    23  	// Don't forget to prepend the Address version 0x17 (23) A
    24  	b := append([]byte{Prefix}, u.BytesBE()...)
    25  	return base58.CheckEncode(b)
    26  }
    27  
    28  // StringToUint160 attempts to decode the given NEO address string
    29  // into a Uint160.
    30  func StringToUint160(s string) (u util.Uint160, err error) {
    31  	b, err := base58.CheckDecode(s)
    32  	if err != nil {
    33  		return u, err
    34  	}
    35  	if b[0] != Prefix {
    36  		return u, errors.New("wrong address prefix")
    37  	}
    38  	return util.Uint160DecodeBytesBE(b[1:21])
    39  }