Skip to content

Commit 2a68c4a

Browse files
committed
cardano-rpc | Add native script deserialised form
1 parent fe7afe3 commit 2a68c4a

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ message MultiAsset {
3737
repeated Asset assets = 2; // List of custom assets.
3838
}
3939

40+
// Represents a native script in Cardano.
41+
message NativeScript {
42+
oneof native_script {
43+
bytes script_pubkey = 1; // Script based on an address key hash.
44+
NativeScriptList script_all = 2; // Script that requires all nested scripts to be satisfied.
45+
NativeScriptList script_any = 3; // Script that requires any of the nested scripts to be satisfied.
46+
ScriptNOfK script_n_of_k = 4; // Script that requires k out of n nested scripts to be satisfied.
47+
uint64 invalid_before = 5; // Slot number before which the script is invalid.
48+
uint64 invalid_hereafter = 6; // Slot number after which the script is invalid.
49+
}
50+
}
51+
52+
// Represents a list of native scripts.
53+
message NativeScriptList {
54+
repeated NativeScript items = 1; // List of native scripts.
55+
}
56+
57+
// Represents a "k out of n" native script.
58+
message ScriptNOfK {
59+
uint32 k = 1; // The number of required satisfied scripts.
60+
repeated NativeScript scripts = 2; // List of native scripts.
61+
}
62+
4063
// Represents a constructor for Plutus data in Cardano.
4164
message Constr {
4265
uint32 tag = 1;
@@ -83,10 +106,9 @@ message PlutusDataArray {
83106

84107

85108
// Represents a script in Cardano.
86-
// TODO u5c: removed native script representation
87109
message Script {
88110
oneof script {
89-
bytes native = 1; // Native script.
111+
NativeScript native = 1; // Native script.
90112
bytes plutus_v1 = 2; // Plutus V1 script.
91113
bytes plutus_v2 = 3; // Plutus V2 script.
92114
bytes plutus_v3 = 4; // Plutus V3 script.

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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
@@ -79,15 +80,34 @@ instance Inject (ReferenceScript era) (Proto UtxoRpc.Script) where
7980
inject ReferenceScriptNone = defMessage
8081
inject (ReferenceScript _ (ScriptInAnyLang _ script)) =
8182
case script of
82-
SimpleScript _ ->
83-
defMessage & #native .~ serialiseToCBOR script
83+
SimpleScript ss ->
84+
defMessage & #native .~ inject ss
8485
PlutusScript PlutusScriptV1 ps ->
8586
defMessage & #plutusV1 .~ serialiseToRawBytes ps
8687
PlutusScript PlutusScriptV2 ps ->
8788
defMessage & #plutusV2 .~ serialiseToRawBytes ps
8889
PlutusScript PlutusScriptV3 ps ->
8990
defMessage & #plutusV3 .~ serialiseToRawBytes ps
9091

92+
instance Inject SimpleScript (Proto UtxoRpc.NativeScript) where
93+
inject = \case
94+
RequireSignature paymentKeyHash ->
95+
defMessage & #scriptPubkey .~ serialiseToRawBytes paymentKeyHash
96+
RequireTimeBefore (SlotNo slotNo) ->
97+
defMessage & #invalidHereafter .~ slotNo
98+
RequireTimeAfter (SlotNo slotNo) ->
99+
defMessage & #invalidBefore .~ slotNo
100+
RequireAllOf scripts ->
101+
defMessage & #scriptAll . #items .~ map inject scripts
102+
RequireAnyOf scripts ->
103+
defMessage & #scriptAny . #items .~ map inject scripts
104+
RequireMOf k scripts -> do
105+
let nScriptsOf =
106+
defMessage
107+
& #k .~ fromIntegral k
108+
& #scripts .~ map inject scripts
109+
defMessage & #scriptNOfK .~ nScriptsOf
110+
91111
instance Inject ScriptData (Proto UtxoRpc.PlutusData) where
92112
inject = \case
93113
ScriptDataBytes bs ->
@@ -120,7 +140,7 @@ instance IsCardanoEra era => Inject (UTxO era) [Proto UtxoRpc.AnyUtxoData] where
120140
toList policyAssets <&> \(assetName, Quantity qty) -> do
121141
defMessage
122142
& #name .~ serialiseToRawBytes assetName
123-
-- we don't have access to info it the coin was minted in the transaction,
143+
-- we don't have access to info if the coin was minted in the transaction,
124144
-- maybe we should add it later
125145
& #maybe'mintCoin .~ Nothing
126146
& #outputCoin .~ fromIntegral qty

0 commit comments

Comments
 (0)