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

-- | Produce and attach Ed25519 transaction witnesses using scoped signing keys.
-- Signatures cover the exact transaction-body hash; witnesses and outer framing
-- never enter the signed message. Operations preserve the source transaction.
module Cometa.Transaction.Signing
  ( verificationWitness,
    sign,
    subTransactionWitness,
    signSubTransaction,
  )
where

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

import qualified Cometa.Crypto.Ed25519.SigningKey as Key
import Cometa.Error (Error)
import qualified Cometa.SubTransaction as Sub
import qualified Cometa.Transaction as Tx
import qualified Cometa.Transaction.Witness as Witness
import qualified Cometa.Transaction.Witnesses as Witnesses
import qualified Cometa.TransactionId as TxId
import Control.Monad.Trans.Except (ExceptT (..), runExceptT)

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

-- | Sign the exact body hash and pair it with the key's public key. The key
-- must remain open throughout the operation. No transaction bytes are changed.
verificationWitness :: Key.SigningKey -> Tx.Transaction -> IO (Either Error Witness.VerificationWitness)
verificationWitness :: SigningKey -> Transaction -> IO (Either Error VerificationWitness)
verificationWitness SigningKey
key Transaction
transaction = SigningKey
-> IO (Either Error TransactionId)
-> IO (Either Error VerificationWitness)
witnessForHash SigningKey
key (Transaction -> IO (Either Error TransactionId)
Tx.transactionId Transaction
transaction)

-- | Add this key's witness, retaining existing partial signatures and exact body
-- bytes. Repeating the same signature is an exact no-op; a conflicting existing
-- signature for that public key fails. Closed keys and native failures fail.
sign :: Key.SigningKey -> Tx.Transaction -> IO (Either Error Tx.Transaction)
sign :: SigningKey -> Transaction -> IO (Either Error Transaction)
sign SigningKey
key Transaction
transaction = ExceptT Error IO Transaction -> IO (Either Error Transaction)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO Transaction -> IO (Either Error Transaction))
-> ExceptT Error IO Transaction -> IO (Either Error Transaction)
forall a b. (a -> b) -> a -> b
$ do
  VerificationWitness
witness <- IO (Either Error VerificationWitness)
-> ExceptT Error IO VerificationWitness
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (SigningKey -> Transaction -> IO (Either Error VerificationWitness)
verificationWitness SigningKey
key Transaction
transaction)
  EncodedWitnesses
merged <- IO (Either Error EncodedWitnesses)
-> ExceptT Error IO EncodedWitnesses
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Either Error EncodedWitnesses -> IO (Either Error EncodedWitnesses)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VerificationWitness
-> EncodedWitnesses -> Either Error EncodedWitnesses
Witnesses.addVerificationWitness VerificationWitness
witness (Transaction -> EncodedWitnesses
Tx.witnesses Transaction
transaction)))
  Transaction -> ExceptT Error IO Transaction
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EncodedWitnesses -> Transaction -> Transaction
Tx.setWitnesses EncodedWitnesses
merged Transaction
transaction)

-- | Sign a sub-transaction's own body hash, independent of its enclosing body.
subTransactionWitness :: Key.SigningKey -> Sub.SubTransaction -> IO (Either Error Witness.VerificationWitness)
subTransactionWitness :: SigningKey
-> SubTransaction -> IO (Either Error VerificationWitness)
subTransactionWitness SigningKey
key SubTransaction
transaction = SigningKey
-> IO (Either Error TransactionId)
-> IO (Either Error VerificationWitness)
witnessForHash SigningKey
key (SubTransaction -> IO (Either Error TransactionId)
Sub.transactionId SubTransaction
transaction)

-- | Add a signature to this sub-transaction while preserving its exact body.
signSubTransaction :: Key.SigningKey -> Sub.SubTransaction -> IO (Either Error Sub.SubTransaction)
signSubTransaction :: SigningKey -> SubTransaction -> IO (Either Error SubTransaction)
signSubTransaction SigningKey
key SubTransaction
transaction = ExceptT Error IO SubTransaction -> IO (Either Error SubTransaction)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO SubTransaction
 -> IO (Either Error SubTransaction))
-> ExceptT Error IO SubTransaction
-> IO (Either Error SubTransaction)
forall a b. (a -> b) -> a -> b
$ do
  VerificationWitness
witness <- IO (Either Error VerificationWitness)
-> ExceptT Error IO VerificationWitness
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (SigningKey
-> SubTransaction -> IO (Either Error VerificationWitness)
subTransactionWitness SigningKey
key SubTransaction
transaction)
  EncodedWitnesses
merged <- IO (Either Error EncodedWitnesses)
-> ExceptT Error IO EncodedWitnesses
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Either Error EncodedWitnesses -> IO (Either Error EncodedWitnesses)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VerificationWitness
-> EncodedWitnesses -> Either Error EncodedWitnesses
Witnesses.addVerificationWitness VerificationWitness
witness (SubTransaction -> EncodedWitnesses
Sub.witnesses SubTransaction
transaction)))
  SubTransaction -> ExceptT Error IO SubTransaction
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EncodedWitnesses -> SubTransaction -> SubTransaction
Sub.setWitnesses EncodedWitnesses
merged SubTransaction
transaction)

witnessForHash :: Key.SigningKey -> IO (Either Error TxId.TransactionId) -> IO (Either Error Witness.VerificationWitness)
witnessForHash :: SigningKey
-> IO (Either Error TransactionId)
-> IO (Either Error VerificationWitness)
witnessForHash SigningKey
key IO (Either Error TransactionId)
hashAction = ExceptT Error IO VerificationWitness
-> IO (Either Error VerificationWitness)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO VerificationWitness
 -> IO (Either Error VerificationWitness))
-> ExceptT Error IO VerificationWitness
-> IO (Either Error VerificationWitness)
forall a b. (a -> b) -> a -> b
$ do
  TransactionId
digest <- IO (Either Error TransactionId) -> ExceptT Error IO TransactionId
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT IO (Either Error TransactionId)
hashAction
  PublicKey
public <- IO (Either Error PublicKey) -> ExceptT Error IO PublicKey
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (SigningKey -> IO (Either Error PublicKey)
Key.publicKey SigningKey
key)
  Signature
signature <- IO (Either Error Signature) -> ExceptT Error IO Signature
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (SigningKey -> ByteString -> IO (Either Error Signature)
Key.sign SigningKey
key (TransactionId -> ByteString
TxId.toBytes TransactionId
digest))
  VerificationWitness -> ExceptT Error IO VerificationWitness
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PublicKey -> Signature -> VerificationWitness
Witness.verificationWitness PublicKey
public Signature
signature)