{-# OPTIONS_HADDOCK hide #-}
module Bitcoin.Address.Internal
 ( op0to16
 , scriptBytes
 , hush
 ) where

import qualified Data.Binary as Bin
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.Bitcoin.Script as S

--------------------------------------------------------------------------------

-- | Converts the numbers [0 … 16] to the corresponding ['S.OP_0' … 'S.OP_16'].
op0to16 :: Int -> Maybe S.ScriptOp
op0to16 :: Int -> Maybe ScriptOp
op0to16 i :: Int
i = case Int
i of
  0  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_0
  1  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_1
  2  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_2
  3  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_3
  4  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_4
  5  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_5
  6  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_6
  7  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_7
  8  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_8
  9  -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_9
  10 -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_10
  11 -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_11
  12 -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_12
  13 -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_13
  14 -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_14
  15 -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_15
  16 -> ScriptOp -> Maybe ScriptOp
forall a. a -> Maybe a
Just ScriptOp
S.OP_16
  _  -> Maybe ScriptOp
forall a. Maybe a
Nothing

-------------------------------------------------------------------------------

-- | Obtain the raw 'S.Script' bytes.
scriptBytes :: S.Script -> B.ByteString
scriptBytes :: Script -> ByteString
scriptBytes = ByteString -> ByteString
BL.toStrict (ByteString -> ByteString)
-> (Script -> ByteString) -> Script -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Script -> ByteString
forall a. Binary a => a -> ByteString
Bin.encode
{-# INLINE scriptBytes #-}

--------------------------------------------------------------------------------

hush :: Either a b -> Maybe b
hush :: Either a b -> Maybe b
hush = (a -> Maybe b) -> (b -> Maybe b) -> Either a b -> Maybe b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\_ -> Maybe b
forall a. Maybe a
Nothing) b -> Maybe b
forall a. a -> Maybe a
Just
{-# INLINE hush #-}