@@ -524,54 +524,53 @@ func (s *covenantlessService) SubmitRedeemTx(
524
524
return "" , "" , fmt .Errorf ("failed to sign redeem tx: %s" , err )
525
525
}
526
526
527
- // Create new vtxos, update spent vtxos state
528
- newVtxos := make ([]domain.Vtxo , 0 , len (ptx .UnsignedTx .TxOut ))
529
- for outIndex , out := range outputs {
530
- vtxoTapKey , err := schnorr .ParsePubKey (out .PkScript [2 :])
531
- if err != nil {
532
- return "" , "" , fmt .Errorf ("failed to parse vtxo taproot key: %s" , err )
527
+ go func (ptx * psbt.Packet , signedRedeemTx , redeemTxid string ) {
528
+ ctx := context .Background ()
529
+ // Create new vtxos, update spent vtxos state
530
+ newVtxos := make ([]domain.Vtxo , 0 , len (ptx .UnsignedTx .TxOut ))
531
+ for outIndex , out := range outputs {
532
+ //notlint:all
533
+ vtxoPubkey := hex .EncodeToString (out .PkScript [2 :])
534
+
535
+ newVtxos = append (newVtxos , domain.Vtxo {
536
+ VtxoKey : domain.VtxoKey {
537
+ Txid : redeemTxid ,
538
+ VOut : uint32 (outIndex ),
539
+ },
540
+ PubKey : vtxoPubkey ,
541
+ Amount : uint64 (out .Value ),
542
+ ExpireAt : expiration ,
543
+ RoundTxid : roundTxid ,
544
+ RedeemTx : signedRedeemTx ,
545
+ CreatedAt : time .Now ().Unix (),
546
+ })
533
547
}
534
548
535
- vtxoPubkey := hex .EncodeToString (schnorr .SerializePubKey (vtxoTapKey ))
536
-
537
- newVtxos = append (newVtxos , domain.Vtxo {
538
- VtxoKey : domain.VtxoKey {
539
- Txid : redeemTxid ,
540
- VOut : uint32 (outIndex ),
541
- },
542
- PubKey : vtxoPubkey ,
543
- Amount : uint64 (out .Value ),
544
- ExpireAt : expiration ,
545
- RoundTxid : roundTxid ,
546
- RedeemTx : signedRedeemTx ,
547
- CreatedAt : time .Now ().Unix (),
548
- })
549
- }
549
+ if err := s .repoManager .Vtxos ().AddVtxos (ctx , newVtxos ); err != nil {
550
+ log .WithError (err ).Warn ("failed to add vtxos" )
551
+ return
552
+ }
553
+ log .Debugf ("added %d vtxos" , len (newVtxos ))
550
554
551
- if err := s .repoManager .Vtxos ().AddVtxos (ctx , newVtxos ); err != nil {
552
- return "" , "" , fmt .Errorf ("failed to add vtxos: %s" , err )
553
- }
554
- log .Infof ("added %d vtxos" , len (newVtxos ))
555
- if err := s .startWatchingVtxos (newVtxos ); err != nil {
556
- log .WithError (err ).Warn (
557
- "failed to start watching vtxos" ,
558
- )
559
- }
560
- log .Debugf ("started watching %d vtxos" , len (newVtxos ))
555
+ if err := s .repoManager .Vtxos ().SpendVtxos (ctx , spentVtxoKeys , redeemTxid ); err != nil {
556
+ log .WithError (err ).Warn ("failed to spend vtxos" )
557
+ return
558
+ }
559
+ log .Debugf ("spent %d vtxos" , len (spentVtxos ))
561
560
562
- if err := s .repoManager .Vtxos ().SpendVtxos (ctx , spentVtxoKeys , redeemTxid ); err != nil {
563
- return "" , "" , fmt .Errorf ("failed to spend vtxo: %s" , err )
564
- }
565
- log .Infof ("spent %d vtxos" , len (spentVtxos ))
561
+ if err := s .startWatchingVtxos (newVtxos ); err != nil {
562
+ log .WithError (err ).Warn ("failed to start watching vtxos" )
563
+ } else {
564
+ log .Debugf ("started watching %d vtxos" , len (newVtxos ))
565
+ }
566
566
567
- go func () {
568
567
s .transactionEventsCh <- RedeemTransactionEvent {
569
568
RedeemTxid : redeemTxid ,
570
569
SpentVtxos : spentVtxos ,
571
570
SpendableVtxos : newVtxos ,
572
571
TxHex : signedRedeemTx ,
573
572
}
574
- }()
573
+ }(ptx , signedRedeemTx , redeemTxid )
575
574
576
575
return signedRedeemTx , redeemTxid , nil
577
576
}
@@ -1179,20 +1178,23 @@ func (s *covenantlessService) RegisterCosignerNonces(
1179
1178
if err != nil {
1180
1179
return fmt .Errorf ("failed to decode nonces: %s" , err )
1181
1180
}
1182
- session .lock .Lock ()
1183
- defer session .lock .Unlock ()
1184
1181
1185
- if _ , ok := session . nonces [ pubkey ]; ok {
1186
- return nil // skip if we already have nonces for this pubkey
1187
- }
1182
+ go func ( session * musigSigningSession ) {
1183
+ session . lock . Lock ()
1184
+ defer session . lock . Unlock ()
1188
1185
1189
- session .nonces [pubkey ] = nonces
1186
+ if _ , ok := session .nonces [pubkey ]; ok {
1187
+ return // skip if we already have nonces for this pubkey
1188
+ }
1190
1189
1191
- if len (session .nonces ) == session .nbCosigners - 1 { // exclude the server
1192
- go func () {
1193
- session .nonceDoneC <- struct {}{}
1194
- }()
1195
- }
1190
+ session .nonces [pubkey ] = nonces
1191
+
1192
+ if len (session .nonces ) == session .nbCosigners - 1 { // exclude the server
1193
+ go func () {
1194
+ session .nonceDoneC <- struct {}{}
1195
+ }()
1196
+ }
1197
+ }(session )
1196
1198
1197
1199
return nil
1198
1200
}
@@ -1215,20 +1217,22 @@ func (s *covenantlessService) RegisterCosignerSignatures(
1215
1217
return fmt .Errorf ("failed to decode signatures: %s" , err )
1216
1218
}
1217
1219
1218
- session .lock .Lock ()
1219
- defer session .lock .Unlock ()
1220
+ go func (session * musigSigningSession ) {
1221
+ session .lock .Lock ()
1222
+ defer session .lock .Unlock ()
1220
1223
1221
- if _ , ok := session .signatures [pubkey ]; ok {
1222
- return nil // skip if we already have signatures for this pubkey
1223
- }
1224
+ if _ , ok := session .signatures [pubkey ]; ok {
1225
+ return // skip if we already have signatures for this pubkey
1226
+ }
1224
1227
1225
- session .signatures [pubkey ] = signatures
1228
+ session .signatures [pubkey ] = signatures
1226
1229
1227
- if len (session .signatures ) == session .nbCosigners - 1 { // exclude the server
1228
- go func () {
1229
- session .sigDoneC <- struct {}{}
1230
- }()
1231
- }
1230
+ if len (session .signatures ) == session .nbCosigners - 1 { // exclude the server
1231
+ go func () {
1232
+ session .sigDoneC <- struct {}{}
1233
+ }()
1234
+ }
1235
+ }(session )
1232
1236
1233
1237
return nil
1234
1238
}
@@ -1248,13 +1252,18 @@ func (s *covenantlessService) SetNostrRecipient(ctx context.Context, nostrRecipi
1248
1252
vtxoKeys = append (vtxoKeys , signedVtxo .Outpoint )
1249
1253
}
1250
1254
1251
- return s .repoManager .Entities ().Add (
1252
- ctx ,
1253
- domain.Entity {
1254
- NostrRecipient : nprofileRecipient ,
1255
- },
1256
- vtxoKeys ,
1257
- )
1255
+ entity := domain.Entity {NostrRecipient : nprofileRecipient }
1256
+ go func (entity domain.Entity , vtxoKeys []domain.VtxoKey ) {
1257
+ ctx := context .Background ()
1258
+
1259
+ if err := s .repoManager .Entities ().Add (ctx , entity , vtxoKeys ); err != nil {
1260
+ log .WithError (err ).Warn ("failed to add nostr identity, retrying..." )
1261
+ return
1262
+ }
1263
+ log .Debug ("added new nostr identity" )
1264
+ }(entity , vtxoKeys )
1265
+
1266
+ return nil
1258
1267
}
1259
1268
1260
1269
func (s * covenantlessService ) DeleteNostrRecipient (ctx context.Context , signedVtxoOutpoints []SignedVtxoOutpoint ) error {
@@ -1267,7 +1276,28 @@ func (s *covenantlessService) DeleteNostrRecipient(ctx context.Context, signedVt
1267
1276
vtxoKeys = append (vtxoKeys , signedVtxo .Outpoint )
1268
1277
}
1269
1278
1270
- return s .repoManager .Entities ().Delete (ctx , vtxoKeys )
1279
+ fetchedKeys := make ([]domain.VtxoKey , 0 , len (vtxoKeys ))
1280
+ for _ , vtxoKey := range vtxoKeys {
1281
+ entities , err := s .repoManager .Entities ().Get (ctx , vtxoKey )
1282
+ if err != nil || len (entities ) == 0 {
1283
+ continue
1284
+ }
1285
+ fetchedKeys = append (fetchedKeys , vtxoKey )
1286
+ }
1287
+ if len (fetchedKeys ) <= 0 {
1288
+ return nil
1289
+ }
1290
+
1291
+ go func (vtxoKeys []domain.VtxoKey ) {
1292
+ ctx := context .Background ()
1293
+ if err := s .repoManager .Entities ().Delete (ctx , vtxoKeys ); err != nil {
1294
+ log .WithError (err ).Warn ("failed to delete nostr identity, retrying..." )
1295
+ return
1296
+ }
1297
+ log .Debug ("deleted nostr identities" )
1298
+ }(fetchedKeys )
1299
+
1300
+ return nil
1271
1301
}
1272
1302
1273
1303
func (s * covenantlessService ) start () {
0 commit comments