@@ -5,14 +5,19 @@ import (
55 "context"
66 "database/sql"
77 "encoding/hex"
8+ "fmt"
89
10+ "github.com/btcsuite/btcd/btcec/v2"
911 "github.com/btcsuite/btcd/btcutil"
1012 "github.com/btcsuite/btcd/chaincfg/chainhash"
1113 "github.com/btcsuite/btcd/wire"
1214 "github.com/lightninglabs/loop/fsm"
1315 "github.com/lightninglabs/loop/loopdb"
1416 "github.com/lightninglabs/loop/loopdb/sqlc"
17+ "github.com/lightninglabs/loop/staticaddr/address"
18+ "github.com/lightninglabs/loop/staticaddr/version"
1519 "github.com/lightningnetwork/lnd/clock"
20+ "github.com/lightningnetwork/lnd/keychain"
1621 "github.com/lightningnetwork/lnd/lntypes"
1722)
1823
@@ -35,13 +40,21 @@ func NewSqlStore(db *loopdb.BaseDB) *SqlStore {
3540
3641// CreateDeposit creates a static address deposit record in the database.
3742func (s * SqlStore ) CreateDeposit (ctx context.Context , deposit * Deposit ) error {
43+ if deposit .AddressID <= 0 {
44+ return fmt .Errorf ("static address ID must be set" )
45+ }
46+
3847 createArgs := sqlc.CreateDepositParams {
3948 DepositID : deposit .ID [:],
4049 TxHash : deposit .Hash [:],
4150 OutIndex : int32 (deposit .Index ),
4251 Amount : int64 (deposit .Value ),
4352 ConfirmationHeight : deposit .ConfirmationHeight ,
4453 TimeoutSweepPkScript : deposit .TimeOutSweepPkScript ,
54+ StaticAddressID : sql.NullInt32 {
55+ Int32 : deposit .AddressID ,
56+ Valid : true ,
57+ },
4558 }
4659
4760 updateArgs := sqlc.InsertDepositUpdateParams {
@@ -136,7 +149,23 @@ func (s *SqlStore) GetDeposit(ctx context.Context, id ID) (*Deposit, error) {
136149 return err
137150 }
138151
139- deposit , err = ToDeposit (row , latestUpdate )
152+ allDepositsRow := sqlc.AllDepositsRow {
153+ ID : row .ID ,
154+ DepositID : row .DepositID ,
155+ TxHash : row .TxHash ,
156+ OutIndex : row .OutIndex ,
157+ Amount : row .Amount ,
158+ ConfirmationHeight : row .ConfirmationHeight ,
159+ TimeoutSweepPkScript : row .TimeoutSweepPkScript ,
160+ ExpirySweepTxid : row .ExpirySweepTxid ,
161+ FinalizedWithdrawalTx : row .FinalizedWithdrawalTx ,
162+ SwapHash : row .SwapHash ,
163+ StaticAddressID : row .StaticAddressID ,
164+ ClientPubkey : row .ClientPubkey ,
165+ ServerPubkey : row .ServerPubkey ,
166+ Expiry : row .Expiry ,
167+ }
168+ deposit , err = ToDeposit (allDepositsRow , latestUpdate )
140169 if err != nil {
141170 return err
142171 }
@@ -178,7 +207,24 @@ func (s *SqlStore) DepositForOutpoint(ctx context.Context,
178207 return err
179208 }
180209
181- deposit , err = ToDeposit (row , latestUpdate )
210+ allDepositsRow := sqlc.AllDepositsRow {
211+ ID : row .ID ,
212+ DepositID : row .DepositID ,
213+ TxHash : row .TxHash ,
214+ OutIndex : row .OutIndex ,
215+ Amount : row .Amount ,
216+ ConfirmationHeight : row .ConfirmationHeight ,
217+ TimeoutSweepPkScript : row .TimeoutSweepPkScript ,
218+ ExpirySweepTxid : row .ExpirySweepTxid ,
219+ FinalizedWithdrawalTx : row .FinalizedWithdrawalTx ,
220+ SwapHash : row .SwapHash ,
221+ StaticAddressID : row .StaticAddressID ,
222+ ClientPubkey : row .ClientPubkey ,
223+ ServerPubkey : row .ServerPubkey ,
224+ Expiry : row .Expiry ,
225+ }
226+
227+ deposit , err = ToDeposit (allDepositsRow , latestUpdate )
182228 if err != nil {
183229 return err
184230 }
@@ -205,15 +251,15 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
205251 return err
206252 }
207253
208- for _ , deposit := range deposits {
254+ for _ , d := range deposits {
209255 latestUpdate , err := q .GetLatestDepositUpdate (
210- ctx , deposit .DepositID ,
256+ ctx , d .DepositID ,
211257 )
212258 if err != nil {
213259 return err
214260 }
215261
216- d , err := ToDeposit (deposit , latestUpdate )
262+ d , err := ToDeposit (d , latestUpdate )
217263 if err != nil {
218264 return err
219265 }
@@ -231,8 +277,8 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
231277}
232278
233279// ToDeposit converts an sql deposit to a deposit.
234- func ToDeposit (row sqlc.Deposit , lastUpdate sqlc. DepositUpdate ) ( * Deposit ,
235- error ) {
280+ func ToDeposit (row sqlc.AllDepositsRow ,
281+ lastUpdate sqlc. DepositUpdate ) ( * Deposit , error ) {
236282
237283 id := ID {}
238284 err := id .FromByteSlice (row .DepositID )
@@ -281,6 +327,31 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
281327 swapHash = & hash
282328 }
283329
330+ clientPubkey , err := btcec .ParsePubKey (row .ClientPubkey )
331+ if err != nil {
332+ return nil , err
333+ }
334+
335+ serverPubkey , err := btcec .ParsePubKey (row .ServerPubkey )
336+ if err != nil {
337+ return nil , err
338+ }
339+
340+ params := & address.Parameters {
341+ ClientPubkey : clientPubkey ,
342+ ServerPubkey : serverPubkey ,
343+ Expiry : uint32 (row .Expiry .Int32 ),
344+ PkScript : row .Pkscript ,
345+ KeyLocator : keychain.KeyLocator {
346+ Family : keychain .KeyFamily (row .ClientKeyFamily .Int32 ),
347+ Index : uint32 (row .ClientKeyIndex .Int32 ),
348+ },
349+ ProtocolVersion : version .AddressProtocolVersion (
350+ row .ProtocolVersion .Int32 ,
351+ ),
352+ InitiationHeight : row .InitiationHeight .Int32 ,
353+ }
354+
284355 return & Deposit {
285356 ID : id ,
286357 state : fsm .StateType (lastUpdate .UpdateState ),
@@ -294,6 +365,7 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
294365 ExpirySweepTxid : expirySweepTxid ,
295366 SwapHash : swapHash ,
296367 FinalizedWithdrawalTx : finalizedWithdrawalTx ,
368+ AddressParams : params ,
297369 }, nil
298370}
299371
@@ -302,11 +374,16 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
302374func (s * SqlStore ) BatchSetStaticAddressID (ctx context.Context ,
303375 staticAddrID int32 ) error {
304376
377+ if staticAddrID <= 0 {
378+ return fmt .Errorf ("static address ID must be set" )
379+ }
380+
305381 return s .baseDB .ExecTx (
306382 ctx , loopdb .NewSqlWriteOpts (), func (q * sqlc.Queries ) error {
307383 return q .SetAllNullDepositsStaticAddressID (
308384 ctx , sql.NullInt32 {
309- Int32 : staticAddrID , Valid : true ,
385+ Int32 : staticAddrID ,
386+ Valid : true ,
310387 },
311388 )
312389 },
0 commit comments