Skip to content

Commit 002e39c

Browse files
committed
cardano-rpc | Add native script deserialised form
1 parent ed16f3d commit 002e39c

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ message MultiAsset {
3838
repeated Asset assets = 2; // List of custom assets.
3939
}
4040

41+
// Represents a native script in Cardano.
42+
message NativeScript {
43+
oneof native_script {
44+
bytes script_pubkey = 1; // Script based on an address key hash.
45+
NativeScriptList script_all = 2; // Script that requires all nested scripts to be satisfied.
46+
NativeScriptList script_any = 3; // Script that requires any of the nested scripts to be satisfied.
47+
ScriptNOfK script_n_of_k = 4; // Script that requires k out of n nested scripts to be satisfied.
48+
uint64 invalid_before = 5; // Slot number before which the script is invalid.
49+
uint64 invalid_hereafter = 6; // Slot number after which the script is invalid.
50+
}
51+
}
52+
53+
// Represents a list of native scripts.
54+
message NativeScriptList {
55+
repeated NativeScript items = 1; // List of native scripts.
56+
}
57+
58+
// Represents a "k out of n" native script.
59+
message ScriptNOfK {
60+
uint32 k = 1; // The number of required satisfied scripts.
61+
repeated NativeScript scripts = 2; // List of native scripts.
62+
}
63+
4164
// Represents a constructor for Plutus data in Cardano.
4265
message Constr {
4366
uint32 tag = 1;
@@ -87,7 +110,7 @@ message PlutusDataArray {
87110
// TODO u5c: removed native script representation, added plutus_v4
88111
message Script {
89112
oneof script {
90-
bytes native = 1; // Native script.
113+
NativeScript native = 1; // Native script.
91114
bytes plutus_v1 = 2; // Plutus V1 script.
92115
bytes plutus_v2 = 3; // Plutus V2 script.
93116
bytes plutus_v3 = 4; // Plutus V3 script.

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
module Cardano.Rpc.Server.Internal.Orphans () where
1313

1414
import Cardano.Api.Address
15+
import Cardano.Api.Block (SlotNo (..))
1516
import Cardano.Api.Era
1617
import Cardano.Api.Error
1718
import Cardano.Api.Ledger qualified as L
1819
import Cardano.Api.Plutus
1920
import Cardano.Api.Pretty
20-
import Cardano.Api.Serialise.Cbor
2121
import Cardano.Api.Serialise.Raw
2222
import Cardano.Api.Tx
2323
import Cardano.Api.Value
@@ -79,8 +79,8 @@ instance Inject (ReferenceScript era) (Proto UtxoRpc.Script) where
7979
inject ReferenceScriptNone = defMessage
8080
inject (ReferenceScript _ (ScriptInAnyLang _ script)) =
8181
case script of
82-
SimpleScript _ ->
83-
defMessage & #native .~ serialiseToCBOR script
82+
SimpleScript ss ->
83+
defMessage & #native .~ inject ss
8484
PlutusScript PlutusScriptV1 ps ->
8585
defMessage & #plutusV1 .~ serialiseToRawBytes ps
8686
PlutusScript PlutusScriptV2 ps ->
@@ -90,6 +90,25 @@ instance Inject (ReferenceScript era) (Proto UtxoRpc.Script) where
9090
PlutusScript PlutusScriptV4 ps ->
9191
defMessage & #plutusV4 .~ serialiseToRawBytes ps
9292

93+
instance Inject SimpleScript (Proto UtxoRpc.NativeScript) where
94+
inject = \case
95+
RequireSignature paymentKeyHash ->
96+
defMessage & #scriptPubkey .~ serialiseToRawBytes paymentKeyHash
97+
RequireTimeBefore (SlotNo slotNo) ->
98+
defMessage & #invalidHereafter .~ slotNo
99+
RequireTimeAfter (SlotNo slotNo) ->
100+
defMessage & #invalidBefore .~ slotNo
101+
RequireAllOf scripts ->
102+
defMessage & #scriptAll . #items .~ map inject scripts
103+
RequireAnyOf scripts ->
104+
defMessage & #scriptAny . #items .~ map inject scripts
105+
RequireMOf k scripts -> do
106+
let nScriptsOf =
107+
defMessage
108+
& #k .~ fromIntegral k
109+
& #scripts .~ map inject scripts
110+
defMessage & #scriptNOfK .~ nScriptsOf
111+
93112
instance Inject ScriptData (Proto UtxoRpc.PlutusData) where
94113
inject = \case
95114
ScriptDataBytes bs ->
@@ -122,7 +141,7 @@ instance IsCardanoEra era => Inject (UTxO era) [Proto UtxoRpc.AnyUtxoData] where
122141
toList policyAssets <&> \(assetName, Quantity qty) -> do
123142
defMessage
124143
& #name .~ serialiseToRawBytes assetName
125-
-- we don't have access to info it the coin was minted in the transaction,
144+
-- we don't have access to info if the coin was minted in the transaction,
126145
-- maybe we should add it later
127146
& #maybe'mintCoin .~ Nothing
128147
& #outputCoin .~ fromIntegral qty

0 commit comments

Comments
 (0)