bitcoin

package
v0.0.0-...-495235e Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OutboundBytesMin = uint64(239)  // 239vB == EstimateSegWitTxSize(2, 2, toP2WPKH)
	OutboundBytesMax = uint64(1543) // 1543v == EstimateSegWitTxSize(21, 2, toP2TR)
	OutboundBytesAvg = uint64(245)  // 245vB is a suggested gas limit for zetacore

)
View Source
const (
	// LengthScriptP2TR is the lenth of P2TR script [OP_1 0x20 <32-byte-hash>]
	LengthScriptP2TR = 34

	// LengthScriptP2WSH is the length of P2WSH script [OP_0 0x20 <32-byte-hash>]
	LengthScriptP2WSH = 34

	// LengthScriptP2WPKH is the length of P2WPKH script [OP_0 0x14 <20-byte-hash>]
	LengthScriptP2WPKH = 22

	// LengthScriptP2SH is the length of P2SH script [OP_HASH160 0x14 <20-byte-hash> OP_EQUAL]
	LengthScriptP2SH = 23

	// LengthScriptP2PKH is the length of P2PKH script [OP_DUP OP_HASH160 0x14 <20-byte-hash> OP_EQUALVERIFY OP_CHECKSIG]
	LengthScriptP2PKH = 25
)

Variables

View Source
var (
	// BtcOutboundBytesDepositor is the outbound size incurred by the depositor: 68vB
	BtcOutboundBytesDepositor = OutboundSizeDepositor()

	// BtcOutboundBytesWithdrawer is the outbound size incurred by the withdrawer: 177vB
	BtcOutboundBytesWithdrawer = OutboundSizeWithdrawer()

	// DefaultDepositorFee is the default depositor fee is 0.00001360 BTC (20 * 68vB / 100000000)
	// default depositor fee calculation is based on a fixed fee rate of 20 sat/byte just for simplicity.
	DefaultDepositorFee = DepositorFee(defaultDepositorFeeRate)
)
View Source
var ErrBitcoinNotEnabled = errors.New("bitcoin is not enabled")

ErrBitcoinNotEnabled is the error returned when bitcoin is not enabled

Functions

func CalcBlockAvgFeeRate

func CalcBlockAvgFeeRate(blockVb *btcjson.GetBlockVerboseTxResult, netParams *chaincfg.Params) (int64, error)

CalcBlockAvgFeeRate calculates the average gas rate (in sat/vByte) for a given block

func CalcDepositorFee

func CalcDepositorFee(
	rpcClient interfaces.BTCRPCClient,
	rawResult *btcjson.TxRawResult,
	netParams *chaincfg.Params,
) (float64, error)

CalcDepositorFee calculates the depositor fee for a given tx result

func DecodeOpReturnMemo

func DecodeOpReturnMemo(scriptHex string) ([]byte, bool, error)

DecodeOpReturnMemo decodes memo from OP_RETURN script returns (memo, found, error)

func DecodeScript

func DecodeScript(script []byte) ([]byte, bool, error)

DecodeScript decodes memo wrapped in an inscription like script in witness returns (memo, found, error)

Note: the format of the script is following that of "inscription" defined in ordinal theory. However, to separate from inscription (as this use case is not an NFT), simplifications are made. The bitcoin envelope script is as follows: OP_DATA_32 <32 byte of public key> OP_CHECKSIG OP_FALSE OP_IF

OP_PUSH 0x...
OP_PUSH 0x...

OP_ENDIF There are no content-type or any other attributes, it's just raw bytes.

func DecodeScriptP2PKH

func DecodeScriptP2PKH(scriptHex string, net *chaincfg.Params) (string, error)

DecodeScriptP2PKH decodes address from P2PKH script

func DecodeScriptP2SH

func DecodeScriptP2SH(scriptHex string, net *chaincfg.Params) (string, error)

DecodeScriptP2SH decodes address from P2SH script

func DecodeScriptP2TR

func DecodeScriptP2TR(scriptHex string, net *chaincfg.Params) (string, error)

DecodeScriptP2TR decodes address from P2TR script

func DecodeScriptP2WPKH

func DecodeScriptP2WPKH(scriptHex string, net *chaincfg.Params) (string, error)

DecodeScriptP2WPKH decodes address from P2WPKH script

func DecodeScriptP2WSH

func DecodeScriptP2WSH(scriptHex string, net *chaincfg.Params) (string, error)

DecodeScriptP2WSH decodes address from P2WSH script

func DecodeSenderFromScript

func DecodeSenderFromScript(pkScript []byte, net *chaincfg.Params) (string, error)

DecodeSenderFromScript decodes sender from a given script

func DecodeTSSVout

func DecodeTSSVout(vout btcjson.Vout, receiverExpected string, chain chains.Chain) (string, int64, error)

DecodeTSSVout decodes receiver and amount from a given TSS vout

func DepositorFee

func DepositorFee(satPerByte int64) float64

DepositorFee calculates the depositor fee in BTC for a given sat/byte fee rate Note: the depositor fee is charged in order to cover the cost of spending the deposited UTXO in the future

func EncodeAddress

func EncodeAddress(hash160 []byte, netID byte) string

EncodeAddress returns a human-readable payment address given a ripemd160 hash and netID which encodes the bitcoin network and address type. It is used in both pay-to-pubkey-hash (P2PKH) and pay-to-script-hash (P2SH) address encoding. Note: this function is a copy of the function in btcutil/address.go

func EstimateOutboundSize

func EstimateOutboundSize(numInputs uint64, payees []btcutil.Address) (uint64, error)

EstimateOutboundSize estimates the size of an outbound in vBytes

func FeeRateToSatPerByte

func FeeRateToSatPerByte(rate float64) *big.Int

FeeRateToSatPerByte converts a fee rate in BTC/KB to sat/byte.

func GetOutputSizeByAddress

func GetOutputSizeByAddress(to btcutil.Address) (uint64, error)

GetOutputSizeByAddress returns the size of a tx output in bytes by the given address

func GetRecentFeeRate

func GetRecentFeeRate(rpcClient interfaces.BTCRPCClient, netParams *chaincfg.Params) (uint64, error)

GetRecentFeeRate gets the highest fee rate from recent blocks Note: this method should be used for testnet ONLY

func GetSatoshis

func GetSatoshis(btc float64) (int64, error)

GetSatoshis converts a bitcoin amount to satoshis

func IsPkScriptP2PKH

func IsPkScriptP2PKH(script []byte) bool

IsPkScriptP2PKH checks if the given script is a P2PKH script

func IsPkScriptP2SH

func IsPkScriptP2SH(script []byte) bool

IsPkScriptP2SH checks if the given script is a P2SH script

func IsPkScriptP2TR

func IsPkScriptP2TR(script []byte) bool

IsPkScriptP2TR checks if the given script is a P2TR script

func IsPkScriptP2WPKH

func IsPkScriptP2WPKH(script []byte) bool

IsPkScriptP2WPKH checks if the given script is a P2WPKH script

func IsPkScriptP2WSH

func IsPkScriptP2WSH(script []byte) bool

IsPkScriptP2WSH checks if the given script is a P2WSH script

func OutboundSizeDepositor

func OutboundSizeDepositor() uint64

OutboundSizeDepositor returns outbound size (68vB) incurred by the depositor

func OutboundSizeWithdrawer

func OutboundSizeWithdrawer() uint64

OutboundSizeWithdrawer returns outbound size (177vB) incurred by the withdrawer (1 input, 3 outputs)

func PrettyPrintStruct

func PrettyPrintStruct(val interface{}) (string, error)

PrettyPrintStruct returns a pretty-printed string representation of a struct

func WiredTxSize

func WiredTxSize(numInputs uint64, numOutputs uint64) uint64

WiredTxSize calculates the wired tx size in bytes

Types

type DepositorFeeCalculator

type DepositorFeeCalculator func(interfaces.BTCRPCClient, *btcjson.TxRawResult, *chaincfg.Params) (float64, error)

DepositorFeeCalculator is a function type to calculate the Bitcoin depositor fee

Directories

Path Synopsis
Package observer implements the Bitcoin chain observer
Package observer implements the Bitcoin chain observer
Package signer implements the ChainSigner interface for BTC
Package signer implements the ChainSigner interface for BTC

Jump to

Keyboard shortcuts

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