btcaddr

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2021 License: Unlicense Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

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 beginning 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")
)

Functions

func Hash160

func Hash160(buf []byte) []byte

Hash160 calculates the hash ripemd160(sha256(b)).

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 bitcoin network.
	IsForNet(*chaincfg.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 Decode

func Decode(addr string, defaultNet *chaincfg.Params) (Address, error)

Decode decodes the string encoding of an address and returns the Address if addr is a valid encoding for a known address type. The bitcoin 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 PubKey

type PubKey struct {
	PubKeyFormat PubKeyFormat
	PublicKey    *ec.PublicKey
	// contains filtered or unexported fields
}

PubKey is an Address for a pay-to-pubkey transaction.

func NewPubKey

func NewPubKey(serializedPubKey []byte, net *chaincfg.Params) (*PubKey, error)

NewPubKey returns a new PubKey which represents a pay-to-pubkey address. The serializedPubKey parameter must be a valid pubkey and can be uncompressed, compressed, or hybrid.

func (*PubKey) EncodeAddress

func (a *PubKey) EncodeAddress() string

EncodeAddress returns the string encoding of the public key as a pay-to-pubkey-hash. Note that the public key format (uncompressed, compressed, etc) will change the resulting address. This is expected since pay-to-pubkey-hash is a hash of the serialized public key which obviously differs with the format. At the time of this writing, most Bitcoin addresses are pay-to-pubkey-hash constructed from the uncompressed public key. Part of the Address interface.

func (*PubKey) Format

func (a *PubKey) Format() PubKeyFormat

Format returns the format (uncompressed, compressed, etc) of the pay-to-pubkey address.

func (*PubKey) IsForNet

func (a *PubKey) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the pay-to-pubkey address is associated with the passed bitcoin network.

func (*PubKey) PubKey

func (a *PubKey) PubKey() *ec.PublicKey

PubKey returns the underlying public key for the address.

func (*PubKey) PubKeyHash

func (a *PubKey) PubKeyHash() *PubKeyHash

PubKeyHash returns the pay-to-pubkey address converted to a pay-to-pubkey-hash address. Note that the public key format (uncompressed, compressed, etc) will change the resulting address. This is expected since pay-to-pubkey-hash is a hash of the serialized public key which obviously differs with the format. At the time of this writing, most Bitcoin addresses are pay-to-pubkey-hash constructed from the uncompressed public key.

func (*PubKey) ScriptAddress

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

ScriptAddress returns the bytes to be included in a txout script to pay to a public key. Setting the public key format will affect the output of this function accordingly. Part of the Address interface.

func (*PubKey) SetFormat

func (a *PubKey) SetFormat(pkFormat PubKeyFormat)

SetFormat sets the format (uncompressed, compressed, etc) of the pay-to-pubkey address.

func (*PubKey) String

func (a *PubKey) String() string

String returns the hex-encoded human-readable string for the pay-to-pubkey address. This is not the same as calling EncodeAddress.

type PubKeyFormat

type PubKeyFormat int

PubKeyFormat describes what format to use for a pay-to-pubkey address.

const (
	// PKFUncompressed indicates the pay-to-pubkey address format is an uncompressed public key.
	PKFUncompressed PubKeyFormat = iota
	// PKFCompressed indicates the pay-to-pubkey address format is a compressed public key.
	PKFCompressed
	// PKFHybrid indicates the pay-to-pubkey address format is a hybrid public key.
	PKFHybrid
)

type PubKeyHash

type PubKeyHash struct {
	Hash  [ripemd160.Size]byte
	NetID byte
}

PubKeyHash is an Address for a pay-to-pubkey-hash (P2PKH) transaction.

func NewPubKeyHash

func NewPubKeyHash(pkHash []byte, net *chaincfg.Params) (*PubKeyHash, error)

NewPubKeyHash returns a new PubKeyHash. pkHash must be 20 bytes.

func (*PubKeyHash) EncodeAddress

func (a *PubKeyHash) EncodeAddress() string

EncodeAddress returns the string encoding of a pay-to-pubkey-hash address. Part of the Address interface.

func (*PubKeyHash) Hash160

func (a *PubKeyHash) Hash160() *[ripemd160.Size]byte

Hash160 returns the underlying array of the pubkey hash. This can be useful when an array is more appropriate than a slice (for example, when used as map keys).

func (*PubKeyHash) IsForNet

func (a *PubKeyHash) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the pay-to-pubkey-hash address is associated with the passed bitcoin network.

func (*PubKeyHash) ScriptAddress

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

ScriptAddress returns the bytes to be included in a txout script to pay to a pubkey hash. Part of the Address interface.

func (*PubKeyHash) String

func (a *PubKeyHash) String() string

String returns a human-readable string for the pay-to-pubkey-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.

type ScriptHash

type ScriptHash struct {
	Hash  [ripemd160.Size]byte
	NetID byte
}

ScriptHash is an Address for a pay-to-script-hash (P2SH) transaction.

func NewScriptHash

func NewScriptHash(serializedScript []byte, net *chaincfg.Params) (*ScriptHash, error)

NewScriptHash returns a new ScriptHash.

func NewScriptHashFromHash

func NewScriptHashFromHash(scriptHash []byte, net *chaincfg.Params) (*ScriptHash, error)

NewScriptHashFromHash returns a new ScriptHash. scriptHash must be 20 bytes.

func (*ScriptHash) EncodeAddress

func (a *ScriptHash) EncodeAddress() string

EncodeAddress returns the string encoding of a pay-to-script-hash address. Part of the Address interface.

func (*ScriptHash) Hash160

func (a *ScriptHash) Hash160() *[ripemd160.Size]byte

Hash160 returns the underlying array of the script hash. This can be useful when an array is more appropriate than a slice (for example, when used as map keys).

func (*ScriptHash) IsForNet

func (a *ScriptHash) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the pay-to-script-hash address is associated with the passed bitcoin network.

func (*ScriptHash) ScriptAddress

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

ScriptAddress returns the bytes to be included in a txout script to pay to a script hash. Part of the Address interface.

func (*ScriptHash) String

func (a *ScriptHash) String() string

String returns a human-readable string for the pay-to-script-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.

Jump to

Keyboard shortcuts

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