{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE DeriveGeneric #-}

-- |
-- Module: Lightning.Protocol.BOLT7.Validate
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Stateless checks of BOLT #7 messages.

module Lightning.Protocol.BOLT7.Validate (
    ValidationError(..)
  , validate_channel_announcement
  , validate_node_announcement
  , validate_channel_update
  , validate_query_short_channel_ids
  , validate_query_channel_range
  , validate_reply_channel_range
  ) where

import Control.DeepSeq (NFData)
import Data.Word (Word64)
import GHC.Generics (Generic)
import Lightning.Protocol.BOLT1 (ShortChannelId)
import Lightning.Protocol.BOLT7.Messages
import Lightning.Protocol.BOLT7.Types

-- | A rule a message breaks.
data ValidationError
  = ValidateNodeIdOrdering
    -- ^ @node_id_1@ is not lexicographically less than @node_id_2@
  | ValidateMultipleDns
    -- ^ more than one DNS hostname descriptor
  | ValidateHtlcAmounts
    -- ^ @htlc_minimum_msat@ exceeds @htlc_maximum_msat@
  | ValidateZeroBlocks
    -- ^ @number_of_blocks@ is 0
  | ValidateBlockOverflow
    -- ^ the block range extends past block 2^32 - 1
  | ValidateScidNotAscending
    -- ^ short channel ids are not in strictly ascending order
  deriving (ValidationError -> ValidationError -> Bool
(ValidationError -> ValidationError -> Bool)
-> (ValidationError -> ValidationError -> Bool)
-> Eq ValidationError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ValidationError -> ValidationError -> Bool
== :: ValidationError -> ValidationError -> Bool
$c/= :: ValidationError -> ValidationError -> Bool
/= :: ValidationError -> ValidationError -> Bool
Eq, Int -> ValidationError -> ShowS
[ValidationError] -> ShowS
ValidationError -> String
(Int -> ValidationError -> ShowS)
-> (ValidationError -> String)
-> ([ValidationError] -> ShowS)
-> Show ValidationError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ValidationError -> ShowS
showsPrec :: Int -> ValidationError -> ShowS
$cshow :: ValidationError -> String
show :: ValidationError -> String
$cshowList :: [ValidationError] -> ShowS
showList :: [ValidationError] -> ShowS
Show, (forall x. ValidationError -> Rep ValidationError x)
-> (forall x. Rep ValidationError x -> ValidationError)
-> Generic ValidationError
forall x. Rep ValidationError x -> ValidationError
forall x. ValidationError -> Rep ValidationError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ValidationError -> Rep ValidationError x
from :: forall x. ValidationError -> Rep ValidationError x
$cto :: forall x. Rep ValidationError x -> ValidationError
to :: forall x. Rep ValidationError x -> ValidationError
Generic)

instance NFData ValidationError

-- | Check that @node_id_1@ is lexicographically less than @node_id_2@;
--   a receiver must ignore the message otherwise.
--
--   Signatures and feature bits are not checked here; see
--   'Lightning.Protocol.BOLT7.verify_channel_announcement' and
--   ppad-bolt9's @validate_remote@.
validate_channel_announcement
  :: ChannelAnnouncement -> Either ValidationError ()
validate_channel_announcement :: ChannelAnnouncement -> Either ValidationError ()
validate_channel_announcement ChannelAnnouncement
m
  | ChannelAnnouncement -> Point
ca_node_id_1 ChannelAnnouncement
m Point -> Point -> Bool
forall a. Ord a => a -> a -> Bool
< ChannelAnnouncement -> Point
ca_node_id_2 ChannelAnnouncement
m = () -> Either ValidationError ()
forall a b. b -> Either a b
Right ()
  | Bool
otherwise                       = ValidationError -> Either ValidationError ()
forall a b. a -> Either a b
Left ValidationError
ValidateNodeIdOrdering

-- | Check that at most one DNS hostname is announced; a receiver must
--   not forward the message otherwise.
--
--   Descriptors a receiver should merely ignore (Tor v2, port 0, a
--   hostname that isn't ASCII) are left to
--   'Lightning.Protocol.BOLT7.usable_addresses'.
validate_node_announcement :: NodeAnnouncement -> Either ValidationError ()
validate_node_announcement :: NodeAnnouncement -> Either ValidationError ()
validate_node_announcement NodeAnnouncement
m
  | [()] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ () | AddrDNS Hostname
_ Word16
_ <- NodeAnnouncement -> [Address]
na_addresses NodeAnnouncement
m ] Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 =
      ValidationError -> Either ValidationError ()
forall a b. a -> Either a b
Left ValidationError
ValidateMultipleDns
  | Bool
otherwise = () -> Either ValidationError ()
forall a b. b -> Either a b
Right ()

-- | Check that @htlc_minimum_msat@ does not exceed @htlc_maximum_msat@;
--   a receiver should not route through the channel otherwise.
validate_channel_update :: ChannelUpdate -> Either ValidationError ()
validate_channel_update :: ChannelUpdate -> Either ValidationError ()
validate_channel_update ChannelUpdate
m
  | ChannelUpdate -> MilliSatoshi
cu_htlc_minimum_msat ChannelUpdate
m MilliSatoshi -> MilliSatoshi -> Bool
forall a. Ord a => a -> a -> Bool
> ChannelUpdate -> MilliSatoshi
cu_htlc_maximum_msat ChannelUpdate
m = ValidationError -> Either ValidationError ()
forall a b. a -> Either a b
Left ValidationError
ValidateHtlcAmounts
  | Bool
otherwise = () -> Either ValidationError ()
forall a b. b -> Either a b
Right ()

-- | Check that the short channel ids are in strictly ascending order, as
--   their encoding requires.
validate_query_short_channel_ids
  :: QueryShortChannelIds -> Either ValidationError ()
validate_query_short_channel_ids :: QueryShortChannelIds -> Either ValidationError ()
validate_query_short_channel_ids = [ShortChannelId] -> Either ValidationError ()
ascending ([ShortChannelId] -> Either ValidationError ())
-> (QueryShortChannelIds -> [ShortChannelId])
-> QueryShortChannelIds
-> Either ValidationError ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QueryShortChannelIds -> [ShortChannelId]
qsci_short_channel_ids

-- | Check that @number_of_blocks@ is at least 1, as the sender must
--   ensure, and that the range ends at or before block 2^32 - 1 (a
--   library rule: such a range can't be represented).
validate_query_channel_range
  :: QueryChannelRange -> Either ValidationError ()
validate_query_channel_range :: QueryChannelRange -> Either ValidationError ()
validate_query_channel_range QueryChannelRange
m
  | Word64
num Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0                  = ValidationError -> Either ValidationError ()
forall a b. a -> Either a b
Left ValidationError
ValidateZeroBlocks
  | Word64
first Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
num Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
0x100000000 = ValidationError -> Either ValidationError ()
forall a b. a -> Either a b
Left ValidationError
ValidateBlockOverflow
  | Bool
otherwise                 = () -> Either ValidationError ()
forall a b. b -> Either a b
Right ()
  where
    first :: Word64
first = Word32 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (QueryChannelRange -> Word32
qcr_first_blocknum QueryChannelRange
m) :: Word64
    num :: Word64
num   = Word32 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (QueryChannelRange -> Word32
qcr_number_of_blocks QueryChannelRange
m) :: Word64

-- | Check that the short channel ids are in strictly ascending order, as
--   their encoding requires.
validate_reply_channel_range
  :: ReplyChannelRange -> Either ValidationError ()
validate_reply_channel_range :: ReplyChannelRange -> Either ValidationError ()
validate_reply_channel_range = [ShortChannelId] -> Either ValidationError ()
ascending ([ShortChannelId] -> Either ValidationError ())
-> (ReplyChannelRange -> [ShortChannelId])
-> ReplyChannelRange
-> Either ValidationError ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReplyChannelRange -> [ShortChannelId]
rcr_short_channel_ids

ascending :: [ShortChannelId] -> Either ValidationError ()
ascending :: [ShortChannelId] -> Either ValidationError ()
ascending (ShortChannelId
a : rest :: [ShortChannelId]
rest@(ShortChannelId
b : [ShortChannelId]
_))
  | ShortChannelId
a ShortChannelId -> ShortChannelId -> Bool
forall a. Ord a => a -> a -> Bool
< ShortChannelId
b     = [ShortChannelId] -> Either ValidationError ()
ascending [ShortChannelId]
rest
  | Bool
otherwise = ValidationError -> Either ValidationError ()
forall a b. a -> Either a b
Left ValidationError
ValidateScidNotAscending
ascending [ShortChannelId]
_ = () -> Either ValidationError ()
forall a b. b -> Either a b
Right ()