{-# OPTIONS_HADDOCK prune #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Bitcoin.Prim.Tx (
Tx(..)
, TxIn(..)
, TxOut(..)
, OutPoint(..)
, Witness(..)
, TxId
, mk_txid
, un_txid
, null_txid
, to_bytes
, from_bytes
, to_bytes_legacy
, to_base16
, from_base16
, txid
) where
import Bitcoin.Prim.Tx.Internal
import qualified Crypto.Hash.SHA256 as SHA256
import Data.Bits ((.|.), shiftL)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Builder as BSB
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NE
import Data.Word (Word32, Word64)
mk_txid :: BS.ByteString -> Maybe TxId
mk_txid :: ByteString -> Maybe TxId
mk_txid ByteString
bs
| ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = TxId -> Maybe TxId
forall a. a -> Maybe a
Just (ByteString -> TxId
TxId ByteString
bs)
| Bool
otherwise = Maybe TxId
forall a. Maybe a
Nothing
un_txid :: TxId -> BS.ByteString
un_txid :: TxId -> ByteString
un_txid (TxId ByteString
bs) = ByteString
bs
{-# INLINE un_txid #-}
null_txid :: TxId
null_txid :: TxId
null_txid = ByteString -> TxId
TxId (Int -> Word8 -> ByteString
BS.replicate Int
32 Word8
0x00)
to_bytes :: Tx -> BS.ByteString
to_bytes :: Tx -> ByteString
to_bytes tx :: Tx
tx@Tx {Word32
NonEmpty TxOut
NonEmpty TxIn
tx_version :: Word32
tx_inputs :: NonEmpty TxIn
tx_outputs :: NonEmpty TxOut
tx_locktime :: Word32
tx_locktime :: Tx -> Word32
tx_outputs :: Tx -> NonEmpty TxOut
tx_inputs :: Tx -> NonEmpty TxIn
tx_version :: Tx -> Word32
..}
| Bool -> Bool
not ((TxIn -> Bool) -> NonEmpty TxIn -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Witness -> Bool
has_witness (Witness -> Bool) -> (TxIn -> Witness) -> TxIn -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxIn -> Witness
txin_witness) NonEmpty TxIn
tx_inputs) = Tx -> ByteString
to_bytes_legacy Tx
tx
| Bool
otherwise = Builder -> ByteString
to_strict (Builder -> ByteString) -> Builder -> ByteString
forall a b. (a -> b) -> a -> b
$
Word32 -> Builder
put_word32_le Word32
tx_version
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
BSB.word8 Word8
0x00
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
BSB.word8 Word8
0x01
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word64 -> Builder
put_compact (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NonEmpty TxIn -> Int
forall a. NonEmpty a -> Int
NE.length NonEmpty TxIn
tx_inputs))
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (TxIn -> Builder) -> NonEmpty TxIn -> Builder
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap TxIn -> Builder
put_txin NonEmpty TxIn
tx_inputs
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word64 -> Builder
put_compact (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NonEmpty TxOut -> Int
forall a. NonEmpty a -> Int
NE.length NonEmpty TxOut
tx_outputs))
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (TxOut -> Builder) -> NonEmpty TxOut -> Builder
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap TxOut -> Builder
put_txout NonEmpty TxOut
tx_outputs
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (TxIn -> Builder) -> NonEmpty TxIn -> Builder
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Witness -> Builder
put_witness (Witness -> Builder) -> (TxIn -> Witness) -> TxIn -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxIn -> Witness
txin_witness) NonEmpty TxIn
tx_inputs
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word32 -> Builder
put_word32_le Word32
tx_locktime
has_witness :: Witness -> Bool
has_witness :: Witness -> Bool
has_witness (Witness [ByteString]
items) = Bool -> Bool
not ([ByteString] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ByteString]
items)
{-# INLINE has_witness #-}
to_bytes_legacy :: Tx -> BS.ByteString
to_bytes_legacy :: Tx -> ByteString
to_bytes_legacy Tx {Word32
NonEmpty TxOut
NonEmpty TxIn
tx_locktime :: Tx -> Word32
tx_outputs :: Tx -> NonEmpty TxOut
tx_inputs :: Tx -> NonEmpty TxIn
tx_version :: Tx -> Word32
tx_version :: Word32
tx_inputs :: NonEmpty TxIn
tx_outputs :: NonEmpty TxOut
tx_locktime :: Word32
..} = Builder -> ByteString
to_strict (Builder -> ByteString) -> Builder -> ByteString
forall a b. (a -> b) -> a -> b
$
Word32 -> Builder
put_word32_le Word32
tx_version
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word64 -> Builder
put_compact (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NonEmpty TxIn -> Int
forall a. NonEmpty a -> Int
NE.length NonEmpty TxIn
tx_inputs))
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (TxIn -> Builder) -> NonEmpty TxIn -> Builder
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap TxIn -> Builder
put_txin NonEmpty TxIn
tx_inputs
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word64 -> Builder
put_compact (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NonEmpty TxOut -> Int
forall a. NonEmpty a -> Int
NE.length NonEmpty TxOut
tx_outputs))
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (TxOut -> Builder) -> NonEmpty TxOut -> Builder
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap TxOut -> Builder
put_txout NonEmpty TxOut
tx_outputs
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word32 -> Builder
put_word32_le Word32
tx_locktime
to_base16 :: Tx -> BS.ByteString
to_base16 :: Tx -> ByteString
to_base16 Tx
tx = ByteString -> ByteString
B16.encode (Tx -> ByteString
to_bytes Tx
tx)
from_base16 :: BS.ByteString -> Maybe Tx
from_base16 :: ByteString -> Maybe Tx
from_base16 ByteString
b16 = do
bs <- ByteString -> Maybe ByteString
B16.decode ByteString
b16
from_bytes bs
from_bytes :: BS.ByteString -> Maybe Tx
from_bytes :: ByteString -> Maybe Tx
from_bytes !ByteString
bs = do
Bool -> Maybe ()
guard (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
4)
let !version :: Word32
version = ByteString -> Int -> Word32
get_word32_le ByteString
bs Int
0
!off0 :: Int
off0 = Int
4
if ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
off0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
Bool -> Bool -> Bool
&& HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs Int
off0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x00
Bool -> Bool -> Bool
&& HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x01
then ByteString -> Word32 -> Int -> Maybe Tx
parse_segwit ByteString
bs Word32
version (Int
off0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
else ByteString -> Word32 -> Int -> Maybe Tx
parse_legacy ByteString
bs Word32
version Int
off0
parse_legacy :: BS.ByteString -> Word32 -> Int -> Maybe Tx
parse_legacy :: ByteString -> Word32 -> Int -> Maybe Tx
parse_legacy !ByteString
bs !Word32
version !Int
off0 = do
(input_count, off1) <- ByteString -> Int -> Maybe (Word64, Int)
get_compact ByteString
bs Int
off0
(inputs_list, off2) <- get_many get_txin bs off1 input_count
inputs <- NE.nonEmpty inputs_list
(output_count, off3) <- get_compact bs off2
(outputs_list, off4) <- get_many get_txout bs off3 output_count
outputs <- NE.nonEmpty outputs_list
guard (BS.length bs >= off4 + 4)
let !locktime = ByteString -> Int -> Word32
get_word32_le ByteString
bs Int
off4
!off5 = Int
off4 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
guard (off5 == BS.length bs)
pure $! Tx version inputs outputs locktime
parse_segwit :: BS.ByteString -> Word32 -> Int -> Maybe Tx
parse_segwit :: ByteString -> Word32 -> Int -> Maybe Tx
parse_segwit !ByteString
bs !Word32
version !Int
off0 = do
(input_count, off1) <- ByteString -> Int -> Maybe (Word64, Int)
get_compact ByteString
bs Int
off0
(inputs_list, off2) <- get_many get_txin bs off1 input_count
inputs <- NE.nonEmpty inputs_list
(output_count, off3) <- get_compact bs off2
(outputs_list, off4) <- get_many get_txout bs off3 output_count
outputs <- NE.nonEmpty outputs_list
(witnesses, off5) <- get_many get_witness bs off4 input_count
guard (any has_witness witnesses)
guard (BS.length bs >= off5 + 4)
let !locktime = ByteString -> Int -> Word32
get_word32_le ByteString
bs Int
off5
!off6 = Int
off5 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
guard (off6 == BS.length bs)
pure $! Tx version (attach_witnesses inputs witnesses) outputs locktime
attach_witnesses :: NonEmpty TxIn -> [Witness] -> NonEmpty TxIn
attach_witnesses :: NonEmpty TxIn -> [Witness] -> NonEmpty TxIn
attach_witnesses (TxIn
i :| [TxIn]
is) [Witness]
ws = case [Witness]
ws of
(Witness
w : [Witness]
rest) -> TxIn -> Witness -> TxIn
set TxIn
i Witness
w TxIn -> [TxIn] -> NonEmpty TxIn
forall a. a -> [a] -> NonEmpty a
:| (TxIn -> Witness -> TxIn) -> [TxIn] -> [Witness] -> [TxIn]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith TxIn -> Witness -> TxIn
set [TxIn]
is [Witness]
rest
[] -> TxIn
i TxIn -> [TxIn] -> NonEmpty TxIn
forall a. a -> [a] -> NonEmpty a
:| [TxIn]
is
where
set :: TxIn -> Witness -> TxIn
set TxIn
x Witness
w = TxIn
x { txin_witness = w }
guard :: Bool -> Maybe ()
guard :: Bool -> Maybe ()
guard Bool
True = () -> Maybe ()
forall a. a -> Maybe a
Just ()
guard Bool
False = Maybe ()
forall a. Maybe a
Nothing
{-# INLINE guard #-}
get_word32_le :: BS.ByteString -> Int -> Word32
get_word32_le :: ByteString -> Int -> Word32
get_word32_le !ByteString
bs !Int
off =
let !b0 :: Word32
b0 = Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs Int
off) :: Word32
!b1 :: Word32
b1 = Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) :: Word32
!b2 :: Word32
b2 = Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)) :: Word32
!b3 :: Word32
b3 = Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)) :: Word32
in Word32
b0 Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32
b1 Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`shiftL` Int
8) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32
b2 Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`shiftL` Int
16) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32
b3 Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`shiftL` Int
24)
{-# INLINE get_word32_le #-}
get_word64_le :: BS.ByteString -> Int -> Word64
get_word64_le :: ByteString -> Int -> Word64
get_word64_le !ByteString
bs !Int
off =
let !b0 :: Word64
b0 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs Int
off) :: Word64
!b1 :: Word64
b1 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) :: Word64
!b2 :: Word64
b2 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)) :: Word64
!b3 :: Word64
b3 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)) :: Word64
!b4 :: Word64
b4 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4)) :: Word64
!b5 :: Word64
b5 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5)) :: Word64
!b6 :: Word64
b6 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
6)) :: Word64
!b7 :: Word64
b7 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7)) :: Word64
in Word64
b0 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
b1 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
8) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
b2 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
16) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
b3 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
24)
Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
b4 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
32) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
b5 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
40)
Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
b6 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
48) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
b7 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
56)
{-# INLINE get_word64_le #-}
get_word16_le :: BS.ByteString -> Int -> Word64
get_word16_le :: ByteString -> Int -> Word64
get_word16_le !ByteString
bs !Int
off =
let !b0 :: Word64
b0 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs Int
off) :: Word64
!b1 :: Word64
b1 = Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) :: Word64
in Word64
b0 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64
b1 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
8)
{-# INLINE get_word16_le #-}
get_compact :: BS.ByteString -> Int -> Maybe (Word64, Int)
get_compact :: ByteString -> Int -> Maybe (Word64, Int)
get_compact !ByteString
bs !Int
off
| Int
off Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= ByteString -> Int
BS.length ByteString
bs = Maybe (Word64, Int)
forall a. Maybe a
Nothing
| Bool
otherwise = case HasCallStack => ByteString -> Int -> Word8
ByteString -> Int -> Word8
BS.index ByteString
bs Int
off of
Word8
tag | Word8
tag Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
0xfc ->
(Word64, Int) -> Maybe (Word64, Int)
forall a. a -> Maybe a
Just (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
tag, Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Word8
0xfd ->
if ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3
then Maybe (Word64, Int)
forall a. Maybe a
Nothing
else
let !val :: Word64
val = ByteString -> Int -> Word64
get_word16_le ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
in if Word64
val Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0xfd
then Maybe (Word64, Int)
forall a. Maybe a
Nothing
else (Word64, Int) -> Maybe (Word64, Int)
forall a. a -> Maybe a
Just (Word64
val, Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)
Word8
0xfe ->
if ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5
then Maybe (Word64, Int)
forall a. Maybe a
Nothing
else
let !val :: Word64
val = Word32 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int -> Word32
get_word32_le ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) :: Word64
in if Word64
val Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0xffff
then Maybe (Word64, Int)
forall a. Maybe a
Nothing
else (Word64, Int) -> Maybe (Word64, Int)
forall a. a -> Maybe a
Just (Word64
val, Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5)
Word8
_ ->
if ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
9
then Maybe (Word64, Int)
forall a. Maybe a
Nothing
else
let !val :: Word64
val = ByteString -> Int -> Word64
get_word64_le ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
in if Word64
val Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0xffffffff
then Maybe (Word64, Int)
forall a. Maybe a
Nothing
else (Word64, Int) -> Maybe (Word64, Int)
forall a. a -> Maybe a
Just (Word64
val, Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
9)
{-# INLINE get_compact #-}
get_outpoint :: BS.ByteString -> Int -> Maybe (OutPoint, Int)
get_outpoint :: ByteString -> Int -> Maybe (OutPoint, Int)
get_outpoint !ByteString
bs !Int
off
| ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
36 = Maybe (OutPoint, Int)
forall a. Maybe a
Nothing
| Bool
otherwise =
let !txid_bytes :: ByteString
txid_bytes = Int -> ByteString -> ByteString
BS.take Int
32 (Int -> ByteString -> ByteString
BS.drop Int
off ByteString
bs)
!vout :: Word32
vout = ByteString -> Int -> Word32
get_word32_le ByteString
bs (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
32)
in (OutPoint, Int) -> Maybe (OutPoint, Int)
forall a. a -> Maybe a
Just (TxId -> Word32 -> OutPoint
OutPoint (ByteString -> TxId
TxId ByteString
txid_bytes) Word32
vout, Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
36)
{-# INLINE get_outpoint #-}
get_txin :: BS.ByteString -> Int -> Maybe (TxIn, Int)
get_txin :: ByteString -> Int -> Maybe (TxIn, Int)
get_txin !ByteString
bs !Int
off0 = do
(outpoint, off1) <- ByteString -> Int -> Maybe (OutPoint, Int)
get_outpoint ByteString
bs Int
off0
(script_sig, off3) <- get_bytes bs off1
guard (BS.length bs >= off3 + 4)
let !seqn = ByteString -> Int -> Word32
get_word32_le ByteString
bs Int
off3
!off4 = Int
off3 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
pure (TxIn outpoint script_sig seqn (Witness []), off4)
get_txout :: BS.ByteString -> Int -> Maybe (TxOut, Int)
get_txout :: ByteString -> Int -> Maybe (TxOut, Int)
get_txout !ByteString
bs !Int
off0 = do
Bool -> Maybe ()
guard (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
off0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8)
let !value :: Word64
value = ByteString -> Int -> Word64
get_word64_le ByteString
bs Int
off0
!off1 :: Int
off1 = Int
off0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8
(script_pk, off2) <- ByteString -> Int -> Maybe (ByteString, Int)
get_bytes ByteString
bs Int
off1
pure (TxOut value script_pk, off2)
get_witness :: BS.ByteString -> Int -> Maybe (Witness, Int)
get_witness :: ByteString -> Int -> Maybe (Witness, Int)
get_witness !ByteString
bs !Int
off0 = do
(item_count, off1) <- ByteString -> Int -> Maybe (Word64, Int)
get_compact ByteString
bs Int
off0
(items, off2) <- get_many get_bytes bs off1 item_count
pure (Witness items, off2)
get_bytes :: BS.ByteString -> Int -> Maybe (BS.ByteString, Int)
get_bytes :: ByteString -> Int -> Maybe (ByteString, Int)
get_bytes !ByteString
bs !Int
off0 = do
(len, off1) <- ByteString -> Int -> Maybe (Word64, Int)
get_compact ByteString
bs Int
off0
guard (len <= fromIntegral (BS.length bs - off1))
let !n = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
len
pure (BS.take n (BS.drop off1 bs), off1 + n)
get_many :: (BS.ByteString -> Int -> Maybe (a, Int))
-> BS.ByteString -> Int -> Word64 -> Maybe ([a], Int)
get_many :: forall a.
(ByteString -> Int -> Maybe (a, Int))
-> ByteString -> Int -> Word64 -> Maybe ([a], Int)
get_many ByteString -> Int -> Maybe (a, Int)
getter !ByteString
bs !Int
off0 !Word64
count
| Word64
count Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
off0) = Maybe ([a], Int)
forall a. Maybe a
Nothing
| Bool
otherwise = [a] -> Int -> Int -> Maybe ([a], Int)
forall {t}. (Ord t, Num t) => [a] -> Int -> t -> Maybe ([a], Int)
go [] Int
off0 (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
count :: Int)
where
go :: [a] -> Int -> t -> Maybe ([a], Int)
go ![a]
acc !Int
off !t
n
| t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = ([a], Int) -> Maybe ([a], Int)
forall a. a -> Maybe a
Just ([a] -> [a]
forall a. [a] -> [a]
reverse [a]
acc, Int
off)
| Bool
otherwise = do
(item, off') <- ByteString -> Int -> Maybe (a, Int)
getter ByteString
bs Int
off
go (item : acc) off' (n - 1)
{-# INLINE get_many #-}
txid :: Tx -> TxId
txid :: Tx -> TxId
txid Tx
tx = ByteString -> TxId
TxId (ByteString -> ByteString
SHA256.hash (ByteString -> ByteString
SHA256.hash (Tx -> ByteString
to_bytes_legacy Tx
tx)))