-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- | Base, enterprise and pointer payment addresses. Construction and byte conversion
-- are pure. Byron and reward addresses return 'UnsupportedAddressType' when imported as bytes.
--
-- @
-- import qualified Cometa.Address.Shelley as Address
-- import qualified Cometa.Credential as Credential
--
-- receivingAddress :: Credential.KeyHash Credential.Payment -> Address.Address
-- receivingAddress key = Address.enterprise Address.mainnet (Credential.KeyCredential key)
-- @
module Cometa.Address.Shelley
  ( -- * Networks
    NetworkId,
    mkNetworkId,
    networkIdValue,
    mainnet,
    testnet,

    -- * Construction
    Address,
    StakePointer (..),
    pointer,
    pointerRef,
    base,
    enterprise,

    -- * Inspection
    networkId,
    paymentCredential,
    stakeCredential,

    -- * Encoding
    fromBytes,
    toBytes,
    fromText,
    toText,
  )
where

-- IMPORTS --------------------------------------------------------------------

import Cometa.Credential (Credential (..), Payment, Stake)
import qualified Cometa.Credential as Credential
import Cometa.Error (Error (..))
import qualified Cometa.Internal.AddressText as AddressText
import Cometa.NetworkId (NetworkId, mainnet, mkNetworkId, networkIdValue, testnet)
import Data.Bits (shiftL, shiftR, testBit, (.&.), (.|.))
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)
import Data.Word (Word32, Word8)

-- TYPES ----------------------------------------------------------------------

-- | An immutable address. Equality and ordering compare form (base,
-- enterprise, pointer), network, then credentials; 'Show' is diagnostic.
data Address
  = Base !NetworkId !(Credential Payment) !(Credential Stake)
  | Enterprise !NetworkId !(Credential Payment)
  | Pointer !NetworkId !(Credential Payment) !StakePointer
  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)

-- | Location of a stake registration. Each component is an unsigned 32-bit value.
data StakePointer = StakePointer
  { -- | Absolute slot containing the registration.
    StakePointer -> Word32
slot :: !Word32,
    -- | Zero-based transaction index within that slot.
    StakePointer -> Word32
transactionIndex :: !Word32,
    -- | Zero-based certificate index within that transaction.
    StakePointer -> Word32
certificateIndex :: !Word32
  }
  deriving (StakePointer -> StakePointer -> Bool
(StakePointer -> StakePointer -> Bool)
-> (StakePointer -> StakePointer -> Bool) -> Eq StakePointer
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StakePointer -> StakePointer -> Bool
== :: StakePointer -> StakePointer -> Bool
$c/= :: StakePointer -> StakePointer -> Bool
/= :: StakePointer -> StakePointer -> Bool
Eq, Eq StakePointer
Eq StakePointer =>
(StakePointer -> StakePointer -> Ordering)
-> (StakePointer -> StakePointer -> Bool)
-> (StakePointer -> StakePointer -> Bool)
-> (StakePointer -> StakePointer -> Bool)
-> (StakePointer -> StakePointer -> Bool)
-> (StakePointer -> StakePointer -> StakePointer)
-> (StakePointer -> StakePointer -> StakePointer)
-> Ord StakePointer
StakePointer -> StakePointer -> Bool
StakePointer -> StakePointer -> Ordering
StakePointer -> StakePointer -> StakePointer
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 :: StakePointer -> StakePointer -> Ordering
compare :: StakePointer -> StakePointer -> Ordering
$c< :: StakePointer -> StakePointer -> Bool
< :: StakePointer -> StakePointer -> Bool
$c<= :: StakePointer -> StakePointer -> Bool
<= :: StakePointer -> StakePointer -> Bool
$c> :: StakePointer -> StakePointer -> Bool
> :: StakePointer -> StakePointer -> Bool
$c>= :: StakePointer -> StakePointer -> Bool
>= :: StakePointer -> StakePointer -> Bool
$cmax :: StakePointer -> StakePointer -> StakePointer
max :: StakePointer -> StakePointer -> StakePointer
$cmin :: StakePointer -> StakePointer -> StakePointer
min :: StakePointer -> StakePointer -> StakePointer
Ord, Int -> StakePointer -> ShowS
[StakePointer] -> ShowS
StakePointer -> String
(Int -> StakePointer -> ShowS)
-> (StakePointer -> String)
-> ([StakePointer] -> ShowS)
-> Show StakePointer
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StakePointer -> ShowS
showsPrec :: Int -> StakePointer -> ShowS
$cshow :: StakePointer -> String
show :: StakePointer -> String
$cshowList :: [StakePointer] -> ShowS
showList :: [StakePointer] -> ShowS
Show)

-- DEFINITIONS ----------------------------------------------------------------

-- | Assemble a base address from network, payment authority and stake authority,
-- in that order. Key and script credentials are supported in both positions.
base :: NetworkId -> Credential Payment -> Credential Stake -> Address
base :: NetworkId -> Credential Payment -> Credential Stake -> Address
base = NetworkId -> Credential Payment -> Credential Stake -> Address
Base

-- | Assemble an address with payment authority and no staking rights.
enterprise :: NetworkId -> Credential Payment -> Address
enterprise :: NetworkId -> Credential Payment -> Address
enterprise = NetworkId -> Credential Payment -> Address
Enterprise

-- | Assemble a pointer address from payment authority and a registration location.
pointer :: NetworkId -> Credential Payment -> StakePointer -> Address
pointer :: NetworkId -> Credential Payment -> StakePointer -> Address
pointer = NetworkId -> Credential Payment -> StakePointer -> Address
Pointer

-- | Registration location, present only in pointer addresses.
pointerRef :: Address -> Maybe StakePointer
pointerRef :: Address -> Maybe StakePointer
pointerRef (Pointer NetworkId
_ Credential Payment
_ StakePointer
location) = StakePointer -> Maybe StakePointer
forall a. a -> Maybe a
Just StakePointer
location
pointerRef Address
_ = Maybe StakePointer
forall a. Maybe a
Nothing

-- | Network tag carried by the header.
networkId :: Address -> NetworkId
networkId :: Address -> NetworkId
networkId Address
address = case Address
address of
  Base NetworkId
network Credential Payment
_ Credential Stake
_ -> NetworkId
network
  Enterprise NetworkId
network Credential Payment
_ -> NetworkId
network
  Pointer NetworkId
network Credential Payment
_ StakePointer
_ -> NetworkId
network

-- | Payment authority carried by every payment address.
paymentCredential :: Address -> Credential Payment
paymentCredential :: Address -> Credential Payment
paymentCredential Address
address = case Address
address of
  Base NetworkId
_ Credential Payment
credential Credential Stake
_ -> Credential Payment
credential
  Enterprise NetworkId
_ Credential Payment
credential -> Credential Payment
credential
  Pointer NetworkId
_ Credential Payment
credential StakePointer
_ -> Credential Payment
credential

-- | Staking authority; absent for enterprise and pointer addresses.
stakeCredential :: Address -> Maybe (Credential Stake)
stakeCredential :: Address -> Maybe (Credential Stake)
stakeCredential Address
address = case Address
address of
  Base NetworkId
_ Credential Payment
_ Credential Stake
credential -> Credential Stake -> Maybe (Credential Stake)
forall a. a -> Maybe a
Just Credential Stake
credential
  Enterprise {} -> Maybe (Credential Stake)
forall a. Maybe a
Nothing
  Pointer {} -> Maybe (Credential Stake)
forall a. Maybe a
Nothing

-- | Serialize exact header and credential bytes: 57 bytes for base addresses,
-- 29 for enterprise addresses, and 32–44 for pointer addresses.
toBytes :: Address -> ByteString
toBytes :: Address -> ByteString
toBytes Address
address = case Address
address of
  Base NetworkId
network Credential Payment
payment Credential Stake
stake -> NetworkId -> Word8 -> [ByteString] -> ByteString
encode NetworkId
network (Credential Payment -> Word8
forall purpose. Credential purpose -> Word8
kind Credential Payment
payment Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8
2 Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
* Credential Stake -> Word8
forall purpose. Credential purpose -> Word8
kind Credential Stake
stake) [Credential Payment -> ByteString
forall purpose. Credential purpose -> ByteString
credentialBytes Credential Payment
payment, Credential Stake -> ByteString
forall purpose. Credential purpose -> ByteString
credentialBytes Credential Stake
stake]
  Enterprise NetworkId
network Credential Payment
payment -> NetworkId -> Word8 -> [ByteString] -> ByteString
encode NetworkId
network (Word8
6 Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Credential Payment -> Word8
forall purpose. Credential purpose -> Word8
kind Credential Payment
payment) [Credential Payment -> ByteString
forall purpose. Credential purpose -> ByteString
credentialBytes Credential Payment
payment]
  Pointer NetworkId
network Credential Payment
payment (StakePointer Word32
a Word32
b Word32
c) -> NetworkId -> Word8 -> [ByteString] -> ByteString
encode NetworkId
network (Word8
4 Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Credential Payment -> Word8
forall purpose. Credential purpose -> Word8
kind Credential Payment
payment) [Credential Payment -> ByteString
forall purpose. Credential purpose -> ByteString
credentialBytes Credential Payment
payment, Word32 -> ByteString
variable Word32
a, Word32 -> ByteString
variable Word32
b, Word32 -> ByteString
variable Word32
c]
  where
    encode :: NetworkId -> Word8 -> [ByteString] -> ByteString
encode NetworkId
network Word8
tag [ByteString]
parts = Word8 -> ByteString -> ByteString
ByteString.cons ((Word8
tag Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shiftL` Int
4) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. NetworkId -> Word8
networkIdValue NetworkId
network) ([ByteString] -> ByteString
ByteString.concat [ByteString]
parts)

-- | Import exact bytes. Byron, reward and reserved headers return
-- 'UnsupportedAddressType'. Empty input or incorrect exact lengths return
-- 'InvalidShelleyAddress'. Pointer components must be minimally encoded, fit
-- 32 bits and consume the full input. All four-bit network tags are retained.
fromBytes :: ByteString -> Either Error Address
fromBytes :: ByteString -> Either Error Address
fromBytes ByteString
bytes = case ByteString -> Maybe (Word8, ByteString)
ByteString.uncons ByteString
bytes of
  Maybe (Word8, ByteString)
Nothing -> Error -> Either Error Address
forall a b. a -> Either a b
Left Error
InvalidShelleyAddress
  Just (Word8
header, ByteString
payload) -> do
    let tag :: Word8
tag = Word8
header Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shiftR` Int
4
        networkValue :: Word8
networkValue = Word8
header Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
15
        (ByteString
payment, ByteString
stake) = Int -> ByteString -> (ByteString, ByteString)
ByteString.splitAt Int
28 ByteString
payload
    NetworkId
network <- Integer -> Either Error NetworkId
mkNetworkId (Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger Word8
networkValue)
    case Word8
tag of
      Word8
value
        | Word8
value Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
3 ->
            if ByteString -> Int
ByteString.length ByteString
payload Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
56
              then Error -> Either Error Address
forall a b. a -> Either a b
Left Error
InvalidShelleyAddress
              else NetworkId -> Credential Payment -> Credential Stake -> Address
Base NetworkId
network (Credential Payment -> Credential Stake -> Address)
-> Either Error (Credential Payment)
-> Either Error (Credential Stake -> Address)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> ByteString -> Either Error (Credential Payment)
forall {purpose}.
Bool -> ByteString -> Either Error (Credential purpose)
decodeCredential (Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
tag Int
0) ByteString
payment Either Error (Credential Stake -> Address)
-> Either Error (Credential Stake) -> Either Error Address
forall a b.
Either Error (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Bool -> ByteString -> Either Error (Credential Stake)
forall {purpose}.
Bool -> ByteString -> Either Error (Credential purpose)
decodeCredential (Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
tag Int
1) ByteString
stake
      Word8
value | Word8
value Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
4 Bool -> Bool -> Bool
|| Word8
value Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
5 -> do
        Credential Payment
credential <- Bool -> ByteString -> Either Error (Credential Payment)
forall {purpose}.
Bool -> ByteString -> Either Error (Credential purpose)
decodeCredential (Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
tag Int
0) ByteString
payment
        (Word32
a, ByteString
restA) <- ByteString -> Either Error (Word32, ByteString)
readVariable ByteString
stake
        (Word32
b, ByteString
restB) <- ByteString -> Either Error (Word32, ByteString)
readVariable ByteString
restA
        (Word32
c, ByteString
restC) <- ByteString -> Either Error (Word32, ByteString)
readVariable ByteString
restB
        if ByteString -> Bool
ByteString.null ByteString
restC
          then Address -> Either Error Address
forall a b. b -> Either a b
Right (NetworkId -> Credential Payment -> StakePointer -> Address
Pointer NetworkId
network Credential Payment
credential (Word32 -> Word32 -> Word32 -> StakePointer
StakePointer Word32
a Word32
b Word32
c))
          else Error -> Either Error Address
forall a b. a -> Either a b
Left Error
InvalidShelleyAddress
      Word8
value | Word8
value Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
6 Bool -> Bool -> Bool
|| Word8
value Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
7 -> NetworkId -> Credential Payment -> Address
Enterprise NetworkId
network (Credential Payment -> Address)
-> Either Error (Credential Payment) -> Either Error Address
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> ByteString -> Either Error (Credential Payment)
forall {purpose}.
Bool -> ByteString -> Either Error (Credential purpose)
decodeCredential (Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
tag Int
0) ByteString
payload
      Word8
_ -> Error -> Either Error Address
forall a b. a -> Either a b
Left (Word8 -> Error
UnsupportedAddressType Word8
tag)
  where
    decodeCredential :: Bool -> ByteString -> Either Error (Credential purpose)
decodeCredential Bool
script ByteString
payload
      | ByteString -> Int
ByteString.length ByteString
payload Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
28 = Error -> Either Error (Credential purpose)
forall a b. a -> Either a b
Left Error
InvalidShelleyAddress
      | Bool
script = ScriptHash -> Credential purpose
forall purpose. ScriptHash -> Credential purpose
ScriptCredential (ScriptHash -> Credential purpose)
-> Either Error ScriptHash -> Either Error (Credential purpose)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either Error ScriptHash
Credential.scriptHashFromBytes ByteString
payload
      | Bool
otherwise = KeyHash purpose -> Credential purpose
forall purpose. KeyHash purpose -> Credential purpose
KeyCredential (KeyHash purpose -> Credential purpose)
-> Either Error (KeyHash purpose)
-> Either Error (Credential purpose)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either Error (KeyHash purpose)
forall purpose. ByteString -> Either Error (KeyHash purpose)
Credential.keyHashFromBytes ByteString
payload

-- | Encode canonical lowercase Bech32, with @addr@ for tag 1 and
-- @addr_test@ for other tags.
toText :: Address -> Text
toText :: Address -> Text
toText Address
address = Bool -> Bool -> ByteString -> Text
AddressText.toText Bool
False (Address -> NetworkId
networkId Address
address NetworkId -> NetworkId -> Bool
forall a. Eq a => a -> a -> Bool
== NetworkId
mainnet) (Address -> ByteString
toBytes Address
address)

-- | Parse lowercase or all-uppercase Bech32. Mixed case, non-ASCII, empty,
-- overlong (more than 120 characters), or mismatched network/form prefixes
-- return 'InvalidShelleyAddressText'. Malformed encoding or checksum returns
-- 'InvalidShelleyAddressText'; unsupported headers return 'UnsupportedAddressType';
-- incorrect exact lengths return 'InvalidShelleyAddress'.
fromText :: Text -> Either Error Address
fromText :: Text -> Either Error Address
fromText = (ByteString -> Either Error Address)
-> (Address -> Text) -> Text -> Either Error Address
forall address.
(ByteString -> Either Error address)
-> (address -> Text) -> Text -> Either Error address
AddressText.fromText ByteString -> Either Error Address
fromBytes Address -> Text
toText

kind :: Credential purpose -> Word8
kind :: forall purpose. Credential purpose -> Word8
kind Credential purpose
credential = case Credential purpose
credential of
  KeyCredential KeyHash purpose
_ -> Word8
0
  ScriptCredential ScriptHash
_ -> Word8
1

credentialBytes :: Credential purpose -> ByteString
credentialBytes :: forall purpose. Credential purpose -> ByteString
credentialBytes Credential purpose
credential = case Credential purpose
credential of
  KeyCredential KeyHash purpose
hash -> KeyHash purpose -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes KeyHash purpose
hash
  ScriptCredential ScriptHash
hash -> ScriptHash -> ByteString
Credential.scriptHashBytes ScriptHash
hash

variable :: Word32 -> ByteString
variable :: Word32 -> ByteString
variable Word32
value = [Word8] -> ByteString
ByteString.pack (Word32 -> [Word8] -> [Word8]
forall {a} {a}.
(Integral a, Num a, Bits a, Bits a) =>
a -> [a] -> [a]
go Word32
value [])
  where
    go :: a -> [a] -> [a]
go a
remaining [a]
tailBytes
      | a
remaining a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
128 = a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
remaining a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
tailBytes
      | Bool
otherwise = a -> [a] -> [a]
forall {a} {a}.
(Integral a, Num a, Bits a, Bits a) =>
a -> [a] -> [a]
continue (a
remaining a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) [a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
remaining a -> a -> a
forall a. Bits a => a -> a -> a
.&. a
127)]
    continue :: a -> [a] -> [a]
continue a
remaining [a]
tailBytes
      | a
remaining a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
128 = (a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
remaining a -> a -> a
forall a. Bits a => a -> a -> a
.|. a
128) a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
tailBytes
      | Bool
otherwise = a -> [a] -> [a]
continue (a
remaining a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) ((a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
remaining a -> a -> a
forall a. Bits a => a -> a -> a
.&. a
127) a -> a -> a
forall a. Bits a => a -> a -> a
.|. a
128) a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
tailBytes)

readVariable :: ByteString -> Either Error (Word32, ByteString)
readVariable :: ByteString -> Either Error (Word32, ByteString)
readVariable ByteString
original = Int -> Integer -> ByteString -> Either Error (Word32, ByteString)
forall {t}.
(Ord t, Num t) =>
t -> Integer -> ByteString -> Either Error (Word32, ByteString)
go (Int
0 :: Int) Integer
0 ByteString
original
  where
    go :: t -> Integer -> ByteString -> Either Error (Word32, ByteString)
go t
count Integer
value ByteString
bytes = case ByteString -> Maybe (Word8, ByteString)
ByteString.uncons ByteString
bytes of
      Maybe (Word8, ByteString)
Nothing -> Error -> Either Error (Word32, ByteString)
forall a b. a -> Either a b
Left Error
InvalidShelleyAddress
      Just (Word8
byte, ByteString
rest)
        | t
count t -> t -> Bool
forall a. Ord a => a -> a -> Bool
>= t
5 -> Error -> Either Error (Word32, ByteString)
forall a b. a -> Either a b
Left Error
InvalidShelleyAddress
        | Integer
value Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> (Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word32
forall a. Bounded a => a
maxBound :: Word32) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
127)) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
128 -> Error -> Either Error (Word32, ByteString)
forall a b. a -> Either a b
Left Error
InvalidShelleyAddress
        | Bool
otherwise ->
            let next :: Integer
next = Integer
value Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
128 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
127)
             in if Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
byte Int
7
                  then t -> Integer -> ByteString -> Either Error (Word32, ByteString)
go (t
count t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) Integer
next ByteString
rest
                  else
                    let result :: Word32
result = Integer -> Word32
forall a. Num a => Integer -> a
fromInteger Integer
next
                        consumed :: Int
consumed = ByteString -> Int
ByteString.length ByteString
original Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
ByteString.length ByteString
rest
                     in if Int -> ByteString -> ByteString
ByteString.take Int
consumed ByteString
original ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Word32 -> ByteString
variable Word32
result
                          then (Word32, ByteString) -> Either Error (Word32, ByteString)
forall a b. b -> Either a b
Right (Word32
result, ByteString
rest)
                          else Error -> Either Error (Word32, ByteString)
forall a b. a -> Either a b
Left Error
InvalidShelleyAddress