Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
SCP-3130: Modified the tx creation constraints to allow to specify th…
Browse files Browse the repository at this point in the history
…e stake public key hash of a transaction output address. (#181)

* Added the constraints `mustPayToPubKeyAddress` and `mustPayWithDatumToPubKeyAddress`

* Added newtype wrappers to differentiate between public key hashes: `PaymentPubKeyHash`, `PaymentPubKey`, `StakePubKeyHash` and `StakePubKey`.

* Added newtype wrapper 'PaymentPrivateKey'

* Replaced most `PubKeyHash` and `PubKey` in code with the newly defined newtypes.

* Modified the name of `WalletEffect.ownPubKeyHash` to `WalletEffect.ownPaymentPubKeyHash`.

* Added the prefix 'mock' to the name of functions in `Wallet.Emulator.Wallet` to refer to Mock wallets.

* Formatted the modified modules based on the `-Wmissing-import-lists` GHC flag.
  • Loading branch information
koslambrou authored Dec 10, 2021
1 parent b4277aa commit 46e831e
Show file tree
Hide file tree
Showing 120 changed files with 4,168 additions and 3,745 deletions.
26 changes: 14 additions & 12 deletions doc/plutus/tutorials/BasicApps.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@ import Control.Monad (void)
import Data.Aeson (FromJSON, ToJSON)
import Data.Text qualified as T
import GHC.Generics (Generic)
import Ledger
import Ledger (Ada, PaymentPubKeyHash (unPaymentPubKeyHash), ScriptContext (ScriptContext, scriptContextTxInfo),
valuePaidTo)
import Ledger.Ada qualified as Ada
import Ledger.Constraints qualified as Constraints
import Ledger.Typed.Scripts qualified as Scripts
import Plutus.Contract
import PlutusTx qualified as PlutusTx
import PlutusTx.Prelude
import Plutus.Contract (Contract, Endpoint, Promise, collectFromScript, endpoint, logInfo, selectList,
submitTxConstraints, submitTxConstraintsSpending, type (.\/), utxosAt)
import PlutusTx qualified
import PlutusTx.Prelude (Bool, Semigroup ((<>)), ($), (&&), (-), (.), (>=))
import Prelude qualified as Haskell
import Schema
import Wallet.Emulator.Wallet
import Schema (ToSchema)
import Wallet.Emulator.Wallet (Wallet, mockWalletPaymentPubKeyHash)

-- BLOCK1

data SplitData =
SplitData
{ recipient1 :: PubKeyHash -- ^ First recipient of the funds
, recipient2 :: PubKeyHash -- ^ Second recipient of the funds
{ recipient1 :: PaymentPubKeyHash -- ^ First recipient of the funds
, recipient2 :: PaymentPubKeyHash -- ^ Second recipient of the funds
, amount :: Ada -- ^ How much Ada we want to lock
}
deriving stock (Haskell.Show, Generic)
Expand All @@ -50,8 +52,8 @@ PlutusTx.makeLift ''SplitData
validateSplit :: SplitData -> () -> ScriptContext -> Bool
validateSplit SplitData{recipient1, recipient2, amount} _ ScriptContext{scriptContextTxInfo} =
let half = Ada.divide amount 2 in
Ada.fromValue (valuePaidTo scriptContextTxInfo recipient1) >= half &&
Ada.fromValue (valuePaidTo scriptContextTxInfo recipient2) >= (amount - half)
Ada.fromValue (valuePaidTo scriptContextTxInfo (unPaymentPubKeyHash recipient1)) >= half &&
Ada.fromValue (valuePaidTo scriptContextTxInfo (unPaymentPubKeyHash recipient2)) >= (amount - half)

-- BLOCK3

Expand Down Expand Up @@ -94,8 +96,8 @@ unlock = endpoint @"unlock" (unlockFunds . mkSplitData)
mkSplitData :: LockArgs -> SplitData
mkSplitData LockArgs{recipient1Wallet, recipient2Wallet, totalAda} =
SplitData
{ recipient1 = walletPubKeyHash recipient1Wallet
, recipient2 = walletPubKeyHash recipient2Wallet
{ recipient1 = mockWalletPaymentPubKeyHash recipient1Wallet
, recipient2 = mockWalletPaymentPubKeyHash recipient2Wallet
, amount = totalAda
}

Expand Down
2 changes: 1 addition & 1 deletion doc/plutus/tutorials/basic-apps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Next you need to turn the two ``Wallet`` values into their public key hashes so
:start-after: BLOCK6
:end-before: BLOCK7

Note that the :hsobj:`Wallet.Emulator.Wallet.walletPubKeyHash` function and the :hsobj:`Wallet.Emulator.Wallet.Wallet` type are only available in the simulated environment used by the Plutus playground and by Plutus tests.
Note that the :hsobj:`Wallet.Emulator.Wallet.mockWalletPaymentPubKeyHash` function and the :hsobj:`Wallet.Emulator.Wallet.Wallet` type are only available in the simulated environment used by the Plutus playground and by Plutus tests.
A real Plutus app would use the metadata server or a custom lookup function for such conversions.

Locking the funds
Expand Down
16 changes: 7 additions & 9 deletions nix/pkgs/haskell/materialized-darwin/.plan.nix/plutus-ledger.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions nix/pkgs/haskell/materialized-linux/.plan.nix/plutus-ledger.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions playground-common/src/PSGenerator/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import Language.PureScript.Bridge (BridgePart, Language (Haskell), PSType, SumTy
import Language.PureScript.Bridge.Builder (BridgeData)
import Language.PureScript.Bridge.PSTypes (psInt, psNumber, psString)
import Language.PureScript.Bridge.TypeParameters (A)
import Ledger (Address, BlockId, ChainIndexTxOut, DatumHash, MintingPolicy, OnChainTx, PubKey, PubKeyHash, RedeemerPtr,
ScriptTag, Signature, StakeValidator, Tx, TxId, TxIn, TxInType, TxOut, TxOutRef, TxOutTx, UtxoIndex,
ValidationPhase, Validator)
import Ledger (Address, BlockId, ChainIndexTxOut, DatumHash, MintingPolicy, OnChainTx, PaymentPubKey, PaymentPubKeyHash,
PubKey, PubKeyHash, RedeemerPtr, ScriptTag, Signature, StakePubKey, StakePubKeyHash, StakeValidator, Tx,
TxId, TxIn, TxInType, TxOut, TxOutRef, TxOutTx, UtxoIndex, ValidationPhase, Validator)
import Ledger.Ada (Ada)
import Ledger.Constraints.OffChain (MkTxError, ScriptOutput, UnbalancedTx)
import Ledger.Credential (Credential, StakingCredential)
Expand Down Expand Up @@ -322,6 +322,10 @@ ledgerTypes =
, order . genericShow . argonaut $ mkSumType @DatumHash
, order . genericShow . argonaut $ mkSumType @PubKey
, order . genericShow . argonaut $ mkSumType @PubKeyHash
, order . genericShow . argonaut $ mkSumType @PaymentPubKey
, order . genericShow . argonaut $ mkSumType @PaymentPubKeyHash
, order . genericShow . argonaut $ mkSumType @StakePubKey
, order . genericShow . argonaut $ mkSumType @StakePubKeyHash
, order . genericShow . argonaut $ mkSumType @Credential
, order . genericShow . argonaut $ mkSumType @StakingCredential
, order . genericShow . argonaut $ mkSumType @DCert
Expand Down
6 changes: 3 additions & 3 deletions playground-common/src/Playground/Contract.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module Playground.Contract
, TraceError(..)
, type (.\/)
, interval
, ownPubKeyHash
, ownPaymentPubKeyHash
, awaitSlot
, modifiesUtxoSet
, utxosAt
Expand All @@ -66,8 +66,8 @@ import Playground.Interpreter.Util
import Playground.Schema (endpointsToSchemas)
import Playground.TH (ensureKnownCurrencies, mkFunction, mkFunctions, mkKnownCurrencies, mkSchemaDefinitions)
import Playground.Types (Expression, FunctionSchema, KnownCurrency (KnownCurrency), adaCurrency)
import Plutus.Contract (AsContractError, Contract, Endpoint, awaitSlot, endpoint, ownPubKeyHash, submitTx, type (.\/),
utxosAt, watchAddressUntilSlot)
import Plutus.Contract (AsContractError, Contract, Endpoint, awaitSlot, endpoint, ownPaymentPubKeyHash, submitTx,
type (.\/), utxosAt, watchAddressUntilSlot)
import Plutus.Contract.Trace (TraceError (..))
import Schema (FormSchema, ToArgument, ToSchema)
import Wallet.Emulator.Types (Wallet (..))
Expand Down
4 changes: 2 additions & 2 deletions playground-common/src/Playground/Interpreter/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import Wallet.Emulator.Folds (EmulatorEventFoldM)
import Wallet.Emulator.Folds qualified as Folds
import Wallet.Emulator.MultiAgent (EmulatorEvent, chainEvent, eteEvent, instanceEvent)
import Wallet.Emulator.Stream (foldEmulatorStreamM)
import Wallet.Emulator.Types (Wallet, WalletNumber, fromWalletNumber, walletPubKeyHash)
import Wallet.Emulator.Types (Wallet, WalletNumber, fromWalletNumber, mockWalletPaymentPubKeyHash)
import Wallet.Types (EndpointDescription (getEndpointDescription))

-- | Unfortunately any uncaught errors in the interpreter kill the
Expand Down Expand Up @@ -94,7 +94,7 @@ isInteresting x =

evaluationResultFold :: [WalletNumber] -> EmulatorEventFoldM effs EvaluationResult
evaluationResultFold wallets =
let pkh wallet = (walletPubKeyHash $ fromWalletNumber wallet, wallet)
let pkh wallet = (mockWalletPaymentPubKeyHash $ fromWalletNumber wallet, wallet)
in Playground.Types.EvaluationResult
<$> L.generalize (reverse <$> Folds.annotatedBlockchain)
<*> L.generalize (filter isInteresting <$> Folds.emulatorLog)
Expand Down
9 changes: 4 additions & 5 deletions playground-common/src/Playground/Types.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE DeriveTraversable #-}
Expand All @@ -26,7 +25,7 @@ import Data.Text (Text)
import GHC.Generics (Generic)
import Language.Haskell.Interpreter (CompilationError, SourceCode)
import Language.Haskell.Interpreter qualified as HI
import Ledger (PubKeyHash, fromSymbol)
import Ledger (PaymentPubKeyHash, fromSymbol)
import Ledger.Ada qualified as Ada
import Ledger.CardanoWallet qualified as CW
import Ledger.Scripts (ValidatorHash)
Expand Down Expand Up @@ -135,8 +134,8 @@ data Evaluation =
}
deriving (Generic, ToJSON, FromJSON)

pubKeys :: Evaluation -> [PubKeyHash]
pubKeys Evaluation {wallets} = CW.pubKeyHash . CW.fromWalletNumber . simulatorWalletWallet <$> wallets
pubKeys :: Evaluation -> [PaymentPubKeyHash]
pubKeys Evaluation {wallets} = CW.paymentPubKeyHash . CW.fromWalletNumber . simulatorWalletWallet <$> wallets

data EvaluationResult =
EvaluationResult
Expand All @@ -145,7 +144,7 @@ data EvaluationResult =
, emulatorTrace :: Text
, fundsDistribution :: [SimulatorWallet]
, feesDistribution :: [SimulatorWallet]
, walletKeys :: [(PubKeyHash, WalletNumber)]
, walletKeys :: [(PaymentPubKeyHash, WalletNumber)]
}
deriving (Show, Generic, ToJSON, FromJSON)

Expand Down
13 changes: 11 additions & 2 deletions playground-common/src/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ import Data.Text qualified as Text
import Data.UUID (UUID)
import GHC.Generics (C1, Constructor, D1, Generic, K1 (K1), M1 (M1), Rec0, Rep, S1, Selector, U1, conIsRecord, conName,
from, selName, (:*:) ((:*:)), (:+:) (L1, R1))
import Ledger (Ada, AssetClass, CurrencySymbol, DatumHash, Interval, POSIXTime, POSIXTimeRange, PubKey, PubKeyHash,
RedeemerHash, Signature, Slot, TokenName, TxId, TxOutRef, ValidatorHash, Value)
import Ledger (Ada, AssetClass, CurrencySymbol, DatumHash, Interval, POSIXTime, POSIXTimeRange, PaymentPubKey,
PaymentPubKeyHash, PubKey, PubKeyHash, RedeemerHash, Signature, Slot, StakePubKey, StakePubKeyHash,
TokenName, TxId, TxOutRef, ValidatorHash, Value)
import Ledger.Bytes (LedgerBytes)
import Ledger.CardanoWallet (WalletNumber)
import Plutus.Contract.Secrets (SecretArgument (EndpointSide, UserSide))
Expand Down Expand Up @@ -397,6 +398,14 @@ deriving anyclass instance ToSchema PubKey

deriving anyclass instance ToSchema PubKeyHash

deriving anyclass instance ToSchema PaymentPubKey

deriving anyclass instance ToSchema PaymentPubKeyHash

deriving anyclass instance ToSchema StakePubKey

deriving anyclass instance ToSchema StakePubKeyHash

deriving anyclass instance ToSchema RedeemerHash

deriving anyclass instance ToSchema Slot
Expand Down
17 changes: 10 additions & 7 deletions plutus-chain-index-core/test/Generators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@ import Hedgehog (MonadGen)
import Hedgehog.Gen qualified as Gen
import Hedgehog.Range qualified as Range
import Ledger.Ada qualified as Ada
import Ledger.Address (pubKeyAddress)
import Ledger.Address (PaymentPubKey (PaymentPubKey), pubKeyAddress)
import Ledger.Generators qualified as Gen
import Ledger.Interval qualified as Interval
import Ledger.Slot (Slot (..))
import Ledger.Tx (Address, TxIn (..), TxOut (..), TxOutRef (..))
import Ledger.TxId (TxId (..))
import Ledger.Slot (Slot (Slot))
import Ledger.Tx (Address, TxIn (TxIn), TxOut (TxOut), TxOutRef (TxOutRef))
import Ledger.TxId (TxId (TxId))
import Ledger.Value (Value)
import Ledger.Value qualified as Value
import Plutus.ChainIndex.Tx (ChainIndexTx (..), ChainIndexTxOutputs (..), txOutRefs)
import Plutus.ChainIndex.Tx (ChainIndexTx (ChainIndexTx), ChainIndexTxOutputs (ValidTx), txOutRefs)
import Plutus.ChainIndex.TxIdState qualified as TxIdState
import Plutus.ChainIndex.TxOutBalance qualified as TxOutBalance
import Plutus.ChainIndex.TxUtxoBalance qualified as TxUtxoBalance
import Plutus.ChainIndex.Types (BlockId (..), BlockNumber (..), Tip (..), TxIdState, TxOutBalance, TxUtxoBalance (..))
import Plutus.ChainIndex.Types (BlockId (BlockId), BlockNumber (BlockNumber),
Tip (Tip, tipBlockId, tipBlockNo, tipSlot), TxIdState, TxOutBalance, TxUtxoBalance)
import PlutusTx.Prelude qualified as PlutusTx

-- | Generate a random tx id
Expand Down Expand Up @@ -90,7 +91,9 @@ genSlot = Slot <$> Gen.integral (Range.linear 0 100000000)

-- | Generate a public key address
genAddress :: MonadGen m => m Address
genAddress = Gen.element $ pubKeyAddress <$> ["000fff", "aabbcc", "123123"]
genAddress = Gen.element
$ pubKeyAddress <$> (PaymentPubKey <$> ["000fff", "aabbcc", "123123"])
<*> pure Nothing

-- | Generate random Value (possibly containing Ada) with a positive Ada value.
genNonZeroAdaValue :: MonadGen m => m Value
Expand Down
3 changes: 1 addition & 2 deletions plutus-contract/src/Plutus/Contract.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module Plutus.Contract(
, Request.utxosTxOutTxFromTx
, Request.getTip
-- * Wallet's own public key
, Request.ownPubKeyHash
, Request.ownPaymentPubKeyHash
-- * Contract instance Id
, Wallet.Types.ContractInstanceId
, Request.ownInstanceId
Expand Down Expand Up @@ -121,7 +121,6 @@ import Plutus.Contract.Types (AsCheckpointError (..), AsContractError (..), Chec
import Control.Monad.Freer.Extras.Log qualified as L
import Control.Monad.Freer.Writer qualified as W
import Data.Functor.Apply (liftF2)
import Prelude
import Wallet.API (WalletAPIError)
import Wallet.Types qualified

Expand Down
Loading

0 comments on commit 46e831e

Please sign in to comment.