@@ -6,13 +6,17 @@ import (
66 "database/sql"
77 "encoding/hex"
88
9+ "github.com/btcsuite/btcd/btcec/v2"
910 "github.com/btcsuite/btcd/btcutil"
1011 "github.com/btcsuite/btcd/chaincfg/chainhash"
1112 "github.com/btcsuite/btcd/wire"
1213 "github.com/lightninglabs/loop/fsm"
1314 "github.com/lightninglabs/loop/loopdb"
1415 "github.com/lightninglabs/loop/loopdb/sqlc"
16+ "github.com/lightninglabs/loop/staticaddr/address"
17+ "github.com/lightninglabs/loop/staticaddr/version"
1518 "github.com/lightningnetwork/lnd/clock"
19+ "github.com/lightningnetwork/lnd/keychain"
1620 "github.com/lightningnetwork/lnd/lntypes"
1721)
1822
@@ -42,6 +46,7 @@ func (s *SqlStore) CreateDeposit(ctx context.Context, deposit *Deposit) error {
4246 Amount : int64 (deposit .Value ),
4347 ConfirmationHeight : deposit .ConfirmationHeight ,
4448 TimeoutSweepPkScript : deposit .TimeOutSweepPkScript ,
49+ StaticAddressID : deposit .AddressID ,
4550 }
4651
4752 updateArgs := sqlc.InsertDepositUpdateParams {
@@ -136,7 +141,23 @@ func (s *SqlStore) GetDeposit(ctx context.Context, id ID) (*Deposit, error) {
136141 return err
137142 }
138143
139- deposit , err = ToDeposit (row , latestUpdate )
144+ allDepositsRow := sqlc.AllDepositsRow {
145+ ID : row .ID ,
146+ DepositID : row .DepositID ,
147+ TxHash : row .TxHash ,
148+ OutIndex : row .OutIndex ,
149+ Amount : row .Amount ,
150+ ConfirmationHeight : row .ConfirmationHeight ,
151+ TimeoutSweepPkScript : row .TimeoutSweepPkScript ,
152+ ExpirySweepTxid : row .ExpirySweepTxid ,
153+ FinalizedWithdrawalTx : row .FinalizedWithdrawalTx ,
154+ SwapHash : row .SwapHash ,
155+ StaticAddressID : row .StaticAddressID ,
156+ ClientPubkey : row .ClientPubkey ,
157+ ServerPubkey : row .ServerPubkey ,
158+ Expiry : row .Expiry ,
159+ }
160+ deposit , err = ToDeposit (allDepositsRow , latestUpdate )
140161 if err != nil {
141162 return err
142163 }
@@ -178,7 +199,24 @@ func (s *SqlStore) DepositForOutpoint(ctx context.Context,
178199 return err
179200 }
180201
181- deposit , err = ToDeposit (row , latestUpdate )
202+ allDepositsRow := sqlc.AllDepositsRow {
203+ ID : row .ID ,
204+ DepositID : row .DepositID ,
205+ TxHash : row .TxHash ,
206+ OutIndex : row .OutIndex ,
207+ Amount : row .Amount ,
208+ ConfirmationHeight : row .ConfirmationHeight ,
209+ TimeoutSweepPkScript : row .TimeoutSweepPkScript ,
210+ ExpirySweepTxid : row .ExpirySweepTxid ,
211+ FinalizedWithdrawalTx : row .FinalizedWithdrawalTx ,
212+ SwapHash : row .SwapHash ,
213+ StaticAddressID : row .StaticAddressID ,
214+ ClientPubkey : row .ClientPubkey ,
215+ ServerPubkey : row .ServerPubkey ,
216+ Expiry : row .Expiry ,
217+ }
218+
219+ deposit , err = ToDeposit (allDepositsRow , latestUpdate )
182220 if err != nil {
183221 return err
184222 }
@@ -205,15 +243,15 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
205243 return err
206244 }
207245
208- for _ , deposit := range deposits {
246+ for _ , d := range deposits {
209247 latestUpdate , err := q .GetLatestDepositUpdate (
210- ctx , deposit .DepositID ,
248+ ctx , d .DepositID ,
211249 )
212250 if err != nil {
213251 return err
214252 }
215253
216- d , err := ToDeposit (deposit , latestUpdate )
254+ d , err := ToDeposit (d , latestUpdate )
217255 if err != nil {
218256 return err
219257 }
@@ -231,8 +269,8 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
231269}
232270
233271// ToDeposit converts an sql deposit to a deposit.
234- func ToDeposit (row sqlc.Deposit , lastUpdate sqlc. DepositUpdate ) ( * Deposit ,
235- error ) {
272+ func ToDeposit (row sqlc.AllDepositsRow ,
273+ lastUpdate sqlc. DepositUpdate ) ( * Deposit , error ) {
236274
237275 id := ID {}
238276 err := id .FromByteSlice (row .DepositID )
@@ -281,6 +319,31 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
281319 swapHash = & hash
282320 }
283321
322+ clientPubkey , err := btcec .ParsePubKey (row .ClientPubkey )
323+ if err != nil {
324+ return nil , err
325+ }
326+
327+ serverPubkey , err := btcec .ParsePubKey (row .ServerPubkey )
328+ if err != nil {
329+ return nil , err
330+ }
331+
332+ params := & address.Parameters {
333+ ClientPubkey : clientPubkey ,
334+ ServerPubkey : serverPubkey ,
335+ Expiry : uint32 (row .Expiry .Int32 ),
336+ PkScript : row .Pkscript ,
337+ KeyLocator : keychain.KeyLocator {
338+ Family : keychain .KeyFamily (row .ClientKeyFamily .Int32 ),
339+ Index : uint32 (row .ClientKeyIndex .Int32 ),
340+ },
341+ ProtocolVersion : version .AddressProtocolVersion (
342+ row .ProtocolVersion .Int32 ,
343+ ),
344+ InitiationHeight : row .InitiationHeight .Int32 ,
345+ }
346+
284347 return & Deposit {
285348 ID : id ,
286349 state : fsm .StateType (lastUpdate .UpdateState ),
@@ -294,6 +357,7 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
294357 ExpirySweepTxid : expirySweepTxid ,
295358 SwapHash : swapHash ,
296359 FinalizedWithdrawalTx : finalizedWithdrawalTx ,
360+ AddressParams : params ,
297361 }, nil
298362}
299363
@@ -305,9 +369,7 @@ func (s *SqlStore) BatchSetStaticAddressID(ctx context.Context,
305369 return s .baseDB .ExecTx (
306370 ctx , loopdb .NewSqlWriteOpts (), func (q * sqlc.Queries ) error {
307371 return q .SetAllNullDepositsStaticAddressID (
308- ctx , sql.NullInt32 {
309- Int32 : staticAddrID , Valid : true ,
310- },
372+ ctx , staticAddrID ,
311373 )
312374 },
313375 )
0 commit comments