{-# LANGUAGE DataKinds #-}

{- |
Module      : Singular.Registry.Signing
Description : Payment-key witnesses and signed transaction capability
License     : Apache-2.0

Signing changes only the witness set. The body and its transaction identity
are preserved. The constructor stays private: submission capabilities accept
only a transaction passed through this payment-key signing boundary.
-}
module Singular.Registry.Signing
    ( SignedTx
    , detachedWitness
    , signTx
    , signedTx
    , assembleSigned
    ) where

import Cardano.Crypto.DSIGN
    ( Ed25519DSIGN
    , SignKeyDSIGN
    , deriveVerKeyDSIGN
    )
import Cardano.Ledger.Api.Tx (addrTxWitsL, txIdTx, witsTxL)
import Cardano.Ledger.Core (extractHash)
import Cardano.Ledger.Keys
    ( KeyRole (..)
    , VKey (..)
    , WitVKey (..)
    , asWitness
    , signedDSIGN
    )
import Cardano.Ledger.TxIn (TxId (..))
import Cardano.Tx.Ledger (ConwayTx)
import Data.Set qualified as Set
import Lens.Micro ((%~), (&), (.~))

-- | A transaction carrying a witness from the key that signed its body.
newtype SignedTx = SignedTx ConwayTx

{- | The verification-key witness of a transaction's body hash, signed
by the key: the same witness 'signTx' adds to a transaction's witness
set, detached, so the offline bundle-signing path can write it to its
own file. Pure.
-}
detachedWitness
    :: SignKeyDSIGN Ed25519DSIGN
    -> ConwayTx
    -> WitVKey Witness
detachedWitness :: SignKeyDSIGN DSIGN -> ConwayTx -> WitVKey Witness
detachedWitness SignKeyDSIGN DSIGN
key ConwayTx
tx =
    let TxId SafeHash EraIndependentTxBody
bodyHash = ConwayTx -> TxId
forall era (l :: TxLevel). EraTx era => Tx l era -> TxId
txIdTx ConwayTx
tx
    in  VKey Witness
-> SignedDSIGN DSIGN (Hash HASH EraIndependentTxBody)
-> WitVKey Witness
forall (kr :: KeyRole).
VKey kr
-> SignedDSIGN DSIGN (Hash HASH EraIndependentTxBody) -> WitVKey kr
WitVKey
            (VKey (ZonkAny 0) -> VKey Witness
forall (a :: KeyRole -> *) (r :: KeyRole).
HasKeyRole a =>
a r -> a Witness
asWitness (VerKeyDSIGN DSIGN -> VKey (ZonkAny 0)
forall (kd :: KeyRole). VerKeyDSIGN DSIGN -> VKey kd
VKey (SignKeyDSIGN DSIGN -> VerKeyDSIGN DSIGN
forall v. DSIGNAlgorithm v => SignKeyDSIGN v -> VerKeyDSIGN v
deriveVerKeyDSIGN SignKeyDSIGN DSIGN
key)))
            (SignKeyDSIGN DSIGN
-> Hash HASH EraIndependentTxBody
-> SignedDSIGN DSIGN (Hash HASH EraIndependentTxBody)
forall a.
Signable DSIGN a =>
SignKeyDSIGN DSIGN -> a -> SignedDSIGN DSIGN a
signedDSIGN SignKeyDSIGN DSIGN
key (SafeHash EraIndependentTxBody -> Hash HASH EraIndependentTxBody
forall i. SafeHash i -> Hash HASH i
extractHash SafeHash EraIndependentTxBody
bodyHash))

-- | Add a payment-key witness, preserving the body and other witnesses.
signTx :: SignKeyDSIGN Ed25519DSIGN -> ConwayTx -> SignedTx
signTx :: SignKeyDSIGN DSIGN -> ConwayTx -> SignedTx
signTx SignKeyDSIGN DSIGN
key ConwayTx
tx =
    ConwayTx -> SignedTx
SignedTx
        (ConwayTx
tx ConwayTx -> (ConwayTx -> ConwayTx) -> ConwayTx
forall a b. a -> (a -> b) -> b
& (TxWits ConwayEra -> Identity (TxWits ConwayEra))
-> ConwayTx -> Identity ConwayTx
(AlonzoTxWits ConwayEra -> Identity (AlonzoTxWits ConwayEra))
-> ConwayTx -> Identity ConwayTx
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxWits era)
forall (l :: TxLevel). Lens' (Tx l ConwayEra) (TxWits ConwayEra)
witsTxL ((AlonzoTxWits ConwayEra -> Identity (AlonzoTxWits ConwayEra))
 -> ConwayTx -> Identity ConwayTx)
-> ((Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
    -> AlonzoTxWits ConwayEra -> Identity (AlonzoTxWits ConwayEra))
-> (Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
-> ConwayTx
-> Identity ConwayTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
-> TxWits ConwayEra -> Identity (TxWits ConwayEra)
(Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
-> AlonzoTxWits ConwayEra -> Identity (AlonzoTxWits ConwayEra)
forall era.
EraTxWits era =>
Lens' (TxWits era) (Set (WitVKey Witness))
Lens' (TxWits ConwayEra) (Set (WitVKey Witness))
addrTxWitsL ((Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
 -> ConwayTx -> Identity ConwayTx)
-> (Set (WitVKey Witness) -> Set (WitVKey Witness))
-> ConwayTx
-> ConwayTx
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ WitVKey Witness -> Set (WitVKey Witness) -> Set (WitVKey Witness)
forall a. Ord a => a -> Set a -> Set a
Set.insert (SignKeyDSIGN DSIGN -> ConwayTx -> WitVKey Witness
detachedWitness SignKeyDSIGN DSIGN
key ConwayTx
tx))

-- | The signed ledger transaction, for identity and durable journalling.
signedTx :: SignedTx -> ConwayTx
signedTx :: SignedTx -> ConwayTx
signedTx (SignedTx ConwayTx
tx) = ConwayTx
tx

{- | Assemble a signed transaction from a transaction and its verified
witnesses: the body bytes and identity of the transaction, with
exactly those witnesses — the key witnesses the envelope's
transaction already carries are replaced, not kept, so an envelope
holding a witness that was never supplied (another body's, or an
unneeded key's) does not smuggle it into the submission. The
witnesses must have been verified against the body beforehand — this
boundary adds them without signing and without verifying; the submit
command verifies first and reconstructs the key witnesses it hands
here.
-}
assembleSigned :: ConwayTx -> [WitVKey Witness] -> SignedTx
assembleSigned :: ConwayTx -> [WitVKey Witness] -> SignedTx
assembleSigned ConwayTx
tx [WitVKey Witness]
witnesses =
    ConwayTx -> SignedTx
SignedTx (ConwayTx
tx ConwayTx -> (ConwayTx -> ConwayTx) -> ConwayTx
forall a b. a -> (a -> b) -> b
& (TxWits ConwayEra -> Identity (TxWits ConwayEra))
-> ConwayTx -> Identity ConwayTx
(AlonzoTxWits ConwayEra -> Identity (AlonzoTxWits ConwayEra))
-> ConwayTx -> Identity ConwayTx
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxWits era)
forall (l :: TxLevel). Lens' (Tx l ConwayEra) (TxWits ConwayEra)
witsTxL ((AlonzoTxWits ConwayEra -> Identity (AlonzoTxWits ConwayEra))
 -> ConwayTx -> Identity ConwayTx)
-> ((Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
    -> AlonzoTxWits ConwayEra -> Identity (AlonzoTxWits ConwayEra))
-> (Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
-> ConwayTx
-> Identity ConwayTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
-> TxWits ConwayEra -> Identity (TxWits ConwayEra)
(Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
-> AlonzoTxWits ConwayEra -> Identity (AlonzoTxWits ConwayEra)
forall era.
EraTxWits era =>
Lens' (TxWits era) (Set (WitVKey Witness))
Lens' (TxWits ConwayEra) (Set (WitVKey Witness))
addrTxWitsL ((Set (WitVKey Witness) -> Identity (Set (WitVKey Witness)))
 -> ConwayTx -> Identity ConwayTx)
-> Set (WitVKey Witness) -> ConwayTx -> ConwayTx
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [WitVKey Witness] -> Set (WitVKey Witness)
forall a. Ord a => [a] -> Set a
Set.fromList [WitVKey Witness]
witnesses)