btc

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2021 License: BlueOak-1.0.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// SecretHashSize is the byte-length of the hash of the secret key used in an
	// atomic swap.
	SecretHashSize = 32

	// SecretKeySize is the byte-length of the secret key used in an atomic swap.
	SecretKeySize = 32

	// ContractHashSize is the size of the script-hash for an atomic swap
	// contract.
	ContractHashSize = 20

	// PubKeyLength is the length of a serialized compressed public key.
	PubKeyLength = 33

	// 4 bytes version + 4 bytes locktime + 2 bytes of varints for the number of
	// transaction inputs and outputs
	MinimumBlockOverHead = 10

	// SwapContractSize is the worst case scenario size for a swap contract,
	// which is the pk-script of the non-change output of an initialization
	// transaction as used in execution of an atomic swap.
	// See ExtractSwapDetails for a breakdown of the bytes.
	SwapContractSize = 97

	// DERSigLength is the maximum length of a DER encoded signature with a
	// sighash type byte.
	DERSigLength = 73

	// RedeemSwapSigScriptSize is the worst case (largest) serialize size
	// of a transaction signature script that redeems atomic swap output contract.
	// It is calculated as:
	//
	//   - OP_DATA_73
	//   - 72 bytes DER signature + 1 byte sighash
	//   - OP_DATA_33
	//   - 33 bytes serialized compressed pubkey
	//   - OP_DATA_32
	//   - 32 bytes secret key
	//   - OP_1
	//   - varint 97
	//   - 97 bytes redeem script
	RedeemSwapSigScriptSize = 1 + DERSigLength + 1 + 33 + 1 + 32 + 1 + 2 + 97

	// RefundSigScriptSize is the worst case (largest) serialize size
	// of a transaction input script that refunds a compressed P2PKH output.
	// It is calculated as:
	//
	//   - OP_DATA_73
	//   - 72 bytes DER signature + 1 byte sighash
	//   - OP_DATA_33
	//   - 33 bytes serialized compressed pubkey
	//   - OP_0
	//   - varint 97 => OP_PUSHDATA1(0x4c) + 0x61
	//   - 97 bytes contract
	RefundSigScriptSize = 1 + DERSigLength + 1 + 33 + 1 + 2 + 97

	// Overhead for a wire.TxIn. See wire.TxIn.SerializeSize.
	// hash 32 bytes + index 4 bytes + sequence 4 bytes.
	TxInOverhead = 32 + 4 + 4 // 40

	// TxOutOverhead is the overhead associated with a transaction output.
	// 8 bytes value + at least 1 byte varint script size
	TxOutOverhead = 8 + 1

	RedeemP2PKSigScriptSize = 1 + DERSigLength

	// RedeemP2PKHSigScriptSize is the worst case (largest) serialize size
	// of a transaction input script that redeems a compressed P2PKH output.
	// It is calculated as:
	//
	//   - OP_DATA_73
	//   - 72 bytes DER signature + 1 byte sighash
	//   - OP_DATA_33
	//   - 33 bytes serialized compressed pubkey
	RedeemP2PKHSigScriptSize = 1 + DERSigLength + 1 + 33 // 108

	// P2PKHPkScriptSize is the size of a transaction output script that
	// pays to a compressed pubkey hash.  It is calculated as:
	//
	//   - OP_DUP
	//   - OP_HASH160
	//   - OP_DATA_20
	//   - 20 bytes pubkey hash
	//   - OP_EQUALVERIFY
	//   - OP_CHECKSIG
	P2PKHPkScriptSize = 1 + 1 + 1 + 20 + 1 + 1 // 25

	// P2PKHOutputSize is the size of the serialized P2PKH output.
	P2PKHOutputSize = TxOutOverhead + P2PKHPkScriptSize // 9 + 25 = 34

	// P2SHPkScriptSize is the size of a transaction output script that
	// pays to a redeem script.  It is calculated as:
	//
	//   - OP_HASH160
	//   - OP_DATA_20
	//   - 20 bytes redeem script hash
	//   - OP_EQUAL
	P2SHPkScriptSize = 1 + 1 + 20 + 1

	// P2SHOutputSize is the size of the serialized P2SH output.
	P2SHOutputSize = TxOutOverhead + P2SHPkScriptSize // 9 + 23 = 32

	// P2WSHPkScriptSize is the size of a segwit transaction output script that
	// pays to a redeem script.  It is calculated as:
	//
	//   - OP_0
	//   - OP_DATA_32
	//   - 32 bytes redeem script hash
	P2WSHPkScriptSize = 1 + 1 + 32

	// P2WSHOutputSize is the size of the serialized P2WSH output.
	P2WSHOutputSize = TxOutOverhead + P2WSHPkScriptSize // 9 + 34 = 43

	// RedeemP2PKHInputSize is the worst case (largest) serialize size of a
	// transaction input redeeming a compressed P2PKH output.  It is
	// calculated as:
	//
	//   - 32 bytes previous tx
	//   - 4 bytes output index
	//   - 4 bytes sequence
	//   - 1 byte compact int encoding value 108
	//   - 108 bytes signature script
	RedeemP2PKHInputSize = TxInOverhead + 1 + RedeemP2PKHSigScriptSize // 40 + 1 + 108 = 149

	// RedeemP2WPKHInputSize is the worst case size of a transaction
	// input redeeming a P2WPKH output. This does not account for witness data,
	// which is considered at a lower weight for fee calculations. It is
	// calculated as
	//
	//   - 32 bytes previous tx
	//   - 4 bytes output index
	//   - 4 bytes sequence
	//   - 1 byte encoding empty redeem script
	//   - 0 bytes signature script
	RedeemP2WPKHInputSize = TxInOverhead + 1

	// RedeemP2WPKHInputWitnessWeight is the worst case weight of
	// a witness for spending P2WPKH and nested P2WPKH outputs. It
	// is calculated as:
	//
	//   - 1 wu compact int encoding value 2 (number of items)
	//   - 1 wu compact int encoding value 73
	//   - 72 wu DER signature + 1 wu sighash
	//   - 1 wu compact int encoding value 33
	//   - 33 wu serialized compressed pubkey
	// NOTE: witness data is not script.
	RedeemP2WPKHInputWitnessWeight = 1 + 1 + DERSigLength + 1 + 33 // 109

	// P2WPKHPkScriptSize is the size of a transaction output script that
	// pays to a witness pubkey hash. It is calculated as:
	//
	//   - OP_0
	//   - OP_DATA_20
	//   - 20 bytes pubkey hash
	P2WPKHPkScriptSize = 1 + 1 + 20

	// P2WPKHOutputSize is the serialize size of a transaction output with a
	// P2WPKH output script. It is calculated as:
	//
	//   - 8 bytes output value
	//   - 1 byte compact int encoding value 22
	//   - 22 bytes P2PKH output script
	P2WPKHOutputSize = TxOutOverhead + P2WPKHPkScriptSize // 31

	// MinimumTxOverhead is the size of an empty transaction.
	// 4 bytes version + 4 bytes locktime + 2 bytes of varints for the number of
	// transaction inputs and outputs
	MinimumTxOverhead = 4 + 4 + 1 + 1 // 10

	// InitTxSizeBase is the size of a standard serialized atomic swap
	// initialization transaction with one change output and no inputs. This is
	// MsgTx overhead + 1 P2PKH change output + 1 P2SH contract output. However,
	// the change output might be P2WPKH, in which case it would be smaller.
	InitTxSizeBase = MinimumTxOverhead + P2PKHOutputSize + P2SHOutputSize // 10 + 34 + 32 = 76

	// InitTxSize is InitTxBaseSize + 1 P2PKH input
	InitTxSize = InitTxSizeBase + RedeemP2PKHInputSize // 76 + 149 = 225

	// InitTxSizeBaseSegwit is the size of a standard serialized atomic swap
	// initialization transaction with one change output and no inputs. The
	// change output is assumed to be segwit. 10 + 31 + 43 = 84
	InitTxSizeBaseSegwit = MinimumTxOverhead + P2WPKHOutputSize + P2WSHOutputSize

	// InitTxSizeSegwit is InitTxSizeSegwit + 1 P2WPKH input.
	// 84 vbytes base tx
	// 41 vbytes base tx input
	// 109wu witness +  2wu segwit marker and flag = 28 vbytes
	// total = 153 vbytes
	InitTxSizeSegwit = InitTxSizeBaseSegwit + RedeemP2WPKHInputSize + ((RedeemP2WPKHInputWitnessWeight + 2 + 3) / 4)
)
View Source
const (
	ScriptP2PKH = 1 << iota
	ScriptP2SH
	ScriptTypeSegwit
	ScriptMultiSig
	ScriptUnsupported
)

Variables

View Source
var RPCPorts = NetPorts{
	Mainnet: "8332",
	Testnet: "18332",
	Simnet:  "18443",
}

RPCPorts are the default BTC ports.

Functions

func ExtractContractHash

func ExtractContractHash(scriptHex string) ([]byte, error)

ExtractContractHash extracts the contract P2SH or P2WSH address from a pkScript. A non-nil error is returned if the hexadecimal encoding of the pkScript is invalid, or if the pkScript is not a P2SH or P2WSH script.

func ExtractPubKeyHash

func ExtractPubKeyHash(script []byte) []byte

ExtractPubKeyHash extracts the pubkey hash from the passed script if it is a standard pay-to-pubkey-hash script. It will return nil otherwise.

func ExtractScriptHash

func ExtractScriptHash(script []byte) []byte

ExtractScriptHash attempts to extract the redeem script hash from a pkScript. If it is not a P2SH or P2WSH pkScript, a nil slice is returned.

func ExtractSwapDetails

func ExtractSwapDetails(pkScript []byte, segwit bool, chainParams *chaincfg.Params) (
	sender, receiver btcutil.Address, lockTime uint64, secretHash []byte, err error)

ExtractSwapDetails extacts the sender and receiver addresses from a swap contract. If the provided script is not a swap contract, an error will be returned.

func FindKeyPush

func FindKeyPush(txIn *wire.TxIn, contractHash []byte, segwit bool, chainParams *chaincfg.Params) ([]byte, error)

FindKeyPush attempts to extract the secret key from the signature script. The contract must be provided for the search algorithm to verify the correct data push. Only contracts of length SwapContractSize that can be validated by ExtractSwapDetails are recognized.

func IsDust

func IsDust(txOut *wire.TxOut, minRelayTxFee uint64) bool

IsDust returns whether or not the passed transaction output amount is considered dust or not based on the passed minimum transaction relay fee. Dust is defined in terms of the minimum transaction relay fee. Based on btcd/policy isDust. See btcd/policy for further documentation.

func MakeContract

func MakeContract(rAddr, sAddr btcutil.Address, secretHash []byte, lockTime int64, segwit bool, chainParams *chaincfg.Params) ([]byte, error)

MakeContract creates a segwit atomic swap contract. The secretHash MUST be computed from a secret of length SecretKeySize bytes or the resulting contract will be invalid.

func MsgTxVBytes

func MsgTxVBytes(msgTx *wire.MsgTx) uint64

MsgTxVBytes returns the transaction's virtual size, which accounts for the segwit input weighting.

func ReadCloneParams

func ReadCloneParams(cloneParams *CloneParams) *chaincfg.Params

ReadCloneParams translates a CloneParams into a btcsuite chaincfg.Params.

func RedeemP2SHContract

func RedeemP2SHContract(contract, sig, pubkey, secret []byte) ([]byte, error)

RedeemP2SHContract returns the signature script to redeem a contract output using the redeemer's signature and the initiator's secret. This function assumes P2SH and appends the contract as the final data push.

func RedeemP2WSHContract

func RedeemP2WSHContract(contract, sig, pubkey, secret []byte) [][]byte

RedeemP2WSHContract returns the witness script to redeem a contract output using the redeemer's signature and the initiator's secret. This function assumes P2WSH and appends the contract as the final data push.

func RefundP2SHContract

func RefundP2SHContract(contract, sig, pubkey []byte) ([]byte, error)

RefundP2SHContract returns the signature script to refund a contract output using the contract author's signature after the locktime has been reached. This function assumes P2SH and appends the contract as the final data push.

func RefundP2WSHContract

func RefundP2WSHContract(contract, sig, pubkey []byte) [][]byte

RefundP2WSHContract returns the witness to refund a contract output using the contract author's signature after the locktime has been reached. This function assumes P2WSH and appends the contract as the final data push.

func SystemConfigPath

func SystemConfigPath(asset string) string

SystemConfigPath will return the default config file path for bitcoin-like assets.

Types

type AddressDecoder added in v0.2.0

type AddressDecoder func(addr string, net *chaincfg.Params) (btcutil.Address, error)

AddressDecoder decodes a string address to a btcutil.Address.

type BTCScriptType

type BTCScriptType uint8

BTCScriptType holds details about a pubkey script and possibly it's redeem script.

func ParseScriptType

func ParseScriptType(pkScript, redeemScript []byte) BTCScriptType

ParseScriptType creates a BTCScriptType bitmap for the script type. A script type will be some combination of pay-to-pubkey-hash, pay-to-script-hash, and stake. If a script type is P2SH, it may or may not be mutli-sig.

func (BTCScriptType) IsMultiSig

func (s BTCScriptType) IsMultiSig() bool

IsMultiSig is whether the pkscript references a multi-sig redeem script. Since the DEX will know the redeem script, we can say whether it's multi-sig.

func (BTCScriptType) IsP2PKH

func (s BTCScriptType) IsP2PKH() bool

IsP2PKH will return boolean true if the script is a P2PKH script.

func (BTCScriptType) IsP2SH

func (s BTCScriptType) IsP2SH() bool

IsP2SH will return boolean true if the script is a P2SH script.

func (BTCScriptType) IsP2WPKH

func (s BTCScriptType) IsP2WPKH() bool

IsP2WPKH will return boolean true if the script is a P2WPKH script.

func (BTCScriptType) IsP2WSH

func (s BTCScriptType) IsP2WSH() bool

IsP2WSH will return boolean true if the script is a P2WSH script.

func (BTCScriptType) IsSegwit

func (s BTCScriptType) IsSegwit() bool

IsSegwit will return boolean true if the script is a P2WPKH or P2WSH script.

type BtcScriptAddrs

type BtcScriptAddrs struct {
	PubKeys   []btcutil.Address
	NumPK     int
	PkHashes  []btcutil.Address
	NumPKH    int
	NRequired int // num sigs needed
}

BtcScriptAddrs is information about the pubkeys or pubkey hashes present in a scriptPubKey (and the redeem script, for p2sh). This information can be used to estimate the spend script size, e.g. pubkeys in a redeem script don't require pubkeys in the witness/scriptSig, but pubkey hashes do.

func ExtractScriptAddrs

func ExtractScriptAddrs(script []byte, chainParams *chaincfg.Params) (*BtcScriptAddrs, bool, error)

ExtractScriptAddrs extracts the addresses from the pubkey script, or the redeem script if the pubkey script is P2SH. The returned bool indicates if the script is non-standard.

type CloneParams

type CloneParams struct {
	// PubKeyHashAddrID: Net ID byte for a pubkey-hash address
	PubKeyHashAddrID byte
	// ScriptHashAddrID: Net ID byte for a script-hash address
	ScriptHashAddrID byte
	// Bech32HRPSegwit: Human-readable part for Bech32 encoded segwit addresses,
	// as defined in BIP 173.
	Bech32HRPSegwit string
	// CoinbaseMaturity: The number of confirmations before a transaction
	// spending a coinbase input can be spent.
	CoinbaseMaturity uint16
	// Net is the network identifier, e.g. wire.BitcoinNet.
	Net uint32
}

CloneParams are the parameters needed by BTC-clone-based Backend and ExchangeWallet implementations. Pass a *CloneParams to ReadCloneParams to create a *chaincfg.Params for server/asset/btc.NewBTCClone and client/asset/btc.BTCCloneWallet.

type Config

type Config struct {
	RPCUser string `ini:"rpcuser"`
	RPCPass string `ini:"rpcpassword"`
	RPCBind string `ini:"rpcbind"`
	RPCPort int    `ini:"rpcport"`

	UseSplitTx       bool    `ini:"txsplit"`
	FallbackFeeRate  float64 `ini:"fallbackfee"`
	FeeRateLimit     float64 `ini:"feeratelimit"`
	RedeemConfTarget uint64  `ini:"redeemconftarget"`
}

Config holds the parameters needed to initialize an RPC connection to a btc wallet or backend. Default values are used for RPCBind and/or RPCPort if not set.

func LoadConfigFromPath

func LoadConfigFromPath(cfgPath string, name string, network dex.Network, ports NetPorts) (*Config, error)

LoadConfigFromPath loads the configuration settings from the specified filepath.

func LoadConfigFromSettings

func LoadConfigFromSettings(settings map[string]string, name string, network dex.Network, ports NetPorts) (*Config, error)

LoadConfigFromSettings loads the configuration settings from a settings map.

type NetPorts

type NetPorts struct {
	Mainnet string
	Testnet string
	Simnet  string
}

NetPorts are a set of port to use with the different networks.

type SpendInfo

type SpendInfo struct {
	SigScriptSize     uint32
	WitnessSize       uint32
	ScriptAddrs       *BtcScriptAddrs
	ScriptType        BTCScriptType
	NonStandardScript bool
}

SpendInfo is information about an input and it's previous outpoint.

func InputInfo

func InputInfo(pkScript, redeemScript []byte, chainParams *chaincfg.Params) (*SpendInfo, error)

InputInfo is some basic information about the input required to spend an output. The pubkey script of the output is provided. If the pubkey script parses as P2SH or P2WSH, the redeem script must be provided.

func (*SpendInfo) VBytes

func (nfo *SpendInfo) VBytes() uint32

VBytes is the virtual bytes of the spending transaction input. Miners use weight/4 = vbytes to calculate contribution to block size limit and therefore transaction fees.

Jump to

Keyboard shortcuts

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