swap

package
v0.29.0-beta.rc1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 18 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// FeeRateTotalParts defines the granularity of the fee rate.
	// Throughout the codebase, we'll use fix based arithmetic to compute
	// fees.
	FeeRateTotalParts = 1e6
)

Variables

View Source
var (

	// QuoteHtlcP2WSH is a template script just used for sweep fee
	// estimation.
	QuoteHtlcP2WSH, _ = NewHtlcV2(
		^int32(0), dummyPubKey, dummyPubKey, quoteHash,
		&chaincfg.MainNetParams,
	)

	// QuoteHtlcP2TR is a template script just used for sweep fee
	// estimation.
	QuoteHtlcP2TR, _ = NewHtlcV3(
		input.MuSig2Version100RC2, ^int32(0), dummyPubKey, dummyPubKey,
		dummyPubKey, dummyPubKey, quoteHash, &chaincfg.MainNetParams,
	)

	// ErrInvalidScriptVersion is returned when an unknown htlc version
	// is provided to NewHtlc. The supported version are HtlcV2, HtlcV3
	// as enums.
	ErrInvalidScriptVersion = fmt.Errorf("invalid script version")

	// ErrInvalidOutputSelected is returned when a taproot output is
	// selected for a v2 script.
	ErrInvalidOutputSelected = fmt.Errorf("taproot output selected for " +
		"non taproot htlc")

	// ErrInvalidOutputType is returned when an unknown output type is
	// associated with a certain swap htlc.
	ErrInvalidOutputType = fmt.Errorf("invalid htlc output type")
)
View Source
var (
	// KeyFamily is the key family used to generate keys that allow
	// spending of the htlc.
	KeyFamily = int32(99)

	// StaticAddressKeyFamily is the key family used to generate static
	// address keys.
	StaticAddressKeyFamily = int32(42060)
)

Functions

func CalcFee

func CalcFee(amount, feeBase btcutil.Amount, feeRate int64) btcutil.Amount

CalcFee returns the swap fee for a given swap amount.

func DecodeInvoice

func DecodeInvoice(params *chaincfg.Params, payReq string) (route.Vertex,
	[][]zpay32.HopHint, lntypes.Hash, btcutil.Amount, error)

DecodeInvoice gets the destination, hash and the amount of an invoice. It requires an amount to be specified.

func FeeRateAsPercentage

func FeeRateAsPercentage(feeRate int64) float64

FeeRateAsPercentage converts a feerate to a percentage.

func GenSuccessPathScript

func GenSuccessPathScript(receiverHtlcKey *btcec.PublicKey,
	swapHash lntypes.Hash) ([]byte, error)

GenSuccessPathScript constructs an HtlcScript for the success payment path. Largest possible bytesize of the script is 32 + 5*1 + 20 + 3*1 = 60.

<receiverHtlcKey> OP_CHECKSIGVERIFY
OP_SIZE 32 OP_EQUALVERIFY
OP_HASH160 <ripemd160h(swapHash)> OP_EQUALVERIFY
1 OP_CHECKSEQUENCEVERIFY

func GenTimeoutPathScript

func GenTimeoutPathScript(senderHtlcKey *btcec.PublicKey, cltvExpiry int64) (
	[]byte, error)

GenTimeoutPathScript constructs an HtlcScript for the timeout payment path. Largest possible bytesize of the script is 32 + 1 + 2 + 1 = 36.

<senderHtlcKey> OP_CHECKSIGVERIFY <cltvExpiry> OP_CHECKLOCKTIMEVERIFY

func GetScriptOutput

func GetScriptOutput(htlcTx *wire.MsgTx, scriptHash []byte) (
	*wire.OutPoint, btcutil.Amount, error)

GetScriptOutput locates the given script in the outputs of a transaction and returns its outpoint and value.

func GetTxInputByOutpoint

func GetTxInputByOutpoint(tx *wire.MsgTx, input *wire.OutPoint) (
	*wire.TxIn, error)

GetTxInputByOutpoint returns a tx input based on a given input outpoint.

func ShortHash

func ShortHash(hash *lntypes.Hash) string

ShortHash returns a shortened version of the hash suitable for use in logging.

Types

type Htlc

type Htlc struct {
	HtlcScript

	Version     ScriptVersion
	PkScript    []byte
	Hash        lntypes.Hash
	OutputType  HtlcOutputType
	ChainParams *chaincfg.Params
	Address     btcutil.Address
	SigScript   []byte
}

Htlc contains relevant htlc information from the receiver perspective.

func NewHtlcV2

func NewHtlcV2(cltvExpiry int32, senderKey, receiverKey [33]byte,
	hash lntypes.Hash, chainParams *chaincfg.Params) (*Htlc, error)

NewHtlcV2 returns a new V2 (P2WSH) HTLC instance.

func NewHtlcV3

func NewHtlcV3(muSig2Version input.MuSig2Version, cltvExpiry int32,
	senderInternalKey, receiverInternalKey, senderKey, receiverKey [33]byte,
	hash lntypes.Hash, chainParams *chaincfg.Params) (*Htlc, error)

NewHtlcV3 returns a new V3 HTLC (P2TR) instance. Internal pubkey generated by both participants must be provided.

func (*Htlc) AddSuccessToEstimator

func (h *Htlc) AddSuccessToEstimator(estimator *input.TxWeightEstimator) error

AddSuccessToEstimator adds a successful spend to a weight estimator.

func (*Htlc) AddTimeoutToEstimator

func (h *Htlc) AddTimeoutToEstimator(estimator *input.TxWeightEstimator) error

AddTimeoutToEstimator adds a timeout spend to a weight estimator.

func (*Htlc) GenSuccessWitness

func (h *Htlc) GenSuccessWitness(receiverSig []byte,
	preimage lntypes.Preimage) (wire.TxWitness, error)

GenSuccessWitness returns the success script to spend this htlc with the preimage.

type HtlcOutputType

type HtlcOutputType uint8

HtlcOutputType defines the output type of the htlc that is published.

const (
	// HtlcP2WSH is a pay-to-witness-script-hash output (segwit only).
	HtlcP2WSH HtlcOutputType = iota

	// HtlcP2TR is a pay-to-taproot output with three separate spend paths.
	HtlcP2TR
)

func (HtlcOutputType) String

func (h HtlcOutputType) String() string

String returns the string value of HtlcOutputType.

type HtlcScript

type HtlcScript interface {

	// GenTimeoutWitness returns the timeout script to spend this htlc after
	// timeout.
	GenTimeoutWitness(senderSig []byte) (wire.TxWitness, error)

	// IsSuccessWitness checks whether the given stack is valid for
	// redeeming the htlc.
	IsSuccessWitness(witness wire.TxWitness) bool

	// MaxSuccessWitnessSize returns the maximum witness size for the
	// success case witness.
	MaxSuccessWitnessSize() lntypes.WeightUnit

	// MaxTimeoutWitnessSize returns the maximum witness size for the
	// timeout case witness.
	MaxTimeoutWitnessSize() lntypes.WeightUnit

	// TimeoutScript returns the redeem script required to unlock the htlc
	// after timeout.
	TimeoutScript() []byte

	// SuccessScript returns the redeem script required to unlock the htlc
	// using the preimage.
	SuccessScript() []byte

	// SuccessSequence returns the sequence to spend this htlc in the
	// success case.
	SuccessSequence() uint32

	// SigHash is the signature hash to use for transactions spending from
	// the htlc.
	SigHash() txscript.SigHashType
	// contains filtered or unexported methods
}

HtlcScript defines an interface for the different HTLC implementations.

type HtlcScriptV2

type HtlcScriptV2 struct {
	// contains filtered or unexported fields
}

HtlcScriptV2 encapsulates the htlc v2 script.

func (*HtlcScriptV2) GenTimeoutWitness

func (h *HtlcScriptV2) GenTimeoutWitness(
	senderSig []byte) (wire.TxWitness, error)

GenTimeoutWitness returns the timeout script to spend this htlc after timeout.

func (*HtlcScriptV2) IsSuccessWitness

func (h *HtlcScriptV2) IsSuccessWitness(witness wire.TxWitness) bool

IsSuccessWitness checks whether the given stack is valid for redeeming the htlc.

func (*HtlcScriptV2) MaxSuccessWitnessSize

func (h *HtlcScriptV2) MaxSuccessWitnessSize() lntypes.WeightUnit

MaxSuccessWitnessSize returns maximum success witness size.

func (*HtlcScriptV2) MaxTimeoutWitnessSize

func (h *HtlcScriptV2) MaxTimeoutWitnessSize() lntypes.WeightUnit

MaxTimeoutWitnessSize returns maximum timeout witness size.

func (*HtlcScriptV2) SigHash

func (h *HtlcScriptV2) SigHash() txscript.SigHashType

Sighash is the signature hash to use for transactions spending from the htlc.

func (*HtlcScriptV2) SuccessScript

func (h *HtlcScriptV2) SuccessScript() []byte

SuccessScript returns the redeem script required to unlock the htlc using the preimage.

In the case of HtlcScriptV2, this is the full segwit v0 script.

func (*HtlcScriptV2) SuccessSequence

func (h *HtlcScriptV2) SuccessSequence() uint32

SuccessSequence returns the sequence to spend this htlc in the success case.

func (*HtlcScriptV2) TimeoutScript

func (h *HtlcScriptV2) TimeoutScript() []byte

TimeoutScript returns the redeem script required to unlock the htlc after timeout.

In the case of HtlcScriptV2, this is the full segwit v0 script.

type HtlcScriptV3

type HtlcScriptV3 struct {

	// InternalPubKey is the public key for the keyspend path which bypasses
	// the above two locking scripts.
	InternalPubKey *btcec.PublicKey

	// TaprootKey is the taproot public key which is created with the above
	// 3 inputs.
	TaprootKey *btcec.PublicKey

	// RootHash is the root hash of the taptree.
	RootHash chainhash.Hash
	// contains filtered or unexported fields
}

HtlcScriptV3 encapsulates the htlc v3 script.

func (*HtlcScriptV3) GenTimeoutWitness

func (h *HtlcScriptV3) GenTimeoutWitness(
	senderSig []byte) (wire.TxWitness, error)

GenTimeoutWitness returns the timeout script to spend this htlc after timeout.

func (*HtlcScriptV3) IsSuccessWitness

func (h *HtlcScriptV3) IsSuccessWitness(witness wire.TxWitness) bool

IsSuccessWitness checks whether the given stack is valid for redeeming the htlc.

func (*HtlcScriptV3) MaxSuccessWitnessSize

func (h *HtlcScriptV3) MaxSuccessWitnessSize() lntypes.WeightUnit

MaxSuccessWitnessSize returns the maximum witness size for the success case witness.

func (*HtlcScriptV3) MaxTimeoutWitnessSize

func (h *HtlcScriptV3) MaxTimeoutWitnessSize() lntypes.WeightUnit

MaxTimeoutWitnessSize returns the maximum witness size for the timeout case witness.

func (*HtlcScriptV3) SigHash

func (h *HtlcScriptV3) SigHash() txscript.SigHashType

Sighash is the signature hash to use for transactions spending from the htlc.

func (*HtlcScriptV3) SuccessScript

func (h *HtlcScriptV3) SuccessScript() []byte

SuccessScript returns the redeem script required to unlock the htlc using the preimage.

In the case of HtlcScriptV3, this is the claim tapleaf.

func (*HtlcScriptV3) SuccessSequence

func (h *HtlcScriptV3) SuccessSequence() uint32

SuccessSequence returns the sequence to spend this htlc in the success case.

func (*HtlcScriptV3) TimeoutScript

func (h *HtlcScriptV3) TimeoutScript() []byte

TimeoutScript returns the redeem script required to unlock the htlc after timeout.

In the case of HtlcScriptV3, this is the timeout tapleaf.

type PrefixLog

type PrefixLog struct {
	// Logger is the underlying based logger.
	Logger btclog.Logger

	// Hash is the hash the identifies the target swap.
	Hash lntypes.Hash
}

PrefixLog logs with a short swap hash prefix.

func (*PrefixLog) Errorf

func (s *PrefixLog) Errorf(format string, params ...interface{})

Errorf formats message according to format specifier and writes to log with LevelError.

func (*PrefixLog) Infof

func (s *PrefixLog) Infof(format string, params ...interface{})

Infof formats message according to format specifier and writes to log with LevelInfo.

func (*PrefixLog) Warnf

func (s *PrefixLog) Warnf(format string, params ...interface{})

Warnf formats message according to format specifier and writes to log with LevelError.

type ScriptVersion

type ScriptVersion uint8

ScriptVersion defines the HTLC script version.

const (
	// HtlcV2 refers to the improved version of the HTLC script.
	HtlcV2 ScriptVersion = iota

	// HtlcV3 refers to an upgraded version of HtlcV2 implemented with
	// tapscript.
	HtlcV3
)

type Type

type Type uint8

Type indicates the type of swap.

const (
	// TypeIn is a loop in swap.
	TypeIn Type = iota

	// TypeOut is a loop out swap.
	TypeOut
)

func (Type) IsOut

func (t Type) IsOut() bool

IsOut returns true if the swap is a loop out swap, false if it is a loop in swap.

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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