{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeSynonymInstances #-}
module PaymentChannel.Internal.Types
( module PaymentChannel.Internal.Types
, RBPCP.SharedSecret
, fromInitialPayment
, module X
, module Network.Haskoin.Transaction
, module Network.Haskoin.Crypto
, module Network.Haskoin.Script
, module Data.List.NonEmpty
, module Control.Monad.Time
, Word32, Word64, NFData
) where
import Bitcoin.LockTime.Util as X
import Bitcoin.SinglePair as X
import Bitcoin.SpendCond.Cond as X
import Bitcoin.Types as X
import Control.DeepSeq (NFData)
import PaymentChannel.Internal.ChanScript as X
import PaymentChannel.Internal.Config as X
import PaymentChannel.Internal.Crypto.PubKey as X
import PaymentChannel.Internal.Types.MonadConf as X
import PaymentChannel.Internal.Util as X
import qualified RBPCP.Types as RBPCP (SharedSecret(..))
import Network.Haskoin.Transaction hiding (signTx)
import Network.Haskoin.Crypto hiding
(DerivPathI (..),
PubKey, hash160,
hash256)
import qualified Network.Haskoin.Crypto as HC
import Network.Haskoin.Script
import qualified Network.Haskoin.Transaction as HT
import Control.Monad.Time
import qualified Data.ByteString.Base16 as B16
import Data.List.NonEmpty (NonEmpty (..))
import Data.Maybe (fromMaybe)
import qualified Data.Serialize as Bin
import Data.Word
import GHC.Generics (Generic)
type Payment = SigSinglePair P2SH ChanParams
type SignedPayment = Payment BtcSig
type UnsignedPayment = Payment ()
instance HasSendPubKey (Payment a) where getSendPubKey = getSendPubKey . pairRedeemScript
instance HasRecvPubKey (Payment a) where getRecvPubKey = getRecvPubKey . pairRedeemScript
instance HasLockTimeDate (Payment a) where
getLockTimeDate = cpLockTime . pairRedeemScript
data PayChanState sigData = MkPayChanState
{ pcsPayment :: Payment sigData
, pcsSecret :: RBPCP.SharedSecret
, pcsSettings :: ServerSettings
} deriving (Eq, Show, Typeable, Generic, Serialize, ToJSON, FromJSON, NFData)
instance HasLockTimeDate (PayChanState a) where
getLockTimeDate = getLockTimeDate . pcsPayment
fromInitialPayment :: SigSinglePair t r BtcSig -> RBPCP.SharedSecret
fromInitialPayment =
RBPCP.SharedSecret . HC.hash256 . Bin.encode . getSigData
instance HasSendPubKey (PayChanState a) where getSendPubKey = getSendPubKey . pcsPayment
instance HasRecvPubKey (PayChanState a) where getRecvPubKey = getRecvPubKey . pcsPayment
instance HasSigData PayChanState where
mapSigData f pcs@MkPayChanState{..} =
pcs { pcsPayment =
mapSigData f pcsPayment
}
instance SetClientChangeAddr PayChanState where
_setClientChangeAddr pcs@MkPayChanState{..} addr =
pcs { pcsPayment =
_setClientChangeAddr pcsPayment addr
}
type EmptyClientPayChan = ClientPayChanI ()
type ClientPayChan = ClientPayChanI BtcSig
data ClientPayChanI sigData = MkClientPayChan
{
spcState :: PayChanState sigData
, spcPrvKey :: HC.PrvKeyC
} deriving (Eq, Typeable, Generic, NFData)
instance HasSendPubKey (ClientPayChanI a) where getSendPubKey = getSendPubKey . spcState
instance HasRecvPubKey (ClientPayChanI a) where getRecvPubKey = getRecvPubKey . spcState
instance HasSigData ClientPayChanI where
mapSigData f cpc@MkClientPayChan{..} =
cpc { spcState =
mapSigData f spcState
}
instance SetClientChangeAddr ClientPayChanI where
_setClientChangeAddr cpc@MkClientPayChan{..} addr =
cpc { spcState =
_setClientChangeAddr spcState addr
}
data FundingTxInfo = CFundingTxInfo {
ftiHash :: HT.TxHash,
ftiOutIndex :: Word32,
ftiOutValue :: NonDustyAmount
} deriving (Eq, Show, Typeable, Generic)
instance Serialize FundingTxInfo where
put (CFundingTxInfo h idx val) =
put h >> putWord32be idx >> put val
get = CFundingTxInfo <$> get <*> getWord32be <*> get
instance ToJSON FundingTxInfo
instance FromJSON FundingTxInfo
instance Show ClientPayChan where
show (MkClientPayChan s _) =
"<ClientPayChanI:\n\t" ++ show s ++ ">"