Skip to content

Commit fc26782

Browse files
committed
Implement JSON trait for all types
1 parent 333eece commit fc26782

File tree

11 files changed

+320
-63
lines changed

11 files changed

+320
-63
lines changed

runtimes/rust/lbr-prelude-derive/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn derive_json_fn(input: TokenStream) -> TokenStream {
4040

4141
let expanded = quote! {
4242
impl #impl_generics lbr_prelude::json::Json for #ident #ty_generics #where_clause {
43+
4344
#to_json_impl
4445
#from_json_impl
4546
}

runtimes/rust/plutus-ledger-types/src/address.rs

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::crypto::Ed25519PubKeyHash;
22
use crate::ledger_state::Slot;
33
use crate::script::ValidatorHash;
4+
#[cfg(feature = "lbf")]
5+
use lbr_prelude::json::{self, Error, Json};
46
use num_bigint::BigInt;
57

68
#[cfg(feature = "serde")]
@@ -13,6 +15,7 @@ use serde::{Deserialize, Serialize};
1315
/// For a better understanding of all the Cardano address types, read [CIP 19](https://cips.cardano.org/cips/cip19/)
1416
#[derive(Debug, PartialEq, Eq)]
1517
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
18+
#[cfg_attr(feature = "lbf", derive(Json))]
1619
pub struct Address {
1720
pub credential: Credential,
1821
pub staking_credential: Option<StakingCredential>,
@@ -26,6 +29,45 @@ pub enum Credential {
2629
Script(ValidatorHash),
2730
}
2831

32+
#[cfg(feature = "lbf")]
33+
impl Json for Credential {
34+
fn to_json(&self) -> Result<serde_json::Value, Error> {
35+
match self {
36+
Credential::PubKey(pkh) => Ok(json::sum_constructor(
37+
"PubKeyCredential",
38+
vec![pkh.to_json()?],
39+
)),
40+
Credential::Script(val_hash) => Ok(json::sum_constructor(
41+
"ScriptCredential",
42+
vec![val_hash.to_json()?],
43+
)),
44+
}
45+
}
46+
47+
fn from_json(value: serde_json::Value) -> Result<Self, Error> {
48+
json::sum_parser(&value).and_then(|obj| match obj {
49+
("PubKeyCredential", ctor_fields) => match &ctor_fields[..] {
50+
[pkh] => Ok(Credential::PubKey(Json::from_json(pkh.clone())?)),
51+
_ => Err(Error::UnexpectedArrayLength {
52+
wanted: 1,
53+
got: ctor_fields.len(),
54+
}),
55+
},
56+
("ScriptCredential", ctor_fields) => match &ctor_fields[..] {
57+
[val_hash] => Ok(Credential::Script(Json::from_json(val_hash.clone())?)),
58+
_ => Err(Error::UnexpectedArrayLength {
59+
wanted: 1,
60+
got: ctor_fields.len(),
61+
}),
62+
},
63+
_ => Err(Error::UnexpectedJsonInvariant {
64+
wanted: "constructor names (Nothing, Just)".to_owned(),
65+
got: "unknown constructor name".to_owned(),
66+
}),
67+
})
68+
}
69+
}
70+
2971
/// Credential (public key hash or pointer) used for staking
3072
#[derive(Debug, PartialEq, Eq)]
3173
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -34,25 +76,68 @@ pub enum StakingCredential {
3476
Pointer(ChainPointer),
3577
}
3678

79+
#[cfg(feature = "lbf")]
80+
impl Json for StakingCredential {
81+
fn to_json(&self) -> Result<serde_json::Value, Error> {
82+
match self {
83+
StakingCredential::Hash(pkh) => {
84+
Ok(json::sum_constructor("StakingHash", vec![pkh.to_json()?]))
85+
}
86+
StakingCredential::Pointer(val_hash) => Ok(json::sum_constructor(
87+
"StakingPtr",
88+
vec![val_hash.to_json()?],
89+
)),
90+
}
91+
}
92+
93+
fn from_json(value: serde_json::Value) -> Result<Self, Error> {
94+
json::sum_parser(&value).and_then(|obj| match obj {
95+
("StakingHash", ctor_fields) => match &ctor_fields[..] {
96+
[pkh] => Ok(StakingCredential::Hash(Json::from_json(pkh.clone())?)),
97+
_ => Err(Error::UnexpectedArrayLength {
98+
wanted: 1,
99+
got: ctor_fields.len(),
100+
}),
101+
},
102+
("StakingPtr", ctor_fields) => match &ctor_fields[..] {
103+
[val_hash] => Ok(StakingCredential::Pointer(Json::from_json(
104+
val_hash.clone(),
105+
)?)),
106+
_ => Err(Error::UnexpectedArrayLength {
107+
wanted: 1,
108+
got: ctor_fields.len(),
109+
}),
110+
},
111+
_ => Err(Error::UnexpectedJsonInvariant {
112+
wanted: "constructor names (Nothing, Just)".to_owned(),
113+
got: "unknown constructor name".to_owned(),
114+
}),
115+
})
116+
}
117+
}
118+
37119
/// In an address, a chain pointer refers to a point of the chain containing a stake key
38120
/// registration certificate. A point is identified by 3 coordinates:
39121
/// - An absolute slot number
40122
/// - A transaction inder (within that slot)
41123
/// - A (delegation) certificate index (within that transacton)
42124
#[derive(Debug, PartialEq, Eq)]
43125
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
126+
#[cfg_attr(feature = "lbf", derive(Json))]
44127
pub struct ChainPointer {
45-
pub slot: Slot,
46-
pub tx_ix: TransactionIndex,
47-
pub cert_ix: CertificateIndex,
128+
pub slot_number: Slot,
129+
pub transaction_index: TransactionIndex,
130+
pub certificate_index: CertificateIndex,
48131
}
49132
/// Position of the certificate in a certain transaction
50133
#[derive(Debug, PartialEq, Eq)]
51134
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
135+
#[cfg_attr(feature = "lbf", derive(Json))]
52136
pub struct CertificateIndex(pub BigInt);
53137

54138
/// Position of a transaction in a given slot
55139
/// This is not identical to the index of a `TransactionInput`
56140
#[derive(Debug, PartialEq, Eq)]
57141
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
58-
pub struct TransactionIndex(pub u32);
142+
#[cfg_attr(feature = "lbf", derive(Json))]
143+
pub struct TransactionIndex(pub BigInt);
Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
1+
#[cfg(feature = "lbf")]
2+
use data_encoding::HEXLOWER;
3+
#[cfg(feature = "lbf")]
4+
use lbr_prelude::json::{Error, Json};
15
#[cfg(feature = "serde")]
26
use serde::{Deserialize, Serialize};
37

48
/// ED25519 public key hash
59
/// This is the standard cryptography in Cardano, commonly referred to as `PubKeyHash` in Plutus
610
/// and other libraries
7-
#[derive(Debug, PartialEq, Eq)]
11+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
812
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9-
pub struct Ed25519PubKeyHash(pub Vec<u8>);
13+
#[cfg_attr(feature = "lbf", derive(Json))]
14+
pub struct Ed25519PubKeyHash(pub LedgerBytes);
1015

1116
/// Standard public key hash used to verify a transaction witness
12-
#[derive(Debug, PartialEq, Eq)]
17+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
1318
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
19+
#[cfg_attr(feature = "lbf", derive(Json))]
1420
pub struct PaymentPubKeyHash(pub Ed25519PubKeyHash);
1521

1622
/// Standard public key hash used to verify a staking
17-
#[derive(Debug, PartialEq, Eq)]
23+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
1824
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
25+
#[cfg_attr(feature = "lbf", derive(Json))]
1926
pub struct StakePubKeyHash(pub Ed25519PubKeyHash);
27+
28+
/// A bytestring in the Cardano ledger context
29+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
30+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
31+
pub struct LedgerBytes(pub Vec<u8>);
32+
33+
#[cfg(feature = "lbf")]
34+
impl Json for LedgerBytes {
35+
fn to_json(&self) -> Result<serde_json::Value, Error> {
36+
String::to_json(&HEXLOWER.encode(&self.0))
37+
}
38+
39+
fn from_json(value: serde_json::Value) -> Result<Self, Error> {
40+
let bytes = String::from_json(value).and_then(|str| {
41+
HEXLOWER
42+
.decode(&str.into_bytes())
43+
.map_err(|_| Error::UnexpectedJsonInvariant {
44+
wanted: "base16 string".to_owned(),
45+
got: "unexpected string".to_owned(),
46+
})
47+
})?;
48+
49+
Ok(Self(bytes))
50+
}
51+
}

runtimes/rust/plutus-ledger-types/src/datum.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::plutus_data::PlutusData;
2+
#[cfg(feature = "lbf")]
3+
use lbr_prelude::json::{self, Error, Json};
24

35
#[cfg(feature = "serde")]
46
use serde::{Deserialize, Serialize};
@@ -16,12 +18,63 @@ pub enum OutputDatum {
1618
InlineDatum(Datum),
1719
}
1820

21+
#[cfg(feature = "lbf")]
22+
impl Json for OutputDatum {
23+
fn to_json(&self) -> Result<serde_json::Value, Error> {
24+
match self {
25+
OutputDatum::None => Ok(json::sum_constructor(
26+
"NoOutputDatum",
27+
Vec::with_capacity(0),
28+
)),
29+
OutputDatum::DatumHash(dat_hash) => Ok(json::sum_constructor(
30+
"OutputDatumHash",
31+
vec![dat_hash.to_json()?],
32+
)),
33+
OutputDatum::InlineDatum(datum) => {
34+
Ok(json::sum_constructor("OutputDatum", vec![datum.to_json()?]))
35+
}
36+
}
37+
}
38+
39+
fn from_json(value: serde_json::Value) -> Result<Self, Error> {
40+
json::sum_parser(&value).and_then(|obj| match obj {
41+
("PubKeyCredential", ctor_fields) => match &ctor_fields[..] {
42+
[] => Ok(OutputDatum::None),
43+
_ => Err(Error::UnexpectedArrayLength {
44+
wanted: 0,
45+
got: ctor_fields.len(),
46+
}),
47+
},
48+
("OutputDatumHash", ctor_fields) => match &ctor_fields[..] {
49+
[dat_hash] => Ok(OutputDatum::DatumHash(Json::from_json(dat_hash.clone())?)),
50+
_ => Err(Error::UnexpectedArrayLength {
51+
wanted: 1,
52+
got: ctor_fields.len(),
53+
}),
54+
},
55+
("OutputDatum", ctor_fields) => match &ctor_fields[..] {
56+
[datum] => Ok(OutputDatum::InlineDatum(Json::from_json(datum.clone())?)),
57+
_ => Err(Error::UnexpectedArrayLength {
58+
wanted: 1,
59+
got: ctor_fields.len(),
60+
}),
61+
},
62+
_ => Err(Error::UnexpectedJsonInvariant {
63+
wanted: "constructor names (Nothing, Just)".to_owned(),
64+
got: "unknown constructor name".to_owned(),
65+
}),
66+
})
67+
}
68+
}
69+
1970
/// blake2b-256 hash of a datum
2071
#[derive(Debug, PartialEq, Eq)]
72+
#[cfg_attr(feature = "lbf", derive(Json))]
2173
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2274
pub struct DataHash(pub Vec<u8>);
2375

2476
/// Piece of information associated with a UTxO encoded into a PlutusData type.
2577
#[derive(Debug, PartialEq, Eq)]
78+
#[cfg_attr(feature = "lbf", derive(Json))]
2679
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2780
pub struct Datum(pub PlutusData);

0 commit comments

Comments
 (0)