1- package staticaddr
1+ package address
22
33import (
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.
6527func (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.
8244func (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.
12284func (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}
0 commit comments