Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staging #596

Merged
merged 12 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Revision history for plutarch

# 1.3.0

- Support newer Plutus
- Remove most of the Nix code and switch to mlabs-tooling.nix

# 1.2.1

- Exported data constructors instance for `Plutarch.Lift (LiftError)`
Expand Down
19 changes: 10 additions & 9 deletions Plutarch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Plutarch (
(PI.:-->),
PI.ClosedTerm,
PI.compile,
PI.Script,
PI.Dig,
PI.hashTerm,
PI.papp,
Expand Down Expand Up @@ -55,16 +56,16 @@ module Plutarch (
PP.PVariant',
) where

import qualified Plutarch.Internal as PI
import qualified Plutarch.Internal.Newtype as PN
import qualified Plutarch.Internal.Other as PO
import qualified Plutarch.Internal.PLam as PL
import qualified Plutarch.Internal.PlutusType as PP
import qualified Plutarch.Internal.Quantification as PQ
import qualified Plutarch.Internal.ScottEncoding as PS
import Plutarch.Internal qualified as PI
import Plutarch.Internal.Newtype qualified as PN
import Plutarch.Internal.Other qualified as PO
import Plutarch.Internal.PLam qualified as PL
import Plutarch.Internal.PlutusType qualified as PP
import Plutarch.Internal.Quantification qualified as PQ
import Plutarch.Internal.ScottEncoding qualified as PS
import Plutarch.Num ()
import qualified Plutarch.Pretty as PPR
import qualified Plutarch.TermCont as PT
import Plutarch.Pretty qualified as PPR
import Plutarch.TermCont qualified as PT

-- import orphan instances
import Prelude ()
16 changes: 9 additions & 7 deletions Plutarch/Api/Internal/Hashing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import Crypto.Hash.Algorithms (
)
import Data.ByteArray (convert)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as Lazy
import Data.ByteString.Lazy (toStrict)
import Data.ByteString.Short (fromShort)

import qualified PlutusLedgerApi.V1 as Plutus
import qualified PlutusLedgerApi.V1.Scripts as Plutus
import qualified PlutusTx.Builtins as PlutusTx
import Plutarch.Script (Script (unScript))
import PlutusLedgerApi.Common (serialiseUPLC)
import PlutusLedgerApi.V1 qualified as Plutus
import PlutusTx.Builtins qualified as PlutusTx

_plutusHashWith :: HashAlgorithm alg => alg -> ByteString -> PlutusTx.BuiltinByteString
_plutusHashWith alg = PlutusTx.toBuiltin . convert @_ @ByteString . hashWith alg
Expand All @@ -29,15 +31,15 @@ hashBlake2b_256 :: ByteString -> PlutusTx.BuiltinByteString
hashBlake2b_256 = _plutusHashWith Blake2b_256

-- | Hash a Script with the given version prefix
hashScriptWithPrefix :: ByteString -> Plutus.Script -> Plutus.ScriptHash
hashScriptWithPrefix :: ByteString -> Script -> Plutus.ScriptHash
hashScriptWithPrefix prefix scr =
Plutus.ScriptHash
. hashBlake2b_224
$ prefix <> Lazy.toStrict (serialise scr)
$ prefix <> (fromShort . serialiseUPLC . unScript $ scr)

-- | Hash Plutus 'Data'.
hashData :: Plutus.Data -> PlutusTx.BuiltinByteString
hashData = hashBlake2b_256 . Lazy.toStrict . serialise
hashData = hashBlake2b_256 . toStrict . serialise

-- | Hash 'LedgerBytes'.
hashLedgerBytes :: Plutus.LedgerBytes -> PlutusTx.BuiltinByteString
Expand Down
96 changes: 31 additions & 65 deletions Plutarch/Api/V1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,15 @@ module Plutarch.Api.V1 (
Scripts.PDatumHash (PDatumHash),
Scripts.PRedeemer (PRedeemer),
Scripts.PRedeemerHash (PRedeemerHash),
Scripts.PStakeValidatorHash (PStakeValidatorHash),
Scripts.PValidatorHash (PValidatorHash),

-- ** Script Utils
validatorHash,
mintingPolicySymbol,
stakeValidatorHash,
scriptHash,
datumHash,
redeemerHash,
dataHash,
mkValidator,
mkStakeValidator,
mkMintingPolicy,
type PValidator,
type PMintingPolicy,
type PStakeValidator,
PValidator,
PStakeValidator,
PMintingPolicy,

-- ** Value
Value.PValue (PValue),
Expand Down Expand Up @@ -84,72 +76,37 @@ module Plutarch.Api.V1 (

--------------------------------------------------------------------------------

import qualified Plutarch.Api.V1.Address as Address
import qualified Plutarch.Api.V1.AssocMap as AssocMap
import qualified Plutarch.Api.V1.Contexts as Contexts
import qualified Plutarch.Api.V1.Crypto as Crypto
import qualified Plutarch.Api.V1.DCert as DCert
import qualified Plutarch.Api.V1.Interval as Interval
import qualified Plutarch.Api.V1.Maybe as Maybe
import qualified Plutarch.Api.V1.Scripts as Scripts
import qualified Plutarch.Api.V1.Time as Time
import qualified Plutarch.Api.V1.Tuple as Tuple
import qualified Plutarch.Api.V1.Tx as Tx
import qualified Plutarch.Api.V1.Value as Value

import Data.Coerce (coerce)
import Plutarch.Api.V1.Address qualified as Address
import Plutarch.Api.V1.AssocMap qualified as AssocMap
import Plutarch.Api.V1.Contexts qualified as Contexts
import Plutarch.Api.V1.Crypto qualified as Crypto
import Plutarch.Api.V1.DCert qualified as DCert
import Plutarch.Api.V1.Interval qualified as Interval
import Plutarch.Api.V1.Maybe qualified as Maybe
import Plutarch.Api.V1.Scripts qualified as Scripts
import Plutarch.Api.V1.Time qualified as Time
import Plutarch.Api.V1.Tuple qualified as Tuple
import Plutarch.Api.V1.Tx qualified as Tx
import Plutarch.Api.V1.Value qualified as Value

import Plutarch (POpaque, (:-->))
import Plutarch.Builtin (PData)

-- note about V2: This should there are no changes in Scripts or V1 itself that affect this module
import qualified PlutusLedgerApi.V1 as Plutus
import qualified PlutusLedgerApi.V1.Scripts as Plutus

import Plutarch (Config, compile)
import Plutarch.Script qualified as Plutus
import PlutusLedgerApi.V1 qualified as Plutus

import Plutarch.Api.Internal.Hashing (hashData, hashScriptWithPrefix)
import Plutarch.Api.V1.Contexts (PScriptContext)
import Plutarch.Prelude

import qualified Data.Text as T
import GHC.Stack (HasCallStack)
import Data.Coerce (coerce)

-- On-chain Script Types

-- | a Validator Term
type PValidator = PData :--> PData :--> PScriptContext :--> POpaque

-- | a MintingPolicy Term
type PMintingPolicy = PData :--> PScriptContext :--> POpaque

-- | a StakeValidator Term
type PStakeValidator = PData :--> PScriptContext :--> POpaque

-- | Compile a Validator
mkValidator :: HasCallStack => Config -> ClosedTerm PValidator -> Plutus.Validator
mkValidator config s = Plutus.Validator $ either (error . T.unpack) id $ compile config s

-- | Compile a MintingPolicy
mkMintingPolicy :: HasCallStack => Config -> ClosedTerm PMintingPolicy -> Plutus.MintingPolicy
mkMintingPolicy config s = Plutus.MintingPolicy $ either (error . T.unpack) id $ compile config s

-- | Compile a StakeValidator
mkStakeValidator :: HasCallStack => Config -> ClosedTerm PStakeValidator -> Plutus.StakeValidator
mkStakeValidator config s = Plutus.StakeValidator $ either (error . T.unpack) id $ compile config s

-- | Hash a Script, with the correct prefix for Plutus V1
scriptHash :: Plutus.Script -> Plutus.ScriptHash
scriptHash = hashScriptWithPrefix "\x01"

-- | Hash a Validator, with the correct prefix for Plutus V1
validatorHash :: Plutus.Validator -> Plutus.ValidatorHash
validatorHash = coerce scriptHash

-- | Hash a MintingPolicy, with the correct prefix for Plutus V1
mintingPolicySymbol :: Plutus.MintingPolicy -> Plutus.CurrencySymbol
mintingPolicySymbol = coerce scriptHash

-- | Hash a StakeValidator, with the correct prefix for Plutus V1
stakeValidatorHash :: Plutus.StakeValidator -> Plutus.StakeValidatorHash
stakeValidatorHash = coerce scriptHash

-- | Hash a Datum.
datumHash :: Plutus.Datum -> Plutus.DatumHash
datumHash = coerce . dataHash
Expand All @@ -161,3 +118,12 @@ redeemerHash = coerce . dataHash
-- | Hash the data encoded representation of given argument.
dataHash :: Plutus.ToData a => a -> Plutus.BuiltinByteString
dataHash = hashData . Plutus.toData

-- | a Validator Term
type PValidator = PData :--> PData :--> Contexts.PScriptContext :--> POpaque

-- | a MintingPolicy Term
type PMintingPolicy = PData :--> Contexts.PScriptContext :--> POpaque

-- | a StakeValidator Term
type PStakeValidator = PData :--> Contexts.PScriptContext :--> POpaque
6 changes: 3 additions & 3 deletions Plutarch/Api/V1/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module Plutarch.Api.V1.Address (
PAddress (PAddress),
) where

import qualified PlutusLedgerApi.V1 as Plutus
import PlutusLedgerApi.V1 qualified as Plutus

import Plutarch.Api.V1.Crypto (PPubKeyHash)
import Plutarch.Api.V1.Maybe (PMaybeData)
import Plutarch.Api.V1.Scripts (PValidatorHash)
import Plutarch.Api.V1.Scripts (PScriptHash)
import Plutarch.DataRepr (
DerivePConstantViaData (DerivePConstantViaData),
PDataFields,
Expand All @@ -26,7 +26,7 @@ import Plutarch.Prelude

data PCredential (s :: S)
= PPubKeyCredential (Term s (PDataRecord '["_0" ':= PPubKeyHash]))
| PScriptCredential (Term s (PDataRecord '["_0" ':= PValidatorHash]))
| PScriptCredential (Term s (PDataRecord '["_0" ':= PScriptHash]))
deriving stock (Generic)
deriving anyclass (PlutusType, PIsData, PEq, PPartialOrd, POrd, PShow, PTryFrom PData)
instance DerivePlutusType PCredential where type DPTStrat _ = PlutusTypeData
Expand Down
Loading