bitcoinaddress

package
v0.0.0-...-7ee7fde Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2020 License: Apache-2.0 Imports: 10 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
	// 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")
)
View Source
var (
	// ErrDuplicateNet describes an error where the parameters for a Bitcoin
	// network could not be set due to the network already being a standard
	// network or previously-registered into this package.
	ErrDuplicateNet = errors.New("duplicate Bitcoin network")

	// ErrUnknownHDKeyID describes an error where the provided id which
	// is intended to identify the network for a hierarchical deterministic
	// private extended key is not registered.
	ErrUnknownHDKeyID = errors.New("unknown hd private extended key bytes")
)
View Source
var DevNetParams = Params{
	Name: "devnet",

	Bech32HRPSegwit: "db",

	PubKeyHashAddrID:        0x6f,
	ScriptHashAddrID:        0xc4,
	WitnessPubKeyHashAddrID: 0x03,
	WitnessScriptHashAddrID: 0x28,
	PrivateKeyID:            0xef,

	HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94},
	HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xcf},

	HDCoinType: 1,
}
View Source
var MainNetParams = Params{
	Name: "mainnet",

	Bech32HRPSegwit: "bc",

	PubKeyHashAddrID:        0x00,
	ScriptHashAddrID:        0x05,
	PrivateKeyID:            0x80,
	WitnessPubKeyHashAddrID: 0x06,
	WitnessScriptHashAddrID: 0x0A,

	HDPrivateKeyID: [4]byte{0x04, 0x88, 0xad, 0xe4},
	HDPublicKeyID:  [4]byte{0x04, 0x88, 0xb2, 0x1e},

	HDCoinType: 0,
}

MainNetParams defines the network parameters for the main Bitcoin network.

View Source
var RegressionNetParams = Params{
	Name: "regtest",

	Bech32HRPSegwit: "bcrt",

	PubKeyHashAddrID: 0x6f,
	ScriptHashAddrID: 0xc4,
	PrivateKeyID:     0xef,

	HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94},
	HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xcf},

	HDCoinType: 1,
}

RegressionNetParams defines the network parameters for the regression test Bitcoin network. Not to be confused with the test Bitcoin network (version 3), this network is sometimes simply called "testnet".

View Source
var TestNet3Params = Params{
	Name: "testnet3",

	Bech32HRPSegwit: "tb",

	PubKeyHashAddrID:        0x6f,
	ScriptHashAddrID:        0xc4,
	WitnessPubKeyHashAddrID: 0x03,
	WitnessScriptHashAddrID: 0x28,
	PrivateKeyID:            0xef,

	HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94},
	HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xcf},

	HDCoinType: 1,
}

TestNet3Params defines the network parameters for the test Bitcoin network (version 3). Not to be confused with the regression test network, this network is sometimes simply called "testnet".

Functions

func EncodePrivkey

func EncodePrivkey(privkey *crypto.PrivateKey, net *Params) string

EncodePrivkey returns a base58 encoded privateKey

func HDPrivateKeyToPublicKeyID

func HDPrivateKeyToPublicKeyID(id []byte) ([]byte, error)

HDPrivateKeyToPublicKeyID accepts a private hierarchical deterministic extended key id and returns the associated public key id. When the provided id is not registered, the ErrUnknownHDKeyID error will be returned.

func IsBech32SegwitPrefix

func IsBech32SegwitPrefix(prefix string) bool

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 IsPubKeyHashAddrID

func IsPubKeyHashAddrID(id byte) bool

IsPubKeyHashAddrID returns whether the id is an identifier known to prefix a pay-to-pubkey-hash address on any default or registered network. This is used when decoding an address string into a specific address type. It is up to the caller to check both this and IsScriptHashAddrID and decide whether an address is a pubkey hash address, script hash address, neither, or undeterminable (if both return true).

func IsScriptHashAddrID

func IsScriptHashAddrID(id byte) bool

IsScriptHashAddrID returns whether the id is an identifier known to prefix a pay-to-script-hash address on any default or registered network. This is used when decoding an address string into a specific address type. It is up to the caller to check both this and IsPubKeyHashAddrID and decide whether an address is a pubkey hash address, script hash address, neither, or undeterminable (if both return true).

func Register

func Register(params *Params) error

Register registers the network parameters for a Bitcoin network. This may error with ErrDuplicateNet if the network is already registered (either due to a previous Register call, or the network being one of the default networks).

Network parameters should be registered into this package by a main package as early as possible. Then, library packages may lookup networks or network parameters based on inputs and work regardless of the network being standard or not.

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(*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

func DecodeAddress(addr string, defaultNet *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 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 AddressPubKey

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

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

func NewAddressPubKey

func NewAddressPubKey(serializedPubKey []byte, net *Params) (*AddressPubKey, error)

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

func (*AddressPubKey) AddressPubKeyHash

func (a *AddressPubKey) AddressPubKeyHash() *AddressPubKeyHash

AddressPubKeyHash 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 (*AddressPubKey) EncodeAddress

func (a *AddressPubKey) 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 (*AddressPubKey) Format

func (a *AddressPubKey) Format() PubKeyFormat

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

func (*AddressPubKey) IsForNet

func (a *AddressPubKey) IsForNet(net *Params) bool

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

func (*AddressPubKey) PubKey

func (a *AddressPubKey) PubKey() *crypto.PublicKey

PubKey returns the underlying public key for the address.

func (*AddressPubKey) ScriptAddress

func (a *AddressPubKey) 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 (*AddressPubKey) SetFormat

func (a *AddressPubKey) SetFormat(pkFormat PubKeyFormat)

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

func (*AddressPubKey) String

func (a *AddressPubKey) 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 AddressPubKeyHash

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

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

func NewAddressPubKeyHash

func NewAddressPubKeyHash(pkHash []byte, net *Params) (*AddressPubKeyHash, error)

NewAddressPubKeyHash returns a new AddressPubKeyHash. pkHash mustbe 20 bytes.

func (*AddressPubKeyHash) EncodeAddress

func (a *AddressPubKeyHash) EncodeAddress() string

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

func (*AddressPubKeyHash) Hash160

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

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

func (*AddressPubKeyHash) IsForNet

func (a *AddressPubKeyHash) IsForNet(net *Params) bool

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

func (*AddressPubKeyHash) ScriptAddress

func (a *AddressPubKeyHash) 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 (*AddressPubKeyHash) String

func (a *AddressPubKeyHash) 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 AddressScriptHash

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

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

func NewAddressScriptHash

func NewAddressScriptHash(serializedScript []byte, net *Params) (*AddressScriptHash, error)

NewAddressScriptHash returns a new AddressScriptHash.

func NewAddressScriptHashFromHash

func NewAddressScriptHashFromHash(scriptHash []byte, net *Params) (*AddressScriptHash, error)

NewAddressScriptHashFromHash returns a new AddressScriptHash. scriptHash must be 20 bytes.

func (*AddressScriptHash) EncodeAddress

func (a *AddressScriptHash) EncodeAddress() string

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

func (*AddressScriptHash) Hash160

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

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

func (*AddressScriptHash) IsForNet

func (a *AddressScriptHash) IsForNet(net *Params) bool

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

func (*AddressScriptHash) ScriptAddress

func (a *AddressScriptHash) 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 (*AddressScriptHash) String

func (a *AddressScriptHash) 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.

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, net *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

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

func (*AddressWitnessPubKeyHash) IsForNet

func (a *AddressWitnessPubKeyHash) IsForNet(net *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, net *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

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

func (*AddressWitnessScriptHash) IsForNet

func (a *AddressWitnessScriptHash) IsForNet(net *Params) bool

IsForNet returns whether or not the AddressWitnessScriptHash is associated with the passed bitcoin 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) 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 Params

type Params struct {
	// Name defines a human-readable identifier for the network.
	Name string

	// Human-readable part for Bech32 encoded segwit addresses, as defined
	// in BIP 173.
	Bech32HRPSegwit string

	// Address encoding magics
	PubKeyHashAddrID        byte // First byte of a P2PKH address
	ScriptHashAddrID        byte // First byte of a P2SH address
	PrivateKeyID            byte // First byte of a WIF private key
	WitnessPubKeyHashAddrID byte // First byte of a P2WPKH address
	WitnessScriptHashAddrID byte // First byte of a P2WSH address

	// BIP32 hierarchical deterministic extended key magics
	HDPrivateKeyID [4]byte
	HDPublicKeyID  [4]byte

	// BIP44 coin type used in the hierarchical deterministic path for
	// address generation.
	HDCoinType uint32
}

Params defines a Bitcoin network by its parameters. These parameters may be used by Bitcoin applications to differentiate networks as well as addresses and keys for one network from those intended for use on another network.

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 UnsupportedWitnessProgLenError

type UnsupportedWitnessProgLenError int

UnsupportedWitnessProgLenError describes an error where a segwit address being decoded has an unsupported witness program length.

func (UnsupportedWitnessProgLenError) Error

type UnsupportedWitnessVerError

type UnsupportedWitnessVerError byte

UnsupportedWitnessVerError describes an error where a segwit address being decoded has an unsupported witness version.

func (UnsupportedWitnessVerError) Error

Jump to

Keyboard shortcuts

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