address

package
v0.0.0-...-3c905e5 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2023 License: BSD-3-Clause Imports: 10 Imported by: 82

README

address

实现公钥到地址的转换, 支持实现自定义地址格式, 地址驱动插件化

addressID

地址驱动类型值

  • 每个地址驱动有唯一的类型及名称值
  • 类型值范围 [0, 8)
  • 底层相关设计, 参考 chain/types/sign.md

配置

地址驱动配置说明

DefaultDriver

配置默认的地址驱动, 默认地址格式作为执行器以及未指定 addressID 时地址格式转换

[address] #示例
DefaultDriver="eth"
EnableHeight

分叉高度配置, 即区块高度达到配置高度后启用插件, 不配置采用内置的启用高度, 负数表示不启用

[address] #示例
[address.enableHeight]
"btc" = -1
"eth"= 100

Documentation

Overview

Package address 计算地址相关的函数

Index

Constants

View Source
const (
	// MaxID 最大id值
	MaxID = 7
	// DefaultID default id flag
	DefaultID = -1
)
View Source
const ForkFormatAddressKey = "ForkFormatAddressKey"

ForkFormatAddressKey 地址key格式化分叉名称,主要针对eth地址

View Source
const MaxExecNameLength = 100

MaxExecNameLength 执行器名最大长度

Variables

View Source
var (
	// ErrDecodeBase58 error decode
	ErrDecodeBase58 = errors.New("ErrDecodeBase58")
	// ErrAddressLength error length
	ErrAddressLength = errors.New("ErrAddressLength")
)
View Source
var (

	//ErrUnknownAddressDriver 未注册驱动
	ErrUnknownAddressDriver = errors.New("ErrUnknownAddressDriver")
	//ErrUnknownAddressType unknown address type
	ErrUnknownAddressType = errors.New("ErrUnknownAddressType")
	//ErrAddressDriverNotEnable 驱动未启用
	ErrAddressDriverNotEnable = errors.New("ErrAddressDriverNotEnable")
)
View Source
var ErrAddressChecksum = errors.New("address checksum error")

ErrAddressChecksum :

View Source
var ErrCheckChecksum = errors.New("Address Checksum error")

ErrCheckChecksum :

View Source
var ErrCheckVersion = errors.New("check version error")

ErrCheckVersion :

View Source
var MultiSignVer byte = 5

MultiSignVer 多重签名地址的版本号

View Source
var NormalVer byte

NormalVer 普通地址的版本号

Functions

func CheckAddress

func CheckAddress(addr string, blockHeight int64) (e error)

CheckAddress check address validity blockHeight is used for enable check, pass -1 if there is no block height context

func CheckBase58Address

func CheckBase58Address(ver byte, addr string) (e error)

CheckBase58Address check base58 format address, usually refers to

func ExecAddress

func ExecAddress(name string) string

ExecAddress 计算量有点大,做一次cache contract address

func ExecPubKey

func ExecPubKey(name string) []byte

ExecPubKey 计算公钥

func FormatAddrKey

func FormatAddrKey(addr string) []byte

FormatAddrKey format addr as db key

func GetAddressType

func GetAddressType(addr string) (int32, error)

GetAddressType get address type id

func GetDefaultAddressID

func GetDefaultAddressID() int32

GetDefaultAddressID get default id

func GetDriverList

func GetDriverList() map[int32]Driver

GetDriverList get driver list

func GetDriverType

func GetDriverType(name string) (int32, error)

GetDriverType get type by name

func GetExecAddress

func GetExecAddress(execName string, addressType int32) (string, error)

GetExecAddress 获取地址

func Init

func Init(config *Config)

Init init with config

func IsEthAddress

func IsEthAddress(addr string) bool

IsEthAddress verifies whether a string can represent a valid hex-encoded eth address

func IsValidAddressID

func IsValidAddressID(id int32) bool

IsValidAddressID is valid

func PubKeyToAddr

func PubKeyToAddr(addressID int32, pubKey []byte) string

PubKeyToAddr pubKey to specific address pass DefaultID for default address format

func RegisterDriver

func RegisterDriver(id int32, driver Driver, enableHeight int64)

RegisterDriver 注册地址驱动 enableHeight, 设置默认启用高度, 负数表示不启用

func SetNormalAddrVer

func SetNormalAddrVer(ver byte)

SetNormalAddrVer 根据配置设置生成普通地址的version版本号,默认是0

func ToLower

func ToLower(addr string) string

ToLower to lower case string

Types

type Address

type Address struct {
	Version  byte
	Hash160  [20]byte // For a stealth address: it's HASH160
	Checksum []byte   // Unused for a stealth address
	Pubkey   []byte   // Unused for a stealth address
	Enc58str string
}

Address btc address Deprecated

func BytesToBtcAddress

func BytesToBtcAddress(version byte, in []byte) *Address

BytesToBtcAddress hash32 to address Deprecated: btc address legacy

func NewBtcAddress

func NewBtcAddress(addr string) (*Address, error)

NewBtcAddress new btc address Deprecated: legacy

func (*Address) SetBytes

func (a *Address) SetBytes(b []byte)

SetBytes 设置地址的bytes

func (*Address) String

func (a *Address) String() string

type Config

type Config struct {

	// DefaultDriver config default driver
	DefaultDriver string `json:"defaultDriver,omitempty"`
	// EnableHeight enable driver at specific block height
	EnableHeight map[string]int64 `json:"enableHeight,omitempty"`
}

Config address driver config address defaultDriver="btc" [address.enableHeight] btc=0 btcMultiSign=0 eth=-1

type Driver

type Driver interface {

	// PubKeyToAddr public key to address
	PubKeyToAddr(pubKey []byte) string
	// ValidateAddr address validation
	ValidateAddr(addr string) error
	// GetName get driver name
	GetName() string
	// FromString decode from string
	FromString(addr string) ([]byte, error)
	// ToString encode to string
	ToString(addr []byte) string
	// FormatAddr to unified format
	FormatAddr(addr string) string
}

Driver address driver

func GetDefaultAddressDriver

func GetDefaultAddressDriver() Driver

GetDefaultAddressDriver get default driver

func LoadDriver

func LoadDriver(id int32, blockHeight int64) (Driver, error)

LoadDriver 根据ID加载插件, 根据区块高度判定是否已启动 不关心启用状态, blockHeight传-1

func MustLoadDriver

func MustLoadDriver(id int32) Driver

MustLoadDriver 根据ID加载插件, 出错panic

type DriverInfo

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

DriverInfo driver info

Jump to

Keyboard shortcuts

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