Skip to content

Commit 6adc00a

Browse files
committed
staticaddr: interfaces
1 parent 401ff43 commit 6adc00a

File tree

4 files changed

+279
-61
lines changed

4 files changed

+279
-61
lines changed
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
package staticaddr
1+
package address
22

33
import (
44
"context"
55
"fmt"
66

77
"github.com/btcsuite/btcd/btcec/v2"
8+
"github.com/lightninglabs/loop/staticaddr"
89
"github.com/lightningnetwork/lnd/keychain"
910
)
1011

1112
var (
1213
ErrAddressAlreadyExists = fmt.Errorf("address already exists")
13-
ErrAddressNotFound = fmt.Errorf("address not found")
1414
)
1515

16-
// AddressStore is the database interface that is used to store and retrieve
16+
// Store is the database interface that is used to store and retrieve
1717
// static addresses.
18-
type AddressStore interface {
18+
type Store interface {
1919
// CreateStaticAddress inserts a new static address with its parameters
2020
// into the store.
21-
CreateStaticAddress(ctx context.Context,
22-
addrParams *AddressParameters) error
21+
CreateStaticAddress(ctx context.Context, addrParams *Parameters) error
2322

2423
// GetStaticAddress fetches static address parameters for a given
2524
// address ID.
26-
GetStaticAddress(ctx context.Context,
27-
pkScript []byte) (*AddressParameters, error)
25+
GetStaticAddress(ctx context.Context, pkScript []byte) (*Parameters,
26+
error)
2827

2928
// GetAllStaticAddresses retrieves all static addresses from the store.
30-
GetAllStaticAddresses(ctx context.Context) (
31-
[]*AddressParameters, error)
29+
GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
30+
error)
3231
}
3332

34-
// AddressParameters holds all the necessary information for the 2-of-2 multisig
33+
// Parameters holds all the necessary information for the 2-of-2 multisig
3534
// address.
36-
type AddressParameters struct {
35+
type Parameters struct {
3736
// ClientPubkey is the client's pubkey for the static address. It is
3837
// used for the 2-of-2 funding output as well as for the client's
3938
// timeout path.
@@ -54,5 +53,5 @@ type AddressParameters struct {
5453
KeyLocator keychain.KeyLocator
5554

5655
// ProtocolVersion is the protocol version of the static address.
57-
ProtocolVersion AddressProtocolVersion
56+
ProtocolVersion staticaddr.AddressProtocolVersion
5857
}
Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package staticaddr
1+
package address
22

33
import (
44
"context"
5-
"errors"
65

76
"github.com/btcsuite/btcd/btcec/v2"
8-
"github.com/jackc/pgx/v4"
97
"github.com/lightninglabs/loop/loopdb"
108
"github.com/lightninglabs/loop/loopdb/sqlc"
9+
"github.com/lightninglabs/loop/staticaddr"
1110
"github.com/lightningnetwork/lnd/keychain"
1211
)
1312

@@ -24,46 +23,9 @@ func NewSqlStore(db *loopdb.BaseDB) *SqlStore {
2423
}
2524
}
2625

27-
// ExecTx is a wrapper for txBody to abstract the creation and commit of a db
28-
// transaction. The db transaction is embedded in a `*sqlc.Queries` that txBody
29-
// needs to use when executing each one of the queries that need to be applied
30-
// atomically.
31-
func (s *SqlStore) ExecTx(ctx context.Context, txOptions loopdb.TxOptions,
32-
txBody func(queries *sqlc.Queries) error) error {
33-
34-
// Create the db transaction.
35-
tx, err := s.baseDB.BeginTx(ctx, txOptions)
36-
if err != nil {
37-
return err
38-
}
39-
40-
// Rollback is safe to call even if the tx is already closed, so if the
41-
// tx commits successfully, this is a no-op.
42-
defer func() {
43-
err := tx.Rollback()
44-
switch {
45-
// If the tx was already closed (it was successfully executed)
46-
// we do not need to log that error.
47-
case errors.Is(err, pgx.ErrTxClosed):
48-
return
49-
50-
// If this is an unexpected error, log it.
51-
case err != nil:
52-
log.Errorf("unable to rollback db tx: %v", err)
53-
}
54-
}()
55-
56-
if err := txBody(s.baseDB.Queries.WithTx(tx)); err != nil {
57-
return err
58-
}
59-
60-
// Commit transaction.
61-
return tx.Commit()
62-
}
63-
6426
// CreateStaticAddress creates a static address record in the database.
6527
func (s *SqlStore) CreateStaticAddress(ctx context.Context,
66-
addrParams *AddressParameters) error {
28+
addrParams *Parameters) error {
6729

6830
createArgs := sqlc.CreateStaticAddressParams{
6931
ClientPubkey: addrParams.ClientPubkey.SerializeCompressed(),
@@ -80,7 +42,7 @@ func (s *SqlStore) CreateStaticAddress(ctx context.Context,
8042

8143
// GetStaticAddress retrieves static address parameters for a given pkScript.
8244
func (s *SqlStore) GetStaticAddress(ctx context.Context,
83-
pkScript []byte) (*AddressParameters, error) {
45+
pkScript []byte) (*Parameters, error) {
8446

8547
staticAddress, err := s.baseDB.Queries.GetStaticAddress(ctx, pkScript)
8648
if err != nil {
@@ -91,15 +53,15 @@ func (s *SqlStore) GetStaticAddress(ctx context.Context,
9153
}
9254

9355
// GetAllStaticAddresses returns all address known to the server.
94-
func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) (
95-
[]*AddressParameters, error) {
56+
func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
57+
error) {
9658

9759
staticAddresses, err := s.baseDB.Queries.AllStaticAddresses(ctx)
9860
if err != nil {
9961
return nil, err
10062
}
10163

102-
var result []*AddressParameters
64+
var result []*Parameters
10365
for _, address := range staticAddresses {
10466
res, err := s.toAddressParameters(address)
10567
if err != nil {
@@ -120,7 +82,7 @@ func (s *SqlStore) Close() {
12082
// toAddressParameters transforms a database representation of a static address
12183
// to an AddressParameters struct.
12284
func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
123-
*AddressParameters, error) {
85+
*Parameters, error) {
12486

12587
clientPubkey, err := btcec.ParsePubKey(row.ClientPubkey)
12688
if err != nil {
@@ -132,7 +94,7 @@ func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
13294
return nil, err
13395
}
13496

135-
return &AddressParameters{
97+
return &Parameters{
13698
ClientPubkey: clientPubkey,
13799
ServerPubkey: serverPubkey,
138100
PkScript: row.Pkscript,
@@ -141,6 +103,6 @@ func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
141103
Family: keychain.KeyFamily(row.ClientKeyFamily),
142104
Index: uint32(row.ClientKeyIndex),
143105
},
144-
ProtocolVersion: AddressProtocolVersion(row.ProtocolVersion),
106+
ProtocolVersion: staticaddr.AddressProtocolVersion(row.ProtocolVersion),
145107
}, nil
146108
}

staticaddr/deposit/interface.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package deposit
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightninglabs/loop/staticaddr/address"
7+
"github.com/lightninglabs/loop/staticaddr/script"
8+
"github.com/lightningnetwork/lnd/lnwallet"
9+
)
10+
11+
const (
12+
IdLength = 32
13+
)
14+
15+
// Store is the database interface that is used to store and retrieve
16+
// static address deposits.
17+
type Store interface {
18+
// CreateDeposit inserts a new deposit into the store.
19+
CreateDeposit(ctx context.Context, deposit *Deposit) error
20+
21+
// UpdateDeposit updates the deposit in the database.
22+
UpdateDeposit(ctx context.Context, deposit *Deposit) error
23+
24+
// GetDeposit retrieves a deposit with depositID from the database.
25+
GetDeposit(ctx context.Context, depositID ID) (*Deposit, error)
26+
27+
// AllDeposits retrieves all deposits from the store.
28+
AllDeposits(ctx context.Context) ([]*Deposit, error)
29+
}
30+
31+
// AddressManager handles fetching of address parameters.
32+
type AddressManager interface {
33+
// GetStaticAddressParameters returns the static address parameters.
34+
GetStaticAddressParameters(ctx context.Context) (*address.Parameters,
35+
error)
36+
37+
// GetStaticAddress returns the deposit address for the given
38+
// client and server public keys.
39+
GetStaticAddress(ctx context.Context) (*script.StaticAddress, error)
40+
41+
// ListUnspent returns a list of utxos at the static address.
42+
ListUnspent(ctx context.Context, minConfs,
43+
maxConfs int32) ([]*lnwallet.Utxo, error)
44+
}

0 commit comments

Comments
 (0)