{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Cometa.Address.Byron
( Address,
Root,
rootFromBytes,
rootBytes,
AddressType (..),
Attributes,
attributes,
derivationPayload,
networkMagic,
address,
root,
addressType,
addressAttributes,
fromBytes,
toBytes,
fromText,
toText,
)
where
import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import qualified Codec.CBOR.Read as R
import qualified Codec.CBOR.Write as W
import Cometa.Error (Error (..))
import Control.Monad (foldM, replicateM, unless)
import Data.Bits (complement, shiftR, xor, (.&.))
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Data.Int (Int64)
import qualified Data.List as List
import Data.Maybe (isNothing)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Word (Word32, Word64)
newtype Root = Root ByteString deriving (Root -> Root -> Bool
(Root -> Root -> Bool) -> (Root -> Root -> Bool) -> Eq Root
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Root -> Root -> Bool
== :: Root -> Root -> Bool
$c/= :: Root -> Root -> Bool
/= :: Root -> Root -> Bool
Eq, Eq Root
Eq Root =>
(Root -> Root -> Ordering)
-> (Root -> Root -> Bool)
-> (Root -> Root -> Bool)
-> (Root -> Root -> Bool)
-> (Root -> Root -> Bool)
-> (Root -> Root -> Root)
-> (Root -> Root -> Root)
-> Ord Root
Root -> Root -> Bool
Root -> Root -> Ordering
Root -> Root -> Root
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Root -> Root -> Ordering
compare :: Root -> Root -> Ordering
$c< :: Root -> Root -> Bool
< :: Root -> Root -> Bool
$c<= :: Root -> Root -> Bool
<= :: Root -> Root -> Bool
$c> :: Root -> Root -> Bool
> :: Root -> Root -> Bool
$c>= :: Root -> Root -> Bool
>= :: Root -> Root -> Bool
$cmax :: Root -> Root -> Root
max :: Root -> Root -> Root
$cmin :: Root -> Root -> Root
min :: Root -> Root -> Root
Ord, Int -> Root -> ShowS
[Root] -> ShowS
Root -> String
(Int -> Root -> ShowS)
-> (Root -> String) -> ([Root] -> ShowS) -> Show Root
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Root -> ShowS
showsPrec :: Int -> Root -> ShowS
$cshow :: Root -> String
show :: Root -> String
$cshowList :: [Root] -> ShowS
showList :: [Root] -> ShowS
Show)
data AddressType
=
PublicKey
|
Script
|
Redeem
deriving (AddressType -> AddressType -> Bool
(AddressType -> AddressType -> Bool)
-> (AddressType -> AddressType -> Bool) -> Eq AddressType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AddressType -> AddressType -> Bool
== :: AddressType -> AddressType -> Bool
$c/= :: AddressType -> AddressType -> Bool
/= :: AddressType -> AddressType -> Bool
Eq, Eq AddressType
Eq AddressType =>
(AddressType -> AddressType -> Ordering)
-> (AddressType -> AddressType -> Bool)
-> (AddressType -> AddressType -> Bool)
-> (AddressType -> AddressType -> Bool)
-> (AddressType -> AddressType -> Bool)
-> (AddressType -> AddressType -> AddressType)
-> (AddressType -> AddressType -> AddressType)
-> Ord AddressType
AddressType -> AddressType -> Bool
AddressType -> AddressType -> Ordering
AddressType -> AddressType -> AddressType
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: AddressType -> AddressType -> Ordering
compare :: AddressType -> AddressType -> Ordering
$c< :: AddressType -> AddressType -> Bool
< :: AddressType -> AddressType -> Bool
$c<= :: AddressType -> AddressType -> Bool
<= :: AddressType -> AddressType -> Bool
$c> :: AddressType -> AddressType -> Bool
> :: AddressType -> AddressType -> Bool
$c>= :: AddressType -> AddressType -> Bool
>= :: AddressType -> AddressType -> Bool
$cmax :: AddressType -> AddressType -> AddressType
max :: AddressType -> AddressType -> AddressType
$cmin :: AddressType -> AddressType -> AddressType
min :: AddressType -> AddressType -> AddressType
Ord, Int -> AddressType -> ShowS
[AddressType] -> ShowS
AddressType -> String
(Int -> AddressType -> ShowS)
-> (AddressType -> String)
-> ([AddressType] -> ShowS)
-> Show AddressType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AddressType -> ShowS
showsPrec :: Int -> AddressType -> ShowS
$cshow :: AddressType -> String
show :: AddressType -> String
$cshowList :: [AddressType] -> ShowS
showList :: [AddressType] -> ShowS
Show, Int -> AddressType
AddressType -> Int
AddressType -> [AddressType]
AddressType -> AddressType
AddressType -> AddressType -> [AddressType]
AddressType -> AddressType -> AddressType -> [AddressType]
(AddressType -> AddressType)
-> (AddressType -> AddressType)
-> (Int -> AddressType)
-> (AddressType -> Int)
-> (AddressType -> [AddressType])
-> (AddressType -> AddressType -> [AddressType])
-> (AddressType -> AddressType -> [AddressType])
-> (AddressType -> AddressType -> AddressType -> [AddressType])
-> Enum AddressType
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: AddressType -> AddressType
succ :: AddressType -> AddressType
$cpred :: AddressType -> AddressType
pred :: AddressType -> AddressType
$ctoEnum :: Int -> AddressType
toEnum :: Int -> AddressType
$cfromEnum :: AddressType -> Int
fromEnum :: AddressType -> Int
$cenumFrom :: AddressType -> [AddressType]
enumFrom :: AddressType -> [AddressType]
$cenumFromThen :: AddressType -> AddressType -> [AddressType]
enumFromThen :: AddressType -> AddressType -> [AddressType]
$cenumFromTo :: AddressType -> AddressType -> [AddressType]
enumFromTo :: AddressType -> AddressType -> [AddressType]
$cenumFromThenTo :: AddressType -> AddressType -> AddressType -> [AddressType]
enumFromThenTo :: AddressType -> AddressType -> AddressType -> [AddressType]
Enum, AddressType
AddressType -> AddressType -> Bounded AddressType
forall a. a -> a -> Bounded a
$cminBound :: AddressType
minBound :: AddressType
$cmaxBound :: AddressType
maxBound :: AddressType
Bounded)
data Attributes = Attributes !(Maybe ByteString) !(Maybe Word64) deriving (Attributes -> Attributes -> Bool
(Attributes -> Attributes -> Bool)
-> (Attributes -> Attributes -> Bool) -> Eq Attributes
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Attributes -> Attributes -> Bool
== :: Attributes -> Attributes -> Bool
$c/= :: Attributes -> Attributes -> Bool
/= :: Attributes -> Attributes -> Bool
Eq, Eq Attributes
Eq Attributes =>
(Attributes -> Attributes -> Ordering)
-> (Attributes -> Attributes -> Bool)
-> (Attributes -> Attributes -> Bool)
-> (Attributes -> Attributes -> Bool)
-> (Attributes -> Attributes -> Bool)
-> (Attributes -> Attributes -> Attributes)
-> (Attributes -> Attributes -> Attributes)
-> Ord Attributes
Attributes -> Attributes -> Bool
Attributes -> Attributes -> Ordering
Attributes -> Attributes -> Attributes
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Attributes -> Attributes -> Ordering
compare :: Attributes -> Attributes -> Ordering
$c< :: Attributes -> Attributes -> Bool
< :: Attributes -> Attributes -> Bool
$c<= :: Attributes -> Attributes -> Bool
<= :: Attributes -> Attributes -> Bool
$c> :: Attributes -> Attributes -> Bool
> :: Attributes -> Attributes -> Bool
$c>= :: Attributes -> Attributes -> Bool
>= :: Attributes -> Attributes -> Bool
$cmax :: Attributes -> Attributes -> Attributes
max :: Attributes -> Attributes -> Attributes
$cmin :: Attributes -> Attributes -> Attributes
min :: Attributes -> Attributes -> Attributes
Ord, Int -> Attributes -> ShowS
[Attributes] -> ShowS
Attributes -> String
(Int -> Attributes -> ShowS)
-> (Attributes -> String)
-> ([Attributes] -> ShowS)
-> Show Attributes
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Attributes -> ShowS
showsPrec :: Int -> Attributes -> ShowS
$cshow :: Attributes -> String
show :: Attributes -> String
$cshowList :: [Attributes] -> ShowS
showList :: [Attributes] -> ShowS
Show)
data Address = Address !ByteString !Root !Attributes !AddressType deriving (Address -> Address -> Bool
(Address -> Address -> Bool)
-> (Address -> Address -> Bool) -> Eq Address
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Address -> Address -> Bool
== :: Address -> Address -> Bool
$c/= :: Address -> Address -> Bool
/= :: Address -> Address -> Bool
Eq, Eq Address
Eq Address =>
(Address -> Address -> Ordering)
-> (Address -> Address -> Bool)
-> (Address -> Address -> Bool)
-> (Address -> Address -> Bool)
-> (Address -> Address -> Bool)
-> (Address -> Address -> Address)
-> (Address -> Address -> Address)
-> Ord Address
Address -> Address -> Bool
Address -> Address -> Ordering
Address -> Address -> Address
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Address -> Address -> Ordering
compare :: Address -> Address -> Ordering
$c< :: Address -> Address -> Bool
< :: Address -> Address -> Bool
$c<= :: Address -> Address -> Bool
<= :: Address -> Address -> Bool
$c> :: Address -> Address -> Bool
> :: Address -> Address -> Bool
$c>= :: Address -> Address -> Bool
>= :: Address -> Address -> Bool
$cmax :: Address -> Address -> Address
max :: Address -> Address -> Address
$cmin :: Address -> Address -> Address
min :: Address -> Address -> Address
Ord, Int -> Address -> ShowS
[Address] -> ShowS
Address -> String
(Int -> Address -> ShowS)
-> (Address -> String) -> ([Address] -> ShowS) -> Show Address
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Address -> ShowS
showsPrec :: Int -> Address -> ShowS
$cshow :: Address -> String
show :: Address -> String
$cshowList :: [Address] -> ShowS
showList :: [Address] -> ShowS
Show)
rootFromBytes :: ByteString -> Either Error Root
rootFromBytes :: ByteString -> Either Error Root
rootFromBytes ByteString
bytes
| ByteString -> Int
B.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
28 = Root -> Either Error Root
forall a b. b -> Either a b
Right (ByteString -> Root
Root ByteString
bytes)
| Bool
otherwise = Error -> Either Error Root
forall a b. a -> Either a b
Left (Text -> Error
InvalidByronAddress Text
"Expected a 28-byte Byron root")
rootBytes :: Root -> ByteString
rootBytes :: Root -> ByteString
rootBytes (Root ByteString
bytes) = ByteString
bytes
attributes :: Maybe ByteString -> Maybe Word64 -> Either Error Attributes
attributes :: Maybe ByteString -> Maybe Word64 -> Either Error Attributes
attributes Maybe ByteString
payload Maybe Word64
magic
| Bool -> (ByteString -> Bool) -> Maybe ByteString -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False ((Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
64) (Int -> Bool) -> (ByteString -> Int) -> ByteString -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
B.length) Maybe ByteString
payload = Error -> Either Error Attributes
forall a b. a -> Either a b
Left (Text -> Error
InvalidByronAddress Text
"Derivation payload exceeds 64 bytes")
| Bool -> (Word64 -> Bool) -> Maybe Word64 -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int64 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64
forall a. Bounded a => a
maxBound :: Int64)) Maybe Word64
magic = Error -> Either Error Attributes
forall a b. a -> Either a b
Left (Text -> Error
InvalidByronAddress Text
"Network magic exceeds 2^63-1")
| Bool
otherwise = Attributes -> Either Error Attributes
forall a b. b -> Either a b
Right (Maybe ByteString -> Maybe Word64 -> Attributes
Attributes (Maybe ByteString
payload Maybe ByteString
-> (ByteString -> Maybe ByteString) -> Maybe ByteString
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Maybe ByteString
nonempty) Maybe Word64
magic)
where
nonempty :: ByteString -> Maybe ByteString
nonempty ByteString
bytes = if ByteString -> Bool
B.null ByteString
bytes then Maybe ByteString
forall a. Maybe a
Nothing else ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
bytes
derivationPayload :: Attributes -> Maybe ByteString
derivationPayload :: Attributes -> Maybe ByteString
derivationPayload (Attributes Maybe ByteString
payload Maybe Word64
_) = Maybe ByteString
payload
networkMagic :: Attributes -> Maybe Word64
networkMagic :: Attributes -> Maybe Word64
networkMagic (Attributes Maybe ByteString
_ Maybe Word64
magic) = Maybe Word64
magic
address :: Root -> Attributes -> AddressType -> Address
address :: Root -> Attributes -> AddressType -> Address
address Root
digest Attributes
attrs AddressType
kind = ByteString -> Root -> Attributes -> AddressType -> Address
Address ByteString
bytes Root
digest Attributes
attrs AddressType
kind
where
payload :: ByteString
payload = Encoding -> ByteString
W.toStrictByteString (Word -> Encoding
E.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (Root -> ByteString
rootBytes Root
digest) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Attributes -> Encoding
encodeAttributes Attributes
attrs Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
E.encodeWord (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (AddressType -> Int
forall a. Enum a => a -> Int
fromEnum AddressType
kind)))
bytes :: ByteString
bytes = Encoding -> ByteString
W.toStrictByteString (Word -> Encoding
E.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
E.encodeTag Word
24 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes ByteString
payload Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word32 -> Encoding
E.encodeWord32 (ByteString -> Word32
crc32 ByteString
payload))
root :: Address -> Root
root :: Address -> Root
root (Address ByteString
_ Root
value Attributes
_ AddressType
_) = Root
value
addressType :: Address -> AddressType
addressType :: Address -> AddressType
addressType (Address ByteString
_ Root
_ Attributes
_ AddressType
value) = AddressType
value
addressAttributes :: Address -> Attributes
addressAttributes :: Address -> Attributes
addressAttributes (Address ByteString
_ Root
_ Attributes
value AddressType
_) = Attributes
value
toBytes :: Address -> ByteString
toBytes :: Address -> ByteString
toBytes (Address ByteString
bytes Root
_ Attributes
_ AddressType
_) = ByteString
bytes
fromBytes :: ByteString -> Either Error Address
fromBytes :: ByteString -> Either Error Address
fromBytes ByteString
bytes = do
ByteString
payload <- (forall s. Decoder s ByteString)
-> ByteString -> Either Error ByteString
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decode Decoder s ByteString
forall s. Decoder s ByteString
outer ByteString
bytes
(Root
digest, Attributes
attrs, AddressType
kind) <- (forall s. Decoder s (Root, Attributes, AddressType))
-> ByteString -> Either Error (Root, Attributes, AddressType)
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decode Decoder s (Root, Attributes, AddressType)
forall s. Decoder s (Root, Attributes, AddressType)
inner ByteString
payload
Address -> Either Error Address
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Root -> Attributes -> AddressType -> Address
Address ByteString
bytes Root
digest Attributes
attrs AddressType
kind)
where
outer :: Decoder s ByteString
outer = Int -> Decoder s ByteString -> Decoder s ByteString
forall s a. Int -> Decoder s a -> Decoder s a
array Int
2 (Decoder s ByteString -> Decoder s ByteString)
-> Decoder s ByteString -> Decoder s ByteString
forall a b. (a -> b) -> a -> b
$ do
Word
tag <- Decoder s Word
forall s. Decoder s Word
D.decodeTag
Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word
tag Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
24) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected encoded-CBOR tag 24")
ByteString
payload <- Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes
Word32
checksum <- Decoder s Word32
forall s. Decoder s Word32
D.decodeWord32
Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word32
checksum Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Word32
crc32 ByteString
payload) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Byron checksum mismatch")
ByteString -> Decoder s ByteString
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
payload
inner :: Decoder s (Root, Attributes, AddressType)
inner = Int
-> Decoder s (Root, Attributes, AddressType)
-> Decoder s (Root, Attributes, AddressType)
forall s a. Int -> Decoder s a -> Decoder s a
array Int
3 (Decoder s (Root, Attributes, AddressType)
-> Decoder s (Root, Attributes, AddressType))
-> Decoder s (Root, Attributes, AddressType)
-> Decoder s (Root, Attributes, AddressType)
forall a b. (a -> b) -> a -> b
$ do
Root
digest <- Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s Root) -> Decoder s Root
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (ByteString -> Either Error Root) -> ByteString -> Decoder s Root
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validate ByteString -> Either Error Root
rootFromBytes
Attributes
attrs <- Decoder s Attributes
forall s. Decoder s Attributes
decodeAttributes
Word
code <- Decoder s Word
forall s. Decoder s Word
D.decodeWord
AddressType
kind <- case Word
code of
Word
0 -> AddressType -> Decoder s AddressType
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AddressType
PublicKey
Word
1 -> AddressType -> Decoder s AddressType
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AddressType
Script
Word
2 -> AddressType -> Decoder s AddressType
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AddressType
Redeem
Word
_ -> String -> Decoder s AddressType
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown Byron address type"
(Root, Attributes, AddressType)
-> Decoder s (Root, Attributes, AddressType)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Root
digest, Attributes
attrs, AddressType
kind)
toText :: Address -> Text
toText :: Address -> Text
toText = ByteString -> Text
encodeBase58 (ByteString -> Text) -> (Address -> ByteString) -> Address -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Address -> ByteString
toBytes
fromText :: Text -> Either Error Address
fromText :: Text -> Either Error Address
fromText Text
text
| Text -> Bool
Text.null Text
text Bool -> Bool -> Bool
|| Text -> Int
Text.length Text
text Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
512 = Error -> Either Error Address
forall a b. a -> Either a b
Left Error
InvalidByronAddressText
| Bool
otherwise = do
ByteString
bytes <- Either Error ByteString
-> (ByteString -> Either Error ByteString)
-> Maybe ByteString
-> Either Error ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error ByteString
forall a b. a -> Either a b
Left Error
InvalidByronAddressText) ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (Text -> Maybe ByteString
decodeBase58 Text
text)
(Error -> Either Error Address)
-> (Address -> Either Error Address)
-> Either Error Address
-> Either Error Address
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error Address -> Error -> Either Error Address
forall a b. a -> b -> a
const (Error -> Either Error Address
forall a b. a -> Either a b
Left Error
InvalidByronAddressText)) Address -> Either Error Address
forall a b. b -> Either a b
Right (ByteString -> Either Error Address
fromBytes ByteString
bytes)
encodeAttributes :: Attributes -> E.Encoding
encodeAttributes :: Attributes -> Encoding
encodeAttributes (Attributes Maybe ByteString
payload Maybe Word64
magic) = Word -> Encoding
E.encodeMapLen Word
count Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> (ByteString -> Encoding) -> Maybe ByteString -> Encoding
forall {t}. Word -> (t -> Encoding) -> Maybe t -> Encoding
field Word
1 ByteString -> Encoding
E.encodeBytes Maybe ByteString
payload Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> (Word64 -> Encoding) -> Maybe Word64 -> Encoding
forall {t}. Word -> (t -> Encoding) -> Maybe t -> Encoding
field Word
2 Word64 -> Encoding
E.encodeWord64 Maybe Word64
magic
where
count :: Word
count = Word -> (ByteString -> Word) -> Maybe ByteString -> Word
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word
0 (Word -> ByteString -> Word
forall a b. a -> b -> a
const Word
1) Maybe ByteString
payload Word -> Word -> Word
forall a. Num a => a -> a -> a
+ Word -> (Word64 -> Word) -> Maybe Word64 -> Word
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word
0 (Word -> Word64 -> Word
forall a b. a -> b -> a
const Word
1) Maybe Word64
magic
field :: Word -> (t -> Encoding) -> Maybe t -> Encoding
field Word
key t -> Encoding
encodeValue = Encoding -> (t -> Encoding) -> Maybe t -> Encoding
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Encoding
forall a. Monoid a => a
mempty (\t
value -> Word -> Encoding
E.encodeWord Word
key Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (Encoding -> ByteString
W.toStrictByteString (t -> Encoding
encodeValue t
value)))
decodeAttributes :: D.Decoder s Attributes
decodeAttributes :: forall s. Decoder s Attributes
decodeAttributes = do
Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
D.decodeMapLenOrIndef
[(Word, ByteString)]
pairs <- Maybe Int -> Decoder s [(Word, ByteString)]
forall {s}. Maybe Int -> Decoder s [(Word, ByteString)]
entries Maybe Int
size
(Maybe ByteString
payload, Maybe Word64
magic) <- ((Maybe ByteString, Maybe Word64)
-> (Word, ByteString)
-> Decoder s (Maybe ByteString, Maybe Word64))
-> (Maybe ByteString, Maybe Word64)
-> [(Word, ByteString)]
-> Decoder s (Maybe ByteString, Maybe Word64)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (Maybe ByteString, Maybe Word64)
-> (Word, ByteString) -> Decoder s (Maybe ByteString, Maybe Word64)
forall {a} {s}.
(Eq a, Num a) =>
(Maybe ByteString, Maybe Word64)
-> (a, ByteString) -> Decoder s (Maybe ByteString, Maybe Word64)
insert (Maybe ByteString
forall a. Maybe a
Nothing, Maybe Word64
forall a. Maybe a
Nothing) [(Word, ByteString)]
pairs
((Maybe ByteString, Maybe Word64) -> Either Error Attributes)
-> (Maybe ByteString, Maybe Word64) -> Decoder s Attributes
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validate ((Maybe ByteString -> Maybe Word64 -> Either Error Attributes)
-> (Maybe ByteString, Maybe Word64) -> Either Error Attributes
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Maybe ByteString -> Maybe Word64 -> Either Error Attributes
attributes) (Maybe ByteString
payload, Maybe Word64
magic)
where
entries :: Maybe Int -> Decoder s [(Word, ByteString)]
entries (Just Int
count) = Int
-> Decoder s (Word, ByteString) -> Decoder s [(Word, ByteString)]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
count Decoder s (Word, ByteString)
forall {s}. Decoder s (Word, ByteString)
pair
entries Maybe Int
Nothing = do
Bool
end <- Decoder s Bool
forall s. Decoder s Bool
D.decodeBreakOr
if Bool
end then [(Word, ByteString)] -> Decoder s [(Word, ByteString)]
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [] else (:) ((Word, ByteString)
-> [(Word, ByteString)] -> [(Word, ByteString)])
-> Decoder s (Word, ByteString)
-> Decoder s ([(Word, ByteString)] -> [(Word, ByteString)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Word, ByteString)
forall {s}. Decoder s (Word, ByteString)
pair Decoder s ([(Word, ByteString)] -> [(Word, ByteString)])
-> Decoder s [(Word, ByteString)] -> Decoder s [(Word, ByteString)]
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe Int -> Decoder s [(Word, ByteString)]
entries Maybe Int
forall a. Maybe a
Nothing
pair :: Decoder s (Word, ByteString)
pair = (,) (Word -> ByteString -> (Word, ByteString))
-> Decoder s Word -> Decoder s (ByteString -> (Word, ByteString))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word
forall s. Decoder s Word
D.decodeWord Decoder s (ByteString -> (Word, ByteString))
-> Decoder s ByteString -> Decoder s (Word, ByteString)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes
insert :: (Maybe ByteString, Maybe Word64)
-> (a, ByteString) -> Decoder s (Maybe ByteString, Maybe Word64)
insert (Maybe ByteString
payload, Maybe Word64
magic) (a
key, ByteString
bytes) = case a
key of
a
1 | Maybe ByteString -> Bool
forall a. Maybe a -> Bool
isNothing Maybe ByteString
payload -> do
ByteString
value <- (ByteString -> Either Error ByteString)
-> ByteString -> Decoder s ByteString
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validate ((forall s. Decoder s ByteString)
-> ByteString -> Either Error ByteString
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decode Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes) ByteString
bytes
(Maybe ByteString, Maybe Word64)
-> Decoder s (Maybe ByteString, Maybe Word64)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
value, Maybe Word64
magic)
a
2 | Maybe Word64 -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Word64
magic -> do
Word64
value <- (ByteString -> Either Error Word64)
-> ByteString -> Decoder s Word64
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validate ((forall s. Decoder s Word64) -> ByteString -> Either Error Word64
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decode Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64) ByteString
bytes
(Maybe ByteString, Maybe Word64)
-> Decoder s (Maybe ByteString, Maybe Word64)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe ByteString
payload, Word64 -> Maybe Word64
forall a. a -> Maybe a
Just Word64
value)
a
_ -> String -> Decoder s (Maybe ByteString, Maybe Word64)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown or repeated Byron attribute"
array :: Int -> D.Decoder s a -> D.Decoder s a
array :: forall s a. Int -> Decoder s a -> Decoder s a
array Int
expected Decoder s a
parser = do
Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
D.decodeListLenOrIndef
case Maybe Int
size of
Just Int
count -> Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
expected) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Incorrect Byron array length")
Maybe Int
Nothing -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
a
result <- Decoder s a
parser
case Maybe Int
size of
Just Int
_ -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Maybe Int
Nothing -> Decoder s Bool
forall s. Decoder s Bool
D.decodeBreakOr Decoder s Bool -> (Bool -> Decoder s ()) -> Decoder s ()
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Bool
end -> Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
end (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Extra Byron array fields")
a -> Decoder s a
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
result
validate :: (a -> Either Error b) -> a -> D.Decoder s b
validate :: forall a b s. (a -> Either Error b) -> a -> Decoder s b
validate a -> Either Error b
parser = (Error -> Decoder s b)
-> (b -> Decoder s b) -> Either Error b -> Decoder s b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s b
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s b)
-> (Error -> String) -> Error -> Decoder s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) b -> Decoder s b
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error b -> Decoder s b)
-> (a -> Either Error b) -> a -> Decoder s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either Error b
parser
decode :: (forall s. D.Decoder s a) -> ByteString -> Either Error a
decode :: forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decode forall s. Decoder s a
parser ByteString
bytes = case (forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
forall a.
(forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
R.deserialiseFromBytes Decoder s a
forall s. Decoder s a
parser (ByteString -> ByteString
L.fromStrict ByteString
bytes) of
Left DeserialiseFailure
err -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidByronAddress (String -> Text
Text.pack (DeserialiseFailure -> String
forall a. Show a => a -> String
show DeserialiseFailure
err)))
Right (ByteString
rest, a
value)
| ByteString -> Bool
L.null ByteString
rest -> a -> Either Error a
forall a b. b -> Either a b
Right a
value
| Bool
otherwise -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidByronAddress Text
"Trailing Byron CBOR")
crc32 :: ByteString -> Word32
crc32 :: ByteString -> Word32
crc32 = Word32 -> Word32
forall a. Bits a => a -> a
complement (Word32 -> Word32)
-> (ByteString -> Word32) -> ByteString -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word32 -> Word8 -> Word32) -> Word32 -> ByteString -> Word32
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B.foldl' Word32 -> Word8 -> Word32
forall {b} {a}. (Bits b, Integral a, Num b) => b -> a -> b
step Word32
forall a. Bounded a => a
maxBound
where
step :: b -> a -> b
step b
checksum a
byte = (b -> Int -> b) -> b -> [Int] -> b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' (\b
value Int
_ -> b -> b
forall {a}. (Bits a, Num a) => a -> a
bit b
value) (b
checksum b -> b -> b
forall a. Bits a => a -> a -> a
`xor` a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
byte) [Int
1 :: Int .. Int
8]
bit :: a -> a
bit a
value = if a
value a -> a -> a
forall a. Bits a => a -> a -> a
.&. a
1 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
1 then (a
value a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` Int
1) a -> a -> a
forall a. Bits a => a -> a -> a
`xor` a
0xedb88320 else a
value a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` Int
1
alphabet :: String
alphabet :: String
alphabet = String
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
encodeBase58 :: ByteString -> Text
encodeBase58 :: ByteString -> Text
encodeBase58 ByteString
bytes = String -> Text
Text.pack (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
zeros Char
'1' String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. [a] -> [a]
reverse ((Integer -> Maybe (Char, Integer)) -> Integer -> String
forall b a. (b -> Maybe (a, b)) -> b -> [a]
List.unfoldr Integer -> Maybe (Char, Integer)
forall {b}. Integral b => b -> Maybe (Char, b)
digit Integer
number))
where
zeros :: Int
zeros = ByteString -> Int
B.length ((Word8 -> Bool) -> ByteString -> ByteString
B.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0) ByteString
bytes)
number :: Integer
number = (Integer -> Word8 -> Integer) -> Integer -> ByteString -> Integer
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B.foldl' (\Integer
n Word8
byte -> Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
256 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger Word8
byte) Integer
0 ByteString
bytes
digit :: b -> Maybe (Char, b)
digit b
0 = Maybe (Char, b)
forall a. Maybe a
Nothing
digit b
value = do
let (b
quotient, b
remainder) = b
value b -> b -> (b, b)
forall a. Integral a => a -> a -> (a, a)
`divMod` b
58
Char
character <- b -> [(b, Char)] -> Maybe Char
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup b
remainder ([b] -> String -> [(b, Char)]
forall a b. [a] -> [b] -> [(a, b)]
zip [b
0 .. b
57] String
alphabet)
(Char, b) -> Maybe (Char, b)
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Char
character, b
quotient)
decodeBase58 :: Text -> Maybe ByteString
decodeBase58 :: Text -> Maybe ByteString
decodeBase58 Text
text = do
Integer
number <- (Integer -> Char -> Maybe Integer)
-> Integer -> String -> Maybe Integer
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Integer -> Char -> Maybe Integer
digit Integer
0 (Text -> String
Text.unpack Text
text)
ByteString -> Maybe ByteString
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Word8] -> ByteString
B.pack (Int -> Word8 -> [Word8]
forall a. Int -> a -> [a]
replicate Int
zeros Word8
0 [Word8] -> [Word8] -> [Word8]
forall a. [a] -> [a] -> [a]
++ Integer -> [Word8] -> [Word8]
forall {a}. Num a => Integer -> [a] -> [a]
bytes Integer
number []))
where
zeros :: Int
zeros = Text -> Int
Text.length ((Char -> Bool) -> Text -> Text
Text.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'1') Text
text)
digit :: Integer -> Char -> Maybe Integer
digit Integer
value Char
character = (Integer
value Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
58 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+) (Integer -> Integer) -> (Int -> Integer) -> Int -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Maybe Int -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> [(Char, Int)] -> Maybe Int
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Char
character (String -> [Int] -> [(Char, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip String
alphabet [Int
0 :: Int ..])
bytes :: Integer -> [a] -> [a]
bytes Integer
0 [a]
result = [a]
result
bytes Integer
value [a]
result = let (Integer
quotient, Integer
remainder) = Integer
value Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`divMod` Integer
256 in Integer -> [a] -> [a]
bytes Integer
quotient (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
remainder a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
result)