{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
module Lightning.Protocol.BOLT5.Detect (
identify_close
, classify_local_commit_outputs
, classify_remote_commit_outputs
, classify_revoked_commit_outputs
, extract_preimage_offered
, extract_preimage_htlc_success
, htlc_timed_out
) where
import Bitcoin.Prim.Tx (OutPoint(..), TxId, Witness(..))
import qualified Bitcoin.Prim.Tx as BT
import qualified Crypto.Hash.SHA256 as SHA256
import qualified Data.ByteString as BS
import qualified Data.List.NonEmpty as NE
import Data.Word (Word32)
import Lightning.Protocol.BOLT1 (PaymentHash, PaymentPreimage)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT3
import Lightning.Protocol.BOLT5.Types
identify_close
:: CommitmentTx
-> CommitmentTx
-> BS.ByteString
-> Maybe CloseType
identify_close :: CommitmentTx -> CommitmentTx -> ByteString -> Maybe CloseType
identify_close !CommitmentTx
local !CommitmentTx
remote !ByteString
bytes = do
!tx <- ByteString -> Maybe Tx
BT.from_bytes ByteString
bytes
let !txid = Tx -> TxId
BT.txid Tx
tx
if commitment_txid local == txid
then Just LocalCommitClose
else if commitment_txid remote == txid
then Just RemoteCommitClose
else Nothing
classify_local_commit_outputs
:: CommitmentTx
-> CommitmentKeys
-> ToSelfDelay
-> CommitmentFormat
-> [UnresolvedOutput]
classify_local_commit_outputs :: CommitmentTx
-> CommitmentKeys
-> ToSelfDelay
-> CommitmentFormat
-> [UnresolvedOutput]
classify_local_commit_outputs !CommitmentTx
tx !CommitmentKeys
keys !ToSelfDelay
delay !CommitmentFormat
fmt =
(OutputType -> OutputResolution)
-> CommitmentTx -> [UnresolvedOutput]
classify_outputs OutputType -> OutputResolution
resolve CommitmentTx
tx
where
resolve :: OutputType -> OutputResolution
resolve OutputType
t = case OutputType
t of
OutputType
OutputToLocal ->
ToSelfDelay
-> RevocationPubkey -> LocalDelayedPubkey -> OutputResolution
SpendToLocal ToSelfDelay
delay (CommitmentKeys -> RevocationPubkey
ck_revocation_pubkey CommitmentKeys
keys)
(CommitmentKeys -> LocalDelayedPubkey
ck_local_delayed CommitmentKeys
keys)
OutputType
OutputToRemote -> OutputResolution
Resolved
OutputType
OutputLocalAnchor -> FundingPubkey -> OutputResolution
AnchorSpend (CommitmentKeys -> FundingPubkey
ck_local_funding CommitmentKeys
keys)
OutputType
OutputRemoteAnchor -> OutputResolution
Resolved
OutputHTLC HTLC
h -> case HTLC -> HTLCDirection
htlc_direction HTLC
h of
HTLCDirection
HTLCOffered -> HTLC -> CommitmentKeys -> CommitmentFormat -> OutputResolution
SpendHTLCTimeout HTLC
h CommitmentKeys
keys CommitmentFormat
fmt
HTLCDirection
HTLCReceived -> HTLC -> CommitmentKeys -> CommitmentFormat -> OutputResolution
SpendHTLCSuccess HTLC
h CommitmentKeys
keys CommitmentFormat
fmt
classify_remote_commit_outputs
:: CommitmentTx
-> CommitmentKeys
-> [UnresolvedOutput]
classify_remote_commit_outputs :: CommitmentTx -> CommitmentKeys -> [UnresolvedOutput]
classify_remote_commit_outputs !CommitmentTx
tx !CommitmentKeys
keys =
(OutputType -> OutputResolution)
-> CommitmentTx -> [UnresolvedOutput]
classify_outputs OutputType -> OutputResolution
resolve CommitmentTx
tx
where
resolve :: OutputType -> OutputResolution
resolve OutputType
t = case OutputType
t of
OutputType
OutputToLocal -> OutputResolution
Resolved
OutputType
OutputToRemote -> OutputResolution
Resolved
OutputType
OutputLocalAnchor -> OutputResolution
Resolved
OutputType
OutputRemoteAnchor -> FundingPubkey -> OutputResolution
AnchorSpend (CommitmentKeys -> FundingPubkey
ck_remote_funding CommitmentKeys
keys)
OutputHTLC HTLC
h -> case HTLC -> HTLCDirection
htlc_direction HTLC
h of
HTLCDirection
HTLCOffered -> HTLC -> OutputResolution
SpendHTLCPreimageDirect HTLC
h
HTLCDirection
HTLCReceived -> HTLC -> OutputResolution
SpendHTLCTimeoutDirect HTLC
h
classify_revoked_commit_outputs
:: CommitmentTx
-> RevocationPubkey
-> [UnresolvedOutput]
classify_revoked_commit_outputs :: CommitmentTx -> RevocationPubkey -> [UnresolvedOutput]
classify_revoked_commit_outputs !CommitmentTx
tx !RevocationPubkey
revpk =
(OutputType -> OutputResolution)
-> CommitmentTx -> [UnresolvedOutput]
classify_outputs OutputType -> OutputResolution
resolve CommitmentTx
tx
where
resolve :: OutputType -> OutputResolution
resolve OutputType
t = case OutputType
t of
OutputType
OutputToLocal -> RevocationPubkey -> OutputResolution
Revoke RevocationPubkey
revpk
OutputType
OutputToRemote -> OutputResolution
Resolved
OutputType
OutputLocalAnchor -> OutputResolution
Resolved
OutputType
OutputRemoteAnchor -> OutputResolution
Resolved
OutputHTLC HTLC
h -> RevocationPubkey -> HTLCOutputType -> OutputResolution
RevokeHTLC RevocationPubkey
revpk (HTLCOutputType -> OutputResolution)
-> HTLCOutputType -> OutputResolution
forall a b. (a -> b) -> a -> b
$ case HTLC -> HTLCDirection
htlc_direction HTLC
h of
HTLCDirection
HTLCOffered -> CltvExpiry -> HTLCOutputType
HTLCOfferedOutput (HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
h)
HTLCDirection
HTLCReceived -> CltvExpiry -> HTLCOutputType
HTLCReceivedOutput (HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
h)
classify_outputs
:: (OutputType -> OutputResolution)
-> CommitmentTx
-> [UnresolvedOutput]
classify_outputs :: (OutputType -> OutputResolution)
-> CommitmentTx -> [UnresolvedOutput]
classify_outputs OutputType -> OutputResolution
resolve CommitmentTx
tx =
(Word32 -> CommitmentOutput -> UnresolvedOutput)
-> [Word32] -> [CommitmentOutput] -> [UnresolvedOutput]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Word32 -> CommitmentOutput -> UnresolvedOutput
classify [Word32
0..] (NonEmpty CommitmentOutput -> [CommitmentOutput]
forall a. NonEmpty a -> [a]
NE.toList (CommitmentTx -> NonEmpty CommitmentOutput
ctx_outputs CommitmentTx
tx))
where
!txid :: TxId
txid = CommitmentTx -> TxId
commitment_txid CommitmentTx
tx
classify :: Word32 -> CommitmentOutput -> UnresolvedOutput
classify :: Word32 -> CommitmentOutput -> UnresolvedOutput
classify !Word32
idx !CommitmentOutput
out = UnresolvedOutput
{ uo_outpoint :: OutPoint
uo_outpoint = TxId -> Word32 -> OutPoint
OutPoint TxId
txid Word32
idx
, uo_value :: Satoshi
uo_value = CommitmentOutput -> Satoshi
co_value CommitmentOutput
out
, uo_type :: OutputResolution
uo_type = OutputType -> OutputResolution
resolve (CommitmentOutput -> OutputType
co_type CommitmentOutput
out)
}
extract_preimage_offered
:: PaymentHash
-> Witness
-> Maybe PaymentPreimage
!PaymentHash
ph (Witness [ByteString]
items) = case [ByteString]
items of
[ByteString
_sig, ByteString
preimage, ByteString
_script] -> PaymentHash -> ByteString -> Maybe PaymentPreimage
check_preimage PaymentHash
ph ByteString
preimage
[ByteString]
_ -> Maybe PaymentPreimage
forall a. Maybe a
Nothing
extract_preimage_htlc_success
:: PaymentHash
-> Witness
-> Maybe PaymentPreimage
!PaymentHash
ph (Witness [ByteString]
items) = case [ByteString]
items of
[ByteString
_zero, ByteString
_remote_sig, ByteString
_local_sig, ByteString
preimage, ByteString
_script] ->
PaymentHash -> ByteString -> Maybe PaymentPreimage
check_preimage PaymentHash
ph ByteString
preimage
[ByteString]
_ -> Maybe PaymentPreimage
forall a. Maybe a
Nothing
check_preimage :: PaymentHash -> BS.ByteString -> Maybe PaymentPreimage
check_preimage :: PaymentHash -> ByteString -> Maybe PaymentPreimage
check_preimage !PaymentHash
ph !ByteString
bs
| ByteString -> ByteString
SHA256.hash ByteString
bs ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== PaymentHash -> ByteString
BOLT1.un_payment_hash PaymentHash
ph = ByteString -> Maybe PaymentPreimage
BOLT1.payment_preimage ByteString
bs
| Bool
otherwise = Maybe PaymentPreimage
forall a. Maybe a
Nothing
htlc_timed_out :: Word32 -> HTLC -> Bool
htlc_timed_out :: Word32 -> HTLC -> Bool
htlc_timed_out !Word32
height !HTLC
htlc =
let CltvExpiry Word32
expiry = HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
htlc
in Word32
height Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word32
expiry
{-# INLINE htlc_timed_out #-}
commitment_txid :: CommitmentTx -> TxId
commitment_txid :: CommitmentTx -> TxId
commitment_txid = Tx -> TxId
BT.txid (Tx -> TxId) -> (CommitmentTx -> Tx) -> CommitmentTx -> TxId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommitmentTx -> Tx
commitment_to_tx
{-# INLINE commitment_txid #-}