module Data.Bitcoin.PaymentChannel.Types
(
PaymentChannel(..),
SenderPaymentChannel(..),
ReceiverPaymentChannel(..),
Config(..),defaultConfig,
Payment,cpSignature,
FullPayment(..),
FundingTxInfo(..),
ChannelParameters(..),
PayChanError(..),
PaymentChannelState,
SendPubKey(..),RecvPubKey(..),IsPubKey(..),
BitcoinAmount,
BitcoinLockTime(..), fromDate,
usesBlockHeight
)
where
import Data.Bitcoin.PaymentChannel.Internal.Types
import Data.Bitcoin.PaymentChannel.Internal.Serialization ()
import qualified Data.Bitcoin.PaymentChannel.Internal.State as S
import qualified Data.Bitcoin.PaymentChannel.Internal.Bitcoin.Script as Script
import Data.Bitcoin.PaymentChannel.Internal.Error (PayChanError(..))
import qualified Data.Serialize as Bin
import qualified Network.Haskoin.Crypto as HC
import qualified Network.Haskoin.Transaction as HT
import Data.Word (Word64)
class PaymentChannel a where
valueToMe :: a -> BitcoinAmount
getChannelState :: a -> PaymentChannelState
getChannelFunding :: a -> HT.OutPoint
getExpirationDate :: a -> BitcoinLockTime
getSenderPubKey :: a -> SendPubKey
getFundingAmount :: a -> BitcoinAmount
getPaymentCount :: a -> Word64
fundingAddress :: a -> HC.Address
getNewestPayment :: a -> Payment
getNewestSig :: a -> HC.Signature
expiresBefore :: BitcoinLockTime -> a -> Bool
_setChannelState :: a -> PaymentChannelState -> a
channelValueLeft :: a -> BitcoinAmount
senderChangeValue :: a -> BitcoinAmount
channelIsExhausted :: a -> Bool
getChannelFunding = S.pcsChannelFundingSource . getChannelState
getExpirationDate = S.pcsExpirationDate . getChannelState
getSenderPubKey = S.pcsClientPubKey . getChannelState
getFundingAmount = S.pcsChannelTotalValue . getChannelState
getPaymentCount = pcsPaymentCount . getChannelState
fundingAddress = Script.getP2SHFundingAddress . pcsParameters . getChannelState
senderChangeValue = pcsClientChangeVal . getChannelState
channelValueLeft = S.channelValueLeft . getChannelState
channelIsExhausted = S.channelIsExhausted . getChannelState
expiresBefore expDate chan = getExpirationDate chan < expDate
getNewestPayment pcs = S.pcsGetPayment (getChannelState pcs)
getNewestSig = psSig . cpSignature . getNewestPayment
data SenderPaymentChannel = CSenderPaymentChannel {
spcState :: PaymentChannelState,
spcSignFunc :: HC.Hash256 -> HC.Signature
}
newtype ReceiverPaymentChannel = CReceiverPaymentChannel {
rpcState :: PaymentChannelState
} deriving (Eq, Bin.Serialize)
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 ++ ">"