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

Commit

Permalink
plutus-contract: change Wallet type to WalletId from cardano-wallet (…
Browse files Browse the repository at this point in the history
…#3995)

* plutus-ledger: Add cardano-wallet constructor to 'WalletId'
  • Loading branch information
j-mueller authored Oct 14, 2021
1 parent a6addd8 commit b7eaa54
Show file tree
Hide file tree
Showing 119 changed files with 1,179 additions and 1,099 deletions.
7 changes: 2 additions & 5 deletions doc/plutus/tutorials/BasicApps.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,9 @@ unlock = endpoint @"unlock" (unlockFunds . mkSplitData)

mkSplitData :: LockArgs -> SplitData
mkSplitData LockArgs{recipient1Wallet, recipient2Wallet, totalAda} =
let convert :: Wallet -> PubKeyHash
convert = pubKeyHash . walletPubKey
in
SplitData
{ recipient1 = convert recipient1Wallet
, recipient2 = convert recipient2Wallet
{ recipient1 = walletPubKeyHash recipient1Wallet
, recipient2 = walletPubKeyHash 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.walletPubKey` 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.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.
A real Plutus app would use the metadata server or a custom lookup function for such conversions.

Locking the funds
Expand Down
21 changes: 14 additions & 7 deletions marlowe-dashboard-client/src/Bridge.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import Data.BigInteger (BigInteger)
import Data.Json.JsonUUID (JsonUUID(..))
import Data.Lens (Iso', iso)
import Data.Map (Map, fromFoldable, toUnfoldable) as Front
import Data.Maybe (Maybe)
import Data.Tuple (Tuple)
import Data.Tuple.Nested ((/\))
import Data.Json.JsonNTuple (JsonNTuple(..))
import Marlowe.PAB (PlutusAppId(..)) as Front
import Marlowe.Semantics (Assets(..), Slot(..)) as Front
import Marlowe.Semantics (Assets(..), Slot(..), PubKeyHash) as Front
import Network.RemoteData (RemoteData)
import Plutus.V1.Ledger.Crypto (PubKey(..), PubKeyHash(..)) as Back
import Plutus.V1.Ledger.Slot (Slot(..)) as Back
Expand All @@ -24,7 +26,7 @@ import PlutusTx.AssocMap (Map, fromTuples, toTuples) as Back
import Servant.PureScript.Ajax (AjaxError)
import Wallet.Emulator.Wallet (Wallet(..)) as Back
import Wallet.Types (ContractInstanceId(..)) as Back
import Contacts.Types (PubKeyHash(..), Wallet(..), WalletInfo(..)) as Front
import Contacts.Types (Wallet(..), WalletInfo(..)) as Front

{-
Note [JSON communication]: To ensure the client and the PAB server understand each other, they have
Expand Down Expand Up @@ -79,6 +81,10 @@ instance eitherBridge :: (Bridge a c, Bridge b d) => Bridge (Either a b) (Either
toFront = bimap toFront toFront
toBack = bimap toBack toBack

instance maybeBridge :: (Bridge a b) => Bridge (Maybe a) (Maybe b) where
toFront = map toFront
toBack = map toBack

instance mapBridge :: (Ord a, Ord c, Bridge a c, Bridge b d) => Bridge (Back.Map a b) (Front.Map c d) where
toFront map = Front.fromFoldable $ toFront <$> Back.toTuples map
toBack map = Back.fromTuples $ toBack <$> Front.toUnfoldable map
Expand Down Expand Up @@ -112,16 +118,17 @@ instance currencySymbolBridge :: Bridge Back.CurrencySymbol String where
toBack unCurrencySymbol = Back.CurrencySymbol { unCurrencySymbol }

instance walletInfoBridge :: Bridge Back.WalletInfo Front.WalletInfo where
toFront (Back.WalletInfo { wiWallet, wiPubKey, wiPubKeyHash }) = Front.WalletInfo { wallet: toFront wiWallet, pubKey: toFront wiPubKey, pubKeyHash: toFront wiPubKeyHash }
toBack (Front.WalletInfo { wallet, pubKey, pubKeyHash }) = Back.WalletInfo { wiWallet: toBack wallet, wiPubKey: toBack pubKey, wiPubKeyHash: toBack pubKeyHash }
toFront (Back.WalletInfo { wiWallet, wiPubKeyHash }) = Front.WalletInfo { wallet: toFront wiWallet, pubKeyHash: toFront wiPubKeyHash }
toBack (Front.WalletInfo { wallet, pubKeyHash }) = Back.WalletInfo { wiWallet: toBack wallet, wiPubKeyHash: toBack pubKeyHash }

instance walletBridge :: Bridge Back.Wallet Front.Wallet where
toFront (Back.Wallet { getWalletId }) = Front.Wallet getWalletId
toBack (Front.Wallet getWalletId) = Back.Wallet { getWalletId }

instance pubKeyHashBridge :: Bridge Back.PubKeyHash Front.PubKeyHash where
toFront (Back.PubKeyHash { getPubKeyHash }) = Front.PubKeyHash getPubKeyHash
toBack (Front.PubKeyHash getPubKeyHash) = Back.PubKeyHash { getPubKeyHash }
-- TODO: Marlowe.Semantics.PubKeyHash is currently just an alias for String
instance pubKeyHashBridge :: Bridge Back.PubKeyHash String where
toFront (Back.PubKeyHash { getPubKeyHash }) = getPubKeyHash
toBack getPubKeyHash = Back.PubKeyHash { getPubKeyHash }

instance contractInstanceIdBridge :: Bridge Back.ContractInstanceId Front.PlutusAppId where
toFront (Back.ContractInstanceId { unContractInstanceId: JsonUUID uuid }) = Front.PlutusAppId uuid
Expand Down
19 changes: 10 additions & 9 deletions marlowe-dashboard-client/src/Capability/Marlowe.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import Capability.MarloweStorage (class ManageMarloweStorage, addAssets, getCont
import Capability.PlutusApps.MarloweApp as MarloweApp
import Capability.Wallet (class ManageWallet)
import Capability.Wallet (createWallet, getWalletInfo, getWalletTotalFunds) as Wallet
import Contacts.Lenses (_companionAppId, _marloweAppId, _pubKey, _pubKeyHash, _wallet, _walletInfo)
import Contacts.Types (PubKeyHash(..), Wallet(..), WalletDetails, WalletInfo(..))
import Contacts.Lenses (_companionAppId, _marloweAppId, _pubKeyHash, _wallet, _walletInfo)
import Contacts.Types (Wallet(..), WalletDetails, WalletInfo(..))
import Control.Monad.Except (ExceptT(..), except, lift, mapExceptT, runExcept, runExceptT, withExceptT)
import Control.Monad.Reader (asks)
import Control.Monad.Reader.Class (ask)
Expand All @@ -55,11 +55,13 @@ import Foreign.Generic (decodeJSON)
import Halogen (HalogenM, liftAff)
import Marlowe.Client (ContractHistory(..))
import Marlowe.PAB (PlutusAppId(..))
import Marlowe.Semantics (Assets(..), Contract, MarloweData(..), MarloweParams(..), TokenName, TransactionInput, _rolePayoutValidatorHash, asset, emptyState)
import Marlowe.Semantics (Assets(..), Contract, MarloweData(..), MarloweParams(..), TokenName, TransactionInput, _rolePayoutValidatorHash, asset, emptyState, PubKeyHash(..))
import MarloweContract (MarloweContract(..))
import Plutus.PAB.Webserver.Types (ContractInstanceClientState)
import Servant.PureScript.Ajax (AjaxError(..), ErrorDescription(..))
import Types (AjaxResponse, CombinedWSStreamToServer(..), DecodedAjaxResponse)
import Contacts.Lenses (_companionAppId, _marloweAppId, _pubKeyHash, _wallet, _walletInfo)
import Contacts.Types (Wallet(..), WalletDetails, WalletInfo(..))
import WebSocket.Support as WS

-- The `ManageMarlowe` class provides a window on the `ManageContract` and `ManageWallet`
Expand Down Expand Up @@ -126,8 +128,7 @@ instance manageMarloweAppM :: ManageMarlowe AppM where
walletInfo =
WalletInfo
{ wallet: Wallet uuidString
, pubKey: uuidString
, pubKeyHash: PubKeyHash uuidString
, pubKeyHash: uuidString
}

assets = Assets $ singleton "" $ singleton "" (fromInt 1000000 * fromInt 10000)
Expand Down Expand Up @@ -248,14 +249,14 @@ instance manageMarloweAppM :: ManageMarlowe AppM where
, marloweState: emptyState zero
}
void $ insertContract marloweParams (marloweData /\ mempty)
void $ insertWalletRoleContracts (view (_walletInfo <<< _pubKey) walletDetails) marloweParams marloweData
void $ insertWalletRoleContracts (view (_walletInfo <<< _pubKeyHash) walletDetails) marloweParams marloweData
let
unfoldableRoles :: Array (Tuple TokenName PubKeyHash)
unfoldableRoles = toUnfoldable roles
void
$ for unfoldableRoles \(tokenName /\ pubKeyHash) -> do
void $ addAssets pubKeyHash $ asset (toString uuid) tokenName (fromInt 1)
void $ insertWalletRoleContracts (unwrap pubKeyHash) marloweParams marloweData
void $ insertWalletRoleContracts pubKeyHash marloweParams marloweData
pure $ Right unit
-- "apply-inputs" to a Marlowe contract on the blockchain
applyTransactionInput walletDetails marloweParams transactionInput = do
Expand Down Expand Up @@ -351,7 +352,7 @@ instance manageMarloweAppM :: ManageMarlowe AppM where
observableStateJson <- withExceptT Left $ ExceptT $ Contract.getContractInstanceObservableState companionAppId
mapExceptT (pure <<< lmap Right <<< unwrap) $ decodeJSON $ unwrap observableStateJson
LocalStorage -> do
roleContracts <- getWalletRoleContracts $ view (_walletInfo <<< _pubKey) walletDetails
roleContracts <- getWalletRoleContracts $ view (_walletInfo <<< _pubKeyHash) walletDetails
pure $ Right roleContracts
-- get all MarloweFollower apps for a given wallet
getFollowerApps walletDetails = do
Expand Down Expand Up @@ -379,7 +380,7 @@ instance manageMarloweAppM :: ManageMarlowe AppM where
Left decodingErrors -> Left decodingErrors
Right observableState -> Right (plutusAppId /\ observableState)
LocalStorage -> do
roleContracts <- getWalletRoleContracts $ view (_walletInfo <<< _pubKey) walletDetails
roleContracts <- getWalletRoleContracts $ view (_walletInfo <<< _pubKeyHash) walletDetails
allContracts <- getContracts
let
roleContractsToHistory :: MarloweParams -> MarloweData -> Maybe (Tuple PlutusAppId ContractHistory)
Expand Down
4 changes: 2 additions & 2 deletions marlowe-dashboard-client/src/Capability/MarloweStorage.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import Foreign.Generic (decodeJSON, encodeJSON)
import Halogen (HalogenM)
import LocalStorage (Key(..), getItem, removeItem, setItem)
import Marlowe.PAB (PlutusAppId)
import Marlowe.Semantics (Assets, MarloweData, MarloweParams, TransactionInput)
import Marlowe.Semantics (Assets, MarloweData, MarloweParams, TransactionInput, PubKeyHash)
import Contacts.Lenses (_assets, _pubKeyHash, _walletInfo, _walletNickname)
import Contacts.Types (PubKeyHash, WalletDetails, WalletLibrary)
import Contacts.Types (WalletDetails, WalletLibrary)

walletLibraryLocalStorageKey :: Key
walletLibraryLocalStorageKey = Key "walletLibrary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Capability.Contract (class ManageContract)
import Capability.Contract (invokeEndpoint) as Contract
import Capability.PlutusApps.MarloweApp.Lenses (_applyInputs, _create, _marloweAppEndpointMutex, _redeem, _requests)
import Capability.PlutusApps.MarloweApp.Types (EndpointMutex, LastResult(..), MarloweAppEndpointMutexEnv)
import Contacts.Types (PubKeyHash)
import Control.Monad.Reader (class MonadAsk, asks)
import Data.Array (findMap, take, (:))
import Data.Foldable (elem)
Expand All @@ -40,7 +39,7 @@ import Effect.Aff.Class (class MonadAff, liftAff)
import Effect.Class (liftEffect)
import Foreign.Generic (class Encode)
import Marlowe.PAB (PlutusAppId)
import Marlowe.Semantics (Contract, MarloweParams, SlotInterval(..), TokenName, TransactionInput(..))
import Marlowe.Semantics (Contract, MarloweParams, SlotInterval(..), TokenName, TransactionInput(..), PubKeyHash)
import Plutus.Contract.Effects (ActiveEndpoint, _ActiveEndpoint)
import Plutus.V1.Ledger.Crypto (PubKeyHash) as Back
import Plutus.V1.Ledger.Slot (Slot) as Back
Expand Down
8 changes: 2 additions & 6 deletions marlowe-dashboard-client/src/Contacts/Lenses.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module Contacts.Lenses
, _assets
, _previousCompanionAppState
, _wallet
, _pubKey
, _pubKeyHash
) where

Expand All @@ -23,9 +22,9 @@ import Data.Map (Map)
import Data.Symbol (SProxy(..))
import InputField.Types (State) as InputField
import Marlowe.PAB (PlutusAppId)
import Marlowe.Semantics (Assets, MarloweData, MarloweParams, PubKey)
import Marlowe.Semantics (Assets, MarloweData, MarloweParams, PubKey, PubKeyHash)
import Types (WebData)
import Contacts.Types (CardSection, PubKeyHash, State, Wallet, WalletIdError, WalletInfo, WalletLibrary, WalletNickname, WalletNicknameError, WalletDetails)
import Contacts.Types (CardSection, State, Wallet, WalletIdError, WalletInfo, WalletLibrary, WalletNickname, WalletNicknameError, WalletDetails)

_walletLibrary :: Lens' State WalletLibrary
_walletLibrary = prop (SProxy :: SProxy "walletLibrary")
Expand Down Expand Up @@ -65,8 +64,5 @@ _previousCompanionAppState = prop (SProxy :: SProxy "previousCompanionAppState")
_wallet :: Lens' WalletInfo Wallet
_wallet = _Newtype <<< prop (SProxy :: SProxy "wallet")

_pubKey :: Lens' WalletInfo PubKey
_pubKey = _Newtype <<< prop (SProxy :: SProxy "pubKey")

_pubKeyHash :: Lens' WalletInfo PubKeyHash
_pubKeyHash = _Newtype <<< prop (SProxy :: SProxy "pubKeyHash")
7 changes: 3 additions & 4 deletions marlowe-dashboard-client/src/Contacts/State.purs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ import InputField.Types (Action(..), State) as InputField
import MainFrame.Types (Action(..)) as MainFrame
import MainFrame.Types (ChildSlots, Msg)
import Marlowe.PAB (PlutusAppId(..))
import Marlowe.Semantics (Assets, Token(..))
import Marlowe.Semantics (Assets, Token(..), PubKeyHash(..))
import Network.RemoteData (RemoteData(..), fromEither)
import Toast.Types (errorToast, successToast)
import Types (WebData)
import Contacts.Lenses (_cardSection, _remoteWalletInfo, _walletIdInput, _walletLibrary, _walletNickname, _walletNicknameInput)
import Contacts.Types (Action(..), CardSection(..), PubKeyHash(..), State, Wallet(..), WalletDetails, WalletIdError(..), WalletInfo(..), WalletLibrary, WalletNickname, WalletNicknameError(..))
import Contacts.Types (Action(..), CardSection(..), State, Wallet(..), WalletDetails, WalletIdError(..), WalletInfo(..), WalletLibrary, WalletNickname, WalletNicknameError(..))

mkInitialState :: WalletLibrary -> State
mkInitialState walletLibrary =
Expand All @@ -69,8 +69,7 @@ defaultWalletInfo :: WalletInfo
defaultWalletInfo =
WalletInfo
{ wallet: Wallet ""
, pubKey: ""
, pubKeyHash: PubKeyHash ""
, pubKeyHash: ""
}

handleAction ::
Expand Down
20 changes: 1 addition & 19 deletions marlowe-dashboard-client/src/Contacts/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Contacts.Types
, WalletDetails
, WalletInfo(..)
, Wallet(..)
, PubKeyHash(..)
, CardSection(..)
, WalletNicknameError(..)
, WalletIdError(..)
Expand All @@ -24,7 +23,7 @@ import Foreign.Generic (defaultOptions, genericDecode, genericEncode)
import InputField.Types (Action, State) as InputField
import InputField.Types (class InputFieldError)
import Marlowe.PAB (PlutusAppId)
import Marlowe.Semantics (Assets, MarloweData, MarloweParams, PubKey)
import Marlowe.Semantics (Assets, MarloweData, MarloweParams, PubKey, PubKeyHash)
import Types (WebData)

type State
Expand Down Expand Up @@ -58,7 +57,6 @@ type WalletDetails
newtype WalletInfo
= WalletInfo
{ wallet :: Wallet
, pubKey :: PubKey
, pubKeyHash :: PubKeyHash
}

Expand Down Expand Up @@ -89,22 +87,6 @@ instance encodeWallet :: Encode Wallet where
instance decodeWallet :: Decode Wallet where
decode value = genericDecode defaultOptions value

-- TODO: move this into Marlowe.Semantics
newtype PubKeyHash
= PubKeyHash String

derive instance newtypePubKeyHash :: Newtype PubKeyHash _

derive instance eqPubKeyHash :: Eq PubKeyHash

derive instance genericPubKeyHash :: Generic PubKeyHash _

instance encodePubKeyHash :: Encode PubKeyHash where
encode value = genericEncode defaultOptions value

instance decodePubKeyHash :: Decode PubKeyHash where
decode value = genericDecode defaultOptions value

data CardSection
= Home
| ViewWallet WalletDetails
Expand Down
2 changes: 1 addition & 1 deletion marlowe-dashboard-client/src/Contract/State.purs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ getUserParties walletDetails marloweParams =

roleTokens = foldMap (Set.map Role <<< Map.keys <<< Map.filter ((/=) zero)) mCurrencyTokens
in
Set.insert (PK $ unwrap pubKeyHash) roleTokens
Set.insert (PK pubKeyHash) roleTokens

withStarted ::
forall action slots msg m.
Expand Down
9 changes: 4 additions & 5 deletions marlowe/src/Language/Marlowe/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Language.Marlowe.Semantics hiding (Contract)
import qualified Language.Marlowe.Semantics as Marlowe
import Language.Marlowe.Util (extractContractRoles)
import Ledger (CurrencySymbol, Datum (..), PubKeyHash, Slot (..), TokenName, TxOut (..),
inScripts, pubKeyHash, txOutValue)
inScripts, txOutValue)
import qualified Ledger
import Ledger.Ada (adaSymbol, adaValueOf)
import Ledger.Address (pubKeyHashAddress, scriptHashAddress)
Expand All @@ -54,7 +54,7 @@ import qualified Ledger.Typed.Scripts as Scripts
import Ledger.Typed.Tx (TypedScriptTxOut (..), tyTxOutData)
import qualified Ledger.Value as Val
import Plutus.ChainIndex (_ValidTx, citxInputs, citxOutputs, citxTxId)
import Plutus.Contract
import Plutus.Contract as Contract
import Plutus.Contract.StateMachine (AsSMContractError (..), StateMachineClient (..), Void,
WaitingResult (..))
import qualified Plutus.Contract.StateMachine as SM
Expand Down Expand Up @@ -387,8 +387,7 @@ setupMarloweParams
-> Contract MarloweContractState s e
(MarloweParams, TxConstraints i o, ScriptLookups (SM.StateMachine MarloweData MarloweInput))
setupMarloweParams owners contract = mapError (review _MarloweError) $ do
creator <- pubKeyHash <$> ownPubKey
let ownAddress = pubKeyHashAddress creator
ownAddress <- pubKeyHashAddress <$> Contract.ownPubKeyHash
let roles = extractContractRoles contract
if Set.null roles
then do
Expand Down Expand Up @@ -527,7 +526,7 @@ marloweCompanionContract = checkExistingRoleTokens
where
checkExistingRoleTokens = do
-- Get the existing unspend outputs of the wallet that activated the companion contract
pkh <- pubKeyHash <$> ownPubKey
pkh <- Contract.ownPubKeyHash
let ownAddress = pubKeyHashAddress pkh
-- Filter those outputs for role tokens and notify the WebSocket subscribers
-- NOTE: CombinedWSStreamToServer has an API to subscribe to WS notifications
Expand Down
4 changes: 2 additions & 2 deletions marlowe/test/Spec/Marlowe/AutoExecute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ awaitUntilTimeoutTest = checkPredicate "Party waits for contract to appear on ch
-- here Bob gets Timeout and closes the contract
void $ Trace.waitNSlots 15

alicePk = PK (pubKeyHash $ walletPubKey alice)
bobPk = PK (pubKeyHash $ walletPubKey bob)
alicePk = PK (walletPubKeyHash alice)
bobPk = PK (walletPubKeyHash bob)

params = defaultMarloweParams

Expand Down
2 changes: 1 addition & 1 deletion marlowe/test/Spec/Marlowe/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ shrinkContract cont = case cont of

pangramContract :: Contract
pangramContract = let
alicePk = PK $ pubKeyHash $ walletPubKey $ knownWallet 1
alicePk = PK $ walletPubKeyHash $ knownWallet 1
aliceAcc = alicePk
bobRole = Role "Bob"
constant = Constant 100
Expand Down
Loading

0 comments on commit b7eaa54

Please sign in to comment.