|
1 | 1 | package loopin |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
5 | 6 | "fmt" |
6 | 7 | "sync/atomic" |
7 | 8 | "time" |
8 | 9 |
|
| 10 | + "github.com/btcsuite/btcd/btcec/v2/schnorr/musig2" |
| 11 | + "github.com/btcsuite/btcd/btcutil/psbt" |
9 | 12 | "github.com/btcsuite/btcd/chaincfg" |
| 13 | + "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 14 | + "github.com/btcsuite/btcd/txscript" |
| 15 | + "github.com/btcsuite/btcd/wire" |
10 | 16 | "github.com/lightninglabs/lndclient" |
11 | 17 | "github.com/lightninglabs/loop" |
12 | 18 | "github.com/lightninglabs/loop/fsm" |
13 | 19 | "github.com/lightninglabs/loop/labels" |
14 | 20 | "github.com/lightninglabs/loop/staticaddr/deposit" |
| 21 | + "github.com/lightninglabs/loop/swapserverrpc" |
15 | 22 | looprpc "github.com/lightninglabs/loop/swapserverrpc" |
16 | 23 | "github.com/lightningnetwork/lnd/lntypes" |
17 | 24 | "github.com/lightningnetwork/lnd/routing/route" |
@@ -63,6 +70,10 @@ type Config struct { |
63 | 70 | // loop-in related records. |
64 | 71 | Store StaticAddressLoopInStore |
65 | 72 |
|
| 73 | + // NotificationManager is the manager that handles the notification |
| 74 | + // subscriptions. |
| 75 | + NotificationManager NotificationManager |
| 76 | + |
66 | 77 | // ValidateLoopInContract validates the contract parameters against our |
67 | 78 | // request. |
68 | 79 | ValidateLoopInContract ValidateLoopInContract |
@@ -150,6 +161,12 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error { |
150 | 161 | return err |
151 | 162 | } |
152 | 163 |
|
| 164 | + // Register for notifications of loop-in sweep requests. |
| 165 | + sweepReqs := m.cfg.NotificationManager. |
| 166 | + SubscribeStaticLoopInSweepRequests( |
| 167 | + ctx, |
| 168 | + ) |
| 169 | + |
153 | 170 | // Communicate to the caller that the address manager has completed its |
154 | 171 | // initialization. |
155 | 172 | close(m.initChan) |
@@ -189,12 +206,177 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error { |
189 | 206 | return ctx.Err() |
190 | 207 | } |
191 | 208 |
|
| 209 | + case sweepReq := <-sweepReqs: |
| 210 | + err = m.handleLoopInSweepReq(ctx, sweepReq) |
| 211 | + if err != nil { |
| 212 | + log.Errorf("Error handling loop-in sweep request: %v", |
| 213 | + err) |
| 214 | + } |
| 215 | + |
192 | 216 | case <-ctx.Done(): |
193 | 217 | return ctx.Err() |
194 | 218 | } |
195 | 219 | } |
196 | 220 | } |
197 | 221 |
|
| 222 | +// handleLoopInSweepReq handles a loop-in sweep request from the server. |
| 223 | +// It first checks if the requested loop-in is finished as expected and if |
| 224 | +// yes will send signature to the server for the provided psbt. |
| 225 | +func (m *Manager) handleLoopInSweepReq(ctx context.Context, |
| 226 | + req *swapserverrpc.ServerStaticLoopInSweepRequest) error { |
| 227 | + |
| 228 | + // First we'll check if the loop-ins are known to us and in |
| 229 | + // the expected state. |
| 230 | + swapHash, err := lntypes.MakeHash(req.SwapHash) |
| 231 | + if err != nil { |
| 232 | + return err |
| 233 | + } |
| 234 | + |
| 235 | + // Fetch the loop-in from the store. |
| 236 | + loopIn, err := m.cfg.Store.FetchLoopInByHash(ctx, swapHash) |
| 237 | + if err != nil { |
| 238 | + return err |
| 239 | + } |
| 240 | + |
| 241 | + // If the loop-in is not in the Succeeded state we return an |
| 242 | + // error. |
| 243 | + if loopIn.state != Succeeded { |
| 244 | + return fmt.Errorf("loop-in %v not in Succeeded state", |
| 245 | + swapHash) |
| 246 | + } |
| 247 | + |
| 248 | + reader := bytes.NewReader(req.SweepTxPsbt) |
| 249 | + sweepPacket, err := psbt.NewFromRawBytes(reader, false) |
| 250 | + if err != nil { |
| 251 | + return err |
| 252 | + } |
| 253 | + |
| 254 | + sweepTx := sweepPacket.UnsignedTx |
| 255 | + |
| 256 | + // Perform a sanity check on the number of unsigned tx inputs and |
| 257 | + // prevout info. |
| 258 | + if len(sweepTx.TxIn) != len(req.PrevoutInfo) { |
| 259 | + return fmt.Errorf("expected %v inputs, got %v", |
| 260 | + len(req.PrevoutInfo), len(sweepTx.TxIn)) |
| 261 | + } |
| 262 | + |
| 263 | + prevoutMap := make(map[wire.OutPoint]*wire.TxOut) |
| 264 | + var depositOutpoint *wire.OutPoint |
| 265 | + |
| 266 | + for i := range req.PrevoutInfo { |
| 267 | + prevout := req.PrevoutInfo[i] |
| 268 | + |
| 269 | + txid, err := chainhash.NewHash(prevout.TxidBytes) |
| 270 | + if err != nil { |
| 271 | + return err |
| 272 | + } |
| 273 | + |
| 274 | + if i == int(req.OutputIndex) { |
| 275 | + depositOutpoint = &wire.OutPoint{ |
| 276 | + Hash: *txid, |
| 277 | + Index: prevout.OutputIndex, |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + prevoutMap[wire.OutPoint{ |
| 282 | + Hash: *txid, |
| 283 | + Index: prevout.OutputIndex, |
| 284 | + }] = &wire.TxOut{ |
| 285 | + Value: int64(req.PrevoutInfo[i].Value), |
| 286 | + PkScript: req.PrevoutInfo[i].PkScript, |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + // Check if the deposit outpoint is part of the loop-in. |
| 291 | + if depositOutpoint == nil { |
| 292 | + return fmt.Errorf("deposit outpoint not part of loop-in") |
| 293 | + } |
| 294 | + |
| 295 | + foundDeposit := false |
| 296 | + for _, loopInDeposit := range loopIn.DepositOutpoints { |
| 297 | + if loopInDeposit == fmt.Sprintf("%v:%v", |
| 298 | + depositOutpoint.Hash, depositOutpoint.Index) { |
| 299 | + |
| 300 | + foundDeposit = true |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + if !foundDeposit { |
| 305 | + return fmt.Errorf("deposit outpoint not part of loop-in") |
| 306 | + } |
| 307 | + |
| 308 | + prevOutputFetcher := txscript.NewMultiPrevOutFetcher( |
| 309 | + prevoutMap, |
| 310 | + ) |
| 311 | + |
| 312 | + sigHashes := txscript.NewTxSigHashes( |
| 313 | + sweepPacket.UnsignedTx, prevOutputFetcher, |
| 314 | + ) |
| 315 | + |
| 316 | + taprootSigHash, err := txscript.CalcTaprootSignatureHash( |
| 317 | + sigHashes, txscript.SigHashDefault, sweepPacket.UnsignedTx, |
| 318 | + int(req.OutputIndex), prevOutputFetcher, |
| 319 | + ) |
| 320 | + if err != nil { |
| 321 | + return err |
| 322 | + } |
| 323 | + |
| 324 | + var ( |
| 325 | + serverNonce [musig2.PubNonceSize]byte |
| 326 | + sigHash [32]byte |
| 327 | + ) |
| 328 | + |
| 329 | + copy(serverNonce[:], req.Nonce) |
| 330 | + musig2Session, err := loopIn.createMusig2Session(ctx, m.cfg.Signer) |
| 331 | + if err != nil { |
| 332 | + return err |
| 333 | + } |
| 334 | + |
| 335 | + haveAllNonces, err := m.cfg.Signer.MuSig2RegisterNonces( |
| 336 | + ctx, musig2Session.SessionID, |
| 337 | + [][musig2.PubNonceSize]byte{serverNonce}, |
| 338 | + ) |
| 339 | + if err != nil { |
| 340 | + return err |
| 341 | + } |
| 342 | + |
| 343 | + if !haveAllNonces { |
| 344 | + return fmt.Errorf("expected all nonces to be registered") |
| 345 | + } |
| 346 | + |
| 347 | + copy(sigHash[:], taprootSigHash) |
| 348 | + |
| 349 | + // Since our MuSig2 session has all nonces, we can now create |
| 350 | + // the local partial signature by signing the sig hash. |
| 351 | + sig, err := m.cfg.Signer.MuSig2Sign( |
| 352 | + ctx, musig2Session.SessionID, sigHash, false, |
| 353 | + ) |
| 354 | + if err != nil { |
| 355 | + return err |
| 356 | + } |
| 357 | + |
| 358 | + txHash := sweepTx.TxHash() |
| 359 | + |
| 360 | + // We'll now push the signature to the server. |
| 361 | + msg := &looprpc.PushSingleDepositVersion{ |
| 362 | + TxHash: txHash[:], |
| 363 | + OutputIndex: req.OutputIndex, |
| 364 | + Nonce: musig2Session.PublicNonce[:], |
| 365 | + Sig: sig, |
| 366 | + } |
| 367 | + |
| 368 | + _, err = m.cfg.Server.PushStaticAddressSweeplessSigs( |
| 369 | + ctx, &looprpc.PushStaticAddressSweeplessSigsRequest{ |
| 370 | + |
| 371 | + SwapHash: loopIn.SwapHash[:], |
| 372 | + Message: &looprpc.PushStaticAddressSweeplessSigsRequest_SingleDepositVersion{ //nolint:lll |
| 373 | + SingleDepositVersion: msg, |
| 374 | + }, |
| 375 | + }, |
| 376 | + ) |
| 377 | + return err |
| 378 | +} |
| 379 | + |
198 | 380 | // recover stars a loop-in state machine for each non-final loop-in to pick up |
199 | 381 | // work where it was left off before the restart. |
200 | 382 | func (m *Manager) recoverLoopIns(ctx context.Context) error { |
|
0 commit comments