Documentation ¶
Overview ¶
Package bitcoin defines types and interfaces required to work with the Bitcoin chain. This package is meant to reflect the part of the Bitcoin protocol to an extent required by the client applications. That is, this package can only hold the components specific to the Bitcoin domain and must remain free of application-specific items. Third-party helper libraries are allowed for use within this package though no external type should leak outside.
Index ¶
- Constants
- func AssembleSpvProof(transactionHash Hash, requiredConfirmations uint, btcChain Chain) (*Transaction, *SpvProof, error)
- func ExtractPublicKeyHash(script Script) ([20]byte, error)
- func PublicKeyHash(publicKey *ecdsa.PublicKey) [20]byte
- func ScriptHash(script Script) [20]byte
- func WitnessScriptHash(script Script) [32]byte
- type BlockHeader
- type ByteOrder
- type Chain
- type CompactSizeUint
- type Hash
- type Network
- type Script
- func NewScriptFromVarLenData(varLenData []byte) (Script, error)
- func PayToPublicKeyHash(publicKeyHash [20]byte) (Script, error)
- func PayToScriptHash(scriptHash [20]byte) (Script, error)
- func PayToWitnessPublicKeyHash(publicKeyHash [20]byte) (Script, error)
- func PayToWitnessScriptHash(witnessScriptHash [32]byte) (Script, error)
- type ScriptType
- type SignatureContainer
- type SpvProof
- type Transaction
- func (t *Transaction) Deserialize(data []byte) error
- func (t *Transaction) Hash() Hash
- func (t *Transaction) Serialize(format ...TransactionSerializationFormat) []byte
- func (t *Transaction) SerializeInputs() []byte
- func (t *Transaction) SerializeLocktime() [4]byte
- func (t *Transaction) SerializeOutputs() []byte
- func (t *Transaction) SerializeVersion() [4]byte
- func (t *Transaction) WitnessHash() Hash
- type TransactionBuilder
- func (tb *TransactionBuilder) AddOutput(output *TransactionOutput)
- func (tb *TransactionBuilder) AddPublicKeyHashInput(utxo *UnspentTransactionOutput) error
- func (tb *TransactionBuilder) AddScriptHashInput(utxo *UnspentTransactionOutput, redeemScript Script) error
- func (tb *TransactionBuilder) AddSignatures(signatures []*SignatureContainer) (*Transaction, error)
- func (tb *TransactionBuilder) ComputeSignatureHashes() ([]*big.Int, error)
- func (tb *TransactionBuilder) TotalInputsValue() int64
- type TransactionFeeEstimator
- type TransactionInput
- type TransactionMerkleProof
- type TransactionOutpoint
- type TransactionOutput
- type TransactionSerializationFormat
- type TransactionSizeEstimator
- func (tse *TransactionSizeEstimator) AddPublicKeyHashInputs(count int, isWitness bool) *TransactionSizeEstimator
- func (tse *TransactionSizeEstimator) AddPublicKeyHashOutputs(count int, isWitness bool) *TransactionSizeEstimator
- func (tse *TransactionSizeEstimator) AddScriptHashInputs(count int, redeemScriptLength int, isWitness bool) *TransactionSizeEstimator
- func (tse *TransactionSizeEstimator) AddScriptHashOutputs(count int, isWitness bool) *TransactionSizeEstimator
- func (tse *TransactionSizeEstimator) VirtualSize() (int64, error)
- type UnspentTransactionOutput
Constants ¶
const BlockHeaderByteLength = 80
BlockHeaderByteLength is the byte length of a serialized block header.
const HashByteLength = 32
HashByteLength is the byte length of the Hash type.
Variables ¶
This section is empty.
Functions ¶
func AssembleSpvProof ¶
func AssembleSpvProof( transactionHash Hash, requiredConfirmations uint, btcChain Chain, ) (*Transaction, *SpvProof, error)
AssembleSpvProof assembles a proof that a given transaction was included in the blockchain and has accumulated the required number of confirmations.
func ExtractPublicKeyHash ¶
ExtractPublicKeyHash extracts the public key hash from a P2WPKH or P2PKH script.
func PublicKeyHash ¶
PublicKeyHash constructs the 20-byte public key hash by applying SHA-256 then RIPEMD-160 on the provided ECDSA public key.
func ScriptHash ¶
ScriptHash constructs the 20-byte script hash by applying SHA-256 then RIPEMD-160 on the provided Script.
func WitnessScriptHash ¶
WitnessScriptHash constructs the 32-byte witness script hash by applying single SHA-256 on the provided Script.
Types ¶
type BlockHeader ¶
type BlockHeader struct { // Version is the block version number that indicates which set of block // validation rules to follow. Version int32 // PreviousBlockHeaderHash is the hash of the previous block's header. PreviousBlockHeaderHash Hash // MerkleRootHash is a hash derived from the hashes of all transactions // included in this block. MerkleRootHash Hash // Time is a Unix epoch time when the miner started hashing the header. Time uint32 // Bits determines the target threshold this block's header hash must be // less than or equal to. Bits uint32 // Nonce is an arbitrary number miners change to modify the header hash // in order to produce a hash less than or equal to the target threshold. Nonce uint32 }
BlockHeader represents the header of a Bitcoin block. For reference, see: https://developer.bitcoin.org/reference/block_chain.html#block-headers
func (*BlockHeader) Deserialize ¶
func (bh *BlockHeader) Deserialize(rawBlockHeader [BlockHeaderByteLength]byte)
Deserialize deserializes a byte array to a BlockHeader using the block header serialization format: [Version][PreviousBlockHeaderHash][MerkleRootHash][Time][Bits][Nonce].
func (*BlockHeader) Difficulty ¶
func (bh *BlockHeader) Difficulty() *big.Int
Difficulty calculates the difficulty of a block header. The difficulty is the measure of how hard it is to mine a valid Bitcoin block. It is calculated by dividing the maximum possible target by the target calculated from the `Bits` field.
func (*BlockHeader) Hash ¶
func (bh *BlockHeader) Hash() Hash
Hash calculates the block header's hash as the double SHA-256 of the block header serialization format: [Version][PreviousBlockHeaderHash][MerkleRootHash][Time][Bits][Nonce].
func (*BlockHeader) Serialize ¶
func (bh *BlockHeader) Serialize() [BlockHeaderByteLength]byte
Serialize serializes the block header to a byte array using the block header serialization format: [Version][PreviousBlockHeaderHash][MerkleRootHash][Time][Bits][Nonce].
func (*BlockHeader) Target ¶
func (bh *BlockHeader) Target() *big.Int
Target calculates the difficulty target of a block header. A Bitcoin block must have its hash lower than or equal to the target calculated from the `Bits` field.
type ByteOrder ¶
type ByteOrder int
ByteOrder represents the byte order used by the Bitcoin byte arrays. The Bitcoin ecosystem is not totally consistent in this regard and different byte orders are used depending on the purpose.
const ( // InternalByteOrder represents the internal byte order used by the Bitcoin // protocol. This is the primary byte order that is suitable for the // use cases related with the protocol logic and cryptography. Byte arrays // using this byte order should be converted to numbers according to // the little-endian sequence. InternalByteOrder ByteOrder = iota // ReversedByteOrder represents the "human" byte order. This is the // byte order that is typically used by the third party services like // block explorers or Bitcoin chain clients. Byte arrays using this byte // order should be converted to numbers according to the big-endian // sequence. This type is also known as the `RPC Byte Order` in the // Bitcoin specification. ReversedByteOrder )
type Chain ¶
type Chain interface { // GetTransaction gets the transaction with the given transaction hash. // If the transaction with the given hash was not found on the chain, // this function returns an error. GetTransaction(transactionHash Hash) (*Transaction, error) // GetTransactionConfirmations gets the number of confirmations for the // transaction with the given transaction hash. If the transaction with the // given hash was not found on the chain, this function returns an error. GetTransactionConfirmations(transactionHash Hash) (uint, error) // BroadcastTransaction broadcasts the given transaction over the // network of the Bitcoin chain nodes. If the broadcast action could not be // done, this function returns an error. This function does not give any // guarantees regarding transaction mining. The transaction may be mined or // rejected eventually. BroadcastTransaction(transaction *Transaction) error // GetLatestBlockHeight gets the height of the latest block (tip). If the // latest block was not determined, this function returns an error. GetLatestBlockHeight() (uint, error) // GetBlockHeader gets the block header for the given block height. If the // block with the given height was not found on the chain, this function // returns an error. GetBlockHeader(blockHeight uint) (*BlockHeader, error) // GetTransactionMerkleProof gets the Merkle proof for a given transaction. // The transaction's hash and the block the transaction was included in the // blockchain need to be provided. GetTransactionMerkleProof( transactionHash Hash, blockHeight uint, ) (*TransactionMerkleProof, error) // GetTransactionsForPublicKeyHash gets the confirmed transactions that pays the // given public key hash using either a P2PKH or P2WPKH script. The returned // transactions are ordered by block height in the ascending order, i.e. // the latest transaction is at the end of the list. The returned list does // not contain unconfirmed transactions living in the mempool at the moment // of request. The returned transactions list can be limited using the // `limit` parameter. For example, if `limit` is set to `5`, only the // latest five transactions will be returned. Note that taking an unlimited // transaction history may be time-consuming as this function fetches // complete transactions with all necessary data. GetTransactionsForPublicKeyHash( publicKeyHash [20]byte, limit int, ) ([]*Transaction, error) // GetTxHashesForPublicKeyHash gets hashes of confirmed transactions that pays // the given public key hash using either a P2PKH or P2WPKH script. The returned // transactions hashes are ordered by block height in the ascending order, i.e. // the latest transaction hash is at the end of the list. The returned list does // not contain unconfirmed transactions hashes living in the mempool at the // moment of request. GetTxHashesForPublicKeyHash( publicKeyHash [20]byte, ) ([]Hash, error) // GetMempoolForPublicKeyHash gets the unconfirmed mempool transactions // that pays the given public key hash using either a P2PKH or P2WPKH script. // The returned transactions are in an indefinite order. GetMempoolForPublicKeyHash(publicKeyHash [20]byte) ([]*Transaction, error) // GetUtxosForPublicKeyHash gets unspent outputs of confirmed transactions that // are controlled by the given public key hash (either a P2PKH or P2WPKH script). // The returned UTXOs are ordered by block height in the ascending order, i.e. // the latest UTXO is at the end of the list. The returned list does not contain // unspent outputs of unconfirmed transactions living in the mempool at the // moment of request. Outputs used as inputs of confirmed or mempool // transactions are not returned as well because they are no longer UTXOs. GetUtxosForPublicKeyHash( publicKeyHash [20]byte, ) ([]*UnspentTransactionOutput, error) // GetMempoolUtxosForPublicKeyHash gets unspent outputs of unconfirmed transactions // that are controlled by the given public key hash (either a P2PKH or P2WPKH script). // The returned UTXOs are in an indefinite order. The returned list does not // contain unspent outputs of confirmed transactions. Outputs used as inputs of // confirmed or mempool transactions are not returned as well because they are // no longer UTXOs. GetMempoolUtxosForPublicKeyHash( publicKeyHash [20]byte, ) ([]*UnspentTransactionOutput, error) // EstimateSatPerVByteFee returns the estimated sat/vbyte fee for a // transaction to be confirmed within the given number of blocks. EstimateSatPerVByteFee(blocks uint32) (int64, error) // GetCoinbaseTxHash gets the hash of the coinbase transaction for the given // block height. GetCoinbaseTxHash(blockHeight uint) (Hash, error) }
Chain defines an interface meant to be used for interaction with the Bitcoin chain.
type CompactSizeUint ¶
type CompactSizeUint uint64
CompactSizeUint is a documentation type that is supposed to capture the details of the Bitcoin's CompactSize Unsigned Integer. It represents a number value encoded to bytes according to the following rules:
---------------- Value ---------------- | Bytes | --------------- Format --------------
--------------------------------------------------------------------------------------- >= 0 && <= 252 | 1 | uint8 >= 253 && <= 0xffff | 3 | 0xfd followed by the number as uint16 >= 0x10000 && <= 0xffffffff | 5 | 0xfe followed by the number as uint32 >= 0x100000000 && <= 0xffffffffffffffff | 9 | 0xff followed by the number as uint64
Worth noting, the encoded number value is represented using the little-endian byte order. For example, to convert the compact size uint 0xfd0302, the 0xfd prefix must be skipped and the 0x0302 must be reversed to 0x0203 and then converted to a decimal number 515.
For reference, see: https://developer.bitcoin.org/reference/transactions.html#compactsize-unsigned-integers
type Hash ¶
type Hash [HashByteLength]byte
Hash represents the double SHA-256 of some arbitrary data using the InternalByteOrder.
func ComputeHash ¶
ComputeHash computes the Hash for the provided data.
func NewHash ¶
NewHash creates a new Hash instance using the given byte slice. The byte slice is interpreted according to the given ByteOrder. That is, the byte slice is taken as is if the ByteOrder is InternalByteOrder and reversed if the ByteOrder is ReversedByteOrder. The byte slice's length must be equal to HashByteLength.
func NewHashFromString ¶
NewHashFromString creates a new Hash instance using the given string. The string is interpreted according to the given ByteOrder. That is, the string is taken as is if the ByteOrder is InternalByteOrder and reversed if the ByteOrder is ReversedByteOrder. The string's length must be equal to 2*HashByteLength.
type Script ¶
type Script []byte
Script represents an arbitrary Bitcoin script, NOT prepended with the byte-length of the script
func NewScriptFromVarLenData ¶
NewScriptFromVarLenData construct a Script instance based on the provided variable length data prepended with a CompactSizeUint.
func PayToPublicKeyHash ¶
PayToPublicKeyHash constructs a P2PKH script for the provided 20-byte public key hash. The function assumes the provided public key hash is valid.
func PayToScriptHash ¶
PayToScriptHash constructs a P2SH script for the provided 20-byte script hash. The function assumes the provided script hash is valid.
func PayToWitnessPublicKeyHash ¶
PayToWitnessPublicKeyHash constructs a P2WPKH script for the provided 20-byte public key hash. The function assumes the provided public key hash is valid.
func PayToWitnessScriptHash ¶
PayToWitnessScriptHash constructs a P2WSH script for the provided 32-byte witness script hash. The function assumes the provided script hash is valid.
func (Script) ToVarLenData ¶
ToVarLenData converts the Script to a byte array prepended with a CompactSizeUint holding the script's byte length.
type ScriptType ¶
type ScriptType uint8
ScriptType represents the possible types of Script.
const ( NonStandardScript ScriptType = iota P2PKHScript P2WPKHScript P2SHScript P2WSHScript )
func GetScriptType ¶
func GetScriptType(script Script) ScriptType
GetScriptType gets the ScriptType of the given Script.
func (ScriptType) String ¶
func (st ScriptType) String() string
type SignatureContainer ¶
SignatureContainer is a helper type holding signature data.
type SpvProof ¶
type SpvProof struct { // MerkleProof is the Merkle proof of transaction inclusion in a block. MerkleProof []byte // TxIndexInBlock is the transaction index in the block (0-indexed). TxIndexInBlock uint // BitcoinHeaders is a chain of block headers that form confirmations of // blockchain inclusion. BitcoinHeaders []byte // CoinbasePreimage is the sha256 preimage of the coinbase transaction hash // i.e., the sha256 hash of the coinbase transaction. CoinbasePreimage [32]byte // CoinbaseProof is the Merkle proof of coinbase transaction inclusion in // a block. CoinbaseProof []byte }
SpvProof contains data required to perform a proof that a given transaction was included in the Bitcoin blockchain.
type Transaction ¶
type Transaction struct { // Version is the transaction version number. Usually, version 1 or 2. Version int32 // Inputs is a slice holding all inputs of the transaction. Inputs []*TransactionInput // Outputs is a slice holding all outputs of the transaction. Outputs []*TransactionOutput // Locktime is the transaction locktime being a Unix epoch time or // block number, interpreted according to the locktime parsing rules: // https://developer.bitcoin.org/devguide/transactions.html#locktime-and-sequence-number Locktime uint32 }
Transaction represents a Bitcoin transaction. For reference, see: https://developer.bitcoin.org/reference/transactions.html#raw-transaction-format
func (*Transaction) Deserialize ¶
func (t *Transaction) Deserialize(data []byte) error
Deserialize deserializes the given byte array to a Transaction.
func (*Transaction) Hash ¶
func (t *Transaction) Hash() Hash
Hash calculates the transaction's hash as the double SHA-256 of the Standard serialization format. The outcome is equivalent to the txid field defined in the Bitcoin specification and is used as transaction identifier.
func (*Transaction) Serialize ¶
func (t *Transaction) Serialize( format ...TransactionSerializationFormat, ) []byte
Serialize serializes the transaction to a byte array using the specified serialization format. The actual result depends on the transaction type as described below.
If the transaction CONTAINS witness inputs and Serialize is called with:
- Standard serialization format, the result is actually in the Standard format and does not include witness data referring to the witness inputs
- Witness serialization format, the result is actually in the Witness format and includes witness data referring to the witness inputs
If the transaction DOES NOT CONTAIN witness inputs and Serialize is called with:
- Standard serialization format, the result is actually in the Standard format
- Witness serialization format, the result is actually in the Standard format because there are no witness inputs whose data can be included
By default, the Witness format is used and that can be changed using the optional format argument. The Witness format is used by default as it fits more use cases.
func (*Transaction) SerializeInputs ¶
func (t *Transaction) SerializeInputs() []byte
SerializeInputs serializes the transaction inputs to a byte array prepended with a CompactSizeUint denoting the total number of inputs.
func (*Transaction) SerializeLocktime ¶
func (t *Transaction) SerializeLocktime() [4]byte
SerializeLocktime serializes the transaction locktime to a little-endian 4-byte array.
func (*Transaction) SerializeOutputs ¶
func (t *Transaction) SerializeOutputs() []byte
SerializeOutputs serializes the transaction outputs to a byte array prepended with a CompactSizeUint denoting the total number of outputs.
func (*Transaction) SerializeVersion ¶
func (t *Transaction) SerializeVersion() [4]byte
SerializeVersion serializes the transaction version to a little-endian 4-byte array.
func (*Transaction) WitnessHash ¶
func (t *Transaction) WitnessHash() Hash
WitnessHash calculates the transaction's witness hash as the double SHA-256 of the Witness serialization format. The outcome is equivalent to the wtxid field defined by BIP-0141. The outcome of WitnessHash is equivalent to the result of Hash for non-witness transactions, i.e. transaction which does not have witness inputs. For reference, see: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#transaction-id
type TransactionBuilder ¶
type TransactionBuilder struct {
// contains filtered or unexported fields
}
TransactionBuilder is a component that is responsible for the whole transaction creation process. It assembles an unsigned transaction, prepares it for signing, and applies the given signatures in order to produce a full-fledged signed transaction that can be broadcast across the Bitcoin network. The builder IS NOT SAFE for concurrent use.
func NewTransactionBuilder ¶
func NewTransactionBuilder(chain Chain) *TransactionBuilder
NewTransactionBuilder constructs a new TransactionBuilder instance.
func (*TransactionBuilder) AddOutput ¶
func (tb *TransactionBuilder) AddOutput(output *TransactionOutput)
AddOutput adds a new transaction's output.
func (*TransactionBuilder) AddPublicKeyHashInput ¶
func (tb *TransactionBuilder) AddPublicKeyHashInput( utxo *UnspentTransactionOutput, ) error
AddPublicKeyHashInput adds an unsigned input pointing to a UTXO locked using a P2PKH or P2WPKH script.
func (*TransactionBuilder) AddScriptHashInput ¶
func (tb *TransactionBuilder) AddScriptHashInput( utxo *UnspentTransactionOutput, redeemScript Script, ) error
AddScriptHashInput adds an unsigned input pointing to a UTXO locked using a P2SH or P2WSH script. This function also requires the plain-text redeemScript whose hash was used to build the P2SH/P2WSH locking script.
func (*TransactionBuilder) AddSignatures ¶
func (tb *TransactionBuilder) AddSignatures( signatures []*SignatureContainer, ) (*Transaction, error)
AddSignatures adds signature data for transaction inputs and returns a signed Transaction instance. The signatures slice should have the same length as the transaction's input vector. The signature with given index should correspond to the input with the same index. Each signature should also contain a public key that can be used for verification, i.e. this should be the public key that corresponds to the private key used to produce the given signature. Each signature is verified and an error is produced if any signature is not valid for their input.
func (*TransactionBuilder) ComputeSignatureHashes ¶
func (tb *TransactionBuilder) ComputeSignatureHashes() ([]*big.Int, error)
ComputeSignatureHashes computes the signature hashes for all transaction inputs and stores them into the builder's state. Elements of the returned slice are ordered in the same way as the transaction inputs they correspond to. That is, an element at the given index matches the input with the same index.
func (*TransactionBuilder) TotalInputsValue ¶
func (tb *TransactionBuilder) TotalInputsValue() int64
TotalInputsValue returns the total value of transaction inputs.
type TransactionFeeEstimator ¶
type TransactionFeeEstimator struct {
// contains filtered or unexported fields
}
TransactionFeeEstimator is a component allowing to estimate the total fee for the given transaction virtual size.
func NewTransactionFeeEstimator ¶
func NewTransactionFeeEstimator(chain Chain) *TransactionFeeEstimator
func (*TransactionFeeEstimator) EstimateFee ¶
func (tfe *TransactionFeeEstimator) EstimateFee( transactionVirtualSize int64, blocks ...uint32, ) (int64, error)
EstimateFee estimates the total fee for the given transaction virtual size, assuming the transaction is supposed to be confirmed within the next Bitcoin block. The desired confirmation period can be adjusted using the optional `blocks` argument.
type TransactionInput ¶
type TransactionInput struct { // Outpoint is the previous transaction outpoint being spent by this input. Outpoint *TransactionOutpoint // SignatureScript is a script-language script that satisfies the conditions // placed in the outpoint's public key script (see TransactionOutput.PublicKeyScript). // This slice MUST NOT start with the byte-length of the script encoded as // a CompactSizeUint as this is done during transaction serialization. // This field is not set (nil or empty) for SegWit transaction inputs. // That means it is mutually exclusive with the below Witness field. SignatureScript []byte // Witness holds the witness data for the given input. It should be interpreted // as a stack with one or many elements. Individual elements MUST NOT start // with the byte-length of the script encoded as a CompactSizeUint as this // is done during transaction serialization. This field is not set // (nil or empty) for non-SegWit transaction inputs. That means it is // mutually exclusive with the above SignatureScript field. Witness [][]byte // Sequence is the sequence number for this input. Default value // is 0xffffffff. For reference, see: // https://developer.bitcoin.org/devguide/transactions.html#locktime-and-sequence-number Sequence uint32 }
TransactionInput represents a Bitcoin transaction input. For reference, see: https://developer.bitcoin.org/reference/transactions.html#txin-a-transaction-input-non-coinbase
type TransactionMerkleProof ¶
type TransactionMerkleProof struct { // BlockHeight is the height of the block the transaction was confirmed in. BlockHeight uint // MerkleNodes is a list of transaction hashes the current hash is paired // with, recursively, in order to trace up to obtain the merkle root of the // including block, deepest pairing first. Each hash is an unprefixed hex // string. MerkleNodes []string // Position is the 0-based index of the transaction's position in the block. Position uint }
TransactionMerkleProof holds information about the merkle branch to a confirmed transaction.
type TransactionOutpoint ¶
type TransactionOutpoint struct { // TransactionHash is the hash of the transaction holding the output // to spend. TransactionHash Hash // OutputIndex is the zero-based index of the output to spend from the // specified transaction. OutputIndex uint32 }
TransactionOutpoint represents a Bitcoin transaction outpoint. For reference, see: https://developer.bitcoin.org/reference/transactions.html#outpoint-the-specific-part-of-a-specific-output
type TransactionOutput ¶
type TransactionOutput struct { // Value denotes the number of satoshis to spend. Zero is a valid value. Value int64 // PublicKeyScript defines the conditions that must be satisfied to spend // this output. As stated in the Script docstring, this field MUST NOT // start with the byte-length of the script encoded as CompactSizeUint as // this is done during transaction serialization. PublicKeyScript Script }
TransactionOutput represents a Bitcoin transaction output. For reference, see: https://developer.bitcoin.org/reference/transactions.html#txout-a-transaction-output
type TransactionSerializationFormat ¶
type TransactionSerializationFormat int
TransactionSerializationFormat represents the Bitcoin transaction serialization format.
const ( // Standard is the traditional transaction serialization format // [version][inputs][outputs][locktime]. Standard TransactionSerializationFormat = iota // Witness is the witness transaction serialization format // [version][marker][flag][inputs][outputs][witness][locktime] // introduced by BIP-0141. For reference, see: // https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#specification Witness )
type TransactionSizeEstimator ¶
type TransactionSizeEstimator struct {
// contains filtered or unexported fields
}
TransactionSizeEstimator is a component allowing to estimate the size of a Bitcoin transaction of the provided shape, without constructing it.
func NewTransactionSizeEstimator ¶
func NewTransactionSizeEstimator() *TransactionSizeEstimator
func (*TransactionSizeEstimator) AddPublicKeyHashInputs ¶
func (tse *TransactionSizeEstimator) AddPublicKeyHashInputs( count int, isWitness bool, ) *TransactionSizeEstimator
AddPublicKeyHashInputs adds the provided count of P2WPKH (isWitness is true) or P2PKH (isWitness is false) inputs to the estimation. If the estimator already errored out during previous actions, this method does nothing.
func (*TransactionSizeEstimator) AddPublicKeyHashOutputs ¶
func (tse *TransactionSizeEstimator) AddPublicKeyHashOutputs( count int, isWitness bool, ) *TransactionSizeEstimator
AddPublicKeyHashOutputs adds the provided count of P2WPKH (isWitness is true) or P2PKH (isWitness is false) outputs to the estimation. If the estimator already errored out during previous actions, this method does nothing.
func (*TransactionSizeEstimator) AddScriptHashInputs ¶
func (tse *TransactionSizeEstimator) AddScriptHashInputs( count int, redeemScriptLength int, isWitness bool, ) *TransactionSizeEstimator
AddScriptHashInputs adds the provided count of P2WSH (isWitness is true) or P2SH (isWitness is false) inputs to the estimation. The redeemScriptLength argument should be used to pass the byte size of the plain-text redeem script used to lock the inputs. If the estimator already errored out during previous actions, this method does nothing.
func (*TransactionSizeEstimator) AddScriptHashOutputs ¶
func (tse *TransactionSizeEstimator) AddScriptHashOutputs( count int, isWitness bool, ) *TransactionSizeEstimator
AddScriptHashOutputs adds the provided count of P2WSH (isWitness is true) or P2SH (isWitness is false) outputs to the estimation. If the estimator already errored out during previous actions, this method does nothing.
func (*TransactionSizeEstimator) VirtualSize ¶
func (tse *TransactionSizeEstimator) VirtualSize() (int64, error)
VirtualSize returns the virtual size of the transaction whose shape was provided to the estimator. If any errors occurred while building the transaction shape, the first error will be returned.
Worth noting that the estimator can slightly overshoot the virtual size estimation as it always assumes the greatest possible byte size of signatures (72 bytes, see signaturePlaceholder docs) while actual transactions can sometimes have shorter signatures (71 bytes or fewer in very rare cases).
type UnspentTransactionOutput ¶
type UnspentTransactionOutput struct { // Outpoint is the transaction outpoint this UTXO points to. Outpoint *TransactionOutpoint // Value denotes the number of unspent satoshis. Value int64 }
UnspentTransactionOutput represents an unspent output (UTXO) of a Bitcoin transaction.