Skip to content

Commit a01686c

Browse files
authored
Merge pull request #63 from mlabs-haskell/bladyjoker/add-plutus-json-cli
Implemented plutus-json-cli and added it to coop-env
2 parents b475f33 + 3dbefc8 commit a01686c

File tree

5 files changed

+123
-11
lines changed

5 files changed

+123
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ The environment should now have the following tools available:
197197
- [coop-plutus-cli](coop-plutus) for providing serialized Plutus programs (ie. on-chain scripts) that implement the [COOP Plutus Protocol](coop-docs/02-plutus-protocol.md),
198198
- [coop-publisher-cli](coop-publisher) for running a [COOP Publisher gRPC](coop-proto/publisher-service.proto) service that implements the [COOP Frontend protocol](coop-docs/03-frontend-protocol.md),
199199
- [json-fs-store-cli](coop-extras/json-fact-statement-store) for running a generic JSON-based implementation of the [COOP FactStatementStore gRPC](coop-proto/fact-statement-store-service.proto) service
200+
- [plutus-json-cli](coop-extras/plutus-json) utility tool for converting between JSON and PlutusData formats
200201

201202
and some other convenience utilities including some Bash functions that conveniently wrap the invocation of above mentioned services and command line tools.
202203

coop-extras/coop-env/build.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
, coopPlutusCli
66
, coopPublisherCli
77
, jsFsStoreCli
8+
, plutusJsonCli
89
, plutipLocalCluster
910
}:
1011
pkgs.mkShell {
@@ -24,6 +25,7 @@ pkgs.mkShell {
2425
coopPlutusCli
2526
coopPublisherCli
2627
jsFsStoreCli
28+
plutusJsonCli
2729
plutipLocalCluster
2830
];
2931
shellHook = ''
Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,104 @@
1-
module Main where
1+
{-# LANGUAGE ImportQualifiedPost #-}
2+
{-# LANGUAGE TypeApplications #-}
3+
4+
module Main (main) where
5+
6+
import Control.Applicative (Alternative (many), (<**>))
7+
8+
import Codec.Serialise (deserialise, deserialiseOrFail)
9+
import Data.Aeson (decode, decodeStrict', encodeFile, json)
10+
import Data.Aeson.Parser (decodeStrictWith)
11+
import Data.ByteString qualified as B
12+
import Data.ByteString.Lazy qualified as LB
13+
import Data.Maybe (fromMaybe)
14+
import Options.Applicative (
15+
Parser,
16+
ParserInfo,
17+
command,
18+
customExecParser,
19+
flag,
20+
fullDesc,
21+
help,
22+
helper,
23+
info,
24+
long,
25+
metavar,
26+
prefs,
27+
progDesc,
28+
short,
29+
showDefault,
30+
showHelpOnEmpty,
31+
showHelpOnError,
32+
strOption,
33+
subparser,
34+
)
35+
import PlutusJson (jsonToPlutusData, plutusDataToJson)
36+
import PlutusTx (Data, ToData (toBuiltinData))
37+
import PlutusTx.Builtins (dataToBuiltinData, fromBuiltin, serialiseData, toBuiltin)
38+
39+
data Command
40+
= ToJson FilePath FilePath
41+
| FromJson FilePath FilePath
42+
43+
toJsonOptsP :: Parser Command
44+
toJsonOptsP =
45+
ToJson
46+
<$> strOption
47+
( long "in"
48+
<> short 'i'
49+
<> metavar "FILEPATH"
50+
<> help "PlutusData file (CBOR encoded) to translate into a Json file"
51+
)
52+
<*> strOption
53+
( long "out"
54+
<> short 'o'
55+
<> metavar "FILEPATH"
56+
<> help "Translated Json file"
57+
)
58+
59+
fromJsonOptsP :: Parser Command
60+
fromJsonOptsP =
61+
FromJson
62+
<$> strOption
63+
( long "in"
64+
<> short 'i'
65+
<> metavar "FILEPATH"
66+
<> help "Json file to translate into a PlutusData file (CBOR encoded)"
67+
)
68+
<*> strOption
69+
( long "out"
70+
<> short 'o'
71+
<> metavar "FILEPATH"
72+
<> help "Translated PlutusData file (CBOR encoded)"
73+
)
74+
75+
optionsP :: Parser Command
76+
optionsP =
77+
subparser $
78+
command
79+
"to-json"
80+
(info (toJsonOptsP <* helper) (progDesc "Translate a PlutusData file (CBOR encoded) into a Json file"))
81+
<> command
82+
"from-json"
83+
(info (fromJsonOptsP <* helper) (progDesc "Translate a Json file into a PlutusData file (CBOR encoded)"))
84+
85+
parserInfo :: ParserInfo Command
86+
parserInfo = info (optionsP <**> helper) (fullDesc <> progDesc "COOP plutus-json")
287

388
main :: IO ()
4-
main = putStrLn "Hello, Haskell!"
89+
main = do
90+
cmd <- customExecParser (prefs (showHelpOnEmpty <> showHelpOnError)) parserInfo
91+
case cmd of
92+
ToJson inf outf -> do
93+
cborBytes <- LB.readFile inf
94+
let errOrDecoded = deserialiseOrFail @Data cborBytes
95+
plData <- either (\err -> error $ "File " <> inf <> " can't be parsed into PlutusData CBOR: " <> show err) return errOrDecoded
96+
jsVal <- plutusDataToJson plData
97+
encodeFile outf jsVal
98+
FromJson inf outf -> do
99+
jsonBytes <- B.readFile inf
100+
let mayDecoded = decodeStrictWith json return jsonBytes
101+
decoded <- maybe (error $ "File " <> inf <> " can't be parsed into Json") return mayDecoded
102+
let plData = jsonToPlutusData decoded
103+
B.writeFile outf (fromBuiltin . serialiseData . dataToBuiltinData $ plData)
104+
return ()

coop-extras/plutus-json/plutus-json.cabal

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ library
1717
, text
1818
, vector
1919

20-
executable plutus-json
20+
executable plutus-json-cli
2121
main-is: Main.hs
2222
hs-source-dirs: app
2323
default-language: Haskell2010
24-
build-depends: base
24+
build-depends:
25+
, aeson
26+
, base
27+
, bytestring
28+
, optparse-applicative
29+
, plutus-json
30+
, plutus-tx
31+
, serialise
2532

2633
test-suite plutus-json-tests
2734
type: exitcode-stdio-1.0

flake.nix

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,15 @@
181181
};
182182

183183
# Extras
184-
coopExtrasPlutusJson = import ./coop-extras/plutus-json/build.nix {
184+
plutusJson = import ./coop-extras/plutus-json/build.nix {
185185
inherit plutarch;
186186
pkgs = pkgsForPlutarch;
187187
inherit (pkgsForPlutarch) haskell-nix;
188188
inherit (pre-commit-check) shellHook;
189189
compiler-nix-name = "ghc923";
190190
};
191-
coopExtrasPlutusJsonFlake = coopExtrasPlutusJson.flake { };
191+
plutusJsonFlake = plutusJson.flake { };
192+
plutusJsonCli = plutusJsonFlake.packages."plutus-json:exe:plutus-json-cli";
192193

193194
coopExtrasJsonFactStatementStore = import ./coop-extras/json-fact-statement-store/build.nix {
194195
inherit pkgs plutip http2-grpc-native;
@@ -214,7 +215,7 @@
214215
coopEnvShell = import ./coop-extras/coop-env/build.nix {
215216
inherit pkgs;
216217
plutipLocalCluster = plutip.packages.${system}."plutip:exe:local-cluster";
217-
inherit coopPabCli coopPlutusCli jsFsStoreCli coopPublisherCli;
218+
inherit coopPabCli coopPlutusCli jsFsStoreCli coopPublisherCli plutusJsonCli;
218219
cardanoNode = coopPabProj.hsPkgs.cardano-node.components.exes.cardano-node;
219220
cardanoCli = coopPabProj.hsPkgs.cardano-cli.components.exes.cardano-cli;
220221
};
@@ -223,14 +224,15 @@
223224
in
224225
rec {
225226
# Useful for nix repl
226-
inherit pkgs pkgsWithOverlay pkgsForPlutarch;
227+
inherit pkgs pkgsWithOverlay pkgsForPlutarch plutusJsonCli;
227228

228229
# Standard flake attributes
229-
packages = coopPlutusFlake.packages // coopPublisherFlake.packages // coopPabFlake.packages // coopHsTypesFlake.packages // {
230+
packages = coopPlutusFlake.packages // coopPublisherFlake.packages // coopPabFlake.packages // coopHsTypesFlake.packages // plutusJsonFlake.packages // {
230231
"coop-plutus-cli" = coopPlutusCli;
231232
"coop-pab-cli" = coopPabCli;
232233
"coop-publisher-cli" = coopPublisherCli;
233234
"js-fs-store-cli" = jsFsStoreCli;
235+
"plutus-json-cli" = plutusJsonCli;
234236
"default" = coopPabCli;
235237
};
236238

@@ -242,7 +244,7 @@
242244
dev-docs = coopDocsDevShell;
243245
dev-pab = coopPabFlake.devShell;
244246
dev-hs-types = coopHsTypesFlake.devShell;
245-
dev-extras-plutus-json = coopExtrasPlutusJsonFlake.devShell;
247+
dev-extras-plutus-json = plutusJsonFlake.devShell;
246248
dev-extras-json-store = coopExtrasJsonFactStatementStoreFlake.devShell;
247249
coop-env = coopEnvShell;
248250
dev-cardano-proto-extras = cardanoProtoExtrasFlake.devShell;
@@ -255,7 +257,7 @@
255257
coopPublisherFlake.checks //
256258
coopPabFlake.checks //
257259
coopHsTypesFlake.checks //
258-
coopExtrasPlutusJsonFlake.checks //
260+
plutusJsonFlake.checks //
259261
coopExtrasJsonFactStatementStoreFlake.checks //
260262
cardanoProtoExtrasFlake.checks
261263
) //

0 commit comments

Comments
 (0)