module Data.Bitcoin.PaymentChannel.Types
(
PaymentChannel(..),
SenderPaymentChannel(..),
ReceiverPaymentChannel(..),
Payment,
FundingTxInfo(..),
ChannelParameters(..),
PayChanError(..),
PaymentChannelState,
SendPubKey(..),RecvPubKey(..),IsPubKey(..),
BitcoinAmount, toWord64,
BitcoinLockTime(..), fromDate,
usesBlockHeight,
b64Encode,
dUST_LIMIT,
mIN_CHANNEL_SIZE
)
where
import Data.Bitcoin.PaymentChannel.Internal.Types
(PaymentChannelState(..), Payment(..)
,FundingTxInfo(..), ChannelParameters(..),
PaymentSignature(..), SendPubKey(..), RecvPubKey(..), IsPubKey(..),
dUST_LIMIT, mIN_CHANNEL_SIZE)
import Data.Bitcoin.PaymentChannel.Internal.Serialization
import Data.Bitcoin.PaymentChannel.Internal.Util
(BitcoinAmount(..), toWord64,
BitcoinLockTime(..), fromDate, usesBlockHeight)
import qualified Data.Bitcoin.PaymentChannel.Internal.State as S
import Data.Bitcoin.PaymentChannel.Internal.Error (PayChanError(..))
import qualified Data.Binary as Bin
import Data.Aeson as JSON
import qualified Network.Haskoin.Crypto as HC
import qualified Network.Haskoin.Transaction as HT
import qualified Network.Haskoin.Script as HS
class PaymentChannel a where
valueToMe :: a -> BitcoinAmount
getChannelState :: a -> PaymentChannelState
getChannelID :: a -> HT.OutPoint
getExpirationDate :: a -> BitcoinLockTime
getNewestPayment :: a -> Payment
expiresBefore :: BitcoinLockTime -> a -> Bool
_setChannelState :: a -> PaymentChannelState -> a
channelValueLeft :: a -> BitcoinAmount
channelIsExhausted :: a -> Bool
getChannelID = S.pcsChannelID . getChannelState
getExpirationDate = S.pcsExpirationDate . getChannelState
channelValueLeft = S.channelValueLeft . getChannelState
channelIsExhausted = S.channelIsExhausted . getChannelState
expiresBefore expDate chan = getExpirationDate chan < expDate
getNewestPayment pcs = case S.pcsGetPayment (getChannelState pcs) of
Just payment -> payment
Nothing -> error "BUG: PaymentChannel interface shouldn't allow payment-less PaymentChannel"
data SenderPaymentChannel = CSenderPaymentChannel {
spcState :: PaymentChannelState,
spcSignFunc :: HC.Hash256 -> HC.Signature
}
newtype ReceiverPaymentChannel = CReceiverPaymentChannel {
rpcState :: PaymentChannelState
} deriving (Eq, Bin.Binary, FromJSON, ToJSON)
instance PaymentChannel SenderPaymentChannel where
valueToMe = channelValueLeft
getChannelState = spcState
_setChannelState spc s = spc { spcState = s }
instance PaymentChannel ReceiverPaymentChannel where
valueToMe rpc@(CReceiverPaymentChannel s) =
S.pcsChannelTotalValue s channelValueLeft rpc
getChannelState = rpcState
_setChannelState rpc s = rpc { rpcState = s }
instance Show SenderPaymentChannel where
show (CSenderPaymentChannel s _) =
"<SenderPaymentChannel:\n\t" ++ show s ++ ">"
instance Show ReceiverPaymentChannel where
show (CReceiverPaymentChannel s) =
"<ReceiverPaymentChannel:\n\t" ++ show s ++ ">"