Documentation ¶
Overview ¶
Package bt provides functions needed to create and manipulate Bitcoin transactions.
Index ¶
- Constants
- func DecodeVarInt(b []byte) (uint64, int)
- func DecodeVarIntFromReader(r io.Reader) (uint64, int, error)
- func GetLittleEndianBytes(v uint32, l uint32) []byte
- func ReverseBytes(a []byte) []byte
- func VarInt(i uint64) []byte
- func VarIntUpperLimitInc(length uint64) int
- type Fee
- type FeeUnit
- type Input
- type InternalSigner
- type Output
- func NewHashPuzzleOutput(secret, publicKeyHash string, satoshis uint64) (*Output, error)
- func NewOpReturnOutput(data []byte) (*Output, error)
- func NewOpReturnPartsOutput(data [][]byte) (*Output, error)
- func NewOutputFromBytes(b []byte) (*Output, int, error)
- func NewOutputFromReader(r io.Reader) (*Output, error)
- func NewP2PKHOutputFromAddress(addr string, satoshis uint64) (*Output, error)
- func NewP2PKHOutputFromPubKeyBytes(publicKeyBytes []byte, satoshis uint64) (*Output, error)
- func NewP2PKHOutputFromPubKeyHashStr(publicKeyHash string, satoshis uint64) (*Output, error)
- func NewP2PKHOutputFromPubKeyStr(publicKey string, satoshis uint64) (*Output, error)
- type Signer
- type Tx
- func (tx *Tx) AddInput(input *Input)
- func (tx *Tx) AddInputFromTx(pvsTx *Tx, matchPK []byte) error
- func (tx *Tx) AddOutput(output *Output)
- func (tx *Tx) ApplyUnlockingScript(index uint32, s *bscript.Script) error
- func (tx *Tx) CalculateFee(f []*Fee) (uint64, error)
- func (tx *Tx) Change(s *bscript.Script, f []*Fee) error
- func (tx *Tx) ChangeToAddress(addr string, f []*Fee) error
- func (tx *Tx) ChangeToOutput(index uint, f []*Fee) error
- func (tx *Tx) From(txID string, vout uint32, prevTxLockingScript string, satoshis uint64) error
- func (tx *Tx) GetInputPreimage(inputNumber uint32, sigHashFlag sighash.Flag) ([]byte, error)
- func (tx *Tx) GetInputSignatureHash(inputNumber uint32, sigHashFlag sighash.Flag) ([]byte, error)
- func (tx *Tx) GetInputs() []*Input
- func (tx *Tx) GetOutputs() []*Output
- func (tx *Tx) GetTotalInputSatoshis() (total uint64)
- func (tx *Tx) GetTotalOutputSatoshis() (total uint64)
- func (tx *Tx) GetTxID() string
- func (tx *Tx) GetTxIDAsBytes() []byte
- func (tx *Tx) HasDataOutputs() bool
- func (tx *Tx) HasOutputsWithAddress(addr string) ([]int, bool, error)
- func (tx *Tx) HasOutputsWithScript(s *bscript.Script) ([]int, bool)
- func (tx *Tx) InputCount() int
- func (tx *Tx) IsCoinbase() bool
- func (tx *Tx) OutputCount() int
- func (tx *Tx) PayTo(addr string, satoshis uint64) error
- func (tx *Tx) Sign(index uint32, s Signer) error
- func (tx *Tx) SignAuto(s Signer) (inputsSigned []int, err error)
- func (tx *Tx) ToBytes() []byte
- func (tx *Tx) ToBytesWithClearedInputs(index int, lockingScript []byte) []byte
- func (tx *Tx) ToString() string
Constants ¶
const ( // FeeTypeStandard is the fee type for standard tx parts FeeTypeStandard = "standard" // FeeTypeData is the fee type for data tx parts FeeTypeData = "data" )
const DefaultSequenceNumber uint32 = 0xFFFFFFFF
DefaultSequenceNumber is the default starting sequence number
const (
// DustLimit is the current minimum output satoshis accepted by the network.
DustLimit = 136
)
Variables ¶
This section is empty.
Functions ¶
func DecodeVarInt ¶
DecodeVarInt takes a byte array in VarInt format and returns the decoded unsigned integer value of the length and it's size in bytes. See http://learnmeabitcoin.com/glossary/varint
func DecodeVarIntFromReader ¶ added in v1.0.1
DecodeVarIntFromReader takes an io.Reader and returns the decoded unsigned integer value of the length. See http://learnmeabitcoin.com/glossary/varint
func GetLittleEndianBytes ¶
GetLittleEndianBytes returns a byte array in little endian from an unsigned integer of 32 bytes.
func ReverseBytes ¶
ReverseBytes reverses the bytes (little endian/big endian). This is used when computing merkle trees in Bitcoin, for example.
func VarInt ¶
VarInt takes an unsigned integer and returns a byte array in VarInt format. See http://learnmeabitcoin.com/glossary/varint
func VarIntUpperLimitInc ¶
VarIntUpperLimitInc returns true if a number is at the upper limit of a VarInt and will result in a VarInt length change if incremented. The value returned will indicate how many bytes will be increase if the length in incremented. -1 will be returned when the upper limit of VarInt is reached.
Types ¶
type Fee ¶
type Fee struct { FeeType string `json:"feeType"` // standard || data MiningFee FeeUnit `json:"miningFee"` RelayFee FeeUnit `json:"relayFee"` // Fee for retaining Tx in secondary mempool }
Fee displays the MiningFee as well as the RelayFee for a specific FeeType, for example 'standard' or 'data' see https://github.com/bitcoin-sv-specs/brfc-misc/tree/master/feespec
func DefaultDataFee ¶
func DefaultDataFee() *Fee
DefaultDataFee returns the default data fees offered by most miners.
func DefaultFees ¶
func DefaultFees() (f []*Fee)
DefaultFees returns an array of the default standard and data fees offered by most miners.
func DefaultStandardFee ¶
func DefaultStandardFee() *Fee
DefaultStandardFee returns the default standard fees offered by most miners.
func GetDataFee ¶
GetDataFee returns the data fee in the fees array supplied.
func GetStandardFee ¶
GetStandardFee returns the standard fee in the fees array supplied.
type FeeUnit ¶
type FeeUnit struct { Satoshis int `json:"satoshis"` // Fee in satoshis of the amount of Bytes Bytes int `json:"bytes"` // Number of bytes that the Fee covers }
FeeUnit displays the amount of Satoshis needed for a specific amount of Bytes in a transaction see https://github.com/bitcoin-sv-specs/brfc-misc/tree/master/feespec
type Input ¶
type Input struct { PreviousTxIDBytes []byte PreviousTxID string PreviousTxSatoshis uint64 PreviousTxScript *bscript.Script UnlockingScript *bscript.Script PreviousTxOutIndex uint32 SequenceNumber uint32 }
Input is a representation of a transaction input
DO NOT CHANGE ORDER - Optimized for memory via maligned
func NewInput ¶
func NewInput() *Input
NewInput creates a new empty Input object with a finalized sequence number.
func NewInputFromBytes ¶
NewInputFromBytes returns a transaction input from the bytes provided.
func NewInputFromReader ¶ added in v1.0.1
NewInputFromReader returns a transaction input from the io.Reader provided.
type InternalSigner ¶
type InternalSigner struct { PrivateKey *bsvec.PrivateKey SigHashFlag sighash.Flag }
InternalSigner implements the Signer interface. It is used to sign a Tx locally given a PrivateKey and SIGHASH type.
func (*InternalSigner) Sign ¶
func (is *InternalSigner) Sign(index uint32, unsignedTx *Tx) (signedTx *Tx, err error)
Sign a transaction at a given input index using the PrivateKey passed in through the InternalSigner struct.
func (*InternalSigner) SignAuto ¶
func (is *InternalSigner) SignAuto(unsignedTx *Tx) (signedTx *Tx, inputsSigned []int, err error)
SignAuto goes through each input of the transaction and automatically signs the P2PKH inputs that it is able to sign using the specific PrivateKey passed in through the InternalSigner struct.
type Output ¶
Output is a representation of a transaction output
func NewHashPuzzleOutput ¶
NewHashPuzzleOutput makes an output to a hash puzzle + PKH with a value.
func NewOpReturnOutput ¶
NewOpReturnOutput creates a new Output with OP_FALSE OP_RETURN and then the data passed in encoded as hex.
func NewOpReturnPartsOutput ¶
NewOpReturnPartsOutput creates a new Output with OP_FALSE OP_RETURN and then uses OP_PUSHDATA format to encode the multiple byte arrays passed in.
func NewOutputFromBytes ¶
NewOutputFromBytes returns a transaction Output from the bytes provided
func NewOutputFromReader ¶ added in v1.0.1
NewOutputFromReader returns a transaction Output from the io.Reader provided
func NewP2PKHOutputFromAddress ¶
NewP2PKHOutputFromAddress makes an output to a PKH with a value.
func NewP2PKHOutputFromPubKeyBytes ¶
NewP2PKHOutputFromPubKeyBytes makes an output to a PKH with a value.
func NewP2PKHOutputFromPubKeyHashStr ¶
NewP2PKHOutputFromPubKeyHashStr makes an output to a PKH with a value.
func NewP2PKHOutputFromPubKeyStr ¶
NewP2PKHOutputFromPubKeyStr makes an output to a PKH with a value.
func (*Output) GetBytesForSigHash ¶
GetBytesForSigHash returns the proper serialization of an output to be hashed and signed (sighash).
func (*Output) GetLockingScriptHexString ¶
GetLockingScriptHexString returns the locking script of an output encoded as a hex string.
type Signer ¶
type Signer interface { Sign(index uint32, unsignedTx *Tx) (signedTx *Tx, err error) SignAuto(unsignedTx *Tx) (signedTx *Tx, inputsSigned []int, err error) }
Signer interface to allow custom implementations of different signing mechanisms. Implement the Sign function as shown in InternalSigner, for example. Sign function takes an unsigned Tx and returns a signed Tx.
type Tx ¶
type Tx struct { // TODO: make variables private? Inputs []*Input Outputs []*Output Version uint32 LockTime uint32 }
Tx wraps a bitcoin transaction
DO NOT CHANGE ORDER - Optimized memory via malign
func NewTxFromBytes ¶
NewTxFromBytes takes an array of bytes, constructs a Tx and returns it. This function assumes that the byte slice contains exactly 1 transaction.
func NewTxFromReader ¶ added in v1.0.1
NewTxFromReader creates a transaction from an io.Reader
func NewTxFromStream ¶
NewTxFromStream takes an array of bytes and contructs a Tx from it, returning the Tx and the bytes used. Despite the name, this is not actually reading a stream in the true sense: it is a byte slice that contains many transactions one after another.
func NewTxFromString ¶
NewTxFromString takes a toBytesHelper string representation of a bitcoin transaction and returns a Tx object.
func (*Tx) AddInputFromTx ¶ added in v0.0.8
AddInputFromTx take all outputs from previous transaction that match a specific public key, add it as input to this new transaction.
func (*Tx) ApplyUnlockingScript ¶
ApplyUnlockingScript applies a script to the transaction at a specific index in unlocking script field.
func (*Tx) CalculateFee ¶ added in v0.0.11
CalculateFee will return the amount of fees the current transaction will require.
func (*Tx) Change ¶
Change calculates the amount of fees needed to cover the transaction and adds the left over change in a new output using the script provided.
func (*Tx) ChangeToAddress ¶
ChangeToAddress calculates the amount of fees needed to cover the transaction and adds the left over change in a new P2PKH output using the address provided.
func (*Tx) ChangeToOutput ¶ added in v0.0.11
ChangeToOutput will calculate fees and add them to an output at the index specified (0 based). If an invalid index is supplied and error is returned.
func (*Tx) GetInputPreimage ¶ added in v0.0.7
GetInputPreimage serializes the transaction based on the input index and the SIGHASH flag see https://github.com/bitcoin-sv/bitcoin-sv/blob/master/doc/abc/replay-protected-sighash.md#digest-algorithm
func (*Tx) GetInputSignatureHash ¶
GetInputSignatureHash gets the preimage of the specified input and hashes it.
func (*Tx) GetOutputs ¶
GetOutputs returns an array of all outputs in the transaction.
func (*Tx) GetTotalInputSatoshis ¶
GetTotalInputSatoshis returns the total Satoshis inputted to the transaction.
func (*Tx) GetTotalOutputSatoshis ¶
GetTotalOutputSatoshis returns the total Satoshis outputted from the transaction.
func (*Tx) GetTxID ¶
GetTxID returns the transaction ID of the transaction (which is also the transaction hash).
func (*Tx) GetTxIDAsBytes ¶ added in v0.0.8
GetTxIDAsBytes returns the transaction ID of the transaction as bytes (which is also the transaction hash).
func (*Tx) HasDataOutputs ¶
HasDataOutputs returns true if the transaction has at least one data (OP_RETURN) output in it.
func (*Tx) HasOutputsWithAddress ¶ added in v1.0.0
HasOutputsWithAddress will return the index of any outputs found matching the address 'addr'.
bool will be false if none have been found. err will not be nil if the addr is not a valid P2PKH address.
func (*Tx) HasOutputsWithScript ¶ added in v1.0.0
HasOutputsWithScript will return the index of any outputs found matching the locking script 's'.
bool will be false if none have been found.
func (*Tx) InputCount ¶
InputCount returns the number of transaction inputs.
func (*Tx) IsCoinbase ¶
IsCoinbase determines if this transaction is a coinbase by checking if the tx input is a standard coinbase input.
func (*Tx) OutputCount ¶
OutputCount returns the number of transaction inputs.
func (*Tx) PayTo ¶
PayTo creates a new P2PKH output from a BitCoin address (base58) and the satoshis amount and adds that to the transaction.
func (*Tx) Sign ¶
Sign is used to sign the transaction at a specific input index. It takes a Signed interface as a parameter so that different signing implementations can be used to sign the transaction - for example internal/local or external signing.
func (*Tx) SignAuto ¶
SignAuto is used to automatically check which P2PKH inputs are able to be signed (match the public key) and then sign them. It takes a Signed interface as a parameter so that different signing implementations can be used to sign the transaction - for example internal/local or external signing.
func (*Tx) ToBytes ¶
ToBytes encodes the transaction into a byte array. See https://chainquery.com/bitcoin-cli/decoderawtransaction
func (*Tx) ToBytesWithClearedInputs ¶
ToBytesWithClearedInputs encodes the transaction into a byte array but clears its inputs first. This is used when signing transactions.