{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide #-}
module Cometa.Internal.ContextualTransaction (findCertificate, requiredKeyHashes, runRequiredKeys) where
import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import qualified Cometa.Certificate as Certificate
import qualified Cometa.Crypto.Blake2b as Blake2b
import Cometa.Error (Error (..))
import qualified Cometa.Error.Code as Code
import qualified Cometa.Internal.FFI.ContextualTransaction as FFI
import qualified Cometa.Internal.Runtime as Runtime
import qualified Cometa.Internal.TransactionCbor as Cbor
import qualified Cometa.Transaction as Tx
import qualified Cometa.Transaction.Body as Body
import qualified Cometa.Transaction.Fields as Fields
import qualified Cometa.Transaction.Utxo as Utxo
import Control.Monad (unless)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Maybe (fromMaybe)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Word (Word64)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Storable (peek)
findCertificate :: Certificate.EncodedCertificate -> Maybe ByteString -> IO (Either Error (Maybe Word64))
findCertificate :: EncodedCertificate
-> Maybe ByteString -> IO (Either Error (Maybe Word64))
findCertificate EncodedCertificate
_ Maybe ByteString
Nothing = Either Error (Maybe Word64) -> IO (Either Error (Maybe Word64))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Word64 -> Either Error (Maybe Word64)
forall a b. b -> Either a b
Right Maybe Word64
forall a. Maybe a
Nothing)
findCertificate EncodedCertificate
certificate (Just ByteString
collection) =
ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error (Maybe Word64)))
-> IO (Either Error (Maybe Word64))
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes ByteString
collection ((Ptr Word8 -> CSize -> IO (Either Error (Maybe Word64)))
-> IO (Either Error (Maybe Word64)))
-> (Ptr Word8 -> CSize -> IO (Either Error (Maybe Word64)))
-> IO (Either Error (Maybe Word64))
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
bytes CSize
size ->
ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error (Maybe Word64)))
-> IO (Either Error (Maybe Word64))
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes (EncodedCertificate -> ByteString
Certificate.toCbor EncodedCertificate
certificate) ((Ptr Word8 -> CSize -> IO (Either Error (Maybe Word64)))
-> IO (Either Error (Maybe Word64)))
-> (Ptr Word8 -> CSize -> IO (Either Error (Maybe Word64)))
-> IO (Either Error (Maybe Word64))
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
target CSize
targetSize ->
(Ptr Word64 -> IO (Either Error (Maybe Word64)))
-> IO (Either Error (Maybe Word64))
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr Word64 -> IO (Either Error (Maybe Word64)))
-> IO (Either Error (Maybe Word64)))
-> (Ptr Word64 -> IO (Either Error (Maybe Word64)))
-> IO (Either Error (Maybe Word64))
forall a b. (a -> b) -> a -> b
$ \Ptr Word64
index -> do
NativeCode
status <- Ptr Word8
-> CSize -> Ptr Word8 -> CSize -> Ptr Word64 -> IO NativeCode
FFI.cCertificateIndex Ptr Word8
bytes CSize
size Ptr Word8
target CSize
targetSize Ptr Word64
index
if NativeCode -> Integer
forall a. Integral a => a -> Integer
toInteger NativeCode
status Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== ErrorCode -> Integer
Code.toNumber ErrorCode
Code.ElementNotFound
then Either Error (Maybe Word64) -> IO (Either Error (Maybe Word64))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Word64 -> Either Error (Maybe Word64)
forall a b. b -> Either a b
Right Maybe Word64
forall a. Maybe a
Nothing)
else do
Either Error ()
checked <- Text -> NativeCode -> IO (Either Error ())
Runtime.checkStatus Text
"transaction.indices.certificate" NativeCode
status
case Either Error ()
checked of
Left Error
failure -> Either Error (Maybe Word64) -> IO (Either Error (Maybe Word64))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error (Maybe Word64)
forall a b. a -> Either a b
Left Error
failure)
Right () -> Maybe Word64 -> Either Error (Maybe Word64)
forall a b. b -> Either a b
Right (Maybe Word64 -> Either Error (Maybe Word64))
-> (Word64 -> Maybe Word64)
-> Word64
-> Either Error (Maybe Word64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Maybe Word64
forall a. a -> Maybe a
Just (Word64 -> Either Error (Maybe Word64))
-> IO Word64 -> IO (Either Error (Maybe Word64))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word64 -> IO Word64
forall a. Storable a => Ptr a -> IO a
peek Ptr Word64
index
requiredKeyHashes :: Tx.Transaction -> [Utxo.Utxo] -> IO (Either Error (Set Blake2b.Blake2bHash))
requiredKeyHashes :: Transaction -> [Utxo] -> IO (Either Error (Set Blake2bHash))
requiredKeyHashes Transaction
transaction [Utxo]
resolved = case Transaction -> [Utxo] -> Either Error ()
validateResolution Transaction
transaction [Utxo]
resolved of
Left Error
failure -> Either Error (Set Blake2bHash)
-> IO (Either Error (Set Blake2bHash))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error (Set Blake2bHash)
forall a b. a -> Either a b
Left Error
failure)
Right () -> do
Either Error ByteString
result <- ByteString -> ByteString -> IO (Either Error ByteString)
runRequiredKeys (Transaction -> ByteString
Tx.encodeCbor Transaction
transaction) ([Utxo] -> ByteString
encodeUtxos [Utxo]
resolved)
Either Error (Set Blake2bHash)
-> IO (Either Error (Set Blake2bHash))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error (Set Blake2bHash)
-> IO (Either Error (Set Blake2bHash)))
-> Either Error (Set Blake2bHash)
-> IO (Either Error (Set Blake2bHash))
forall a b. (a -> b) -> a -> b
$ do
ByteString
bytes <- Either Error ByteString
result
[ByteString]
hashes <- (forall s. Decoder s [ByteString])
-> ByteString -> Either Error [ByteString]
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode (Decoder s ByteString -> Decoder s [ByteString]
forall s a. Decoder s a -> Decoder s [a]
Cbor.setList Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes) ByteString
bytes
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((ByteString -> Bool) -> [ByteString] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
28) (Int -> Bool) -> (ByteString -> Int) -> ByteString -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
B.length) [ByteString]
hashes) (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
"transaction.authority.hashSize"))
[Blake2bHash] -> Set Blake2bHash
forall a. Ord a => [a] -> Set a
Set.fromList ([Blake2bHash] -> Set Blake2bHash)
-> Either Error [Blake2bHash] -> Either Error (Set Blake2bHash)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> Either Error Blake2bHash)
-> [ByteString] -> Either Error [Blake2bHash]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ByteString -> Either Error Blake2bHash
Blake2b.fromBytes [ByteString]
hashes
validateResolution :: Tx.Transaction -> [Utxo.Utxo] -> Either Error ()
validateResolution :: Transaction -> [Utxo] -> Either Error ()
validateResolution Transaction
transaction [Utxo]
resolved = do
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Set Input -> Int
forall a. Set a -> Int
Set.size Set Input
available Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [Input] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Input]
references) (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidTransactionField Text
"Duplicate resolved UTxO references"))
Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Input -> Bool) -> [Input] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Input -> Set Input -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Input
available) [Input]
required) (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidTransactionField Text
"All spending and collateral inputs must resolve"))
where
body :: Body
body = EncodedBody -> Body
Body.toBody (Transaction -> EncodedBody
Tx.body Transaction
transaction)
required :: [Input]
required = Fields -> [Input]
Fields.fieldInputs (Body -> Fields
Body.bodyFields Body
body) [Input] -> [Input] -> [Input]
forall a. [a] -> [a] -> [a]
++ [Input] -> Maybe [Input] -> [Input]
forall a. a -> Maybe a -> a
fromMaybe [] (Body -> Maybe [Input]
Body.bodyCollateral Body
body)
references :: [Input]
references = (Utxo -> Input) -> [Utxo] -> [Input]
forall a b. (a -> b) -> [a] -> [b]
map Utxo -> Input
Utxo.input [Utxo]
resolved
available :: Set Input
available = [Input] -> Set Input
forall a. Ord a => [a] -> Set a
Set.fromList [Input]
references
encodeUtxos :: [Utxo.Utxo] -> ByteString
encodeUtxos :: [Utxo] -> ByteString
encodeUtxos [Utxo]
values = Encoding -> ByteString
Cbor.encode (Word -> Encoding
E.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Utxo] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Utxo]
values)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Utxo -> Encoding) -> [Utxo] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (ByteString -> Encoding
Cbor.raw (ByteString -> Encoding)
-> (Utxo -> ByteString) -> Utxo -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> ByteString
Utxo.encodeCbor) [Utxo]
values)
runRequiredKeys :: ByteString -> ByteString -> IO (Either Error ByteString)
runRequiredKeys :: ByteString -> ByteString -> IO (Either Error ByteString)
runRequiredKeys ByteString
transaction ByteString
resolved =
ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes ByteString
transaction ((Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString))
-> (Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
bytes CSize
size ->
ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes ByteString
resolved ((Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString))
-> (Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
utxos CSize
utxosSize ->
Text
-> (Ptr (Ptr Buffer) -> IO NativeCode)
-> IO (Either Error ByteString)
Runtime.withOwnedBuffer Text
"transaction.authority.requiredKeyHashes" (Ptr Word8
-> CSize -> Ptr Word8 -> CSize -> Ptr (Ptr Buffer) -> IO NativeCode
FFI.cRequiredKeys Ptr Word8
bytes CSize
size Ptr Word8
utxos CSize
utxosSize)