common

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2021 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package common contains various helper functions.

Index

Constants

View Source
const (
	HashLength       = 32
	AddressLength    = 42
	PubkeyHashLength = 20
)

Variables

View Source
var (
	// ErrChecksumMismatch describes an error where decoding failed due
	// to a bad checksum.
	ErrChecksumMismatch = errors.New("checksum mismatch")

	// ErrUnknownAddressType describes an error where an address can not
	// decoded as a specific address type due to the string encoding
	// begining with an identifier byte unknown to any standard or
	// registered (via chaincfg.Register) network.
	ErrUnknownAddressType = errors.New("unknown address type")

	// ErrAddressCollision describes an error where an address can not
	// be uniquely determined as either a pay-to-pubkey-hash or
	// pay-to-script-hash address since the leading identifier is used for
	// describing both address kinds, but for different networks.  Rather
	// than assuming or defaulting to one or the other, this error is
	// returned and the caller must decide how to decode the address.
	ErrAddressCollision = errors.New("address collision")

	// ErrUnsupportedWitnessVer describes an error where a segwit address being
	// decoded has an unsupported witness version.
	ErrUnsupportedWitnessVer = errors.New("unsupported witness version")

	// ErrUnsupportedWitnessProgLen describes an error where a segwit address
	// being decoded has an unsupported witness program length.
	ErrUnsupportedWitnessProgLen = errors.New("unsupported witness program length")
)

Functions

func Bytes2Hex

func Bytes2Hex(d []byte) string

func BytesToUnit64 added in v0.5.0

func BytesToUnit64(b []byte) uint64

func EmptyHash

func EmptyHash(h Hash) bool

func FromHex

func FromHex(s string) []byte

func Hex2Bytes

func Hex2Bytes(str string) []byte

func Unit64ToBytes added in v0.5.0

func Unit64ToBytes(n uint64) []byte

Types

type Address

type Address interface {
	// String returns the string encoding of the transaction output
	// destination.
	//
	// Please note that String differs subtly from EncodeAddress: String
	// will return the value as a string without any conversion, while
	// EncodeAddress may convert destination types (for example,
	// converting pubkeys to P2PKH addresses) before encoding as a
	// payment address string.
	String() string

	// EncodeAddress returns the string encoding of the payment address
	// associated with the Address value.  See the comment on String
	// for how this method differs from String.
	EncodeAddress() string

	// ScriptAddress returns the raw bytes of the address to be used
	// when inserting the address into a txout's script.
	ScriptAddress() []byte

	// IsForNet returns whether or not the address is associated with the
	// passed bytom network.
	IsForNet(*consensus.Params) bool
}

Address is an interface type for any type of destination a transaction output may spend to. This includes pay-to-pubkey (P2PK), pay-to-pubkey-hash (P2PKH), and pay-to-script-hash (P2SH). Address is designed to be generic enough that other kinds of addresses may be added in the future without changing the decoding and encoding API.

func DecodeAddress added in v0.1.4

func DecodeAddress(addr string, param *consensus.Params) (Address, error)

DecodeAddress decodes the string encoding of an address and returns the Address if addr is a valid encoding for a known address type.

The bytom network the address is associated with is extracted if possible. When the address does not encode the network, such as in the case of a raw public key, the address will be associated with the passed defaultNet.

type AddressWitnessPubKeyHash added in v0.1.4

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

AddressWitnessPubKeyHash is an Address for a pay-to-witness-pubkey-hash (P2WPKH) output. See BIP 173 for further details regarding native segregated witness address encoding: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki

func NewAddressWitnessPubKeyHash added in v0.1.4

func NewAddressWitnessPubKeyHash(witnessProg []byte, param *consensus.Params) (*AddressWitnessPubKeyHash, error)

NewAddressWitnessPubKeyHash returns a new AddressWitnessPubKeyHash.

func (*AddressWitnessPubKeyHash) EncodeAddress added in v0.1.4

func (a *AddressWitnessPubKeyHash) EncodeAddress() string

EncodeAddress returns the bech32 string encoding of an AddressWitnessPubKeyHash. Part of the Address interface.

func (*AddressWitnessPubKeyHash) Hash160 added in v0.1.4

func (a *AddressWitnessPubKeyHash) Hash160() *[20]byte

Hash160 returns the witness program of the AddressWitnessPubKeyHash as a byte array.

func (*AddressWitnessPubKeyHash) Hrp added in v0.1.4

Hrp returns the human-readable part of the bech32 encoded AddressWitnessPubKeyHash.

func (*AddressWitnessPubKeyHash) IsForNet added in v0.1.4

func (a *AddressWitnessPubKeyHash) IsForNet(param *consensus.Params) bool

IsForNet returns whether or not the AddressWitnessPubKeyHash is associated with the passed bitcoin network. Part of the Address interface.

func (*AddressWitnessPubKeyHash) ScriptAddress added in v0.1.4

func (a *AddressWitnessPubKeyHash) ScriptAddress() []byte

ScriptAddress returns the witness program for this address. Part of the Address interface.

func (*AddressWitnessPubKeyHash) String added in v0.1.4

func (a *AddressWitnessPubKeyHash) String() string

String returns a human-readable string for the AddressWitnessPubKeyHash. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer. Part of the Address interface.

func (*AddressWitnessPubKeyHash) WitnessProgram added in v0.1.4

func (a *AddressWitnessPubKeyHash) WitnessProgram() []byte

WitnessProgram returns the witness program of the AddressWitnessPubKeyHash.

func (*AddressWitnessPubKeyHash) WitnessVersion added in v0.1.4

func (a *AddressWitnessPubKeyHash) WitnessVersion() byte

WitnessVersion returns the witness version of the AddressWitnessPubKeyHash.

type AddressWitnessScriptHash added in v0.1.4

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

AddressWitnessScriptHash is an Address for a pay-to-witness-script-hash (P2WSH) output. See BIP 173 for further details regarding native segregated witness address encoding: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki

func NewAddressWitnessScriptHash added in v0.1.4

func NewAddressWitnessScriptHash(witnessProg []byte, param *consensus.Params) (*AddressWitnessScriptHash, error)

NewAddressWitnessScriptHash returns a new AddressWitnessPubKeyHash.

func (*AddressWitnessScriptHash) EncodeAddress added in v0.1.4

func (a *AddressWitnessScriptHash) EncodeAddress() string

EncodeAddress returns the bech32 string encoding of an AddressWitnessScriptHash. Part of the Address interface.

func (*AddressWitnessScriptHash) Hrp added in v0.1.4

Hrp returns the human-readable part of the bech32 encoded AddressWitnessScriptHash.

func (*AddressWitnessScriptHash) IsForNet added in v0.1.4

func (a *AddressWitnessScriptHash) IsForNet(param *consensus.Params) bool

IsForNet returns whether or not the AddressWitnessScriptHash is associated with the passed bytom network. Part of the Address interface.

func (*AddressWitnessScriptHash) ScriptAddress added in v0.1.4

func (a *AddressWitnessScriptHash) ScriptAddress() []byte

ScriptAddress returns the witness program for this address. Part of the Address interface.

func (*AddressWitnessScriptHash) Sha256 added in v0.1.4

func (a *AddressWitnessScriptHash) Sha256() *[32]byte

Sha256 returns the witness program of the AddressWitnessPubKeyHash as a byte array.

func (*AddressWitnessScriptHash) String added in v0.1.4

func (a *AddressWitnessScriptHash) String() string

String returns a human-readable string for the AddressWitnessScriptHash. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer. Part of the Address interface.

func (*AddressWitnessScriptHash) WitnessProgram added in v0.1.4

func (a *AddressWitnessScriptHash) WitnessProgram() []byte

WitnessProgram returns the witness program of the AddressWitnessScriptHash.

func (*AddressWitnessScriptHash) WitnessVersion added in v0.1.4

func (a *AddressWitnessScriptHash) WitnessVersion() byte

WitnessVersion returns the witness version of the AddressWitnessScriptHash.

type Cache added in v1.2.1

type Cache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Cache is an LRU cache. It is safe for concurrent access.

func NewCache added in v1.2.1

func NewCache(maxEntries int) *Cache

NewCache creates a new Cache. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.

func (*Cache) Add added in v1.2.1

func (c *Cache) Add(key, value interface{})

Add adds a value to the cache.

func (*Cache) Clear added in v1.2.1

func (c *Cache) Clear()

Clear purges all stored items from the cache.

func (*Cache) Get added in v1.2.1

func (c *Cache) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*Cache) Len added in v1.2.1

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) Remove added in v1.2.1

func (c *Cache) Remove(key interface{})

Remove removes the provided key from the cache.

func (*Cache) RemoveOldest added in v1.2.1

func (c *Cache) RemoveOldest()

RemoveOldest removes the oldest item from the cache.

type Hash

type Hash [HashLength]byte

func BigToHash

func BigToHash(b *big.Int) Hash

func BytesToHash

func BytesToHash(b []byte) Hash

func StringToHash

func StringToHash(s string) Hash

func (Hash) Bytes

func (h Hash) Bytes() []byte

func (Hash) Generate

func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value

Generate implements testing/quick.Generator.

func (Hash) Hex

func (h Hash) Hex() string

func (Hash) MarshalJSON

func (h Hash) MarshalJSON() ([]byte, error)

Serialize given hash to JSON

func (*Hash) Set

func (h *Hash) Set(other Hash)

Sets h to other

func (*Hash) SetBytes

func (h *Hash) SetBytes(b []byte)

Sets the hash to the value of b. If b is larger than len(h) it will panic

func (Hash) Str

func (h Hash) Str() string

Get the string representation of the underlying hash

func (*Hash) UnmarshalJSON

func (h *Hash) UnmarshalJSON(input []byte) error

UnmarshalJSON parses a hash in its hex from to a hash.

type TimeSorter added in v0.4.1

type TimeSorter []uint64

timeSorter implements sort.Interface to allow a slice of timestamps to be sorted.

func (TimeSorter) Len added in v0.4.1

func (s TimeSorter) Len() int

Len returns the number of timestamps in the slice. It is part of the sort.Interface implementation.

func (TimeSorter) Less added in v0.4.1

func (s TimeSorter) Less(i, j int) bool

Less returns whether the timstamp with index i should sort before the timestamp with index j. It is part of the sort.Interface implementation.

func (TimeSorter) Swap added in v0.4.1

func (s TimeSorter) Swap(i, j int)

Swap swaps the timestamps at the passed indices. It is part of the sort.Interface implementation.

Directories

Path Synopsis
Package bech32 provides a Go implementation of the bech32 format specified in BIP 173.
Package bech32 provides a Go implementation of the bech32 format specified in BIP 173.

Jump to

Keyboard shortcuts

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