loopin: push the htlc internal key if the invoice is swept

This commit adds key reveal to MuSig2 loopin swaps' success path. In
this case the client reveals their internal HTLC key to the server when
the swap invoice is settled. With this key the server can sweep the swap
HTLC without any more interaction from the client. We'll do this every
block (after the invoice has been settled).
This commit is contained in:
Andras Banki-Horvath 2023-01-06 18:02:29 +01:00
parent 90ae922adf
commit b47f67a6de
No known key found for this signature in database
GPG key ID: 80E5375C094198D8
6 changed files with 458 additions and 190 deletions

View file

@ -839,6 +839,7 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
htlcSpend := false
invoiceFinalized := false
htlcKeyRevealed := false
for !htlcSpend || !invoiceFinalized {
select {
// Spend notification error.
@ -855,6 +856,10 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
return err
}
if invoiceFinalized && !htlcKeyRevealed {
htlcKeyRevealed = s.tryPushHtlcKey(ctx)
}
// The htlc spend is confirmed. Inspect the spending tx to
// determine the final swap state.
case spendDetails := <-spendChan:
@ -918,6 +923,7 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
}
invoiceFinalized = true
htlcKeyRevealed = s.tryPushHtlcKey(ctx)
// Canceled invoice has no effect on server cost
// balance.
@ -933,6 +939,36 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
return nil
}
// tryPushHtlcKey attempts to push the htlc key to the server. If the server
// returns an error of any kind we'll log it as a warning but won't act as
// the swap execution can just go on without the server gaining knowledge of
// our internal key.
func (s *loopInSwap) tryPushHtlcKey(ctx context.Context) bool {
if s.ProtocolVersion < loopdb.ProtocolVersionMuSig2 {
return false
}
log.Infof("Attempting to reveal internal HTLC key to the server")
internalPrivKey, err := sharedSecretFromHash(
ctx, s.swapConfig.lnd.Signer, s.hash,
)
if err != nil {
s.log.Warnf("Unable to derive HTLC internal private key: %v",
err)
return false
}
err = s.server.PushKey(ctx, s.ProtocolVersion, s.hash, internalPrivKey)
if err != nil {
s.log.Warnf("Internal HTLC key reveal failed: %v", err)
return false
}
return true
}
func (s *loopInSwap) processHtlcSpend(ctx context.Context,
spend *chainntnfs.SpendDetail, htlcValue,
sweepFee btcutil.Amount) error {