cfxaddress

package
v1.5.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2024 License: LGPL-3.0 Imports: 14 Imported by: 60

Documentation

Index

Constants

View Source
const (
	NetworkTypeMainnetPrefix NetworkType = "cfx"
	NetworkTypeTestNetPrefix NetworkType = "cfxtest"

	NetowrkTypeMainnetID uint32 = 1029
	NetworkTypeTestnetID uint32 = 1
)

Variables

View Source
var (
	ErrorBodyLen = errors.New("Body length must be 34")
)

Functions

func FormatAddressStrToHex

func FormatAddressStrToHex(address string) string

FormatAddressStrToHex format hex or base32 address to hex string

func SetConfig added in v1.1.3

func SetConfig(c Config)

Types

type Address

type Address struct {
	// contains filtered or unexported fields
}

Address represents base32 address accroding to CIP37 Use NewXXX or MustNewXXX to create an Address object and don't use Address{} which is an invalid address.

func MustNew

func MustNew(base32OrHex string, networkID ...uint32) Address

MustNew create conflux address by base32 string or hex40 string, if base32OrHex is base32 and networkID is setted it will check if networkID match, it will painc if error occured.

func MustNewFromBase32

func MustNewFromBase32(base32Str string) (address Address)

MustNewFromBase32 creates address by base32 string and panic if error

func MustNewFromBytes

func MustNewFromBytes(hexAddress []byte, networkID ...uint32) (address Address)

MustNewFromBytes creates an address from hexAddress byte slice with networkID and panic if error

func MustNewFromCommon

func MustNewFromCommon(commonAddress common.Address, networkID ...uint32) (address Address)

MustNewFromCommon creates an address from common.Address with networkID and panic if error

func MustNewFromHex

func MustNewFromHex(hexAddressStr string, networkID ...uint32) (val Address)

MustNewFromHex creates address by hex address string with networkID and panic if error

func New

func New(base32OrHex string, networkID ...uint32) (Address, error)

New create conflux address by base32 string or hex40 string, if base32OrHex is base32 and networkID is passed it will create cfx Address use networkID.

func NewFromBase32

func NewFromBase32(base32Str string) (cfxAddress Address, err error)

NewFromBase32 creates address by base32 string

func NewFromBytes

func NewFromBytes(hexAddress []byte, networkID ...uint32) (val Address, err error)

NewFromBytes creates an address from hexAddress byte slice with networkID

func NewFromCommon

func NewFromCommon(commonAddress common.Address, networkID ...uint32) (val Address, err error)

NewFromCommon creates an address from common.Address with networkID

func NewFromHex

func NewFromHex(hexAddressStr string, networkID ...uint32) (val Address, err error)

NewFromHex creates address by hex address string with networkID If not pass networkID, it will be auto completed when it could be obtained form context.

func (*Address) CompleteByClient

func (a *Address) CompleteByClient(client networkIDGetter) error

CompleteByClient will set networkID by client.GetNetworkID() if a.networkID not be 0

func (*Address) CompleteByNetworkID

func (a *Address) CompleteByNetworkID(networkID uint32) error

CompleteByNetworkID will set networkID if current networkID isn't 0

func (*Address) DecodeRLP

func (a *Address) DecodeRLP(r *rlp.Stream) error

DecodeRLP implements the rlp.Decoder interface.

func (Address) EncodeRLP

func (a Address) EncodeRLP(w io.Writer) error

EncodeRLP implements the rlp.Encoder interface.

func (*Address) Equals

func (a *Address) Equals(target *Address) bool

Equals reports whether a and target are equal

func (*Address) GetAddressType

func (a *Address) GetAddressType() AddressType

GetAddressType returuns address type

func (*Address) GetBody

func (a *Address) GetBody() Body

GetBody returns body

func (*Address) GetChecksum

func (a *Address) GetChecksum() Checksum

GetChecksum returns checksum

func (*Address) GetHexAddress

func (a *Address) GetHexAddress() string

GetHexAddress returns hex format address and panic if error

func (*Address) GetMappedEVMSpaceAddress

func (a *Address) GetMappedEVMSpaceAddress() common.Address

GetMappedEVMSpaceAddress calculate CFX space address's mapped EVM address, which is the last 20 bytes of cfx address's keccak256 hash

func (*Address) GetNetworkID

func (a *Address) GetNetworkID() uint32

GetNetworkID returns networkID and panic if error

func (*Address) GetNetworkType

func (a *Address) GetNetworkType() NetworkType

GetNetworkType returns network type

func (*Address) GetShortenAddress

func (a *Address) GetShortenAddress(isTail4Char ...bool) string

GetShortenAddress returns shorten string for display in dapp. When isTail4Char is 'true', the result will be like 'cfx:aat…sa4w', otherwise 'cfx:aat…5m81sa4w'

func (*Address) IsValid

func (a *Address) IsValid() bool

IsValid return true if address is valid

func (Address) MarshalText

func (a Address) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Address) MustGetBase32Address

func (a *Address) MustGetBase32Address() string

MustGetBase32Address returns base32 string of address which doesn't include address type

func (*Address) MustGetCommonAddress

func (a *Address) MustGetCommonAddress() common.Address

MustGetCommonAddress returns common address and panic if error

func (*Address) MustGetVerboseBase32Address

func (a *Address) MustGetVerboseBase32Address() string

MustGetVerboseBase32Address returns base32 string of address with address type

func (Address) String

func (a Address) String() string

String returns verbose base32 string of address

func (*Address) ToCommon

func (a *Address) ToCommon() (address common.Address, networkID uint32, err error)

ToCommon returns common.Address and networkID

func (*Address) ToHex

func (a *Address) ToHex() (hexAddressStr string, networkID uint32)

ToHex returns hex address and networkID

func (*Address) UnmarshalJSON

func (a *Address) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Address) UnmarshalText

func (a *Address) UnmarshalText(data []byte) error

type AddressType

type AddressType string
const (
	AddressTypeBuiltin  AddressType = "builtin"
	AddressTypeUser     AddressType = "user"
	AddressTypeContract AddressType = "contract"
	AddressTypeNull     AddressType = "null"
	AddressTypeUnknown  AddressType = "unknown"
)

func CalcAddressType

func CalcAddressType(hexAddress []byte) (AddressType, error)

CalcAddressType calculate address type of hexAddress

func (AddressType) String

func (a AddressType) String() string

func (AddressType) ToByte

func (a AddressType) ToByte() (byte, error)

ToByte returns byte represents of address type according to CIP-37

type Body

type Body [34]byte

Base32-encode address: To create the payload, first, concatenate the version-byte with addr to get a 21-byte array. Then, encode it left-to-right, mapping each 5-bit sequence to the corresponding ASCII character (see alphabet below). Pad to the right with zero bits to complete any unfinished chunk at the end. In our case, 21-byte payload + 2 bit 0-padding will result in a 34-byte base32-encoded string.

We use the following alphabet: abcdefghjkmnprstuvwxyz0123456789 (i, l, o, q removed).

0x00 => a    0x08 => j    0x10 => u    0x18 => 2
0x01 => b    0x09 => k    0x11 => v    0x19 => 3
0x02 => c    0x0a => m    0x12 => w    0x1a => 4
0x03 => d    0x0b => n    0x13 => x    0x1b => 5
0x04 => e    0x0c => p    0x14 => y    0x1c => 6
0x05 => f    0x0d => r    0x15 => z    0x1d => 7
0x06 => g    0x0e => s    0x16 => 0    0x1e => 8
0x07 => h    0x0f => t    0x17 => 1    0x1f => 9

Body reperents 5bits byte array of concating version byte with hex address

func NewBodyByHexAddress

func NewBodyByHexAddress(vrsByte VersionByte, hexAddress []byte) (b Body, err error)

NewBodyByHexAddress convert concat of version type and hex address to 5 bits slice

func NewBodyByString

func NewBodyByString(base32Str string) (body Body, err error)

NewBodyByString creates body by base32 string which contains version byte and hex address

func (Body) String

func (b Body) String() string

String return base32 string

func (Body) ToHexAddress

func (b Body) ToHexAddress() (vrsType VersionByte, hexAddress []byte, err error)

ToHexAddress decode bits5 array to version byte and hex address

type Checksum

type Checksum [8]byte

Checksum represents by 5bits byte array

func CalcChecksum

func CalcChecksum(nt NetworkType, body Body) (c Checksum, err error)

CalcChecksum calculates checksum by network type and body

func (Checksum) String

func (c Checksum) String() string

String returns base32 string of checksum according to CIP-37

type Config added in v1.1.3

type Config struct {
	AddressStringVerbose bool
}

func GetConfig added in v1.1.3

func GetConfig() Config

type NetworkType

type NetworkType string

NetworkType reprents network type mapped with network-id

func NewNetowrkType

func NewNetowrkType(netType string) (NetworkType, error)

NewNetowrkType creates network type by string

func NewNetworkTypeByID

func NewNetworkTypeByID(networkID uint32) NetworkType

NewNetworkTypeByID creates network type by network ID

func (NetworkType) String

func (n NetworkType) String() string

func (NetworkType) ToNetworkID

func (n NetworkType) ToNetworkID() (uint32, error)

ToNetworkID returns network ID

type VersionByte

type VersionByte struct {
	TypeBits uint8
	// current is constant 0, it's different with AddressType defined in address_type.go
	AddressType uint8
	SizeBits    uint8
}

VersionByte conmposites by type bits, address type and size bits according above description from CIP-37

func CalcVersionByte

func CalcVersionByte(hexAddress []byte) (versionByte VersionByte, err error)

CalcVersionByte calculates version byte of hex address

func NewVersionByte

func NewVersionByte(b byte) (vt VersionByte)

NewVersionByte creates version byte by byte

func (VersionByte) ToByte

func (v VersionByte) ToByte() (byte, error)

ToByte returns byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL