Skip to content

Commit

Permalink
Plutus datum CDDL codegen tool (#325)
Browse files Browse the repository at this point in the history
* Plutus datum CDDL codegen tool

* finished (?) datum tool

- can use cargo run
- exports wasm utils
- many misc fixes
- update wasm wrappers to specify JsValue/Error

* cargo fmt

* Include prelude + docs + fixes

* Hook into CML types
  • Loading branch information
rooooooooob authored Jul 30, 2024
1 parent bf34fe4 commit 5f0f3d0
Show file tree
Hide file tree
Showing 43 changed files with 1,160 additions and 41 deletions.
2 changes: 1 addition & 1 deletion chain/wasm/src/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

pub mod utils;

Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/assets/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use crate::{assets::AssetName, AssetNameList, MapAssetNameToNonZeroInt64, PolicyId, PolicyIdList};
use wasm_bindgen::{prelude::wasm_bindgen, JsError, JsValue};
use wasm_bindgen::{prelude::wasm_bindgen, JsError};

use cml_core_wasm::{
impl_raw_bytes_api, impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_map,
Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/auxdata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use super::{NativeScriptList, PlutusV1ScriptList, PlutusV2ScriptList, PlutusV3ScriptList};
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

pub mod metadata;
pub mod utils;
Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
use crate::crypto::{KESSignature, VRFCert, Vkey};
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use cml_crypto_wasm::{BlockBodyHash, BlockHeaderHash, Ed25519Signature, KESVkey, VRFVkey};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/byron/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use self::utils::{AddressId, ByronScript, ProtocolMagic, StakeholderId};
pub use cml_chain::byron::ByronAddrType;
use cml_core_wasm::{impl_wasm_cbor_event_serialize_api, impl_wasm_conversions};
use cml_crypto_wasm::{Bip32PublicKey, PublicKey};
use wasm_bindgen::prelude::{wasm_bindgen, JsError};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/certs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::governance::Anchor;

use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use cml_crypto_wasm::{Ed25519KeyHash, PoolMetadataHash, ScriptHash, VRFKeyHash};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use cml_crypto_wasm::{
pub mod hash;
pub mod utils;

use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::{wasm_bindgen, JsError};

use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};

Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/governance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
pub use cml_chain::governance::Vote;
use cml_core::ordered_hash_map::OrderedHashMap;
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

pub mod utils;

Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
clippy::new_without_default
)]

use ::wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use ::wasm_bindgen::prelude::{wasm_bindgen, JsError};
use auxdata::TransactionMetadatumList;
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_list};

Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/plutus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{LegacyRedeemerList, MapRedeemerKeyToRedeemerVal};
pub use cml_chain::plutus::{Language, RedeemerTag};
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_map};
pub use utils::{ConstrPlutusData, PlutusMap};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

impl_wasm_map!(
u64,
Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/plutus/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cml_core_wasm::{
impl_raw_bytes_api, impl_wasm_cbor_api, impl_wasm_cbor_json_api, impl_wasm_conversions,
};
use cml_crypto_wasm::ScriptHash;
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::{wasm_bindgen, JsError};

use super::{ExUnits, PlutusV1Script, PlutusV2Script, PlutusV3Script};

Expand Down
2 changes: 2 additions & 0 deletions chain/wasm/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use cml_crypto_wasm::{
AuxiliaryDataHash, DatumHash, Ed25519KeyHash, ScriptDataHash, TransactionHash,
};

use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};

pub mod utils;

#[derive(Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Int, Script, ScriptHash};
use cml_chain::plutus::Language;
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::{wasm_bindgen, JsError};

use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_list};

Expand Down
2 changes: 1 addition & 1 deletion cip25/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use utils::CIP25LabelMetadata;

pub use cml_cip25::CIP25Version;

use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};

/// This is the entire metadata schema for CIP-25
/// It can be parsed by passing in the CBOR bytes of the entire transaction metadata
Expand Down
2 changes: 1 addition & 1 deletion cip36/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/dcSpark/cddl-codegen

use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_json_api};

Expand Down
28 changes: 14 additions & 14 deletions core/wasm/src/wasm_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,11 @@ macro_rules! impl_wasm_cbor_api {
/**
* Create this type from CBOR bytes
*/
pub fn from_cbor_bytes(cbor_bytes: &[u8]) -> Result<$wasm_name, JsError> {
pub fn from_cbor_bytes(cbor_bytes: &[u8]) -> Result<$wasm_name, wasm_bindgen::JsError> {
cml_core::serialization::Deserialize::from_cbor_bytes(cbor_bytes)
.map(Self)
.map_err(|e| {
JsError::new(&format!(
wasm_bindgen::JsError::new(&format!(
concat!(stringify!($wasm_name), "::from_bytes: {}"),
e
))
Expand All @@ -444,10 +444,10 @@ macro_rules! impl_wasm_cbor_api {
* Create this type from the CBOR bytes encoded as a hex string.
* This is useful for interfacing with CIP30
*/
pub fn from_cbor_hex(cbor_bytes: &str) -> Result<$wasm_name, JsError> {
pub fn from_cbor_hex(cbor_bytes: &str) -> Result<$wasm_name, wasm_bindgen::JsError> {
hex::decode(cbor_bytes)
.map_err(|e| {
JsError::new(&format!(
wasm_bindgen::JsError::new(&format!(
concat!(stringify!($wasm_name), "::from_cbor_hex: {}"),
e
))
Expand Down Expand Up @@ -477,11 +477,11 @@ macro_rules! impl_wasm_cbor_event_serialize_api {
/**
* Create this type from CBOR bytes
*/
pub fn from_cbor_bytes(cbor_bytes: &[u8]) -> Result<$wasm_name, JsError> {
pub fn from_cbor_bytes(cbor_bytes: &[u8]) -> Result<$wasm_name, wasm_bindgen::JsError> {
cml_core::serialization::Deserialize::from_cbor_bytes(cbor_bytes)
.map(Self)
.map_err(|e| {
JsError::new(&format!(
wasm_bindgen::JsError::new(&format!(
concat!(stringify!($wasm_name), "::from_cbor_bytes: {}"),
e
))
Expand All @@ -501,10 +501,10 @@ macro_rules! impl_wasm_cbor_event_serialize_api {
* Create this type from the CBOR bytes encoded as a hex string.
* This is useful for interfacing with CIP30
*/
pub fn from_cbor_hex(cbor_bytes: &str) -> Result<$wasm_name, JsError> {
pub fn from_cbor_hex(cbor_bytes: &str) -> Result<$wasm_name, wasm_bindgen::JsError> {
hex::decode(cbor_bytes)
.map_err(|e| {
JsError::new(&format!(
wasm_bindgen::JsError::new(&format!(
concat!(stringify!($wasm_name), "::from_cbor_hex: {}"),
e
))
Expand All @@ -521,27 +521,27 @@ macro_rules! impl_wasm_json_api {
($wasm_name:ident) => {
#[wasm_bindgen::prelude::wasm_bindgen]
impl $wasm_name {
pub fn to_json(&self) -> Result<String, JsError> {
pub fn to_json(&self) -> Result<String, wasm_bindgen::JsError> {
serde_json::to_string_pretty(&self.0).map_err(|e| {
JsError::new(&format!(
wasm_bindgen::JsError::new(&format!(
concat!(stringify!($wasm_name), "::to_json: {}"),
e
))
})
}

pub fn to_js_value(&self) -> Result<JsValue, JsError> {
pub fn to_js_value(&self) -> Result<wasm_bindgen::JsValue, wasm_bindgen::JsError> {
serde_wasm_bindgen::to_value(&self.0).map_err(|e| {
JsError::new(&format!(
wasm_bindgen::JsError::new(&format!(
concat!(stringify!($wasm_name), "::to_js_value: {}"),
e
))
})
}

pub fn from_json(json: &str) -> Result<$wasm_name, JsError> {
pub fn from_json(json: &str) -> Result<$wasm_name, wasm_bindgen::JsError> {
serde_json::from_str(json).map(Self).map_err(|e| {
JsError::new(&format!(
wasm_bindgen::JsError::new(&format!(
concat!(stringify!($wasm_name), "::from_json: {}"),
e
))
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/allegra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use cml_crypto_wasm::Ed25519KeyHash;
use cml_crypto_wasm::{AuxiliaryDataHash, GenesisDelegateHash, GenesisHash, VRFKeyHash};
use cml_multi_era::allegra::MIRPot;
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/alonzo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use cml_core::ordered_hash_map::OrderedHashMap;
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use cml_crypto_wasm::{AuxiliaryDataHash, GenesisHash, ScriptDataHash};
use cml_multi_era::alonzo::AlonzoRedeemerTag;
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/babbage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use cml_chain_wasm::{
use cml_core::ordered_hash_map::OrderedHashMap;
use cml_core::TransactionIndex;
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/byron/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::byron::{
use cml_core_wasm::{
impl_wasm_cbor_json_api_cbor_event_serialize, impl_wasm_conversions, impl_wasm_list,
};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/byron/delegation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::byron::{ByronPubKey, ByronSignature, EpochId};
use cml_core_wasm::{impl_wasm_cbor_json_api_cbor_event_serialize, impl_wasm_conversions};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/byron/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod update;
#[macro_use]
pub mod utils;

use ::wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use ::wasm_bindgen::prelude::wasm_bindgen;
use cml_chain_wasm::byron::ByronTxOut;
use cml_chain_wasm::utils::BigInteger;
use cml_core_wasm::{
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/byron/mpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cml_core_wasm::{
impl_wasm_cbor_json_api_cbor_event_serialize, impl_wasm_conversions, impl_wasm_list,
impl_wasm_map_btree,
};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/byron/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::byron::{
use cml_core_wasm::{
impl_wasm_cbor_json_api_cbor_event_serialize, impl_wasm_conversions, impl_wasm_map_btree,
};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

impl_wasm_map_btree!(
cml_multi_era::byron::ByronAny,
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/byron/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::byron::{
use crate::byron::{Blake2b256, ByronPubKey, ByronSignature, ByronUpdateId, EpochId};
use cml_chain_wasm::utils::BigInteger;
use cml_core_wasm::{impl_wasm_cbor_json_api_cbor_event_serialize, impl_wasm_conversions};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use cml_core_wasm::{
impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_list, impl_wasm_map,
};
use cml_crypto_wasm::GenesisHash;
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

impl_wasm_list!(
cml_multi_era::allegra::AllegraCertificate,
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/mary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use cml_chain_wasm::TransactionInputList;
use cml_chain_wasm::{address::Address, Value, Withdrawals};
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use cml_crypto_wasm::AuxiliaryDataHash;
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/shelley/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use cml_crypto_wasm::{
GenesisHash, VRFKeyHash, VRFVkey,
};
use cml_multi_era::allegra::MIRPot;
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};
use wasm_bindgen::prelude::wasm_bindgen;

#[derive(Clone, Debug)]
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion multi-era/wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use cml_core_wasm::{impl_wasm_conversions, impl_wasm_json_api, impl_wasm_list, i
use cml_crypto_wasm::{
AuxiliaryDataHash, BlockBodyHash, BlockHeaderHash, ScriptDataHash, TransactionHash, VRFVkey,
};
use wasm_bindgen::{prelude::wasm_bindgen, JsError, JsValue};
use wasm_bindgen::{prelude::wasm_bindgen, JsError};

use crate::{
allegra::MoveInstantaneousRewardsCert, shelley::GenesisKeyDelegation,
Expand Down
12 changes: 12 additions & 0 deletions tools/plutus-datum-codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "plutus-datum-codegen"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cddl = "0.9.1"
cddl-codegen = { git = "https://github.com/dcSpark/cddl-codegen" }
clap = { version = "4.3.12", features = ["derive"] }
codegen = { git = "https://github.com/dcSpark/codegen", branch = "master" }
Loading

0 comments on commit 5f0f3d0

Please sign in to comment.