Documentation ¶
Index ¶
- Constants
- Variables
- func Hash160(buf []byte) []byte
- func Keccak256Hash160(buf []byte) []byte
- type Address
- type AddressPubKey
- func (a *AddressPubKey) AddressPubKeyHash() *AddressPubKeyHash
- func (a *AddressPubKey) EncodeAddress() string
- func (a *AddressPubKey) Format() PubKeyFormat
- func (a *AddressPubKey) IsForNet(net *chaincfg.Params) bool
- func (a *AddressPubKey) ScriptAddress() []byte
- func (a *AddressPubKey) SetFormat(pkFormat PubKeyFormat)
- func (a *AddressPubKey) String() string
- type AddressPubKeyHash
- type AddressScriptHash
- type AddressWitnessPubKeyHash
- func (a *AddressWitnessPubKeyHash) EncodeAddress() string
- func (a *AddressWitnessPubKeyHash) Hash160() *[20]byte
- func (a *AddressWitnessPubKeyHash) Hrp() string
- func (a *AddressWitnessPubKeyHash) IsForNet(net *chaincfg.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(net *chaincfg.Params) bool
- func (a *AddressWitnessScriptHash) ScriptAddress() []byte
- func (a *AddressWitnessScriptHash) String() string
- func (a *AddressWitnessScriptHash) WitnessProgram() []byte
- func (a *AddressWitnessScriptHash) WitnessVersion() byte
- type Amount
- type AmountUnit
- type Block
- func (b *Block) Bytes() ([]byte, error)
- func (b *Block) BytesNoWitness() ([]byte, error)
- func (b *Block) Hash() *chainhash.Hash
- func (b *Block) Height() int32
- func (b *Block) MsgBlock() *wire.MsgBlock
- func (b *Block) SetHeight(height int32)
- func (b *Block) Transactions() []*Tx
- func (b *Block) Tx(txNum int) (*Tx, error)
- func (b *Block) TxHash(txNum int) (*chainhash.Hash, error)
- func (b *Block) TxLoc() ([]wire.TxLoc, error)
- type OutOfRangeError
- type PubKeyFormat
- type Tx
- type UnsupportedWitnessProgLenError
- type UnsupportedWitnessVerError
Constants ¶
const ( // SatoshiPerBitcent is the number of satoshi in one bitcoin cent. SatoshiPerBitcent = 1e6 // SatoshiPerBitcoin is the number of satoshi in one bitcoin (1 BTC). SatoshiPerBitcoin = 1e8 // MaxSatoshi is the maximum transaction amount allowed in satoshi. MaxSatoshi = 21e6 * SatoshiPerBitcoin )
const BlockHeightUnknown = int32(-1)
BlockHeightUnknown is the value returned for a block height that is unknown. This is typically because the block has not been inserted into the main chain yet.
const TxIndexUnknown = -1
TxIndexUnknown is the value returned for a transaction index that is unknown. This is typically because the transaction has not been inserted into a block yet.
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") )
Functions ¶
func Keccak256Hash160 ¶
keccak256Hash160 calculates the hash ripemd160(sha3-256(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 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 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 *chaincfg.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 *chaincfg.Params) bool
IsForNet returns whether or not the pay-to-pubkey address is associated with the passed bitcoin network.
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)
Does nothing
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 *chaincfg.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 *chaincfg.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 *chaincfg.Params) (*AddressScriptHash, error)
NewAddressScriptHash returns a new AddressScriptHash.
func NewAddressScriptHashFromHash ¶
func NewAddressScriptHashFromHash(scriptHash []byte, net *chaincfg.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 *chaincfg.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 *chaincfg.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(net *chaincfg.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 *chaincfg.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(net *chaincfg.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 Amount ¶
type Amount int64
Amount represents the base bitcoin monetary unit (colloquially referred to as a `Satoshi'). A single Amount is equal to 1e-8 of a bitcoin.
func NewAmount ¶
NewAmount creates an Amount from a floating point value representing some value in bitcoin. NewAmount errors if f is NaN or +-Infinity, but does not check that the amount is within the total amount of bitcoin producible as f may not refer to an amount at a single moment in time.
NewAmount is for specifically for converting BTC to Satoshi. For creating a new Amount with an int64 value which denotes a quantity of Satoshi, do a simple type conversion from type int64 to Amount. See GoDoc for example: http://godoc.org/github.com/nbcorg/btcutil#example-Amount
func (Amount) Format ¶
func (a Amount) Format(u AmountUnit) string
Format formats a monetary amount counted in bitcoin base units as a string for a given unit. The conversion will succeed for any unit, however, known units will be formated with an appended label describing the units with SI notation, or "Satoshi" for the base unit.
func (Amount) MulF64 ¶
MulF64 multiplies an Amount by a floating point value. While this is not an operation that must typically be done by a full node or wallet, it is useful for services that build on top of bitcoin (for example, calculating a fee by multiplying by a percentage).
func (Amount) ToUnit ¶
func (a Amount) ToUnit(u AmountUnit) float64
ToUnit converts a monetary amount counted in bitcoin base units to a floating point value representing an amount of bitcoin.
type AmountUnit ¶
type AmountUnit int
AmountUnit describes a method of converting an Amount to something other than the base unit of a bitcoin. The value of the AmountUnit is the exponent component of the decadic multiple to convert from an amount in bitcoin to an amount counted in units.
const ( AmountMegaBTC AmountUnit = 6 AmountKiloBTC AmountUnit = 3 AmountBTC AmountUnit = 0 AmountMilliBTC AmountUnit = -3 AmountMicroBTC AmountUnit = -6 AmountSatoshi AmountUnit = -8 )
These constants define various units used when describing a bitcoin monetary amount.
func (AmountUnit) String ¶
func (u AmountUnit) String() string
String returns the unit as a string. For recognized units, the SI prefix is used, or "Satoshi" for the base unit. For all unrecognized units, "1eN BTC" is returned, where N is the AmountUnit.
type Block ¶
type Block struct {
// contains filtered or unexported fields
}
Block defines a bitcoin block that provides easier and more efficient manipulation of raw blocks. It also memoizes hashes for the block and its transactions on their first access so subsequent accesses don't have to repeat the relatively expensive hashing operations.
func NewBlock ¶
NewBlock returns a new instance of a bitcoin block given an underlying wire.MsgBlock. See Block.
func NewBlockFromBlockAndBytes ¶
NewBlockFromBlockAndBytes returns a new instance of a bitcoin block given an underlying wire.MsgBlock and the serialized bytes for it. See Block.
func NewBlockFromBytes ¶
NewBlockFromBytes returns a new instance of a bitcoin block given the serialized bytes. See Block.
func NewBlockFromReader ¶
NewBlockFromReader returns a new instance of a bitcoin block given a Reader to deserialize the block. See Block.
func (*Block) Bytes ¶
Bytes returns the serialized bytes for the Block. This is equivalent to calling Serialize on the underlying wire.MsgBlock, however it caches the result so subsequent calls are more efficient.
func (*Block) BytesNoWitness ¶
BytesNoWitness returns the serialized bytes for the block with transactions encoded without any witness data.
func (*Block) Hash ¶
Hash returns the block identifier hash for the Block. This is equivalent to calling BlockHash on the underlying wire.MsgBlock, however it caches the result so subsequent calls are more efficient.
func (*Block) Height ¶
Height returns the saved height of the block in the block chain. This value will be BlockHeightUnknown if it hasn't already explicitly been set.
func (*Block) Transactions ¶
Transactions returns a slice of wrapped transactions (btcutil.Tx) for all transactions in the Block. This is nearly equivalent to accessing the raw transactions (wire.MsgTx) in the underlying wire.MsgBlock, however it instead provides easy access to wrapped versions (btcutil.Tx) of them.
func (*Block) Tx ¶
Tx returns a wrapped transaction (btcutil.Tx) for the transaction at the specified index in the Block. The supplied index is 0 based. That is to say, the first transaction in the block is txNum 0. This is nearly equivalent to accessing the raw transaction (wire.MsgTx) from the underlying wire.MsgBlock, however the wrapped transaction has some helpful properties such as caching the hash so subsequent calls are more efficient.
func (*Block) TxHash ¶
TxHash returns the hash for the requested transaction number in the Block. The supplied index is 0 based. That is to say, the first transaction in the block is txNum 0. This is equivalent to calling TxHash on the underlying wire.MsgTx, however it caches the result so subsequent calls are more efficient.
type OutOfRangeError ¶
type OutOfRangeError string
OutOfRangeError describes an error due to accessing an element that is out of range.
func (OutOfRangeError) Error ¶
func (e OutOfRangeError) Error() string
Error satisfies the error interface and prints human-readable errors.
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 Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx defines a bitcoin transaction that provides easier and more efficient manipulation of raw transactions. It also memoizes the hash for the transaction on its first access so subsequent accesses don't have to repeat the relatively expensive hashing operations.
func NewTx ¶
NewTx returns a new instance of a bitcoin transaction given an underlying wire.MsgTx. See Tx.
func NewTxFromBytes ¶
NewTxFromBytes returns a new instance of a bitcoin transaction given the serialized bytes. See Tx.
func NewTxFromReader ¶
NewTxFromReader returns a new instance of a bitcoin transaction given a Reader to deserialize the transaction. See Tx.
func (*Tx) HasWitness ¶
HasWitness returns false if none of the inputs within the transaction contain witness data, true false otherwise. This equivalent to calling HasWitness on the underlying wire.MsgTx, however it caches the result so subsequent calls are more efficient.
func (*Tx) Hash ¶
Hash returns the hash of the transaction. This is equivalent to calling TxHash on the underlying wire.MsgTx, however it caches the result so subsequent calls are more efficient.
func (*Tx) Index ¶
Index returns the saved index of the transaction within a block. This value will be TxIndexUnknown if it hasn't already explicitly been set.
func (*Tx) WitnessHash ¶
WitnessHash returns the witness hash (wtxid) of the transaction. This is equivalent to calling WitnessHash on the underlying wire.MsgTx, however it caches the result so subsequent calls are more efficient.
type UnsupportedWitnessProgLenError ¶
type UnsupportedWitnessProgLenError int
UnsupportedWitnessProgLenError describes an error where a segwit address being decoded has an unsupported witness program length.
func (UnsupportedWitnessProgLenError) Error ¶
func (e UnsupportedWitnessProgLenError) Error() string
type UnsupportedWitnessVerError ¶
type UnsupportedWitnessVerError byte
UnsupportedWitnessVerError describes an error where a segwit address being decoded has an unsupported witness version.
func (UnsupportedWitnessVerError) Error ¶
func (e UnsupportedWitnessVerError) Error() string