1
- package staticaddr
1
+ package address
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
7
6
"github.com/btcsuite/btcd/btcec/v2"
8
- "github.com/jackc/pgx/v4"
9
7
"github.com/lightninglabs/loop/loopdb"
10
8
"github.com/lightninglabs/loop/loopdb/sqlc"
9
+ "github.com/lightninglabs/loop/staticaddr"
11
10
"github.com/lightningnetwork/lnd/keychain"
12
11
)
13
12
@@ -24,46 +23,9 @@ func NewSqlStore(db *loopdb.BaseDB) *SqlStore {
24
23
}
25
24
}
26
25
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
-
64
26
// CreateStaticAddress creates a static address record in the database.
65
27
func (s * SqlStore ) CreateStaticAddress (ctx context.Context ,
66
- addrParams * AddressParameters ) error {
28
+ addrParams * Parameters ) error {
67
29
68
30
createArgs := sqlc.CreateStaticAddressParams {
69
31
ClientPubkey : addrParams .ClientPubkey .SerializeCompressed (),
@@ -80,7 +42,7 @@ func (s *SqlStore) CreateStaticAddress(ctx context.Context,
80
42
81
43
// GetStaticAddress retrieves static address parameters for a given pkScript.
82
44
func (s * SqlStore ) GetStaticAddress (ctx context.Context ,
83
- pkScript []byte ) (* AddressParameters , error ) {
45
+ pkScript []byte ) (* Parameters , error ) {
84
46
85
47
staticAddress , err := s .baseDB .Queries .GetStaticAddress (ctx , pkScript )
86
48
if err != nil {
@@ -91,15 +53,15 @@ func (s *SqlStore) GetStaticAddress(ctx context.Context,
91
53
}
92
54
93
55
// 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 ) {
96
58
97
59
staticAddresses , err := s .baseDB .Queries .AllStaticAddresses (ctx )
98
60
if err != nil {
99
61
return nil , err
100
62
}
101
63
102
- var result []* AddressParameters
64
+ var result []* Parameters
103
65
for _ , address := range staticAddresses {
104
66
res , err := s .toAddressParameters (address )
105
67
if err != nil {
@@ -120,7 +82,7 @@ func (s *SqlStore) Close() {
120
82
// toAddressParameters transforms a database representation of a static address
121
83
// to an AddressParameters struct.
122
84
func (s * SqlStore ) toAddressParameters (row sqlc.StaticAddress ) (
123
- * AddressParameters , error ) {
85
+ * Parameters , error ) {
124
86
125
87
clientPubkey , err := btcec .ParsePubKey (row .ClientPubkey )
126
88
if err != nil {
@@ -132,7 +94,7 @@ func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
132
94
return nil , err
133
95
}
134
96
135
- return & AddressParameters {
97
+ return & Parameters {
136
98
ClientPubkey : clientPubkey ,
137
99
ServerPubkey : serverPubkey ,
138
100
PkScript : row .Pkscript ,
@@ -141,6 +103,6 @@ func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
141
103
Family : keychain .KeyFamily (row .ClientKeyFamily ),
142
104
Index : uint32 (row .ClientKeyIndex ),
143
105
},
144
- ProtocolVersion : AddressProtocolVersion (row .ProtocolVersion ),
106
+ ProtocolVersion : staticaddr . AddressProtocolVersion (row .ProtocolVersion ),
145
107
}, nil
146
108
}
0 commit comments