Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion loopd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
Signer: d.lnd.Signer,
Store: withdrawalStore,
}
withdrawalManager = withdraw.NewManager(withdrawalCfg, blockHeight)
withdrawalManager = withdraw.NewManager(withdrawalCfg)

// Static address loop-in manager setup.
staticAddressLoopInStore := loopin.NewSqlStore(
Expand Down
11 changes: 11 additions & 0 deletions staticaddr/address/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ func (m *mockStaticAddressClient) ServerWithdrawDeposits(ctx context.Context,
args.Error(1)
}

func (m *mockStaticAddressClient) ServerPsbtWithdrawDeposits(ctx context.Context,
in *swapserverrpc.ServerPsbtWithdrawRequest,
opts ...grpc.CallOption) (*swapserverrpc.ServerPsbtWithdrawResponse,
error) {

args := m.Called(ctx, in, opts)

return args.Get(0).(*swapserverrpc.ServerPsbtWithdrawResponse),
args.Error(1)
}

func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context,
in *swapserverrpc.ServerNewAddressRequest, opts ...grpc.CallOption) (
*swapserverrpc.ServerNewAddressResponse, error) {
Expand Down
11 changes: 11 additions & 0 deletions staticaddr/deposit/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func (m *mockStaticAddressClient) ServerWithdrawDeposits(ctx context.Context,
args.Error(1)
}

func (m *mockStaticAddressClient) ServerPsbtWithdrawDeposits(ctx context.Context,
in *swapserverrpc.ServerPsbtWithdrawRequest,
opts ...grpc.CallOption) (*swapserverrpc.ServerPsbtWithdrawResponse,
error) {

args := m.Called(ctx, in, opts)

return args.Get(0).(*swapserverrpc.ServerPsbtWithdrawResponse),
args.Error(1)
}

func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context,
in *swapserverrpc.ServerNewAddressRequest, opts ...grpc.CallOption) (
*swapserverrpc.ServerNewAddressResponse, error) {
Expand Down
14 changes: 10 additions & 4 deletions staticaddr/loopin/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/fsm"
"github.com/lightninglabs/loop/staticaddr/deposit"
"github.com/lightninglabs/loop/staticaddr/staticutil"
"github.com/lightninglabs/loop/staticaddr/version"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/swapserverrpc"
Expand Down Expand Up @@ -318,8 +319,11 @@ func (f *FSM) SignHtlcTxAction(ctx context.Context,

// Create a musig2 session for each deposit and different htlc tx fee
// rates.
createSession := f.loopIn.createMusig2Sessions
htlcSessions, clientHtlcNonces, err := createSession(ctx, f.cfg.Signer)
createSession := staticutil.CreateMusig2Sessions
htlcSessions, clientHtlcNonces, err := createSession(
ctx, f.cfg.Signer, f.loopIn.Deposits, f.loopIn.AddressParams,
f.loopIn.Address,
)
if err != nil {
err = fmt.Errorf("unable to create musig2 sessions: %w", err)

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

htlcSessionsHighFee, highFeeNonces, err := createSession(
ctx, f.cfg.Signer,
ctx, f.cfg.Signer, f.loopIn.Deposits, f.loopIn.AddressParams,
f.loopIn.Address,
)
if err != nil {
return f.HandleError(err)
}
defer f.cleanUpSessions(ctx, htlcSessionsHighFee)

htlcSessionsExtremelyHighFee, extremelyHighNonces, err := createSession(
ctx, f.cfg.Signer,
ctx, f.cfg.Signer, f.loopIn.Deposits, f.loopIn.AddressParams,
f.loopIn.Address,
)
if err != nil {
err = fmt.Errorf("unable to convert nonces: %w", err)
Expand Down
69 changes: 4 additions & 65 deletions staticaddr/loopin/loopin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/lightninglabs/loop/staticaddr/address"
"github.com/lightninglabs/loop/staticaddr/deposit"
"github.com/lightninglabs/loop/staticaddr/script"
"github.com/lightninglabs/loop/staticaddr/staticutil"
"github.com/lightninglabs/loop/staticaddr/version"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/input"
Expand Down Expand Up @@ -169,55 +170,16 @@ func (l *StaticAddressLoopIn) getHtlc(chainParams *chaincfg.Params) (*swap.Htlc,
)
}

// createMusig2Sessions creates a musig2 session for a number of deposits.
func (l *StaticAddressLoopIn) createMusig2Sessions(ctx context.Context,
signer lndclient.SignerClient) ([]*input.MuSig2SessionInfo, [][]byte,
error) {

musig2Sessions := make([]*input.MuSig2SessionInfo, len(l.Deposits))
clientNonces := make([][]byte, len(l.Deposits))

// Create the sessions and nonces from the deposits.
for i := 0; i < len(l.Deposits); i++ {
session, err := l.createMusig2Session(ctx, signer)
if err != nil {
return nil, nil, err
}

musig2Sessions[i] = session
clientNonces[i] = session.PublicNonce[:]
}

return musig2Sessions, clientNonces, nil
}

// Musig2CreateSession creates a musig2 session for the deposit.
func (l *StaticAddressLoopIn) createMusig2Session(ctx context.Context,
signer lndclient.SignerClient) (*input.MuSig2SessionInfo, error) {

signers := [][]byte{
l.AddressParams.ClientPubkey.SerializeCompressed(),
l.AddressParams.ServerPubkey.SerializeCompressed(),
}

expiryLeaf := l.Address.TimeoutLeaf

rootHash := expiryLeaf.TapHash()

return signer.MuSig2CreateSession(
ctx, input.MuSig2Version100RC2, &l.AddressParams.KeyLocator,
signers, lndclient.MuSig2TaprootTweakOpt(rootHash[:], false),
)
}

// signMusig2Tx adds the server nonces to the musig2 sessions and signs the
// transaction.
func (l *StaticAddressLoopIn) signMusig2Tx(ctx context.Context,
tx *wire.MsgTx, signer lndclient.SignerClient,
musig2sessions []*input.MuSig2SessionInfo,
counterPartyNonces [][musig2.PubNonceSize]byte) ([][]byte, error) {

prevOuts, err := l.toPrevOuts(l.Deposits, l.AddressParams.PkScript)
prevOuts, err := staticutil.ToPrevOuts(
l.Deposits, l.AddressParams.PkScript,
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -523,29 +485,6 @@ func (l *StaticAddressLoopIn) Outpoints() []wire.OutPoint {
return outpoints
}

func (l *StaticAddressLoopIn) toPrevOuts(deposits []*deposit.Deposit,
pkScript []byte) (map[wire.OutPoint]*wire.TxOut, error) {

prevOuts := make(map[wire.OutPoint]*wire.TxOut, len(deposits))
for _, d := range deposits {
outpoint := wire.OutPoint{
Hash: d.Hash,
Index: d.Index,
}
txOut := &wire.TxOut{
Value: int64(d.Value),
PkScript: pkScript,
}
if _, ok := prevOuts[outpoint]; ok {
return nil, fmt.Errorf("duplicate outpoint %v",
outpoint)
}
prevOuts[outpoint] = txOut
}

return prevOuts, nil
}

// GetState returns the current state of the loop-in swap.
func (l *StaticAddressLoopIn) GetState() fsm.StateType {
l.mu.Lock()
Expand Down
5 changes: 3 additions & 2 deletions staticaddr/loopin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/lightninglabs/loop/labels"
"github.com/lightninglabs/loop/staticaddr/address"
"github.com/lightninglabs/loop/staticaddr/deposit"
"github.com/lightninglabs/loop/staticaddr/staticutil"
"github.com/lightninglabs/loop/swapserverrpc"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lntypes"
Expand Down Expand Up @@ -386,8 +387,8 @@ func (m *Manager) handleLoopInSweepReq(ctx context.Context,
)

copy(serverNonce[:], nonce)
musig2Session, err := loopIn.createMusig2Session(
ctx, m.cfg.Signer,
musig2Session, err := staticutil.CreateMusig2Session(
ctx, m.cfg.Signer, loopIn.AddressParams, loopIn.Address,
)
if err != nil {
return err
Expand Down
25 changes: 25 additions & 0 deletions staticaddr/staticutil/outpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package staticutil

import (
"fmt"

"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/lnrpc"
)

// ToWireOutpoints converts lnrpc.OutPoint protos into wire.OutPoint structs so
// they can be consumed by lower level transaction building code.
func ToWireOutpoints(outpoints []*lnrpc.OutPoint) ([]wire.OutPoint, error) {
serverOutpoints := make([]wire.OutPoint, 0, len(outpoints))
for _, o := range outpoints {
outpointStr := fmt.Sprintf("%s:%d", o.TxidStr, o.OutputIndex)
newOutpoint, err := wire.NewOutPointFromString(outpointStr)
if err != nil {
return nil, err
}

serverOutpoints = append(serverOutpoints, *newOutpoint)
}

return serverOutpoints, nil
}
Loading