-- 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.

-- | Verification-key signatures and Byron bootstrap witnesses. Construction
-- assembles previously validated keys and signatures; it does not verify them.
-- @verificationWitness key signature@ assembles a Shelley witness.
module Cometa.Transaction.Witness
  ( VerificationWitness,
    verificationWitness,
    publicKey,
    signature,
    BootstrapWitness,
    mkBootstrapWitness,
    bootstrapPublicKey,
    bootstrapSignature,
    chainCode,
    attributes,
    encodeCbor,
    decodeCbor,
    encodeBootstrapCbor,
    decodeBootstrapCbor,
  )
where

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

import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import Cometa.Crypto.Ed25519.PublicKey (PublicKey)
import qualified Cometa.Crypto.Ed25519.PublicKey as Key
import Cometa.Crypto.Ed25519.Signature (Signature)
import qualified Cometa.Crypto.Ed25519.Signature as Sig
import Cometa.Error (Error (..))
import qualified Cometa.Internal.TransactionCbor as C
import Data.ByteString (ByteString)
import qualified Data.ByteString as B

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

-- | A verification key and its signature of a transaction body hash.
data VerificationWitness = VerificationWitness !PublicKey !Signature deriving (VerificationWitness -> VerificationWitness -> Bool
(VerificationWitness -> VerificationWitness -> Bool)
-> (VerificationWitness -> VerificationWitness -> Bool)
-> Eq VerificationWitness
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VerificationWitness -> VerificationWitness -> Bool
== :: VerificationWitness -> VerificationWitness -> Bool
$c/= :: VerificationWitness -> VerificationWitness -> Bool
/= :: VerificationWitness -> VerificationWitness -> Bool
Eq, Eq VerificationWitness
Eq VerificationWitness =>
(VerificationWitness -> VerificationWitness -> Ordering)
-> (VerificationWitness -> VerificationWitness -> Bool)
-> (VerificationWitness -> VerificationWitness -> Bool)
-> (VerificationWitness -> VerificationWitness -> Bool)
-> (VerificationWitness -> VerificationWitness -> Bool)
-> (VerificationWitness
    -> VerificationWitness -> VerificationWitness)
-> (VerificationWitness
    -> VerificationWitness -> VerificationWitness)
-> Ord VerificationWitness
VerificationWitness -> VerificationWitness -> Bool
VerificationWitness -> VerificationWitness -> Ordering
VerificationWitness -> VerificationWitness -> VerificationWitness
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 :: VerificationWitness -> VerificationWitness -> Ordering
compare :: VerificationWitness -> VerificationWitness -> Ordering
$c< :: VerificationWitness -> VerificationWitness -> Bool
< :: VerificationWitness -> VerificationWitness -> Bool
$c<= :: VerificationWitness -> VerificationWitness -> Bool
<= :: VerificationWitness -> VerificationWitness -> Bool
$c> :: VerificationWitness -> VerificationWitness -> Bool
> :: VerificationWitness -> VerificationWitness -> Bool
$c>= :: VerificationWitness -> VerificationWitness -> Bool
>= :: VerificationWitness -> VerificationWitness -> Bool
$cmax :: VerificationWitness -> VerificationWitness -> VerificationWitness
max :: VerificationWitness -> VerificationWitness -> VerificationWitness
$cmin :: VerificationWitness -> VerificationWitness -> VerificationWitness
min :: VerificationWitness -> VerificationWitness -> VerificationWitness
Ord, Int -> VerificationWitness -> ShowS
[VerificationWitness] -> ShowS
VerificationWitness -> String
(Int -> VerificationWitness -> ShowS)
-> (VerificationWitness -> String)
-> ([VerificationWitness] -> ShowS)
-> Show VerificationWitness
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VerificationWitness -> ShowS
showsPrec :: Int -> VerificationWitness -> ShowS
$cshow :: VerificationWitness -> String
show :: VerificationWitness -> String
$cshowList :: [VerificationWitness] -> ShowS
showList :: [VerificationWitness] -> ShowS
Show)

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

-- | Assemble a key witness. Cryptographic verification is a separate effect.
verificationWitness :: PublicKey -> Signature -> VerificationWitness
verificationWitness :: PublicKey -> Signature -> VerificationWitness
verificationWitness = PublicKey -> Signature -> VerificationWitness
VerificationWitness

-- | The signing verification key.
publicKey :: VerificationWitness -> PublicKey
publicKey :: VerificationWitness -> PublicKey
publicKey (VerificationWitness PublicKey
key Signature
_) = PublicKey
key

-- | Signature bytes validated for Ed25519 length.
signature :: VerificationWitness -> Signature
signature :: VerificationWitness -> Signature
signature (VerificationWitness PublicKey
_ Signature
sig) = Signature
sig

-- | A Byron witness with a 32-byte derivation chain code and opaque serialized
-- address attributes. Equality compares all fields.
data BootstrapWitness = BootstrapWitness !PublicKey !Signature !ByteString !ByteString deriving (BootstrapWitness -> BootstrapWitness -> Bool
(BootstrapWitness -> BootstrapWitness -> Bool)
-> (BootstrapWitness -> BootstrapWitness -> Bool)
-> Eq BootstrapWitness
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BootstrapWitness -> BootstrapWitness -> Bool
== :: BootstrapWitness -> BootstrapWitness -> Bool
$c/= :: BootstrapWitness -> BootstrapWitness -> Bool
/= :: BootstrapWitness -> BootstrapWitness -> Bool
Eq, Eq BootstrapWitness
Eq BootstrapWitness =>
(BootstrapWitness -> BootstrapWitness -> Ordering)
-> (BootstrapWitness -> BootstrapWitness -> Bool)
-> (BootstrapWitness -> BootstrapWitness -> Bool)
-> (BootstrapWitness -> BootstrapWitness -> Bool)
-> (BootstrapWitness -> BootstrapWitness -> Bool)
-> (BootstrapWitness -> BootstrapWitness -> BootstrapWitness)
-> (BootstrapWitness -> BootstrapWitness -> BootstrapWitness)
-> Ord BootstrapWitness
BootstrapWitness -> BootstrapWitness -> Bool
BootstrapWitness -> BootstrapWitness -> Ordering
BootstrapWitness -> BootstrapWitness -> BootstrapWitness
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 :: BootstrapWitness -> BootstrapWitness -> Ordering
compare :: BootstrapWitness -> BootstrapWitness -> Ordering
$c< :: BootstrapWitness -> BootstrapWitness -> Bool
< :: BootstrapWitness -> BootstrapWitness -> Bool
$c<= :: BootstrapWitness -> BootstrapWitness -> Bool
<= :: BootstrapWitness -> BootstrapWitness -> Bool
$c> :: BootstrapWitness -> BootstrapWitness -> Bool
> :: BootstrapWitness -> BootstrapWitness -> Bool
$c>= :: BootstrapWitness -> BootstrapWitness -> Bool
>= :: BootstrapWitness -> BootstrapWitness -> Bool
$cmax :: BootstrapWitness -> BootstrapWitness -> BootstrapWitness
max :: BootstrapWitness -> BootstrapWitness -> BootstrapWitness
$cmin :: BootstrapWitness -> BootstrapWitness -> BootstrapWitness
min :: BootstrapWitness -> BootstrapWitness -> BootstrapWitness
Ord, Int -> BootstrapWitness -> ShowS
[BootstrapWitness] -> ShowS
BootstrapWitness -> String
(Int -> BootstrapWitness -> ShowS)
-> (BootstrapWitness -> String)
-> ([BootstrapWitness] -> ShowS)
-> Show BootstrapWitness
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BootstrapWitness -> ShowS
showsPrec :: Int -> BootstrapWitness -> ShowS
$cshow :: BootstrapWitness -> String
show :: BootstrapWitness -> String
$cshowList :: [BootstrapWitness] -> ShowS
showList :: [BootstrapWitness] -> ShowS
Show)

-- | Assemble a bootstrap witness; the chain code must contain exactly 32 bytes.
-- Attributes are the serialized Byron address attributes, retained verbatim.
mkBootstrapWitness :: PublicKey -> Signature -> ByteString -> ByteString -> Either Error BootstrapWitness
mkBootstrapWitness :: PublicKey
-> Signature
-> ByteString
-> ByteString
-> Either Error BootstrapWitness
mkBootstrapWitness PublicKey
key Signature
sig ByteString
chain ByteString
attrs
  | ByteString -> Int
B.length ByteString
chain Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
32 = Error -> Either Error BootstrapWitness
forall a b. a -> Either a b
Left (Int -> Error
InvalidBootstrapChainCodeLength (ByteString -> Int
B.length ByteString
chain))
  | Bool
otherwise = BootstrapWitness -> Either Error BootstrapWitness
forall a b. b -> Either a b
Right (PublicKey
-> Signature -> ByteString -> ByteString -> BootstrapWitness
BootstrapWitness PublicKey
key Signature
sig ByteString
chain ByteString
attrs)

-- | Bootstrap signing key.
bootstrapPublicKey :: BootstrapWitness -> PublicKey
bootstrapPublicKey :: BootstrapWitness -> PublicKey
bootstrapPublicKey (BootstrapWitness PublicKey
key Signature
_ ByteString
_ ByteString
_) = PublicKey
key

-- | Bootstrap signature.
bootstrapSignature :: BootstrapWitness -> Signature
bootstrapSignature :: BootstrapWitness -> Signature
bootstrapSignature (BootstrapWitness PublicKey
_ Signature
sig ByteString
_ ByteString
_) = Signature
sig

-- | Exactly 32 chain-code bytes.
chainCode :: BootstrapWitness -> ByteString
chainCode :: BootstrapWitness -> ByteString
chainCode (BootstrapWitness PublicKey
_ Signature
_ ByteString
chain ByteString
_) = ByteString
chain

-- | Opaque serialized Byron address attributes.
attributes :: BootstrapWitness -> ByteString
attributes :: BootstrapWitness -> ByteString
attributes (BootstrapWitness PublicKey
_ Signature
_ ByteString
_ ByteString
attrs) = ByteString
attrs

-- | Encode the two-element key/signature array.
encodeCbor :: VerificationWitness -> ByteString
encodeCbor :: VerificationWitness -> ByteString
encodeCbor (VerificationWitness PublicKey
key Signature
sig) = Encoding -> ByteString
C.encode (Word -> Encoding
E.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (PublicKey -> ByteString
Key.toBytes PublicKey
key) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (Signature -> ByteString
Sig.toBytes Signature
sig))

-- | Decode one key/signature array, validating lengths and rejecting trailing bytes.
decodeCbor :: ByteString -> Either Error VerificationWitness
decodeCbor :: ByteString -> Either Error VerificationWitness
decodeCbor = (Text -> Error)
-> (forall s. Decoder s VerificationWitness)
-> ByteString
-> Either Error VerificationWitness
forall a.
(Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
C.decodeAs Text -> Error
InvalidWitnessCbor (Int
-> Decoder s VerificationWitness -> Decoder s VerificationWitness
forall s a. Int -> Decoder s a -> Decoder s a
C.array Int
2 (PublicKey -> Signature -> VerificationWitness
VerificationWitness (PublicKey -> Signature -> VerificationWitness)
-> Decoder s PublicKey
-> Decoder s (Signature -> VerificationWitness)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s PublicKey
forall s. Decoder s PublicKey
keyDecoder Decoder s (Signature -> VerificationWitness)
-> Decoder s Signature -> Decoder s VerificationWitness
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 Signature
forall s. Decoder s Signature
signatureDecoder))

-- | Encode the four-element bootstrap array.
encodeBootstrapCbor :: BootstrapWitness -> ByteString
encodeBootstrapCbor :: BootstrapWitness -> ByteString
encodeBootstrapCbor (BootstrapWitness PublicKey
key Signature
sig ByteString
chain ByteString
attrs) = Encoding -> ByteString
C.encode (Word -> Encoding
E.encodeListLen Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (PublicKey -> ByteString
Key.toBytes PublicKey
key) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (Signature -> ByteString
Sig.toBytes Signature
sig) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes ByteString
chain Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes ByteString
attrs)

-- | Decode a bootstrap array, validating lengths and rejecting trailing bytes.
decodeBootstrapCbor :: ByteString -> Either Error BootstrapWitness
decodeBootstrapCbor :: ByteString -> Either Error BootstrapWitness
decodeBootstrapCbor = (Text -> Error)
-> (forall {s}. Decoder s BootstrapWitness)
-> ByteString
-> Either Error BootstrapWitness
forall a.
(Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
C.decodeAs Text -> Error
InvalidWitnessCbor ((forall {s}. Decoder s BootstrapWitness)
 -> ByteString -> Either Error BootstrapWitness)
-> (forall {s}. Decoder s BootstrapWitness)
-> ByteString
-> Either Error BootstrapWitness
forall a b. (a -> b) -> a -> b
$ Int -> Decoder s BootstrapWitness -> Decoder s BootstrapWitness
forall s a. Int -> Decoder s a -> Decoder s a
C.array Int
4 (Decoder s BootstrapWitness -> Decoder s BootstrapWitness)
-> Decoder s BootstrapWitness -> Decoder s BootstrapWitness
forall a b. (a -> b) -> a -> b
$ do
  PublicKey
key <- Decoder s PublicKey
forall s. Decoder s PublicKey
keyDecoder
  Signature
sig <- Decoder s Signature
forall s. Decoder s Signature
signatureDecoder
  ByteString
chain <- Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes
  ByteString
attrs <- Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes
  (Error -> Decoder s BootstrapWitness)
-> (BootstrapWitness -> Decoder s BootstrapWitness)
-> Either Error BootstrapWitness
-> Decoder s BootstrapWitness
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s BootstrapWitness
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s BootstrapWitness)
-> (Error -> String) -> Error -> Decoder s BootstrapWitness
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) BootstrapWitness -> Decoder s BootstrapWitness
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PublicKey
-> Signature
-> ByteString
-> ByteString
-> Either Error BootstrapWitness
mkBootstrapWitness PublicKey
key Signature
sig ByteString
chain ByteString
attrs)

keyDecoder :: D.Decoder s PublicKey
keyDecoder :: forall s. Decoder s PublicKey
keyDecoder = Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s PublicKey) -> Decoder s PublicKey
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
>>= (Error -> Decoder s PublicKey)
-> (PublicKey -> Decoder s PublicKey)
-> Either Error PublicKey
-> Decoder s PublicKey
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s PublicKey
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s PublicKey)
-> (Error -> String) -> Error -> Decoder s PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) PublicKey -> Decoder s PublicKey
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error PublicKey -> Decoder s PublicKey)
-> (ByteString -> Either Error PublicKey)
-> ByteString
-> Decoder s PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error PublicKey
Key.fromBytes

signatureDecoder :: D.Decoder s Signature
signatureDecoder :: forall s. Decoder s Signature
signatureDecoder = Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s Signature) -> Decoder s Signature
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
>>= (Error -> Decoder s Signature)
-> (Signature -> Decoder s Signature)
-> Either Error Signature
-> Decoder s Signature
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s Signature
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s Signature)
-> (Error -> String) -> Error -> Decoder s Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) Signature -> Decoder s Signature
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Signature -> Decoder s Signature)
-> (ByteString -> Either Error Signature)
-> ByteString
-> Decoder s Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error Signature
Sig.fromBytes