Documentation ¶
Overview ¶
Package common contains various helper functions.
Index ¶
- Constants
- Variables
- func Bytes2Hex(d []byte) string
- func BytesToUnit64(b []byte) uint64
- func EmptyHash(h Hash) bool
- func FromHex(s string) []byte
- func Hex2Bytes(str string) []byte
- func IsBech32SegwitPrefix(prefix string, params *consensus.Params) bool
- func IsOpenFederationIssueAsset(rawDefinitionByte []byte) bool
- func Unit64ToBytes(n uint64) []byte
- type Address
- type AddressWitnessPubKeyHash
- func (a *AddressWitnessPubKeyHash) EncodeAddress() string
- func (a *AddressWitnessPubKeyHash) Hash160() *[20]byte
- func (a *AddressWitnessPubKeyHash) Hrp() string
- func (a *AddressWitnessPubKeyHash) IsForNet(param *consensus.Params) bool
- func (a *AddressWitnessPubKeyHash) ScriptAddress() []byte
- func (a *AddressWitnessPubKeyHash) String() string
- func (a *AddressWitnessPubKeyHash) WitnessProgram() []byte
- func (a *AddressWitnessPubKeyHash) WitnessVersion() byte
- type AddressWitnessScriptHash
- func (a *AddressWitnessScriptHash) EncodeAddress() string
- func (a *AddressWitnessScriptHash) Hrp() string
- func (a *AddressWitnessScriptHash) IsForNet(param *consensus.Params) bool
- func (a *AddressWitnessScriptHash) ScriptAddress() []byte
- func (a *AddressWitnessScriptHash) Sha256() *[32]byte
- func (a *AddressWitnessScriptHash) String() string
- func (a *AddressWitnessScriptHash) WitnessProgram() []byte
- func (a *AddressWitnessScriptHash) WitnessVersion() byte
- type Cache
- type Hash
- func (h Hash) Bytes() []byte
- func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value
- func (h Hash) Hex() string
- func (h Hash) MarshalJSON() ([]byte, error)
- func (h *Hash) Set(other Hash)
- func (h *Hash) SetBytes(b []byte)
- func (h Hash) Str() string
- func (h *Hash) UnmarshalJSON(input []byte) error
- type OrderedSet
- type TimeSorter
Constants ¶
const ( HashLength = 32 AddressLength = 42 PubkeyHashLength = 20 )
Variables ¶
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 BytesToUnit64 ¶
func IsBech32SegwitPrefix ¶ added in v0.3.0
IsBech32SegwitPrefix returns whether the prefix is a known prefix for segwit addresses on any default or registered network. This is used when decoding an address string into a specific address type.
func IsOpenFederationIssueAsset ¶ added in v1.0.5
func Unit64ToBytes ¶
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 ¶
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 ¶
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 ¶
func NewAddressWitnessPubKeyHash(witnessProg []byte, param *consensus.Params) (*AddressWitnessPubKeyHash, error)
NewAddressWitnessPubKeyHash returns a new AddressWitnessPubKeyHash.
func (*AddressWitnessPubKeyHash) EncodeAddress ¶
func (a *AddressWitnessPubKeyHash) EncodeAddress() string
EncodeAddress returns the bech32 string encoding of an AddressWitnessPubKeyHash. Part of the Address interface.
func (*AddressWitnessPubKeyHash) Hash160 ¶
func (a *AddressWitnessPubKeyHash) Hash160() *[20]byte
Hash160 returns the witness program of the AddressWitnessPubKeyHash as a byte array.
func (*AddressWitnessPubKeyHash) Hrp ¶
func (a *AddressWitnessPubKeyHash) Hrp() string
Hrp returns the human-readable part of the bech32 encoded AddressWitnessPubKeyHash.
func (*AddressWitnessPubKeyHash) IsForNet ¶
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 ¶
func (a *AddressWitnessPubKeyHash) ScriptAddress() []byte
ScriptAddress returns the witness program for this address. Part of the Address interface.
func (*AddressWitnessPubKeyHash) String ¶
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 ¶
func (a *AddressWitnessPubKeyHash) WitnessProgram() []byte
WitnessProgram returns the witness program of the AddressWitnessPubKeyHash.
func (*AddressWitnessPubKeyHash) WitnessVersion ¶
func (a *AddressWitnessPubKeyHash) WitnessVersion() byte
WitnessVersion returns the witness version of the AddressWitnessPubKeyHash.
type AddressWitnessScriptHash ¶
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 ¶
func NewAddressWitnessScriptHash(witnessProg []byte, param *consensus.Params) (*AddressWitnessScriptHash, error)
NewAddressWitnessScriptHash returns a new AddressWitnessPubKeyHash.
func (*AddressWitnessScriptHash) EncodeAddress ¶
func (a *AddressWitnessScriptHash) EncodeAddress() string
EncodeAddress returns the bech32 string encoding of an AddressWitnessScriptHash. Part of the Address interface.
func (*AddressWitnessScriptHash) Hrp ¶
func (a *AddressWitnessScriptHash) Hrp() string
Hrp returns the human-readable part of the bech32 encoded AddressWitnessScriptHash.
func (*AddressWitnessScriptHash) IsForNet ¶
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 ¶
func (a *AddressWitnessScriptHash) ScriptAddress() []byte
ScriptAddress returns the witness program for this address. Part of the Address interface.
func (*AddressWitnessScriptHash) Sha256 ¶
func (a *AddressWitnessScriptHash) Sha256() *[32]byte
Sha256 returns the witness program of the AddressWitnessPubKeyHash as a byte array.
func (*AddressWitnessScriptHash) String ¶
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 ¶
func (a *AddressWitnessScriptHash) WitnessProgram() []byte
WitnessProgram returns the witness program of the AddressWitnessScriptHash.
func (*AddressWitnessScriptHash) WitnessVersion ¶
func (a *AddressWitnessScriptHash) WitnessVersion() byte
WitnessVersion returns the witness version of the AddressWitnessScriptHash.
type Cache ¶
Cache is an LRU cache. It is safe for concurrent access.
func NewCache ¶
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) Remove ¶
func (c *Cache) Remove(key interface{})
Remove removes the provided key from the cache.
func (*Cache) RemoveOldest ¶
func (c *Cache) RemoveOldest()
RemoveOldest removes the oldest item from the cache.
type Hash ¶
type Hash [HashLength]byte
func BytesToHash ¶
func StringToHash ¶
func (*Hash) UnmarshalJSON ¶
UnmarshalJSON parses a hash in its hex from to a hash.
type OrderedSet ¶ added in v0.3.0
type OrderedSet struct {
// contains filtered or unexported fields
}
OrderedSet is a set with limited capacity. Items are evicted according to their insertion order.
func NewOrderedSet ¶ added in v0.3.0
func NewOrderedSet(capacity int) (*OrderedSet, error)
NewOrderedSet creates an ordered set with given capacity
func (*OrderedSet) Add ¶ added in v0.3.0
func (os *OrderedSet) Add(items ...interface{})
Add inserts items into the set. If capacity is reached, oldest items are evicted
func (*OrderedSet) Has ¶ added in v0.3.0
func (os *OrderedSet) Has(item interface{}) bool
Has checks if certain items exists in the set
func (*OrderedSet) Size ¶ added in v0.3.0
func (os *OrderedSet) Size() int
Size returns the size of the set
type TimeSorter ¶
type TimeSorter []uint64
timeSorter implements sort.Interface to allow a slice of timestamps to be sorted.
func (TimeSorter) Len ¶
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 ¶
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 ¶
func (s TimeSorter) Swap(i, j int)
Swap swaps the timestamps at the passed indices. It is part of the sort.Interface implementation.
Source Files ¶
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. |