@@ -5,14 +5,19 @@ import (
5
5
"context"
6
6
"database/sql"
7
7
"encoding/hex"
8
+ "fmt"
8
9
10
+ "github.com/btcsuite/btcd/btcec/v2"
9
11
"github.com/btcsuite/btcd/btcutil"
10
12
"github.com/btcsuite/btcd/chaincfg/chainhash"
11
13
"github.com/btcsuite/btcd/wire"
12
14
"github.com/lightninglabs/loop/fsm"
13
15
"github.com/lightninglabs/loop/loopdb"
14
16
"github.com/lightninglabs/loop/loopdb/sqlc"
17
+ "github.com/lightninglabs/loop/staticaddr/address"
18
+ "github.com/lightninglabs/loop/staticaddr/version"
15
19
"github.com/lightningnetwork/lnd/clock"
20
+ "github.com/lightningnetwork/lnd/keychain"
16
21
"github.com/lightningnetwork/lnd/lntypes"
17
22
)
18
23
@@ -35,13 +40,21 @@ func NewSqlStore(db *loopdb.BaseDB) *SqlStore {
35
40
36
41
// CreateDeposit creates a static address deposit record in the database.
37
42
func (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
+
38
47
createArgs := sqlc.CreateDepositParams {
39
48
DepositID : deposit .ID [:],
40
49
TxHash : deposit .Hash [:],
41
50
OutIndex : int32 (deposit .Index ),
42
51
Amount : int64 (deposit .Value ),
43
52
ConfirmationHeight : deposit .ConfirmationHeight ,
44
53
TimeoutSweepPkScript : deposit .TimeOutSweepPkScript ,
54
+ StaticAddressID : sql.NullInt32 {
55
+ Int32 : deposit .AddressID ,
56
+ Valid : true ,
57
+ },
45
58
}
46
59
47
60
updateArgs := sqlc.InsertDepositUpdateParams {
@@ -136,7 +149,23 @@ func (s *SqlStore) GetDeposit(ctx context.Context, id ID) (*Deposit, error) {
136
149
return err
137
150
}
138
151
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 )
140
169
if err != nil {
141
170
return err
142
171
}
@@ -178,7 +207,24 @@ func (s *SqlStore) DepositForOutpoint(ctx context.Context,
178
207
return err
179
208
}
180
209
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 )
182
228
if err != nil {
183
229
return err
184
230
}
@@ -205,15 +251,15 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
205
251
return err
206
252
}
207
253
208
- for _ , deposit := range deposits {
254
+ for _ , d := range deposits {
209
255
latestUpdate , err := q .GetLatestDepositUpdate (
210
- ctx , deposit .DepositID ,
256
+ ctx , d .DepositID ,
211
257
)
212
258
if err != nil {
213
259
return err
214
260
}
215
261
216
- d , err := ToDeposit (deposit , latestUpdate )
262
+ d , err := ToDeposit (d , latestUpdate )
217
263
if err != nil {
218
264
return err
219
265
}
@@ -231,8 +277,8 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
231
277
}
232
278
233
279
// 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 ) {
236
282
237
283
id := ID {}
238
284
err := id .FromByteSlice (row .DepositID )
@@ -281,6 +327,31 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
281
327
swapHash = & hash
282
328
}
283
329
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
+
284
355
return & Deposit {
285
356
ID : id ,
286
357
state : fsm .StateType (lastUpdate .UpdateState ),
@@ -294,6 +365,8 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
294
365
ExpirySweepTxid : expirySweepTxid ,
295
366
SwapHash : swapHash ,
296
367
FinalizedWithdrawalTx : finalizedWithdrawalTx ,
368
+ AddressParams : params ,
369
+ AddressID : row .StaticAddressID .Int32 ,
297
370
}, nil
298
371
}
299
372
@@ -302,11 +375,16 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
302
375
func (s * SqlStore ) BatchSetStaticAddressID (ctx context.Context ,
303
376
staticAddrID int32 ) error {
304
377
378
+ if staticAddrID <= 0 {
379
+ return fmt .Errorf ("static address ID must be set" )
380
+ }
381
+
305
382
return s .baseDB .ExecTx (
306
383
ctx , loopdb .NewSqlWriteOpts (), func (q * sqlc.Queries ) error {
307
384
return q .SetAllNullDepositsStaticAddressID (
308
385
ctx , sql.NullInt32 {
309
- Int32 : staticAddrID , Valid : true ,
386
+ Int32 : staticAddrID ,
387
+ Valid : true ,
310
388
},
311
389
)
312
390
},
0 commit comments