Skip to content

Commit 7a8e227

Browse files
committed
staticaddr: arbitrary withdrawal amount
1 parent 852f961 commit 7a8e227

File tree

1 file changed

+149
-60
lines changed

1 file changed

+149
-60
lines changed

staticaddr/withdraw/manager.go

Lines changed: 149 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"reflect"
88
"strings"
99

10+
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1011
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
1112
"github.com/btcsuite/btcd/btcutil"
1213
"github.com/btcsuite/btcd/chaincfg"
@@ -75,6 +76,7 @@ type newWithdrawalRequest struct {
7576
respChan chan *newWithdrawalResponse
7677
destAddr string
7778
satPerVbyte int64
79+
amount int64
7880
}
7981

8082
// newWithdrawalResponse is used to return withdrawal info and error to the
@@ -156,10 +158,10 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
156158
err)
157159
}
158160

159-
case request := <-m.newWithdrawalRequestChan:
161+
case req := <-m.newWithdrawalRequestChan:
160162
txHash, pkScript, err = m.WithdrawDeposits(
161-
ctx, request.outpoints, request.destAddr,
162-
request.satPerVbyte,
163+
ctx, req.outpoints, req.destAddr,
164+
req.satPerVbyte, req.amount,
163165
)
164166
if err != nil {
165167
log.Errorf("Error withdrawing deposits: %v",
@@ -174,7 +176,7 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
174176
err: err,
175177
}
176178
select {
177-
case request.respChan <- resp:
179+
case req.respChan <- resp:
178180

179181
case <-ctx.Done():
180182
// Notify subroutines that the main loop has
@@ -259,10 +261,11 @@ func (m *Manager) WaitInitComplete() {
259261
<-m.initChan
260262
}
261263

262-
// WithdrawDeposits starts a deposits withdrawal flow.
264+
// WithdrawDeposits starts a deposits withdrawal flow. If the amount is set to 0
265+
// the full amount of the selected deposits will be withdrawn.
263266
func (m *Manager) WithdrawDeposits(ctx context.Context,
264-
outpoints []wire.OutPoint, destAddr string, satPerVbyte int64) (string,
265-
string, error) {
267+
outpoints []wire.OutPoint, destAddr string, satPerVbyte int64,
268+
amount int64) (string, string, error) {
266269

267270
if len(outpoints) == 0 {
268271
return "", "", fmt.Errorf("no outpoints selected to " +
@@ -272,7 +275,8 @@ func (m *Manager) WithdrawDeposits(ctx context.Context,
272275
// Ensure that the deposits are in a state in which they can be
273276
// withdrawn.
274277
deposits, allActive := m.cfg.DepositManager.AllOutpointsActiveDeposits(
275-
outpoints, deposit.Deposited)
278+
outpoints, deposit.Deposited,
279+
)
276280

277281
if !allActive {
278282
return "", "", ErrWithdrawingInactiveDeposits
@@ -303,7 +307,7 @@ func (m *Manager) WithdrawDeposits(ctx context.Context,
303307
}
304308

305309
finalizedTx, err := m.createFinalizedWithdrawalTx(
306-
ctx, deposits, withdrawalAddress, satPerVbyte,
310+
ctx, deposits, withdrawalAddress, satPerVbyte, amount,
307311
)
308312
if err != nil {
309313
return "", "", err
@@ -355,7 +359,8 @@ func (m *Manager) WithdrawDeposits(ctx context.Context,
355359

356360
func (m *Manager) createFinalizedWithdrawalTx(ctx context.Context,
357361
deposits []*deposit.Deposit, withdrawalAddress btcutil.Address,
358-
satPerVbyte int64) (*wire.MsgTx, error) {
362+
satPerVbyte int64, selectedWithdrawalAmount int64) (*wire.MsgTx,
363+
error) {
359364

360365
// Create a musig2 session for each deposit.
361366
withdrawalSessions, clientNonces, err := m.createMusig2Sessions(
@@ -380,59 +385,43 @@ func (m *Manager) createFinalizedWithdrawalTx(ctx context.Context,
380385
).FeePerKWeight()
381386
}
382387

383-
// We'll now check the selected fee rate leaves a withdrawal output that
384-
// is above the dust limit. If not we cancel the withdrawal instead of
385-
// requesting a signature from the server.
386-
addressParams, err := m.cfg.AddressManager.GetStaticAddressParameters(
387-
ctx,
388-
)
388+
params, err := m.cfg.AddressManager.GetStaticAddressParameters(ctx)
389389
if err != nil {
390390
return nil, fmt.Errorf("couldn't get confirmation height for "+
391391
"deposit, %w", err)
392392
}
393393

394-
// Calculate the fee value in satoshis.
395394
outpoints := toOutpoints(deposits)
396-
weight, err := withdrawalFee(len(outpoints), withdrawalAddress)
395+
prevOuts := m.toPrevOuts(deposits, params.PkScript)
396+
totalValue := withdrawalValue(prevOuts)
397+
withdrawalTx, withdrawAmount, changeAmount, err := m.createWithdrawalTx(
398+
ctx, outpoints, totalValue,
399+
btcutil.Amount(selectedWithdrawalAmount), withdrawalAddress,
400+
withdrawalSweepFeeRate,
401+
)
397402
if err != nil {
398403
return nil, err
399404
}
400-
feeValue := withdrawalSweepFeeRate.FeeForWeight(weight)
401-
402-
var (
403-
prevOuts = m.toPrevOuts(deposits, addressParams.PkScript)
404-
totalValue = withdrawalValue(prevOuts)
405-
outputValue = int64(totalValue) - int64(feeValue)
406-
// P2TRSize calculates a dust limit based on a 40 byte maximum
407-
// size witness output.
408-
dustLimit = lnwallet.DustLimitForSize(input.P2TRSize)
409-
)
410-
411-
if outputValue < int64(dustLimit) {
412-
return nil, fmt.Errorf("withdrawal output value %d sats "+
413-
"below dust limit %d sats", outputValue, dustLimit)
414-
}
415405

406+
// Request the server to sign the withdrawal transaction.
407+
//
408+
// The withdrawal and change amount are sent to the server with the
409+
// expectation that the server just signs the transaction, without
410+
// performing fee calculations and dust considerations. The client is
411+
// responsible for that.
416412
resp, err := m.cfg.StaticAddressServerClient.ServerWithdrawDeposits(
417413
ctx, &staticaddressrpc.ServerWithdrawRequest{
418-
Outpoints: toPrevoutInfo(outpoints),
419-
ClientNonces: clientNonces,
420-
ClientSweepAddr: withdrawalAddress.String(),
421-
TxFeeRate: uint64(withdrawalSweepFeeRate),
414+
Outpoints: toPrevoutInfo(outpoints),
415+
ClientNonces: clientNonces,
416+
ClientWithdrawalAddr: withdrawalAddress.String(),
417+
WithdrawAmount: int64(withdrawAmount),
418+
ChangeAmount: int64(changeAmount),
422419
},
423420
)
424421
if err != nil {
425422
return nil, err
426423
}
427424

428-
withdrawalOutputValue := int64(totalValue - feeValue)
429-
withdrawalTx, err := m.createWithdrawalTx(
430-
outpoints, withdrawalOutputValue, withdrawalAddress,
431-
)
432-
if err != nil {
433-
return nil, err
434-
}
435-
436425
coopServerNonces, err := toNonces(resp.ServerNonces)
437426
if err != nil {
438427
return nil, err
@@ -634,9 +623,11 @@ func byteSliceTo66ByteSlice(b []byte) ([musig2.PubNonceSize]byte, error) {
634623
return res, nil
635624
}
636625

637-
func (m *Manager) createWithdrawalTx(outpoints []wire.OutPoint,
638-
withdrawlOutputValue int64, clientSweepAddress btcutil.Address) (
639-
*wire.MsgTx, error) {
626+
func (m *Manager) createWithdrawalTx(ctx context.Context,
627+
outpoints []wire.OutPoint, totalWithdrawalAmount btcutil.Amount,
628+
selectedWithdrawalAmount btcutil.Amount, withdrawAddr btcutil.Address,
629+
feeRate chainfee.SatPerKWeight) (*wire.MsgTx, btcutil.Amount,
630+
btcutil.Amount, error) {
640631

641632
// First Create the tx.
642633
msgTx := wire.NewMsgTx(2)
@@ -649,25 +640,117 @@ func (m *Manager) createWithdrawalTx(outpoints []wire.OutPoint,
649640
})
650641
}
651642

652-
pkscript, err := txscript.PayToAddrScript(clientSweepAddress)
643+
var (
644+
hasChange bool
645+
dustLimit = lnwallet.DustLimitForSize(input.P2TRSize)
646+
withdrawalAmount btcutil.Amount
647+
changeAmount btcutil.Amount
648+
)
649+
650+
// Estimate the transaction weight without change.
651+
weight, err := withdrawalTxWeight(len(outpoints), withdrawAddr, false)
653652
if err != nil {
654-
return nil, err
653+
return nil, 0, 0, err
655654
}
655+
feeWithoutChange := feeRate.FeeForWeight(weight)
656656

657-
// Create the sweep output
658-
sweepOutput := &wire.TxOut{
659-
Value: withdrawlOutputValue,
660-
PkScript: pkscript,
657+
// If the user selected a fraction of the sum of the selected deposits
658+
// to withdraw, check if a change output is needed.
659+
if selectedWithdrawalAmount > 0 {
660+
// Estimate the transaction weight with change.
661+
weight, err = withdrawalTxWeight(
662+
len(outpoints), withdrawAddr, true,
663+
)
664+
if err != nil {
665+
return nil, 0, 0, err
666+
}
667+
feeWithChange := feeRate.FeeForWeight(weight)
668+
669+
// The available change that can cover fees is the total
670+
// selected deposit amount minus the selected withdrawal amount.
671+
change := totalWithdrawalAmount - selectedWithdrawalAmount
672+
673+
switch {
674+
case change-feeWithChange >= dustLimit:
675+
// If the change can cover the fees without turning into
676+
// dust, add a non-dust change output.
677+
hasChange = true
678+
changeAmount = change - feeWithChange
679+
withdrawalAmount = selectedWithdrawalAmount
680+
681+
case change-feeWithoutChange >= 0:
682+
// If the change is dust, we give it to the miners.
683+
hasChange = false
684+
withdrawalAmount = selectedWithdrawalAmount
685+
686+
default:
687+
// If the fees eat into our withdrawal amount, we fail
688+
// the withdrawal.
689+
return nil, 0, 0, fmt.Errorf("the change doesn't " +
690+
"cover for fees. Consider lowering the fee " +
691+
"rate or decrease the withdrawal amount")
692+
}
693+
} else {
694+
// If the user wants to withdraw the full amount, we don't need
695+
// a change output.
696+
hasChange = false
697+
withdrawalAmount = totalWithdrawalAmount - feeWithoutChange
661698
}
662699

663-
msgTx.AddTxOut(sweepOutput)
700+
if withdrawalAmount < dustLimit {
701+
return nil, 0, 0, fmt.Errorf("withdrawal amount is below " +
702+
"dust limit")
703+
}
704+
705+
if changeAmount < 0 {
706+
return nil, 0, 0, fmt.Errorf("change amount is negative")
707+
}
708+
709+
withdrawScript, err := txscript.PayToAddrScript(withdrawAddr)
710+
if err != nil {
711+
return nil, 0, 0, err
712+
}
713+
714+
// Create the withdrawal output.
715+
msgTx.AddTxOut(&wire.TxOut{
716+
Value: int64(withdrawalAmount),
717+
PkScript: withdrawScript,
718+
})
719+
720+
if hasChange {
721+
// Send change back to the same static address.
722+
staticAddress, err := m.cfg.AddressManager.GetStaticAddress(ctx)
723+
if err != nil {
724+
log.Errorf("error retrieving taproot address %w", err)
725+
726+
return nil, 0, 0, fmt.Errorf("withdrawal failed")
727+
}
728+
729+
changeAddress, err := btcutil.NewAddressTaproot(
730+
schnorr.SerializePubKey(staticAddress.TaprootKey),
731+
m.cfg.ChainParams,
732+
)
733+
if err != nil {
734+
return nil, 0, 0, err
735+
}
736+
737+
changeScript, err := txscript.PayToAddrScript(changeAddress)
738+
if err != nil {
739+
return nil, 0, 0, err
740+
}
664741

665-
return msgTx, nil
742+
msgTx.AddTxOut(&wire.TxOut{
743+
Value: int64(changeAmount),
744+
PkScript: changeScript,
745+
})
746+
}
747+
748+
return msgTx, withdrawalAmount, changeAmount, nil
666749
}
667750

668751
// withdrawalFee returns the weight for the withdrawal transaction.
669-
func withdrawalFee(numInputs int,
670-
sweepAddress btcutil.Address) (lntypes.WeightUnit, error) {
752+
func withdrawalTxWeight(numInputs int, sweepAddress btcutil.Address,
753+
hasChange bool) (lntypes.WeightUnit, error) {
671754

672755
var weightEstimator input.TxWeightEstimator
673756
for i := 0; i < numInputs; i++ {
@@ -689,6 +772,11 @@ func withdrawalFee(numInputs int,
689772
sweepAddress)
690773
}
691774

775+
// If there's a change output add the weight of the static address.
776+
if hasChange {
777+
weightEstimator.AddP2TROutput()
778+
}
779+
692780
return weightEstimator.Weight(), nil
693781
}
694782

@@ -827,13 +915,14 @@ func (m *Manager) republishWithdrawals(ctx context.Context) error {
827915
// DeliverWithdrawalRequest forwards a withdrawal request to the manager main
828916
// loop.
829917
func (m *Manager) DeliverWithdrawalRequest(ctx context.Context,
830-
outpoints []wire.OutPoint, destAddr string, satPerVbyte int64) (string,
831-
string, error) {
918+
outpoints []wire.OutPoint, destAddr string, satPerVbyte int64,
919+
amount int64) (string, string, error) {
832920

833921
request := newWithdrawalRequest{
834922
outpoints: outpoints,
835923
destAddr: destAddr,
836924
satPerVbyte: satPerVbyte,
925+
amount: amount,
837926
respChan: make(chan *newWithdrawalResponse),
838927
}
839928

0 commit comments

Comments
 (0)