module Data.Bitcoin.PaymentChannel.Internal.Bitcoin.Script where
import Data.Bitcoin.PaymentChannel.Internal.Types
import Data.Bitcoin.PaymentChannel.Internal.Serialization ()
import Data.Bitcoin.PaymentChannel.Internal.Util
import qualified Network.Haskoin.Internals as HI
import qualified Network.Haskoin.Crypto as HC
import qualified Data.ByteString as B
valReceiverSigHash = SigAll True
paymentChannelRedeemScript :: SendPubKey -> RecvPubKey -> BitcoinLockTime -> Script
paymentChannelRedeemScript clientPK serverPK lockTime =
let
encodeScriptInt = convertEmptyPush . opPushData . B.pack .
HI.encodeInt . fromIntegral . toWord32
convertEmptyPush (OP_PUSHDATA "" OPCODE) = OP_0
convertEmptyPush whatever = whatever
serverPubKey = getPubKey serverPK
clientPubKey = getPubKey clientPK
in Script
[OP_IF,
opPushData $ serialize serverPubKey, OP_CHECKSIGVERIFY,
OP_ELSE,
encodeScriptInt $ lockTime, op_CHECKLOCKTIMEVERIFY, OP_DROP,
OP_ENDIF,
opPushData $ serialize clientPubKey, OP_CHECKSIG]
paymentTxScriptSig :: PaymentSignature -> PaymentSignature -> Script --ScriptSig
paymentTxScriptSig clientSig serverSig = Script
[opPushData $ serialize clientSig,
opPushData $ serialize serverSig,
OP_1]
refundTxScriptSig :: HC.Signature -> Script
refundTxScriptSig clientSig = Script
[opPushData (B.append (serialize clientSig) hashTypeByte),
OP_0]
where hashTypeByte = serialize (SigAll False)
-----Util-----
op_CHECKLOCKTIMEVERIFY = OP_NOP2
scriptToP2SHAddress :: Script -> HC.Address
scriptToP2SHAddress = HC.ScriptAddress . HC.hash160 . HC.getHash256 . HC.hash256 . serialize
getP2SHFundingAddress :: ChannelParameters -> HC.Address
getP2SHFundingAddress = scriptToP2SHAddress . getRedeemScript
getRedeemScript :: ChannelParameters -> Script
getRedeemScript (CChannelParameters senderPK recvrPK lockTime _) =
paymentChannelRedeemScript senderPK recvrPK lockTime
getRedeemScriptBS :: ChannelParameters -> B.ByteString
getRedeemScriptBS = serialize . getRedeemScript
getP2SHInputScript :: ChannelParameters -> Script -> Script
getP2SHInputScript cp scriptSig =
Script $ scriptOps scriptSig ++ redeemScript
where
redeemScript = [opPushData $ getRedeemScriptBS cp]