Skip to content

Commit 8b89f77

Browse files
committed
staticaddr: move address.Parameters to script package
The Parameters struct describes the keys, expiry and pkScript that define the static address script, so its natural home is the script package. Moving it there lets staticutil drop its dependency on the address package and lets callers reuse a single type alongside script.StaticAddress and script.NewStaticAddress. No behavior change. Closes lightninglabs#1056
1 parent 62177f9 commit 8b89f77

17 files changed

Lines changed: 81 additions & 83 deletions

loopd/swapclient_server_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightninglabs/loop/looprpc"
2020
"github.com/lightninglabs/loop/staticaddr/address"
2121
"github.com/lightninglabs/loop/staticaddr/deposit"
22+
"github.com/lightninglabs/loop/staticaddr/script"
2223
"github.com/lightninglabs/loop/swap"
2324
mock_lnd "github.com/lightninglabs/loop/test"
2425
"github.com/lightningnetwork/lnd/lntypes"
@@ -947,18 +948,18 @@ func TestListSwapsFilterAndPagination(t *testing.T) {
947948

948949
// mockAddressStore is a minimal in-memory store for address parameters.
949950
type mockAddressStore struct {
950-
params []*address.Parameters
951+
params []*script.Parameters
951952
}
952953

953954
func (s *mockAddressStore) CreateStaticAddress(_ context.Context,
954-
p *address.Parameters) error {
955+
p *script.Parameters) error {
955956

956957
s.params = append(s.params, p)
957958
return nil
958959
}
959960

960961
func (s *mockAddressStore) GetStaticAddress(_ context.Context, _ []byte) (
961-
*address.Parameters, error) {
962+
*script.Parameters, error) {
962963

963964
if len(s.params) == 0 {
964965
return nil, nil
@@ -968,7 +969,7 @@ func (s *mockAddressStore) GetStaticAddress(_ context.Context, _ []byte) (
968969
}
969970

970971
func (s *mockAddressStore) GetAllStaticAddresses(_ context.Context) (
971-
[]*address.Parameters, error) {
972+
[]*script.Parameters, error) {
972973

973974
return s.params, nil
974975
}
@@ -1019,14 +1020,14 @@ func TestListUnspentDeposits(t *testing.T) {
10191020
_, client := mock_lnd.CreateKey(1)
10201021
_, server := mock_lnd.CreateKey(2)
10211022
pkScript := []byte("pkscript")
1022-
addrParams := &address.Parameters{
1023+
addrParams := &script.Parameters{
10231024
ClientPubkey: client,
10241025
ServerPubkey: server,
10251026
Expiry: 10,
10261027
PkScript: pkScript,
10271028
}
10281029

1029-
addrStore := &mockAddressStore{params: []*address.Parameters{addrParams}}
1030+
addrStore := &mockAddressStore{params: []*script.Parameters{addrParams}}
10301031

10311032
// Build an address manager using our mock lnd and fake address store.
10321033
addrMgr, err := address.NewManager(&address.ManagerConfig{

staticaddr/address/interface.go

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,18 @@ package address
33
import (
44
"context"
55

6-
"github.com/btcsuite/btcd/btcec/v2"
7-
"github.com/lightninglabs/loop/staticaddr/version"
8-
"github.com/lightningnetwork/lnd/keychain"
6+
"github.com/lightninglabs/loop/staticaddr/script"
97
)
108

119
// Store is the database interface that is used to store and retrieve
1210
// static addresses.
1311
type Store interface {
1412
// CreateStaticAddress inserts a new static address with its parameters
1513
// into the store.
16-
CreateStaticAddress(ctx context.Context, addrParams *Parameters) error
14+
CreateStaticAddress(ctx context.Context,
15+
addrParams *script.Parameters) error
1716

1817
// GetAllStaticAddresses retrieves all static addresses from the store.
19-
GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
18+
GetAllStaticAddresses(ctx context.Context) ([]*script.Parameters,
2019
error)
2120
}
22-
23-
// Parameters holds all the necessary information for the 2-of-2 multisig
24-
// address.
25-
type Parameters struct {
26-
// ClientPubkey is the client's pubkey for the static address. It is
27-
// used for the 2-of-2 funding output as well as for the client's
28-
// timeout path.
29-
ClientPubkey *btcec.PublicKey
30-
31-
// ServerPubkey is the server's pubkey for the static address. It is
32-
// used for the 2-of-2 funding output.
33-
ServerPubkey *btcec.PublicKey
34-
35-
// Expiry is the CSV timout value at which the client can claim the
36-
// static address's timout path.
37-
Expiry uint32
38-
39-
// PkScript is the unique static address's output script.
40-
PkScript []byte
41-
42-
// KeyLocator is the locator of the client's key.
43-
KeyLocator keychain.KeyLocator
44-
45-
// ProtocolVersion is the protocol version of the static address.
46-
ProtocolVersion version.AddressProtocolVersion
47-
48-
// InitiationHeight is the height at which the address was initiated.
49-
InitiationHeight int32
50-
}

staticaddr/address/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
180180

181181
// Create the static address from the parameters the server provided and
182182
// store all parameters in the database.
183-
addrParams := &Parameters{
183+
addrParams := &script.Parameters{
184184
ClientPubkey: clientPubKey.PubKey,
185185
ServerPubkey: serverPubKey,
186186
PkScript: pkScript,
@@ -288,8 +288,8 @@ func (m *Manager) ListUnspentRaw(ctx context.Context, minConfs,
288288
}
289289

290290
// GetStaticAddressParameters returns the parameters of the static address.
291-
func (m *Manager) GetStaticAddressParameters(ctx context.Context) (*Parameters,
292-
error) {
291+
func (m *Manager) GetStaticAddressParameters(ctx context.Context) (
292+
*script.Parameters, error) {
293293

294294
params, err := m.cfg.Store.GetAllStaticAddresses(ctx)
295295
if err != nil {

staticaddr/address/sql_store.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/btcsuite/btcd/btcec/v2"
77
"github.com/lightninglabs/loop/loopdb"
88
"github.com/lightninglabs/loop/loopdb/sqlc"
9+
"github.com/lightninglabs/loop/staticaddr/script"
910
"github.com/lightninglabs/loop/staticaddr/version"
1011
"github.com/lightningnetwork/lnd/keychain"
1112
)
@@ -25,7 +26,7 @@ func NewSqlStore(db *loopdb.BaseDB) *SqlStore {
2526

2627
// CreateStaticAddress creates a static address record in the database.
2728
func (s *SqlStore) CreateStaticAddress(ctx context.Context,
28-
addrParams *Parameters) error {
29+
addrParams *script.Parameters) error {
2930

3031
createArgs := sqlc.CreateStaticAddressParams{
3132
ClientPubkey: addrParams.ClientPubkey.SerializeCompressed(),
@@ -42,15 +43,15 @@ func (s *SqlStore) CreateStaticAddress(ctx context.Context,
4243
}
4344

4445
// GetAllStaticAddresses returns all address known to the server.
45-
func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
46-
error) {
46+
func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) (
47+
[]*script.Parameters, error) {
4748

4849
staticAddresses, err := s.baseDB.Queries.AllStaticAddresses(ctx)
4950
if err != nil {
5051
return nil, err
5152
}
5253

53-
var result []*Parameters
54+
var result []*script.Parameters
5455
for _, address := range staticAddresses {
5556
res, err := s.toAddressParameters(address)
5657
if err != nil {
@@ -66,7 +67,7 @@ func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
6667
// toAddressParameters transforms a database representation of a static address
6768
// to an AddressParameters struct.
6869
func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
69-
*Parameters, error) {
70+
*script.Parameters, error) {
7071

7172
clientPubkey, err := btcec.ParsePubKey(row.ClientPubkey)
7273
if err != nil {
@@ -78,7 +79,7 @@ func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
7879
return nil, err
7980
}
8081

81-
return &Parameters{
82+
return &script.Parameters{
8283
ClientPubkey: clientPubkey,
8384
ServerPubkey: serverPubkey,
8485
PkScript: row.Pkscript,

staticaddr/deposit/fsm.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/btcsuite/btcd/wire"
1010
"github.com/lightninglabs/lndclient"
1111
"github.com/lightninglabs/loop/fsm"
12-
"github.com/lightninglabs/loop/staticaddr/address"
1312
"github.com/lightninglabs/loop/staticaddr/script"
1413
"github.com/lightninglabs/loop/staticaddr/version"
1514
"github.com/lightningnetwork/lnd/input"
@@ -155,7 +154,7 @@ type FSM struct {
155154

156155
deposit *Deposit
157156

158-
params *address.Parameters
157+
params *script.Parameters
159158

160159
address *script.StaticAddress
161160

staticaddr/deposit/interface.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/btcsuite/btcd/btcec/v2"
77
"github.com/btcsuite/btcd/btcutil"
8-
"github.com/lightninglabs/loop/staticaddr/address"
98
"github.com/lightninglabs/loop/staticaddr/script"
109
"github.com/lightningnetwork/lnd/lnwallet"
1110
)
@@ -37,7 +36,7 @@ type Store interface {
3736
// AddressManager handles fetching of address parameters.
3837
type AddressManager interface {
3938
// GetStaticAddressParameters returns the static address parameters.
40-
GetStaticAddressParameters(ctx context.Context) (*address.Parameters,
39+
GetStaticAddressParameters(ctx context.Context) (*script.Parameters,
4140
error)
4241

4342
// GetStaticAddress returns the deposit address for the given

staticaddr/deposit/manager_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/btcsuite/btcd/chaincfg/chainhash"
1212
"github.com/btcsuite/btcd/wire"
1313
"github.com/lightninglabs/lndclient"
14-
"github.com/lightninglabs/loop/staticaddr/address"
1514
"github.com/lightninglabs/loop/staticaddr/script"
1615
"github.com/lightninglabs/loop/swap"
1716
"github.com/lightninglabs/loop/swapserverrpc"
@@ -109,11 +108,11 @@ type mockAddressManager struct {
109108
}
110109

111110
func (m *mockAddressManager) GetStaticAddressParameters(ctx context.Context) (
112-
*address.Parameters, error) {
111+
*script.Parameters, error) {
113112

114113
args := m.Called(ctx)
115114

116-
return args.Get(0).(*address.Parameters),
115+
return args.Get(0).(*script.Parameters),
117116
args.Error(1)
118117
}
119118

@@ -365,7 +364,7 @@ func newManagerTestContext(t *testing.T) *ManagerTestContext {
365364

366365
mockAddressManager.On(
367366
"GetStaticAddressParameters", mock.Anything,
368-
).Return(&address.Parameters{
367+
).Return(&script.Parameters{
369368
Expiry: defaultExpiry,
370369
}, nil)
371370

staticaddr/loopin/actions_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/btcsuite/btcd/wire"
1212
"github.com/lightninglabs/lndclient"
1313
"github.com/lightninglabs/loop/fsm"
14-
"github.com/lightninglabs/loop/staticaddr/address"
1514
"github.com/lightninglabs/loop/staticaddr/deposit"
1615
"github.com/lightninglabs/loop/staticaddr/script"
1716
"github.com/lightninglabs/loop/staticaddr/version"
@@ -62,7 +61,7 @@ func TestMonitorInvoiceAndHtlcTxReRegistersOnConfErr(t *testing.T) {
6261

6362
cfg := &Config{
6463
AddressManager: &mockAddressManager{
65-
params: &address.Parameters{
64+
params: &script.Parameters{
6665
ClientPubkey: clientKey.PubKey(),
6766
ServerPubkey: serverKey.PubKey(),
6867
ProtocolVersion: version.ProtocolVersion_V0,
@@ -274,12 +273,12 @@ func testValidateLoopInContract(_ int32, _ int32) error {
274273
// mockAddressManager is a minimal AddressManager implementation used by the
275274
// test FSM setup.
276275
type mockAddressManager struct {
277-
params *address.Parameters
276+
params *script.Parameters
278277
}
279278

280279
// GetStaticAddressParameters returns the configured address parameters.
281280
func (m *mockAddressManager) GetStaticAddressParameters(_ context.Context) (
282-
*address.Parameters, error) {
281+
*script.Parameters, error) {
283282

284283
return m.params, nil
285284
}

staticaddr/loopin/interface.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/btcsuite/btcd/btcutil"
77
"github.com/lightninglabs/loop"
88
"github.com/lightninglabs/loop/fsm"
9-
"github.com/lightninglabs/loop/staticaddr/address"
109
"github.com/lightninglabs/loop/staticaddr/deposit"
1110
"github.com/lightninglabs/loop/staticaddr/script"
1211
"github.com/lightninglabs/loop/swapserverrpc"
@@ -35,7 +34,7 @@ type (
3534
// AddressManager handles fetching of address parameters.
3635
type AddressManager interface {
3736
// GetStaticAddressParameters returns the static address parameters.
38-
GetStaticAddressParameters(ctx context.Context) (*address.Parameters,
37+
GetStaticAddressParameters(ctx context.Context) (*script.Parameters,
3938
error)
4039

4140
// GetStaticAddress returns the deposit address for the given client and

staticaddr/loopin/loopin.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/btcsuite/btcd/wire"
1919
"github.com/lightninglabs/lndclient"
2020
"github.com/lightninglabs/loop/fsm"
21-
"github.com/lightninglabs/loop/staticaddr/address"
2221
"github.com/lightninglabs/loop/staticaddr/deposit"
2322
"github.com/lightninglabs/loop/staticaddr/script"
2423
"github.com/lightninglabs/loop/staticaddr/staticutil"
@@ -134,7 +133,7 @@ type StaticAddressLoopIn struct {
134133

135134
// AddressParams are the parameters of the address that is used for the
136135
// swap.
137-
AddressParams *address.Parameters
136+
AddressParams *script.Parameters
138137

139138
// Address is the address script that is used for the swap.
140139
Address *script.StaticAddress

0 commit comments

Comments
 (0)