Skip to content

Commit 28d2a3f

Browse files
committed
staticaddr: refactor methods into utils
1 parent 801fdc9 commit 28d2a3f

File tree

9 files changed

+766
-360
lines changed

9 files changed

+766
-360
lines changed

staticaddr/address/manager_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ func (m *mockStaticAddressClient) ServerWithdrawDeposits(ctx context.Context,
7676
args.Error(1)
7777
}
7878

79+
func (m *mockStaticAddressClient) ServerPsbtWithdrawDeposits(ctx context.Context,
80+
in *swapserverrpc.ServerPsbtWithdrawRequest,
81+
opts ...grpc.CallOption) (*swapserverrpc.ServerPsbtWithdrawResponse,
82+
error) {
83+
84+
args := m.Called(ctx, in, opts)
85+
86+
return args.Get(0).(*swapserverrpc.ServerPsbtWithdrawResponse),
87+
args.Error(1)
88+
}
89+
7990
func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context,
8091
in *swapserverrpc.ServerNewAddressRequest, opts ...grpc.CallOption) (
8192
*swapserverrpc.ServerNewAddressResponse, error) {

staticaddr/deposit/manager_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ func (m *mockStaticAddressClient) ServerWithdrawDeposits(ctx context.Context,
9393
args.Error(1)
9494
}
9595

96+
func (m *mockStaticAddressClient) ServerPsbtWithdrawDeposits(ctx context.Context,
97+
in *swapserverrpc.ServerPsbtWithdrawRequest,
98+
opts ...grpc.CallOption) (*swapserverrpc.ServerPsbtWithdrawResponse,
99+
error) {
100+
101+
args := m.Called(ctx, in, opts)
102+
103+
return args.Get(0).(*swapserverrpc.ServerPsbtWithdrawResponse),
104+
args.Error(1)
105+
}
106+
96107
func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context,
97108
in *swapserverrpc.ServerNewAddressRequest, opts ...grpc.CallOption) (
98109
*swapserverrpc.ServerNewAddressResponse, error) {

staticaddr/loopin/actions.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/lightninglabs/loop"
1818
"github.com/lightninglabs/loop/fsm"
1919
"github.com/lightninglabs/loop/staticaddr/deposit"
20+
"github.com/lightninglabs/loop/staticaddr/staticutil"
2021
"github.com/lightninglabs/loop/staticaddr/version"
2122
"github.com/lightninglabs/loop/swap"
2223
"github.com/lightninglabs/loop/swapserverrpc"
@@ -318,8 +319,11 @@ func (f *FSM) SignHtlcTxAction(ctx context.Context,
318319

319320
// Create a musig2 session for each deposit and different htlc tx fee
320321
// rates.
321-
createSession := f.loopIn.createMusig2Sessions
322-
htlcSessions, clientHtlcNonces, err := createSession(ctx, f.cfg.Signer)
322+
createSession := staticutil.CreateMusig2Sessions
323+
htlcSessions, clientHtlcNonces, err := createSession(
324+
ctx, f.cfg.Signer, f.loopIn.Deposits, f.loopIn.AddressParams,
325+
f.loopIn.Address,
326+
)
323327
if err != nil {
324328
err = fmt.Errorf("unable to create musig2 sessions: %w", err)
325329

@@ -328,15 +332,17 @@ func (f *FSM) SignHtlcTxAction(ctx context.Context,
328332
defer f.cleanUpSessions(ctx, htlcSessions)
329333

330334
htlcSessionsHighFee, highFeeNonces, err := createSession(
331-
ctx, f.cfg.Signer,
335+
ctx, f.cfg.Signer, f.loopIn.Deposits, f.loopIn.AddressParams,
336+
f.loopIn.Address,
332337
)
333338
if err != nil {
334339
return f.HandleError(err)
335340
}
336341
defer f.cleanUpSessions(ctx, htlcSessionsHighFee)
337342

338343
htlcSessionsExtremelyHighFee, extremelyHighNonces, err := createSession(
339-
ctx, f.cfg.Signer,
344+
ctx, f.cfg.Signer, f.loopIn.Deposits, f.loopIn.AddressParams,
345+
f.loopIn.Address,
340346
)
341347
if err != nil {
342348
err = fmt.Errorf("unable to convert nonces: %w", err)

staticaddr/loopin/loopin.go

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/lightninglabs/loop/staticaddr/address"
2222
"github.com/lightninglabs/loop/staticaddr/deposit"
2323
"github.com/lightninglabs/loop/staticaddr/script"
24+
"github.com/lightninglabs/loop/staticaddr/staticutil"
2425
"github.com/lightninglabs/loop/staticaddr/version"
2526
"github.com/lightninglabs/loop/swap"
2627
"github.com/lightningnetwork/lnd/input"
@@ -169,55 +170,16 @@ func (l *StaticAddressLoopIn) getHtlc(chainParams *chaincfg.Params) (*swap.Htlc,
169170
)
170171
}
171172

172-
// createMusig2Sessions creates a musig2 session for a number of deposits.
173-
func (l *StaticAddressLoopIn) createMusig2Sessions(ctx context.Context,
174-
signer lndclient.SignerClient) ([]*input.MuSig2SessionInfo, [][]byte,
175-
error) {
176-
177-
musig2Sessions := make([]*input.MuSig2SessionInfo, len(l.Deposits))
178-
clientNonces := make([][]byte, len(l.Deposits))
179-
180-
// Create the sessions and nonces from the deposits.
181-
for i := 0; i < len(l.Deposits); i++ {
182-
session, err := l.createMusig2Session(ctx, signer)
183-
if err != nil {
184-
return nil, nil, err
185-
}
186-
187-
musig2Sessions[i] = session
188-
clientNonces[i] = session.PublicNonce[:]
189-
}
190-
191-
return musig2Sessions, clientNonces, nil
192-
}
193-
194-
// Musig2CreateSession creates a musig2 session for the deposit.
195-
func (l *StaticAddressLoopIn) createMusig2Session(ctx context.Context,
196-
signer lndclient.SignerClient) (*input.MuSig2SessionInfo, error) {
197-
198-
signers := [][]byte{
199-
l.AddressParams.ClientPubkey.SerializeCompressed(),
200-
l.AddressParams.ServerPubkey.SerializeCompressed(),
201-
}
202-
203-
expiryLeaf := l.Address.TimeoutLeaf
204-
205-
rootHash := expiryLeaf.TapHash()
206-
207-
return signer.MuSig2CreateSession(
208-
ctx, input.MuSig2Version100RC2, &l.AddressParams.KeyLocator,
209-
signers, lndclient.MuSig2TaprootTweakOpt(rootHash[:], false),
210-
)
211-
}
212-
213173
// signMusig2Tx adds the server nonces to the musig2 sessions and signs the
214174
// transaction.
215175
func (l *StaticAddressLoopIn) signMusig2Tx(ctx context.Context,
216176
tx *wire.MsgTx, signer lndclient.SignerClient,
217177
musig2sessions []*input.MuSig2SessionInfo,
218178
counterPartyNonces [][musig2.PubNonceSize]byte) ([][]byte, error) {
219179

220-
prevOuts, err := l.toPrevOuts(l.Deposits, l.AddressParams.PkScript)
180+
prevOuts, err := staticutil.ToPrevOuts(
181+
l.Deposits, l.AddressParams.PkScript,
182+
)
221183
if err != nil {
222184
return nil, err
223185
}
@@ -523,29 +485,6 @@ func (l *StaticAddressLoopIn) Outpoints() []wire.OutPoint {
523485
return outpoints
524486
}
525487

526-
func (l *StaticAddressLoopIn) toPrevOuts(deposits []*deposit.Deposit,
527-
pkScript []byte) (map[wire.OutPoint]*wire.TxOut, error) {
528-
529-
prevOuts := make(map[wire.OutPoint]*wire.TxOut, len(deposits))
530-
for _, d := range deposits {
531-
outpoint := wire.OutPoint{
532-
Hash: d.Hash,
533-
Index: d.Index,
534-
}
535-
txOut := &wire.TxOut{
536-
Value: int64(d.Value),
537-
PkScript: pkScript,
538-
}
539-
if _, ok := prevOuts[outpoint]; ok {
540-
return nil, fmt.Errorf("duplicate outpoint %v",
541-
outpoint)
542-
}
543-
prevOuts[outpoint] = txOut
544-
}
545-
546-
return prevOuts, nil
547-
}
548-
549488
// GetState returns the current state of the loop-in swap.
550489
func (l *StaticAddressLoopIn) GetState() fsm.StateType {
551490
l.mu.Lock()

staticaddr/loopin/manager.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/lightninglabs/loop/labels"
2222
"github.com/lightninglabs/loop/staticaddr/address"
2323
"github.com/lightninglabs/loop/staticaddr/deposit"
24+
"github.com/lightninglabs/loop/staticaddr/staticutil"
2425
"github.com/lightninglabs/loop/swapserverrpc"
2526
"github.com/lightningnetwork/lnd/input"
2627
"github.com/lightningnetwork/lnd/lntypes"
@@ -386,8 +387,8 @@ func (m *Manager) handleLoopInSweepReq(ctx context.Context,
386387
)
387388

388389
copy(serverNonce[:], nonce)
389-
musig2Session, err := loopIn.createMusig2Session(
390-
ctx, m.cfg.Signer,
390+
musig2Session, err := staticutil.CreateMusig2Session(
391+
ctx, m.cfg.Signer, loopIn.AddressParams, loopIn.Address,
391392
)
392393
if err != nil {
393394
return err

staticaddr/staticutil/outpoints.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package staticutil
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/btcsuite/btcd/wire"
7+
"github.com/lightningnetwork/lnd/lnrpc"
8+
)
9+
10+
// ToWireOutpoints converts lnrpc.OutPoint protos into wire.OutPoint structs so
11+
// they can be consumed by lower level transaction building code.
12+
func ToWireOutpoints(outpoints []*lnrpc.OutPoint) ([]wire.OutPoint, error) {
13+
serverOutpoints := make([]wire.OutPoint, 0, len(outpoints))
14+
for _, o := range outpoints {
15+
outpointStr := fmt.Sprintf("%s:%d", o.TxidStr, o.OutputIndex)
16+
newOutpoint, err := wire.NewOutPointFromString(outpointStr)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
serverOutpoints = append(serverOutpoints, *newOutpoint)
22+
}
23+
24+
return serverOutpoints, nil
25+
}

0 commit comments

Comments
 (0)