Documentation ¶
Overview ¶
Package bt provides functions needed to create and manipulate Bitcoin transactions.
Index ¶
- Constants
- Variables
- func DecodeVarInt(b []byte) (result uint64, size int)
- func IsValidTxID(txid []byte) bool
- func LittleEndianBytes(v uint32, l uint32) []byte
- func ReverseBytes(a []byte) []byte
- func VarInt(i uint64) []byte
- func VarIntUpperLimitInc(length uint64) int
- type AutoSigner
- type Fee
- type FeeType
- type FeeUnit
- type Input
- func (i *Input) Bytes(clear bool) []byte
- func (i *Input) MarshalJSON() ([]byte, error)
- func (i *Input) PreviousTxID() []byte
- func (i *Input) PreviousTxIDAdd(txID []byte) error
- func (i *Input) PreviousTxIDAddStr(txID string) error
- func (i *Input) PreviousTxIDStr() string
- func (i *Input) String() string
- func (i *Input) UnmarshalJSON(b []byte) error
- type LocalSigner
- func (is *LocalSigner) PublicKey(ctx context.Context) (publicKey []byte, err error)
- func (is *LocalSigner) Sign(ctx context.Context, unsignedTx *Tx, index uint32, shf sighash.Flag) (publicKey []byte, signature []byte, err error)
- func (is *LocalSigner) SignHash(ctx context.Context, hash []byte) (publicKey, signature []byte, err error)
- type Output
- type Signer
- type Tx
- func (tx *Tx) AddHashPuzzleOutput(secret, publicKeyHash string, satoshis uint64) error
- func (tx *Tx) AddOpReturnOutput(data []byte) error
- func (tx *Tx) AddOpReturnPartsOutput(data [][]byte) error
- func (tx *Tx) AddOutput(output *Output)
- func (tx *Tx) AddP2PKHInputsFromTx(pvsTx *Tx, matchPK []byte) error
- func (tx *Tx) AddP2PKHOutputFromAddress(addr string, satoshis uint64) error
- func (tx *Tx) AddP2PKHOutputFromPubKeyBytes(publicKeyBytes []byte, satoshis uint64) error
- func (tx *Tx) AddP2PKHOutputFromPubKeyHashStr(publicKeyHash string, satoshis uint64) error
- func (tx *Tx) AddP2PKHOutputFromPubKeyStr(publicKey string, satoshis uint64) error
- func (tx *Tx) AddP2PKHOutputFromScript(script *bscript.Script, satoshis uint64) error
- func (tx *Tx) ApplyP2PKHUnlockingScript(index uint32, pubKey []byte, sig []byte, shf sighash.Flag) error
- func (tx *Tx) ApplyUnlockingScript(index uint32, s *bscript.Script) error
- func (tx *Tx) Bytes() []byte
- func (tx *Tx) BytesWithClearedInputs(index int, lockingScript []byte) []byte
- func (tx *Tx) CalcInputPreimage(inputNumber uint32, sigHashFlag sighash.Flag) ([]byte, error)
- func (tx *Tx) CalcInputSignatureHash(inputNumber uint32, sigHashFlag sighash.Flag) ([]byte, 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) ChangeToExistingOutput(index uint, f []*Fee) error
- func (tx *Tx) From(prevTxID string, vout uint32, prevTxLockingScript string, satoshis uint64) error
- func (tx *Tx) HasDataOutputs() bool
- func (tx *Tx) InputCount() int
- func (tx *Tx) InputIdx(i int) *Input
- func (tx *Tx) IsCoinbase() bool
- func (tx *Tx) MarshalJSON() ([]byte, error)
- func (tx *Tx) OutputCount() int
- func (tx *Tx) OutputIdx(i int) *Output
- func (tx *Tx) PayTo(script *bscript.Script, satoshis uint64) error
- func (tx *Tx) PayToAddress(addr string, satoshis uint64) error
- func (tx *Tx) Sign(ctx context.Context, s Signer, index uint32, shf sighash.Flag) error
- func (tx *Tx) SignAuto(ctx context.Context, s AutoSigner) (inputsSigned []int, err error)
- func (tx *Tx) SignHash(ctx context.Context, s Signer, index uint32, shf sighash.Flag) error
- func (tx *Tx) String() string
- func (tx *Tx) TotalInputSatoshis() (total uint64)
- func (tx *Tx) TotalOutputSatoshis() (total uint64)
- func (tx *Tx) TxID() string
- func (tx *Tx) TxIDBytes() []byte
- func (tx *Tx) UnmarshalJSON(b []byte) error
Constants ¶
const DefaultSequenceNumber uint32 = 0xFFFFFFFF
DefaultSequenceNumber is the default starting sequence number
Variables ¶
var (
ErrInvalidTxID = errors.New("invalid TxID")
)
Sentinel errors for transactions.
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 IsValidTxID ¶
IsValidTxID will check that the txid bytes are valid.
A txid should be of 32 bytes length.
func LittleEndianBytes ¶
LittleEndianBytes 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 AutoSigner ¶
AutoSigner interface to allow custom implementations of different signing mechanisms. Implement the Sign function as shown in InternalSigner, for example. Sign generates and returns an ECDSA signature for the provided hash digest using the private key as well as the public key corresponding to the private key used. The produced signature is deterministic (same message and same key yield the same signature) and canonical in accordance with RFC6979 and BIP0062.
To automatically sign, the PublicKey() method must also be implemented in order to use the public key to check which Inputs can be signed for before signing.
type Fee ¶
type Fee struct { FeeType FeeType `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 ExtractDataFee ¶
ExtractDataFee returns the data fee in the fees array supplied.
func ExtractStandardFee ¶
ExtractStandardFee returns the standard fee in the fees array supplied.
type FeeType ¶
type FeeType string
FeeType is used to specify which type of fee is used depending on the type of tx data (eg: standard bytes or data bytes).
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 { PreviousTxSatoshis uint64 PreviousTxScript *bscript.Script UnlockingScript *bscript.Script PreviousTxOutIndex uint32 SequenceNumber uint32 // contains filtered or unexported fields }
Input is a representation of a transaction input
DO NOT CHANGE ORDER - Optimised for memory via maligned
func NewInputFromBytes ¶
NewInputFromBytes returns a transaction input from the bytes provided.
func (*Input) MarshalJSON ¶
MarshalJSON will convert an input to json, expanding upon the input struct to add additional fields.
func (*Input) PreviousTxID ¶
PreviousTxID will return the PreviousTxID if set.
func (*Input) PreviousTxIDAdd ¶
PreviousTxIDAdd will add the supplied txID bytes to the Input, if it isn't a valid transaction id an ErrInvalidTxID error will be returned.
func (*Input) PreviousTxIDAddStr ¶
PreviousTxIDAddStr will validate and add the supplied txID string to the Input, if it isn't a valid transaction id an ErrInvalidTxID error will be returned.
func (*Input) PreviousTxIDStr ¶
PreviousTxIDStr returns the Previous TxID as a hex string.
func (*Input) String ¶
String implements the Stringer interface and returns a string representation of a transaction input.
func (*Input) UnmarshalJSON ¶
UnmarshalJSON will convert a JSON input to an input.
type LocalSigner ¶
type LocalSigner struct {
PrivateKey *bec.PrivateKey
}
LocalSigner implements the Signer interface. It is used to sign Tx Inputs locally using a bkec PrivateKey.
func (*LocalSigner) PublicKey ¶
func (is *LocalSigner) PublicKey(ctx context.Context) (publicKey []byte, err error)
PublicKey returns the public key which will be used to sign.
type Output ¶
type Output struct { Satoshis uint64 LockingScript *bscript.Script // contains filtered or unexported fields }
Output is a representation of a transaction output
func NewOutputFromBytes ¶
NewOutputFromBytes returns a transaction Output from the bytes provided
func (*Output) BytesForSigHash ¶
BytesForSigHash returns the proper serialisation of an output to be hashed and signed (sighash).
func (*Output) LockingScriptHexString ¶
LockingScriptHexString returns the locking script of an output encoded as a hex string.
func (*Output) MarshalJSON ¶
MarshalJSON will serialise an output to json.
func (*Output) UnmarshalJSON ¶
UnmarshalJSON will convert a json serialised output to a bt Output.
type Signer ¶
type Signer interface { Sign(ctx context.Context, unsignedTx *Tx, index uint32, shf sighash.Flag) (publicKey, signature []byte, err error) SignHash(ctx context.Context, hash []byte) (publicKey, signature []byte, err error) }
Signer interface to allow custom implementations of different signing mechanisms. Implement the Sign function as shown in InternalSigner, for example. Sign generates and returns an ECDSA signature for the provided hash digest using the private key as well as the public key corresponding to the private key used. The produced signature is deterministic (same message and same key yield the same signature) and canonical in accordance with RFC6979 and BIP0062.
type Tx ¶
Tx wraps a bitcoin transaction
DO NOT CHANGE ORDER - Optimised 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 NewTxFromStream ¶
NewTxFromStream takes an array of bytes and constructs 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) AddHashPuzzleOutput ¶
AddHashPuzzleOutput makes an output to a hash puzzle + PKH with a value.
func (*Tx) AddOpReturnOutput ¶
AddOpReturnOutput creates a new Output with OP_FALSE OP_RETURN and then the data passed in encoded as hex.
func (*Tx) AddOpReturnPartsOutput ¶
AddOpReturnPartsOutput creates a new Output with OP_FALSE OP_RETURN and then uses OP_PUSHDATA format to encode the multiple byte arrays passed in.
func (*Tx) AddP2PKHInputsFromTx ¶
AddP2PKHInputsFromTx will add all Outputs of given previous transaction that match a specific public key to your transaction.
func (*Tx) AddP2PKHOutputFromAddress ¶
AddP2PKHOutputFromAddress makes an output to a PKH with a value.
func (*Tx) AddP2PKHOutputFromPubKeyBytes ¶
AddP2PKHOutputFromPubKeyBytes makes an output to a PKH with a value.
func (*Tx) AddP2PKHOutputFromPubKeyHashStr ¶
AddP2PKHOutputFromPubKeyHashStr makes an output to a PKH with a value.
func (*Tx) AddP2PKHOutputFromPubKeyStr ¶
AddP2PKHOutputFromPubKeyStr makes an output to a PKH with a value.
func (*Tx) AddP2PKHOutputFromScript ¶
AddP2PKHOutputFromScript makes an output to a P2PKH script paid to the provided locking script with a value.
func (*Tx) ApplyP2PKHUnlockingScript ¶
func (tx *Tx) ApplyP2PKHUnlockingScript(index uint32, pubKey []byte, sig []byte, shf sighash.Flag) error
ApplyP2PKHUnlockingScript applies a script to the transaction at a specific index in unlocking script field.
func (*Tx) ApplyUnlockingScript ¶
ApplyUnlockingScript applies a script to the transaction at a specific index in unlocking script field.
func (*Tx) Bytes ¶
Bytes encodes the transaction into a byte array. See https://chainquery.com/bitcoin-cli/decoderawtransaction
func (*Tx) BytesWithClearedInputs ¶
BytesWithClearedInputs encodes the transaction into a byte array but clears its Inputs first. This is used when signing transactions.
func (*Tx) CalcInputPreimage ¶
CalcInputPreimage serialises the transaction based on the input index and the SIGHASH flag and returns the preimage before double hashing (SHA256d).
func (*Tx) CalcInputSignatureHash ¶
CalcInputSignatureHash serialised the transaction and returns the hash digest to be signed. BitCoin (SV) uses a different signature hashing algorithm after the UAHF fork for replay protection.
func (*Tx) CalculateFee ¶
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) ChangeToExistingOutput ¶
ChangeToExistingOutput 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) From ¶
From adds a new input to the transaction from the specified UTXO fields, using the default finalised sequence number (0xFFFFFFFF). If you want a different nSeq, change it manually afterwards.
func (*Tx) HasDataOutputs ¶
HasDataOutputs returns true if the transaction has at least one data (OP_RETURN) output in it.
func (*Tx) InputCount ¶
InputCount returns the number of transaction Inputs.
func (*Tx) InputIdx ¶
InputIdx will return the input at the specified index.
This will consume an overflow error and simply return nil if the input isn't found at the index.
func (*Tx) IsCoinbase ¶
IsCoinbase determines if this transaction is a coinbase by checking if the tx input is a standard coinbase input.
func (*Tx) MarshalJSON ¶
MarshalJSON will serialise a transaction to json.
func (*Tx) OutputCount ¶
OutputCount returns the number of transaction Inputs.
func (*Tx) OutputIdx ¶
OutputIdx will return the output at the specified index.
This will consume an overflow error and simply return nil if the output isn't found at the index.
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) PayToAddress ¶
PayToAddress 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 (hardware wallet).
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) SignHash ¶
SignHash 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 (hardware wallet).
SignHash will only take the final signature hash to be signed so will need to trust that it is getting the right hash to sign as there no way to verify that it is signing the right hash.
func (*Tx) TotalInputSatoshis ¶
TotalInputSatoshis returns the total Satoshis inputted to the transaction.
func (*Tx) TotalOutputSatoshis ¶
TotalOutputSatoshis returns the total Satoshis outputted from the transaction.
func (*Tx) TxID ¶
TxID returns the transaction ID of the transaction (which is also the transaction hash).
func (*Tx) TxIDBytes ¶
TxIDBytes returns the transaction ID of the transaction as bytes (which is also the transaction hash).
func (*Tx) UnmarshalJSON ¶
UnmarshalJSON will unmarshall a transaction that has been marshalled with this library.