Skip to content

Commit ad9d09e

Browse files
Convert Text to TxId, update examples
1 parent c95b103 commit ad9d09e

File tree

6 files changed

+67
-34
lines changed

6 files changed

+67
-34
lines changed

examples/plutus-game/app/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ main = do
6666
, pcDryRun = True
6767
, pcLogLevel = Debug
6868
, pcProtocolParamsFile = "./protocol.json"
69-
, pcEnableTxEndpoint = False
69+
, pcEnableTxEndpoint = True
7070
}
7171
BotPlutusInterface.runPAB @GameContracts pabConf

examples/plutus-nft/app/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ main = do
6666
, pcDryRun = True
6767
, pcLogLevel = Debug
6868
, pcProtocolParamsFile = "./protocol.json"
69-
, pcEnableTxEndpoint = False
69+
, pcEnableTxEndpoint = True
7070
}
7171
BotPlutusInterface.runPAB @MintNFTContracts pabConf

examples/plutus-transfer/app/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ main = do
6565
, pcDryRun = True
6666
, pcLogLevel = Debug
6767
, pcProtocolParamsFile = "./protocol.json"
68+
, pcEnableTxEndpoint = True
6869
}
6970
BotPlutusInterface.runPAB @TransferContracts pabConf

src/BotPlutusInterface/Files.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module BotPlutusInterface.Files (
99
signingKeyFilePath,
1010
txFilePath,
1111
txFileName,
12+
txIdToText,
1213
writeAll,
1314
writePolicyScriptFile,
1415
redeemerJsonFilePath,
@@ -84,7 +85,7 @@ import Plutus.V1.Ledger.Api (
8485
)
8586
import PlutusTx (ToData, toData)
8687
import PlutusTx.Builtins (fromBuiltin)
87-
import System.FilePath (replaceExtension, takeExtension, (</>))
88+
import System.FilePath (takeExtension, (</>))
8889
import Prelude
8990

9091
-- | Filename of a minting policy script
@@ -115,12 +116,13 @@ signingKeyFilePath pabConf (PubKeyHash pubKeyHash) =
115116
in pabConf.pcSigningKeyFileDir <> "/signing-key-" <> h <> ".skey"
116117

117118
txFilePath :: PABConfig -> Text -> Tx.Tx -> Text
118-
txFilePath pabConf ext tx =
119-
let txId = encodeByteString $ fromBuiltin $ TxId.getTxId $ Tx.txId tx
120-
in pabConf.pcTxFileDir <> "/" <> txFileName txId ext
119+
txFilePath pabConf ext tx = pabConf.pcTxFileDir <> "/" <> txFileName (Tx.txId tx) ext
121120

122-
txFileName :: Text -> Text -> Text
123-
txFileName name ext = Text.pack $ replaceExtension ("tx-" <> Text.unpack name) (Text.unpack ext)
121+
txFileName :: TxId.TxId -> Text -> Text
122+
txFileName txId ext = "tx-" <> txIdToText txId <> "." <> ext
123+
124+
txIdToText :: TxId.TxId -> Text
125+
txIdToText = encodeByteString . fromBuiltin . TxId.getTxId
124126

125127
-- | Compiles and writes a script file under the given folder
126128
writePolicyScriptFile ::

src/BotPlutusInterface/Server.hs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ module BotPlutusInterface.Server (
66
WebSocketEndpoint,
77
ActivateContractEndpoint,
88
RawTxEndpoint,
9+
TxIdCapture (TxIdCapture),
910
) where
1011

1112
import BotPlutusInterface.Contract (runContract)
12-
import BotPlutusInterface.Files (txFileName)
13+
import BotPlutusInterface.Files (txFileName, txIdToText)
1314
import BotPlutusInterface.Types (
1415
AppState (AppState),
1516
ContractEnvironment (..),
@@ -25,15 +26,18 @@ import Control.Monad.Error.Class (throwError)
2526
import Control.Monad.IO.Class (liftIO)
2627
import Data.Aeson (FromJSON, ToJSON (toJSON))
2728
import Data.Aeson qualified as JSON
29+
import Data.Bifunctor (bimap)
2830
import Data.ByteString.Lazy qualified as LBS
2931
import Data.Either.Combinators (leftToMaybe)
3032
import Data.Kind (Type)
3133
import Data.Map qualified as Map
3234
import Data.Maybe (catMaybes)
3335
import Data.Proxy (Proxy (Proxy))
3436
import Data.Row (Row)
35-
import Data.Text (Text, unpack)
37+
import Data.Text (Text, pack, unpack)
38+
import Data.Text.Encoding (encodeUtf8)
3639
import Data.UUID.V4 qualified as UUID
40+
import Ledger.TxId (TxId (TxId))
3741
import Network.WebSockets (
3842
Connection,
3943
PendingConnection,
@@ -56,7 +60,19 @@ import Plutus.PAB.Webserver.Types (
5660
ContractActivationArgs (..),
5761
InstanceStatusToClient (ContractFinished, NewObservableState),
5862
)
59-
import Servant.API (Capture, Get, JSON, Post, ReqBody, (:<|>) (..), (:>))
63+
import Plutus.V1.Ledger.Bytes (LedgerBytes (LedgerBytes), fromHex)
64+
import PlutusTx.Prelude (lengthOfByteString)
65+
import Servant.API (
66+
Capture,
67+
FromHttpApiData (parseUrlPiece),
68+
Get,
69+
JSON,
70+
Post,
71+
ReqBody,
72+
ToHttpApiData (toUrlPiece),
73+
(:<|>) ((:<|>)),
74+
(:>),
75+
)
6076
import Servant.API.WebSocket (WebSocketPending)
6177
import Servant.Server (Application, Handler, Server, err404, serve)
6278
import System.Directory (canonicalizePath, doesFileExist, makeAbsolute)
@@ -85,9 +101,26 @@ type ActivateContractEndpoint a =
85101

86102
type RawTxEndpoint =
87103
"raw-tx"
88-
:> Capture "txId" Text
104+
:> Capture "tx-id" TxIdCapture
89105
:> Get '[JSON] RawTx
90106

107+
newtype TxIdCapture = TxIdCapture TxId
108+
109+
instance FromHttpApiData TxIdCapture where
110+
parseUrlPiece :: Text -> Either Text TxIdCapture
111+
parseUrlPiece t = bimap pack bytesToTxIdCapture $ checkLength =<< fromHex (encodeUtf8 t)
112+
where
113+
checkLength :: LedgerBytes -> Either String LedgerBytes
114+
checkLength b@(LedgerBytes bs) =
115+
if lengthOfByteString bs == 32
116+
then Right b
117+
else Left "Invalid length"
118+
bytesToTxIdCapture :: LedgerBytes -> TxIdCapture
119+
bytesToTxIdCapture (LedgerBytes b) = TxIdCapture $ TxId b
120+
121+
instance ToHttpApiData TxIdCapture where
122+
toUrlPiece (TxIdCapture txId) = txIdToText txId
123+
91124
server :: HasDefinitions t => PABConfig -> AppState -> Server (API t)
92125
server pabConfig state =
93126
websocketHandler state
@@ -234,16 +267,16 @@ handleContract pabConf state@(AppState st) contract = liftIO $ do
234267
pure contractInstanceID
235268

236269
-- | This handler will allow to retrieve raw transactions from the pcTxFileDir if pcEnableTxEndpoint is True
237-
rawTxHandler :: PABConfig -> Text -> Handler RawTx
238-
rawTxHandler config txId = do
270+
rawTxHandler :: PABConfig -> TxIdCapture -> Handler RawTx
271+
rawTxHandler config (TxIdCapture txId) = do
239272
-- Check that endpoint is enabled
240273
assert config.pcEnableTxEndpoint
241274
-- Absolute path to pcTxFileDir that is specified in the config
242275
txFolderPath <- liftIO $ makeAbsolute (unpack config.pcTxFileDir)
243276

244277
-- Add/Set .raw extension on path
245278
let suppliedPath :: FilePath
246-
suppliedPath = txFolderPath </> unpack (txFileName txId ".raw")
279+
suppliedPath = txFolderPath </> unpack (txFileName txId "raw")
247280
-- Resolve path indirections
248281
path <- liftIO $ canonicalizePath suppliedPath
249282
-- ensure it does not try to escape txFolderPath

test/Spec/BotPlutusInterface/Server.hs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
module Spec.BotPlutusInterface.Server (tests) where
22

33
import BotPlutusInterface.Files (txFileName)
4-
import BotPlutusInterface.Server (RawTxEndpoint, app, initState)
4+
import BotPlutusInterface.Server (RawTxEndpoint, TxIdCapture (TxIdCapture), app, initState)
55
import BotPlutusInterface.Types (
66
HasDefinitions (..),
77
PABConfig (..),
88
RawTx (..),
99
SomeBuiltin (..),
1010
)
1111

12+
import Ledger.TxId (TxId)
1213
import Playground.Types (FunctionSchema)
1314
import Schema (FormSchema)
1415

@@ -25,14 +26,14 @@ import Data.Aeson (FromJSON, ToJSON, encode)
2526
import Data.ByteString.Lazy qualified as LBS
2627
import Data.Default (def)
2728
import Data.Proxy (Proxy (..))
28-
import Data.Text (Text, pack, unpack)
29+
import Data.Text (pack, unpack)
2930
import Data.Void (Void, absurd)
3031
import System.FilePath ((</>))
3132
import System.IO.Temp (withSystemTempDirectory)
3233
import Prelude
3334

3435
type RawTxEndpointResponse = Either ClientError RawTx
35-
type RawTxTest a = (Text -> IO RawTxEndpointResponse) -> IO a
36+
type RawTxTest a = (TxId -> IO RawTxEndpointResponse) -> IO a
3637

3738
tests :: TestTree
3839
tests =
@@ -46,8 +47,7 @@ rawTxTests =
4647
testGroup
4748
"rawTx"
4849
[ testCase "Can fetch valid tx file" fetchTx
49-
, testCase "If an extension is supplied, it is replaced by .raw" fetchSignedTx
50-
, testCase "Unable to fetch outside tx folder" fetchOutsideTxFolder
50+
, testCase "Returns 404 for missing txs" fetchMissingTx
5151
, testCase "Returns 404 for valid request when the endpoint is disabled" fetchWithDefaultConfig
5252
]
5353
where
@@ -57,16 +57,10 @@ rawTxTests =
5757
result <- runRawTxClient txHash
5858
result @?= Right rawTx
5959

60-
fetchSignedTx :: IO ()
61-
fetchSignedTx = do
60+
fetchMissingTx :: IO ()
61+
fetchMissingTx = do
6262
initServerAndClient enableTxEndpointConfig $ \runRawTxClient -> do
63-
result <- runRawTxClient $ txHash <> ".signed"
64-
result @?= Right rawTx
65-
66-
fetchOutsideTxFolder :: IO ()
67-
fetchOutsideTxFolder = do
68-
initServerAndClient enableTxEndpointConfig $ \runRawTxClient -> do
69-
Left (FailureResponse _ res) <- runRawTxClient "../somefile"
63+
Left (FailureResponse _ res) <- runRawTxClient txHash2
7064
responseStatusCode res @?= status404
7165

7266
fetchWithDefaultConfig :: IO ()
@@ -95,16 +89,19 @@ initServerAndClient config test = do
9589
let clientEnv :: ClientEnv
9690
clientEnv = mkClientEnv manager $ baseUrl {baseUrlPort = port}
9791

98-
runRawTxClient :: Text -> IO RawTxEndpointResponse
99-
runRawTxClient hash = runClientM (client txProxy hash) clientEnv
92+
runRawTxClient :: TxId -> IO RawTxEndpointResponse
93+
runRawTxClient txId = runClientM (client txProxy (TxIdCapture txId)) clientEnv
10094

10195
testToRun runRawTxClient
10296

103-
txHash :: Text
104-
txHash = "test"
97+
txHash :: TxId
98+
txHash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
99+
100+
txHash2 :: TxId
101+
txHash2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
105102

106103
testTxFileName :: FilePath
107-
testTxFileName = unpack $ txFileName txHash ".raw"
104+
testTxFileName = unpack $ txFileName txHash "raw"
108105

109106
rawTx :: RawTx
110107
rawTx =

0 commit comments

Comments
 (0)