From bf59159ddbcfcec86e4c3079624cf49a10c425d2 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Mon, 16 May 2022 18:37:53 +0200 Subject: [PATCH] client: LoopOutQuote to support estimate using v3 htlc --- client.go | 40 +++++++++++++++++++++++++++------------- swap/htlc.go | 29 +++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/client.go b/client.go index d34dfb4..498378e 100644 --- a/client.go +++ b/client.go @@ -499,7 +499,23 @@ func (s *Client) LoopOutQuote(ctx context.Context, log.Infof("Offchain swap destination: %x", quote.SwapPaymentDest) - swapFee := quote.SwapFee + minerFee, err := s.getLoopOutSweepFee(ctx, request.SweepConfTarget) + if err != nil { + return nil, err + } + + return &LoopOutQuote{ + SwapFee: quote.SwapFee, + MinerFee: minerFee, + PrepayAmount: quote.PrepayAmount, + SwapPaymentDest: quote.SwapPaymentDest, + }, nil +} + +// getLoopOutSweepFee is a helper method to estimate the loop out htlc sweep +// fee to a p2wsh address. +func (s *Client) getLoopOutSweepFee(ctx context.Context, confTarget int32) ( + btcutil.Amount, error) { // Generate dummy p2wsh address for fee estimation. The p2wsh address // type is chosen because it adds the most weight of all output types @@ -509,23 +525,21 @@ func (s *Client) LoopOutQuote(ctx context.Context, wsh[:], s.lndServices.ChainParams, ) if err != nil { - return nil, err + return 0, err } - minerFee, err := s.sweeper.GetSweepFee( - ctx, swap.QuoteHtlc.AddSuccessToEstimator, - p2wshAddress, request.SweepConfTarget, + scriptVersion := GetHtlcScriptVersion( + loopdb.CurrentProtocolVersion(), ) - if err != nil { - return nil, err + + htlc := swap.QuoteHtlcP2TR + if scriptVersion != swap.HtlcV3 { + htlc = swap.QuoteHtlcP2WSH } - return &LoopOutQuote{ - SwapFee: swapFee, - MinerFee: minerFee, - PrepayAmount: quote.PrepayAmount, - SwapPaymentDest: quote.SwapPaymentDest, - }, nil + return s.sweeper.GetSweepFee( + ctx, htlc.AddSuccessToEstimator, p2wshAddress, confTarget, + ) } // LoopOutTerms returns the terms on which the server executes swaps. diff --git a/swap/htlc.go b/swap/htlc.go index 56d9618..5df0309 100644 --- a/swap/htlc.go +++ b/swap/htlc.go @@ -108,17 +108,30 @@ type Htlc struct { } var ( - quoteKey [33]byte + // dummyPubKey is a valid public key use for the quote htlc + // construction. + dummyPubKey = [33]byte{ + 0x03, 0x26, 0x89, 0xc7, 0xc2, 0xda, 0xb1, 0x33, 0x09, 0xfb, + 0x14, 0x3e, 0x0e, 0x8f, 0xe3, 0x96, 0x34, 0x25, 0x21, 0x88, + 0x7e, 0x97, 0x66, 0x90, 0xb6, 0xb4, 0x7f, 0x5b, 0x2a, 0x4b, + 0x7d, 0x44, 0x8e, + } + // quoteHash is an empty hash used for the quote htlc construction. quoteHash lntypes.Hash - // QuoteHtlc is a template script just used for fee estimation. It uses - // the maximum value for cltv expiry to get the maximum (worst case) - // script size. - QuoteHtlc, _ = NewHtlc( - HtlcV2, - ^int32(0), quoteKey, quoteKey, quoteHash, HtlcP2WSH, - &chaincfg.MainNetParams, + // QuoteHtlcP2WSH is a template script just used for sweep fee + // estimation. + QuoteHtlcP2WSH, _ = NewHtlc( + HtlcV2, ^int32(0), dummyPubKey, dummyPubKey, quoteHash, + HtlcP2WSH, &chaincfg.MainNetParams, + ) + + // QuoteHtlcP2TR is a template script just used for sweep fee + // estimation. + QuoteHtlcP2TR, _ = NewHtlc( + HtlcV3, ^int32(0), dummyPubKey, dummyPubKey, quoteHash, + HtlcP2TR, &chaincfg.MainNetParams, ) // ErrInvalidScriptVersion is returned when an unknown htlc version