Skip to content

Commit c7c4e63

Browse files
committed
fix(testing-interface): detect signing wallet instead of hardcoding w1
1 parent e0e49d4 commit c7c4e63

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/testing-interface/lib/Convex/TestingInterface.hs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ import Convex.MockChain.Defaults qualified as Defaults
8181
import Convex.MonadLog (MonadLog)
8282
import Convex.NodeParams (NodeParams (..), ledgerProtocolParameters)
8383
import Convex.ThreatModel (ExceptT, ThreatModel, ThreatModelEnv (..), ThreatModelOutcome (..), getThreatModelName, runExceptT, runThreatModelCheck)
84+
import Convex.ThreatModel.Cardano.Api (mockWalletHashes, txSigners)
85+
import Convex.Wallet (Wallet)
8486
import Convex.Wallet.MockWallet qualified as Wallet
8587
import Data.Aeson (ToJSON (..), (.=))
8688
import Data.Aeson qualified as Aeson
@@ -90,7 +92,7 @@ import Data.ByteString.Lazy.Char8 qualified as LBS
9092
import Data.Foldable (foldl', for_, traverse_)
9193
import Data.IORef (IORef, modifyIORef, newIORef, readIORef)
9294
import Data.Map qualified as Map
93-
import Data.Maybe (fromMaybe)
95+
import Data.Maybe (fromMaybe, mapMaybe)
9496
import Data.Set qualified as Set
9597
import Data.Word (Word32)
9698
import GHC.Generics (Generic)
@@ -110,6 +112,18 @@ import PlutusTx.Coverage (
110112
import Prettyprinter qualified as Pretty
111113
import System.Exit (ExitCode)
112114

115+
{- | Detect which mock wallet signed a transaction by examining its witnesses.
116+
Returns an error message if no known mock wallet is found among the signers.
117+
-}
118+
detectSigningWallet :: C.Tx C.ConwayEra -> Either String Wallet
119+
detectSigningWallet tx =
120+
case txSigners tx of
121+
[] -> Left "Transaction has no signers — cannot determine wallet for threat model"
122+
signers ->
123+
case mapMaybe (\h -> lookup h mockWalletHashes) signers of
124+
(w : _) -> Right w
125+
[] -> Left "Transaction signers do not match any known mock wallet"
126+
113127
{- | A testing interface defines the state and behavior of one or more smart contracts.
114128
115129
The type parameter @state@ represents the model's view of the world. It should
@@ -460,9 +474,12 @@ positiveTest opts mGetTmResultsRef tms evs (Actions actions) = monadicIO $ do
460474
-- Run each threat model in an isolated MockchainT context
461475
liftIO $ forM allToRun $ \tm -> do
462476
let name = fromMaybe "Unnamed" (getThreatModelName tm)
463-
(outcome, tmFinalState) <-
464-
runMockchainIO (runThreatModelCheck Wallet.w1 tm [env]) params mcState
465-
pure (name, outcome, mcsCoverageData tmFinalState)
477+
case detectSigningWallet tx of
478+
Left err -> pure (name, TMError err, mempty)
479+
Right wallet -> do
480+
(outcome, tmFinalState) <-
481+
runMockchainIO (runThreatModelCheck wallet tm [env]) params mcState
482+
pure (name, outcome, mcsCoverageData tmFinalState)
466483
_ -> pure []
467484

468485
-- Extract just the (name, outcome) pairs for downstream processing

0 commit comments

Comments
 (0)