Skip to content

Commit 732376d

Browse files
committed
staticutil: channel open functions
1 parent 3349139 commit 732376d

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

staticaddr/loopin/loopin.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lightninglabs/loop/staticaddr/address"
2121
"github.com/lightninglabs/loop/staticaddr/deposit"
2222
"github.com/lightninglabs/loop/staticaddr/script"
23+
"github.com/lightninglabs/loop/staticaddr/staticutil"
2324
"github.com/lightninglabs/loop/staticaddr/version"
2425
"github.com/lightninglabs/loop/swap"
2526
"github.com/lightningnetwork/lnd/input"
@@ -205,7 +206,9 @@ func (l *StaticAddressLoopIn) signMusig2Tx(ctx context.Context,
205206
musig2sessions []*input.MuSig2SessionInfo,
206207
counterPartyNonces [][musig2.PubNonceSize]byte) ([][]byte, error) {
207208

208-
prevOuts, err := l.toPrevOuts(l.Deposits, l.AddressParams.PkScript)
209+
prevOuts, err := staticutil.ToPrevOuts(
210+
l.Deposits, l.AddressParams.PkScript,
211+
)
209212
if err != nil {
210213
return nil, err
211214
}
@@ -474,29 +477,6 @@ func (l *StaticAddressLoopIn) Outpoints() []wire.OutPoint {
474477
return outpoints
475478
}
476479

477-
func (l *StaticAddressLoopIn) toPrevOuts(deposits []*deposit.Deposit,
478-
pkScript []byte) (map[wire.OutPoint]*wire.TxOut, error) {
479-
480-
prevOuts := make(map[wire.OutPoint]*wire.TxOut, len(deposits))
481-
for _, d := range deposits {
482-
outpoint := wire.OutPoint{
483-
Hash: d.Hash,
484-
Index: d.Index,
485-
}
486-
txOut := &wire.TxOut{
487-
Value: int64(d.Value),
488-
PkScript: pkScript,
489-
}
490-
if _, ok := prevOuts[outpoint]; ok {
491-
return nil, fmt.Errorf("duplicate outpoint %v",
492-
outpoint)
493-
}
494-
prevOuts[outpoint] = txOut
495-
}
496-
497-
return prevOuts, nil
498-
}
499-
500480
// GetState returns the current state of the loop-in swap.
501481
func (l *StaticAddressLoopIn) GetState() fsm.StateType {
502482
l.mu.Lock()

staticaddr/staticutil/utils.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"sort"
88

9+
"github.com/btcsuite/btcd/btcutil"
910
"github.com/btcsuite/btcd/chaincfg/chainhash"
1011
"github.com/btcsuite/btcd/wire"
1112
"github.com/lightninglabs/lndclient"
@@ -14,6 +15,7 @@ import (
1415
"github.com/lightninglabs/loop/staticaddr/script"
1516
"github.com/lightninglabs/loop/swapserverrpc"
1617
"github.com/lightningnetwork/lnd/input"
18+
"github.com/lightningnetwork/lnd/lnwallet"
1719
)
1820

1921
func ToPrevOuts(deposits []*deposit.Deposit,
@@ -65,6 +67,36 @@ func CreateMusig2Sessions(ctx context.Context,
6567
return musig2Sessions, clientNonces, nil
6668
}
6769

70+
// CreateMusig2SessionsPerDeposit creates a musig2 session for a number of
71+
// deposits.
72+
func CreateMusig2SessionsPerDeposit(ctx context.Context,
73+
signer lndclient.SignerClient, deposits []*deposit.Deposit,
74+
addrParams *address.Parameters,
75+
staticAddress *script.StaticAddress) (
76+
map[string]*input.MuSig2SessionInfo, map[string][]byte, map[string]int,
77+
error) {
78+
79+
sessions := make(map[string]*input.MuSig2SessionInfo)
80+
nonces := make(map[string][]byte)
81+
depositToIdx := make(map[string]int)
82+
83+
// Create the musig2 sessions for the sweepless sweep tx.
84+
for i, deposit := range deposits {
85+
session, err := createMusig2Session(
86+
ctx, signer, addrParams, staticAddress,
87+
)
88+
if err != nil {
89+
return nil, nil, nil, err
90+
}
91+
92+
sessions[deposit.String()] = session
93+
nonces[deposit.String()] = session.PublicNonce[:]
94+
depositToIdx[deposit.String()] = i
95+
}
96+
97+
return sessions, nonces, depositToIdx, nil
98+
}
99+
68100
// createMusig2Session creates a musig2 session for the deposit.
69101
func createMusig2Session(ctx context.Context,
70102
signer lndclient.SignerClient, addrParams *address.Parameters,
@@ -131,3 +163,42 @@ func bip69inputLess(input1, input2 *swapserverrpc.PrevoutInfo) bool {
131163
}
132164
return bytes.Compare(ihash[:], jhash[:]) == -1
133165
}
166+
167+
// SelectDeposits sorts the deposits by amount in descending order. It then
168+
// selects the deposits that are needed to cover the amount requested without
169+
// leaving a dust change. It returns an error if the sum of deposits minus dust
170+
// is less than the requested amount.
171+
func SelectDeposits(deposits []*deposit.Deposit, amount int64) (
172+
[]*deposit.Deposit, error) {
173+
174+
// Check that sum of deposits covers the swap amount while leaving no
175+
// dust change.
176+
dustLimit := lnwallet.DustLimitForSize(input.P2TRSize)
177+
var depositSum btcutil.Amount
178+
for _, deposit := range deposits {
179+
depositSum += deposit.Value
180+
}
181+
if depositSum-dustLimit < btcutil.Amount(amount) {
182+
return nil, fmt.Errorf("insufficient funds to cover swap " +
183+
"amount, try manually selecting deposits")
184+
}
185+
186+
// Sort the deposits by amount in descending order.
187+
sort.Slice(deposits, func(i, j int) bool {
188+
return deposits[i].Value > deposits[j].Value
189+
})
190+
191+
// Select the deposits that are needed to cover the swap amount without
192+
// leaving a dust change.
193+
var selectedDeposits []*deposit.Deposit
194+
var selectedAmount btcutil.Amount
195+
for _, deposit := range deposits {
196+
if selectedAmount >= btcutil.Amount(amount)+dustLimit {
197+
break
198+
}
199+
selectedDeposits = append(selectedDeposits, deposit)
200+
selectedAmount += deposit.Value
201+
}
202+
203+
return selectedDeposits, nil
204+
}

staticaddr/withdraw/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/lightninglabs/loop/staticaddr/staticutil"
87
"reflect"
98
"strings"
109
"sync"
@@ -20,6 +19,7 @@ import (
2019
"github.com/btcsuite/btcwallet/chain"
2120
"github.com/lightninglabs/lndclient"
2221
"github.com/lightninglabs/loop/staticaddr/deposit"
22+
"github.com/lightninglabs/loop/staticaddr/staticutil"
2323
staticaddressrpc "github.com/lightninglabs/loop/swapserverrpc"
2424
"github.com/lightningnetwork/lnd/chainntnfs"
2525
"github.com/lightningnetwork/lnd/input"

0 commit comments

Comments
 (0)