/
TxOutCBOR.hs
107 lines (99 loc) · 3.63 KB
/
TxOutCBOR.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
module Cardano.Wallet.DB.Store.DeltaUTxO.TxOutCBOR
( serializeTxOut
, deserializeTxOut
)
where
import Prelude
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (TokenBundle) )
import Cardano.Wallet.Primitive.Types.TokenMap
( AssetId (AssetId), fromFlatList, toFlatList )
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenName (UnsafeTokenName), TokenPolicyId (UnsafeTokenPolicyId) )
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (TokenQuantity) )
import Cardano.Wallet.Primitive.Types.Tx.TxOut
( TxOut (TxOut) )
import Codec.CBOR.Decoding
( Decoder, decodeBytes, decodeInteger, decodeListLen )
import Codec.CBOR.Encoding
( Encoding, encodeBytes, encodeInteger, encodeListLen )
import Codec.CBOR.Read
( deserialiseFromBytes )
import Codec.CBOR.Write
( toLazyByteString )
import Codec.Serialise
( DeserialiseFailure )
import Control.Exception
( Exception )
import Control.Monad
( replicateM )
import Data.ByteString.Lazy
( ByteString )
-- | Signal a failure to decode a 'TxOut' from a ByteString.
data FailedDecodingDeltaUTxO
= FailedPatternMatching String
| FailedDecoding DeserialiseFailure
deriving (Show, Eq)
instance Exception FailedDecodingDeltaUTxO
encodeTxOut :: TxOut -> Encoding
encodeTxOut (TxOut (Address addr) (TokenBundle (Coin c) m)) =
encodeListLen 3
<> encodeBytes addr
<> encodeInteger (fromIntegral c)
<> let tokens = toFlatList m
in encodeListLen (fromIntegral $ length tokens)
<> foldMap
( \( AssetId
(UnsafeTokenPolicyId (Hash policy))
(UnsafeTokenName name)
, TokenQuantity quant
) ->
encodeListLen 3
<> encodeBytes policy
<> encodeBytes name
<> encodeInteger (fromIntegral quant)
)
tokens
decodeTxOut :: Decoder s TxOut
decodeTxOut = do
len <- decodeListLen
case len of
3 -> do
addr <- decodeBytes
c <- decodeInteger
tokensLen <- decodeListLen
tokens <- replicateM tokensLen $ do
len' <- decodeListLen
case len' of
3 -> do
policy <- decodeBytes
name <- decodeBytes
quant <- decodeInteger
return
( AssetId
(UnsafeTokenPolicyId (Hash policy))
(UnsafeTokenName name)
, TokenQuantity $
fromIntegral quant
)
_ -> fail $ "decodeTxOut: expected 3, got " ++ show len
return $
TxOut
(Address addr)
( TokenBundle (Coin $ fromIntegral c) $
fromFlatList tokens
)
_ -> fail $ "decodeTxOut: expected 3, got " ++ show len
-- | Read a 'TxOut' from a binary blob.
deserializeTxOut :: ByteString -> Either DeserialiseFailure TxOut
deserializeTxOut = fmap snd . deserialiseFromBytes decodeTxOut
-- | Write a 'TxOut' to a binary blob.
serializeTxOut :: TxOut -> ByteString
serializeTxOut = toLazyByteString . encodeTxOut