Skip to content

Commit aaa7a96

Browse files
committed
staticaddr: remove GetStaticAddress from address manager
1 parent b9a201e commit aaa7a96

File tree

10 files changed

+67
-96
lines changed

10 files changed

+67
-96
lines changed

staticaddr/address/manager.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -390,27 +390,6 @@ func (m *Manager) GetStaticAddressID(ctx context.Context,
390390
return m.cfg.Store.GetStaticAddressID(ctx, pkScript)
391391
}
392392

393-
// GetStaticAddress returns a taproot address for the given client and server
394-
// public keys and expiry.
395-
func (m *Manager) GetStaticAddress(ctx context.Context) (*script.StaticAddress,
396-
error) {
397-
398-
params, err := m.GetStaticAddressParameters(ctx)
399-
if err != nil {
400-
return nil, err
401-
}
402-
403-
address, err := script.NewStaticAddress(
404-
input.MuSig2Version100RC2, int64(params.Expiry),
405-
params.ClientPubkey, params.ServerPubkey,
406-
)
407-
if err != nil {
408-
return nil, err
409-
}
410-
411-
return address, nil
412-
}
413-
414393
// ListUnspent returns a list of utxos at the static address.
415394
func (m *Manager) ListUnspent(ctx context.Context, minConfs,
416395
maxConfs int32) ([]*lnwallet.Utxo, error) {

staticaddr/deposit/actions.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,25 @@ func (f *FSM) PublishDepositExpirySweepAction(ctx context.Context,
6565

6666
prevOut := []*wire.TxOut{txOut}
6767

68-
signDesc, err := f.SignDescriptor(ctx)
68+
addressScript, err := f.deposit.GetStaticAddressScript()
6969
if err != nil {
7070
return f.HandleError(err)
7171
}
7272

73-
rawSigs, err := f.cfg.Signer.SignOutputRaw(
74-
ctx, msgTx, []*lndclient.SignDescriptor{signDesc}, prevOut,
75-
)
73+
signDesc, err := f.SignDescriptor(addressScript)
7674
if err != nil {
7775
return f.HandleError(err)
7876
}
7977

80-
address, err := f.cfg.AddressManager.GetStaticAddress(ctx)
78+
rawSigs, err := f.cfg.Signer.SignOutputRaw(
79+
ctx, msgTx, []*lndclient.SignDescriptor{signDesc}, prevOut,
80+
)
8181
if err != nil {
8282
return f.HandleError(err)
8383
}
8484

8585
sig := rawSigs[0]
86-
msgTx.TxIn[0].Witness, err = address.GenTimeoutWitness(sig)
86+
msgTx.TxIn[0].Witness, err = addressScript.GenTimeoutWitness(sig)
8787
if err != nil {
8888
return f.HandleError(err)
8989
}

staticaddr/deposit/fsm.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package deposit
33
import (
44
"context"
55
"errors"
6+
"fmt"
67

78
"github.com/btcsuite/btcd/txscript"
89
"github.com/btcsuite/btcd/wire"
910
"github.com/lightninglabs/lndclient"
1011
"github.com/lightninglabs/loop/fsm"
1112
"github.com/lightninglabs/loop/staticaddr/address"
13+
"github.com/lightninglabs/loop/staticaddr/script"
1214
"github.com/lightninglabs/loop/staticaddr/version"
1315
"github.com/lightningnetwork/lnd/input"
1416
"github.com/lightningnetwork/lnd/keychain"
@@ -448,16 +450,15 @@ func (f *FSM) Errorf(format string, args ...interface{}) {
448450
}
449451

450452
// SignDescriptor returns the sign descriptor for the static address output.
451-
func (f *FSM) SignDescriptor(ctx context.Context) (*lndclient.SignDescriptor,
452-
error) {
453+
func (f *FSM) SignDescriptor(addressScript *script.StaticAddress) (
454+
*lndclient.SignDescriptor, error) {
453455

454-
address, err := f.cfg.AddressManager.GetStaticAddress(ctx)
455-
if err != nil {
456-
return nil, err
456+
if addressScript == nil {
457+
return nil, fmt.Errorf("address script is nil")
457458
}
458459

459460
return &lndclient.SignDescriptor{
460-
WitnessScript: address.TimeoutLeaf.Script,
461+
WitnessScript: addressScript.TimeoutLeaf.Script,
461462
KeyDesc: keychain.KeyDescriptor{
462463
PubKey: f.params.ClientPubkey,
463464
},

staticaddr/deposit/interface.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55

66
"github.com/lightninglabs/loop/staticaddr/address"
7-
"github.com/lightninglabs/loop/staticaddr/script"
87
"github.com/lightningnetwork/lnd/lnwallet"
98
)
109

@@ -42,10 +41,6 @@ type AddressManager interface {
4241
// pkScript.
4342
GetParameters(pkScript []byte) *address.Parameters
4443

45-
// GetStaticAddress returns the deposit address for the given
46-
// client and server public keys.
47-
GetStaticAddress(ctx context.Context) (*script.StaticAddress, error)
48-
4944
// ListUnspent returns a list of utxos at the static address.
5045
ListUnspent(ctx context.Context, minConfs,
5146
maxConfs int32) ([]*lnwallet.Utxo, error)

staticaddr/loopin/actions.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,6 @@ func (f *FSM) SignHtlcTxAction(ctx context.Context,
308308
return f.HandleError(err)
309309
}
310310

311-
f.loopIn.Address, err = f.cfg.AddressManager.GetStaticAddress(ctx)
312-
if err != nil {
313-
err = fmt.Errorf("unable to get static address: %w", err)
314-
315-
return f.HandleError(err)
316-
}
317-
318311
// Create a musig2 session for each deposit and different htlc tx fee
319312
// rates.
320313
createSession := f.loopIn.createMusig2Sessions

staticaddr/loopin/interface.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/lightninglabs/loop/fsm"
99
"github.com/lightninglabs/loop/staticaddr/address"
1010
"github.com/lightninglabs/loop/staticaddr/deposit"
11-
"github.com/lightninglabs/loop/staticaddr/script"
1211
"github.com/lightninglabs/loop/swapserverrpc"
1312
"github.com/lightningnetwork/lnd/lntypes"
1413
"github.com/lightningnetwork/lnd/routing/route"
@@ -26,10 +25,6 @@ type AddressManager interface {
2625
// GetStaticAddressParameters returns the static address parameters.
2726
GetStaticAddressParameters(ctx context.Context) (*address.Parameters,
2827
error)
29-
30-
// GetStaticAddress returns the deposit address for the given client and
31-
// server public keys.
32-
GetStaticAddress(ctx context.Context) (*script.StaticAddress, error)
3328
}
3429

3530
// DepositManager handles the interaction of loop-ins with deposits.

staticaddr/loopin/loopin.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/lightninglabs/loop/fsm"
2121
"github.com/lightninglabs/loop/staticaddr/address"
2222
"github.com/lightninglabs/loop/staticaddr/deposit"
23-
"github.com/lightninglabs/loop/staticaddr/script"
2423
"github.com/lightninglabs/loop/staticaddr/version"
2524
"github.com/lightninglabs/loop/swap"
2625
"github.com/lightningnetwork/lnd/input"
@@ -131,9 +130,6 @@ type StaticAddressLoopIn struct {
131130
// swap.
132131
AddressParams *address.Parameters
133132

134-
// Address is the address script that is used for the swap.
135-
Address *script.StaticAddress
136-
137133
// HTLC fields.
138134

139135
// HtlcTxFeeRate is the fee rate that is used for the htlc transaction.
@@ -175,7 +171,9 @@ func (l *StaticAddressLoopIn) createMusig2Sessions(ctx context.Context,
175171

176172
// Create the sessions and nonces from the deposits.
177173
for i := 0; i < len(l.Deposits); i++ {
178-
session, err := l.createMusig2Session(ctx, signer)
174+
session, err := l.createMusig2Session(
175+
ctx, signer, l.Deposits[i],
176+
)
179177
if err != nil {
180178
return nil, nil, err
181179
}
@@ -189,14 +187,22 @@ func (l *StaticAddressLoopIn) createMusig2Sessions(ctx context.Context,
189187

190188
// Musig2CreateSession creates a musig2 session for the deposit.
191189
func (l *StaticAddressLoopIn) createMusig2Session(ctx context.Context,
192-
signer lndclient.SignerClient) (*input.MuSig2SessionInfo, error) {
190+
signer lndclient.SignerClient, deposit *deposit.Deposit) (
191+
*input.MuSig2SessionInfo, error) {
192+
193+
addrParams := deposit.AddressParams
193194

194195
signers := [][]byte{
195-
l.AddressParams.ClientPubkey.SerializeCompressed(),
196-
l.AddressParams.ServerPubkey.SerializeCompressed(),
196+
addrParams.ClientPubkey.SerializeCompressed(),
197+
addrParams.ServerPubkey.SerializeCompressed(),
198+
}
199+
200+
addrScript, err := deposit.GetStaticAddressScript()
201+
if err != nil {
202+
return nil, err
197203
}
198204

199-
expiryLeaf := l.Address.TimeoutLeaf
205+
expiryLeaf := addrScript.TimeoutLeaf
200206

201207
rootHash := expiryLeaf.TapHash()
202208

staticaddr/loopin/manager.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,6 @@ func (m *Manager) handleLoopInSweepReq(ctx context.Context,
283283
return err
284284
}
285285

286-
loopIn.Address, err = m.cfg.AddressManager.GetStaticAddress(ctx)
287-
if err != nil {
288-
return err
289-
}
290-
291-
deposits, err := m.cfg.DepositManager.DepositsForOutpoints(
292-
ctx, loopIn.DepositOutpoints,
293-
)
294-
if err != nil {
295-
return err
296-
}
297-
loopIn.Deposits = deposits
298-
299286
reader := bytes.NewReader(req.SweepTxPsbt)
300287
sweepPacket, err := psbt.NewFromRawBytes(reader, false)
301288
if err != nil {
@@ -304,7 +291,7 @@ func (m *Manager) handleLoopInSweepReq(ctx context.Context,
304291

305292
sweepTx := sweepPacket.UnsignedTx
306293

307-
// If the loop-in is not in the Succeeded state we return an
294+
// If the loop-in is not in the Succeeded state, we return an
308295
// error.
309296
if !loopIn.IsInState(Succeeded) {
310297
// We'll notify the server that we don't consider the swap
@@ -322,8 +309,8 @@ func (m *Manager) handleLoopInSweepReq(ctx context.Context,
322309
}
323310

324311
// If the user selected an amount that is less than the total deposit
325-
// amount we'll check that the server sends us the correct change amount
326-
// back to our static address.
312+
// amount, we'll check that the server sends us the correct change
313+
// amount back to our static address.
327314
totalDepositAmount := loopIn.TotalDepositAmount()
328315
changeAmt := totalDepositAmount - loopIn.SelectedAmount
329316
if changeAmt > 0 && changeAmt < totalDepositAmount {
@@ -404,8 +391,21 @@ func (m *Manager) handleLoopInSweepReq(ctx context.Context,
404391
)
405392

406393
copy(serverNonce[:], nonce)
394+
395+
deposit, err := m.cfg.DepositManager.DepositsForOutpoints(
396+
ctx, []string{depositOutpoint},
397+
)
398+
if err != nil {
399+
return err
400+
}
401+
if len(deposit) != 1 {
402+
return fmt.Errorf("expected 1 deposit for "+
403+
"outpoint %v, got %v", depositOutpoint,
404+
len(deposit))
405+
}
406+
407407
musig2Session, err := loopIn.createMusig2Session(
408-
ctx, m.cfg.Signer,
408+
ctx, m.cfg.Signer, deposit[0],
409409
)
410410
if err != nil {
411411
return err
@@ -484,7 +484,7 @@ func (m *Manager) recoverLoopIns(ctx context.Context) error {
484484

485485
// Retrieve all deposits regardless of deposit state. If any of
486486
// the deposits is not active in the in-mem map of the deposits
487-
// manager we log it, but continue to recover the loop-in.
487+
// manager, we log it but continue to recover the loop-in.
488488
var allActive bool
489489
loopIn.Deposits, allActive =
490490
m.cfg.DepositManager.AllStringOutpointsActiveDeposits(
@@ -502,13 +502,6 @@ func (m *Manager) recoverLoopIns(ctx context.Context) error {
502502
return err
503503
}
504504

505-
loopIn.Address, err = m.cfg.AddressManager.GetStaticAddress(
506-
ctx,
507-
)
508-
if err != nil {
509-
return err
510-
}
511-
512505
// Create a state machine for a given loop-in.
513506
var (
514507
recovery = true

staticaddr/withdraw/interface.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/lightninglabs/loop/fsm"
88
"github.com/lightninglabs/loop/staticaddr/address"
99
"github.com/lightninglabs/loop/staticaddr/deposit"
10-
"github.com/lightninglabs/loop/staticaddr/script"
1110
)
1211

1312
// Store is the database interface that is used to store and retrieve
@@ -28,10 +27,6 @@ type AddressManager interface {
2827
GetStaticAddressParameters(ctx context.Context) (*address.Parameters,
2928
error)
3029

31-
// GetStaticAddress returns the deposit address for the given
32-
// client and server public keys.
33-
GetStaticAddress(ctx context.Context) (*script.StaticAddress, error)
34-
3530
// GetDefaultParameters returns the default static address parameters.
3631
// They are used when no other parameters are available, e.g. for
3732
// change outputs of swaps or withdrawals.

staticaddr/withdraw/manager.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/btcsuite/btcwallet/chain"
2020
"github.com/lightninglabs/lndclient"
2121
"github.com/lightninglabs/loop/staticaddr/deposit"
22+
"github.com/lightninglabs/loop/staticaddr/script"
2223
staticaddressrpc "github.com/lightninglabs/loop/swapserverrpc"
2324
"github.com/lightningnetwork/lnd/chainntnfs"
2425
"github.com/lightningnetwork/lnd/input"
@@ -925,15 +926,26 @@ func (m *Manager) createWithdrawalTx(ctx context.Context,
925926

926927
if hasChange {
927928
// Send change back to the same static address.
928-
staticAddress, err := m.cfg.AddressManager.GetStaticAddress(ctx)
929+
defaultParams, err := m.cfg.AddressManager.GetDefaultParameters(
930+
ctx,
931+
)
929932
if err != nil {
930-
log.Errorf("error retrieving taproot address %v", err)
933+
log.Errorf("error retrieving default address "+
934+
"parameters %v", err)
931935

932936
return nil, 0, 0, fmt.Errorf("withdrawal failed")
933937
}
934938

939+
addressScript, err := script.NewStaticAddress(
940+
input.MuSig2Version100RC2, int64(defaultParams.Expiry),
941+
defaultParams.ClientPubkey, defaultParams.ServerPubkey,
942+
)
943+
if err != nil {
944+
return nil, 0, 0, err
945+
}
946+
935947
changeAddress, err := btcutil.NewAddressTaproot(
936-
schnorr.SerializePubKey(staticAddress.TaprootKey),
948+
schnorr.SerializePubKey(addressScript.TaprootKey),
937949
m.cfg.ChainParams,
938950
)
939951
if err != nil {
@@ -1065,13 +1077,15 @@ func (m *Manager) createMusig2Session(ctx context.Context) (
10651077
addressParams.ServerPubkey.SerializeCompressed(),
10661078
}
10671079

1068-
address, err := m.cfg.AddressManager.GetStaticAddress(ctx)
1080+
addressScript, err := script.NewStaticAddress(
1081+
input.MuSig2Version100RC2, int64(addressParams.Expiry),
1082+
addressParams.ClientPubkey, addressParams.ServerPubkey,
1083+
)
10691084
if err != nil {
1070-
return nil, fmt.Errorf("couldn't get confirmation height for "+
1071-
"deposit, %w", err)
1085+
return nil, err
10721086
}
10731087

1074-
expiryLeaf := address.TimeoutLeaf
1088+
expiryLeaf := addressScript.TimeoutLeaf
10751089

10761090
rootHash := expiryLeaf.TapHash()
10771091

0 commit comments

Comments
 (0)