Skip to content

Commit ed16f3d

Browse files
committed
cardano-rpc | Add decoded PlutusData to Datum in proto definition
1 parent 0ca3d5d commit ed16f3d

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

cardano-rpc/proto/utxorpc/v1alpha/cardano/cardano.proto

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ message AddressArray {
1616
repeated bytes items = 1;
1717
}
1818

19-
// TODO u5c: replaced plutus_data with just bytes
2019
message Datum {
2120
bytes hash = 1; // Hash of this datum as seen on-chain
21+
PlutusData payload = 2; // Parsed Plutus data payload
2222
bytes original_cbor = 3; // Original cbor-encoded data as seen on-chain
2323
}
2424

@@ -38,6 +38,51 @@ message MultiAsset {
3838
repeated Asset assets = 2; // List of custom assets.
3939
}
4040

41+
// Represents a constructor for Plutus data in Cardano.
42+
message Constr {
43+
uint32 tag = 1;
44+
uint64 any_constructor = 2;
45+
repeated PlutusData fields = 3;
46+
}
47+
48+
// Represents a big integer for Plutus data in Cardano.
49+
message BigInt {
50+
oneof big_int {
51+
int64 int = 1;
52+
bytes big_u_int = 2;
53+
bytes big_n_int = 3;
54+
}
55+
}
56+
57+
58+
// Represents a key-value pair for Plutus data in Cardano.
59+
message PlutusDataPair {
60+
PlutusData key = 1; // Key of the pair.
61+
PlutusData value = 2; // Value of the pair.
62+
}
63+
64+
// Represents a Plutus data item in Cardano.
65+
message PlutusData {
66+
oneof plutus_data {
67+
Constr constr = 2; // Constructor.
68+
PlutusDataMap map = 3; // Map of Plutus data.
69+
BigInt big_int = 4; // Big integer.
70+
bytes bounded_bytes = 5; // Bounded bytes.
71+
PlutusDataArray array = 6; // Array of Plutus data.
72+
}
73+
}
74+
75+
// Represents a map of Plutus data in Cardano.
76+
message PlutusDataMap {
77+
repeated PlutusDataPair pairs = 1; // List of key-value pairs.
78+
}
79+
80+
// Represents an array of Plutus data in Cardano.
81+
message PlutusDataArray {
82+
repeated PlutusData items = 1; // List of Plutus data items.
83+
}
84+
85+
4186
// Represents a script in Cardano.
4287
// TODO u5c: removed native script representation, added plutus_v4
4388
message Script {

cardano-rpc/src/Cardano/Rpc/Server/Internal/Orphans.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE FlexibleInstances #-}
22
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE LambdaCase #-}
34
{-# LANGUAGE MultiParamTypeClasses #-}
45
{-# LANGUAGE OverloadedLabels #-}
56
{-# LANGUAGE OverloadedLists #-}
@@ -89,6 +90,28 @@ instance Inject (ReferenceScript era) (Proto UtxoRpc.Script) where
8990
PlutusScript PlutusScriptV4 ps ->
9091
defMessage & #plutusV4 .~ serialiseToRawBytes ps
9192

93+
instance Inject ScriptData (Proto UtxoRpc.PlutusData) where
94+
inject = \case
95+
ScriptDataBytes bs ->
96+
defMessage & #boundedBytes .~ bs
97+
ScriptDataNumber int ->
98+
defMessage & #bigInt . #int .~ fromIntegral int
99+
ScriptDataList sds ->
100+
defMessage & #array . #items .~ map inject sds
101+
ScriptDataMap elements -> do
102+
let pairs =
103+
elements <&> \(k, v) ->
104+
defMessage
105+
& #key .~ inject k
106+
& #value .~ inject v
107+
defMessage & #map . #pairs .~ pairs
108+
ScriptDataConstructor tag args -> do
109+
let constr =
110+
defMessage
111+
& #tag .~ fromIntegral tag
112+
& #fields .~ map inject args
113+
defMessage & #constr .~ constr
114+
92115
instance IsCardanoEra era => Inject (UTxO era) [Proto UtxoRpc.AnyUtxoData] where
93116
inject utxo =
94117
toList utxo <&> \(txIn, TxOut addressInEra txOutValue datum script) -> do
@@ -112,10 +135,12 @@ instance IsCardanoEra era => Inject (UTxO era) [Proto UtxoRpc.AnyUtxoData] where
112135
TxOutDatumHash _ scriptDataHash ->
113136
defMessage
114137
& #hash .~ serialiseToRawBytes scriptDataHash
138+
& #maybe'payload .~ Nothing -- we don't have it
115139
& #originalCbor .~ mempty -- we don't have it
116140
TxOutDatumInline _ hashableScriptData ->
117141
defMessage
118142
& #hash .~ serialiseToRawBytes (hashScriptDataBytes hashableScriptData)
143+
& #payload .~ inject (getScriptData hashableScriptData)
119144
& #originalCbor .~ getOriginalScriptDataBytes hashableScriptData
120145

121146
protoTxOut =

0 commit comments

Comments
 (0)