Skip to content

Commit dad422d

Browse files
committed
staticaddr: remove GetStaticAddress and GetStaticAddressParameters
1 parent 158539e commit dad422d

27 files changed

+680
-556
lines changed

loopd/daemon.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,6 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
677677
LndClient: d.lnd.Client,
678678
InvoicesClient: d.lnd.Invoices,
679679
NodePubkey: d.lnd.NodePubkey,
680-
AddressManager: staticAddressManager,
681680
DepositManager: depositManager,
682681
Store: staticAddressLoopInStore,
683682
WalletKit: d.lnd.WalletKit,

loopd/swapclient_server.go

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -931,24 +931,13 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
931931
"deposits: %w", err)
932932
}
933933

934-
// TODO(hieblmi): add params to deposit for multi-address
935-
// support.
936-
params, err := s.staticAddressManager.GetStaticAddressParameters(
937-
ctx,
938-
)
939-
if err != nil {
940-
return nil, fmt.Errorf("unable to retrieve static "+
941-
"address parameters: %w", err)
942-
}
943-
944934
info, err := s.lnd.Client.GetInfo(ctx)
945935
if err != nil {
946936
return nil, fmt.Errorf("unable to get lnd info: %w",
947937
err)
948938
}
949939
selectedDeposits, err := loopin.SelectDeposits(
950-
selectedAmount, deposits, params.Expiry,
951-
info.BlockHeight,
940+
selectedAmount, deposits, info.BlockHeight,
952941
)
953942
if err != nil {
954943
return nil, fmt.Errorf("unable to select deposits: %w",
@@ -1792,6 +1781,11 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
17921781
return nil, err
17931782
}
17941783

1784+
lndInfo, err := s.lnd.Client.GetInfo(ctx)
1785+
if err != nil {
1786+
return nil, err
1787+
}
1788+
17951789
// Deposits filtered by state or outpoints.
17961790
var filteredDeposits []*looprpc.Deposit
17971791
if len(outpoints) > 0 {
@@ -1803,7 +1797,7 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
18031797
}
18041798
return false
18051799
}
1806-
filteredDeposits = filter(allDeposits, network, f)
1800+
filteredDeposits = filter(allDeposits, network, lndInfo, f)
18071801

18081802
if len(outpoints) != len(filteredDeposits) {
18091803
return nil, fmt.Errorf("not all outpoints found in " +
@@ -1819,7 +1813,7 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
18191813

18201814
return d.IsInState(toServerState(req.StateFilter))
18211815
}
1822-
filteredDeposits = filter(allDeposits, network, f)
1816+
filteredDeposits = filter(allDeposits, network, lndInfo, f)
18231817
}
18241818

18251819
// Calculate the blocks until expiry for each deposit.
@@ -1912,13 +1906,6 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
19121906
return nil, err
19131907
}
19141908

1915-
addrParams, err := s.staticAddressManager.GetStaticAddressParameters(
1916-
ctx,
1917-
)
1918-
if err != nil {
1919-
return nil, err
1920-
}
1921-
19221909
// Fetch all deposits at once and index them by swap hash for a quick
19231910
// lookup.
19241911
allDeposits, err := s.depositManager.GetAllDeposits(ctx)
@@ -1957,7 +1944,7 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
19571944
for _, d := range ds {
19581945
state := toClientDepositState(d.GetState())
19591946
blocksUntilExpiry := d.ConfirmationHeight +
1960-
int64(addrParams.Expiry) -
1947+
int64(d.AddressParams.Expiry) -
19611948
int64(lndInfo.BlockHeight)
19621949

19631950
pd := &looprpc.Deposit{
@@ -2102,7 +2089,7 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
21022089
}
21032090
}
21042091

2105-
deprecatedParams, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
2092+
deprecatedParams, err := s.staticAddressManager.GetLegacyParameters(ctx)
21062093
if err != nil {
21072094
return nil, err
21082095
}
@@ -2186,8 +2173,19 @@ func (s *swapClientServer) StaticAddressLoopIn(ctx context.Context,
21862173
}
21872174

21882175
// Build a list of used deposits for the response.
2176+
network, err := s.network.ChainParams()
2177+
if err != nil {
2178+
return nil, err
2179+
}
2180+
2181+
lndInfo, err := s.lnd.Client.GetInfo(ctx)
2182+
if err != nil {
2183+
return nil, err
2184+
}
2185+
21892186
usedDeposits := filter(
2190-
loopIn.Deposits, func(d *deposit.Deposit) bool { return true },
2187+
loopIn.Deposits, network, lndInfo,
2188+
func(d *deposit.Deposit) bool { return true },
21912189
)
21922190

21932191
err = s.populateBlocksUntilExpiry(ctx, usedDeposits)
@@ -2240,14 +2238,10 @@ func (s *swapClientServer) populateBlocksUntilExpiry(ctx context.Context,
22402238
}
22412239

22422240
bestBlockHeight := int64(lndInfo.BlockHeight)
2243-
params, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
2244-
if err != nil {
2245-
return err
2246-
}
22472241
for i := 0; i < len(deposits); i++ {
22482242
deposits[i].BlocksUntilExpiry =
22492243
deposits[i].ConfirmationHeight +
2250-
int64(params.Expiry) - bestBlockHeight
2244+
deposits[i].BlocksUntilExpiry - bestBlockHeight
22512245
}
22522246
return nil
22532247
}
@@ -2284,7 +2278,7 @@ func (s *swapClientServer) StaticOpenChannel(ctx context.Context,
22842278
type filterFunc func(deposits *deposit.Deposit) bool
22852279

22862280
func filter(deposits []*deposit.Deposit, network *chaincfg.Params,
2287-
f filterFunc) []*looprpc.Deposit {
2281+
lndInfo *lndclient.Info, f filterFunc) []*looprpc.Deposit {
22882282

22892283
var clientDeposits []*looprpc.Deposit
22902284
for _, d := range deposits {
@@ -2310,6 +2304,9 @@ func filter(deposits []*deposit.Deposit, network *chaincfg.Params,
23102304
ConfirmationHeight: d.ConfirmationHeight,
23112305
SwapHash: swapHash,
23122306
StaticAddress: staticAddr,
2307+
BlocksUntilExpiry: d.ConfirmationHeight +
2308+
int64(d.AddressParams.Expiry) -
2309+
int64(lndInfo.BlockHeight),
23132310
}
23142311

23152312
clientDeposits = append(clientDeposits, deposit)

loopd/swapclient_server_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,12 @@ func (s *mockAddressStore) GetAllStaticAddresses(_ context.Context) (
957957
return s.params, nil
958958
}
959959

960+
func (s *mockAddressStore) GetLegacyParameters(_ context.Context) (
961+
*address.Parameters, error) {
962+
963+
return s.params[0], nil
964+
}
965+
960966
// mockDepositStore implements deposit.Store minimally for DepositsForOutpoints.
961967
type mockDepositStore struct {
962968
byOutpoint map[string]*deposit.Deposit

loopdb/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/static_addresses.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ INSERT INTO static_addresses (
2424
$6,
2525
$7,
2626
$8
27-
);
27+
);
28+
29+
-- name: GetLegacyAddress :one
30+
SELECT * FROM static_addresses
31+
ORDER BY id ASC
32+
LIMIT 1;
33+

loopdb/sqlc/static_addresses.sql.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staticaddr/address/interface.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ type Store interface {
2525
GetStaticAddressID(ctx context.Context, pkScript []byte) (int32, error)
2626

2727
// GetAllStaticAddresses retrieves all static addresses from the store.
28-
GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
29-
error)
28+
GetAllStaticAddresses(ctx context.Context) ([]*Parameters, error)
29+
30+
// GetLegacyParameters retrieves the parameters of the legacy static
31+
// address.
32+
GetLegacyParameters(ctx context.Context) (*Parameters, error)
3033
}
3134

3235
// Parameters hold all the necessary information for the 2-of-2 multisig

staticaddr/address/manager.go

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package address
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"sync/atomic"
87

98
"github.com/btcsuite/btcd/btcec/v2"
@@ -348,20 +347,17 @@ func (m *Manager) ListUnspentRaw(ctx context.Context, minConfs,
348347
return resultList, nil
349348
}
350349

351-
// GetStaticAddressParameters returns the parameters of the static address.
352-
func (m *Manager) GetStaticAddressParameters(ctx context.Context) (*Parameters,
350+
// GetLegacyParameters returns the first address parameters that were created
351+
// under this L402.
352+
func (m *Manager) GetLegacyParameters(ctx context.Context) (*Parameters,
353353
error) {
354354

355-
params, err := m.cfg.Store.GetAllStaticAddresses(ctx)
355+
params, err := m.cfg.Store.GetLegacyParameters(ctx)
356356
if err != nil {
357357
return nil, err
358358
}
359359

360-
if len(params) == 0 {
361-
return nil, fmt.Errorf("no static address parameters found")
362-
}
363-
364-
return params[0], nil
360+
return params, nil
365361
}
366362

367363
func (m *Manager) GetParameters(pkScript []byte) *Parameters {
@@ -374,25 +370,11 @@ func (m *Manager) GetStaticAddressID(ctx context.Context,
374370
return m.cfg.Store.GetStaticAddressID(ctx, pkScript)
375371
}
376372

377-
// GetStaticAddress returns a taproot address for the given client and server
378-
// public keys and expiry.
379-
func (m *Manager) GetStaticAddress(ctx context.Context) (*script.StaticAddress,
380-
error) {
381-
382-
params, err := m.GetStaticAddressParameters(ctx)
383-
if err != nil {
384-
return nil, err
385-
}
386-
387-
address, err := script.NewStaticAddress(
388-
input.MuSig2Version100RC2, int64(params.Expiry),
389-
params.ClientPubkey, params.ServerPubkey,
390-
)
391-
if err != nil {
392-
return nil, err
393-
}
394-
395-
return address, nil
373+
// IsOurPkScript returns true if the given pkScript is one of our active
374+
// static addresses.
375+
func (m *Manager) IsOurPkScript(pkScript []byte) bool {
376+
_, ok := m.activeStaticAddresses[string(pkScript)]
377+
return ok
396378
}
397379

398380
// ListUnspent returns a list of utxos at the static address.

staticaddr/address/sql_store.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
7474
return result, nil
7575
}
7676

77+
// GetLegacyParameters returns all address known to the server.
78+
func (s *SqlStore) GetLegacyParameters(ctx context.Context) (*Parameters,
79+
error) {
80+
81+
legacyAddress, err := s.baseDB.Queries.GetLegacyAddress(ctx)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
res, err := s.toAddressParameters(legacyAddress)
87+
if err != nil {
88+
return nil, err
89+
}
90+
return res, nil
91+
}
92+
7793
// Close closes the database connection.
7894
func (s *SqlStore) Close() {
7995
s.baseDB.DB.Close()

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
}

0 commit comments

Comments
 (0)