diff --git a/Cargo.lock b/Cargo.lock index 17c4062d..ef523a26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -213,6 +213,26 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "minicbor" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a20020e8e2d1881d8736f64011bb5ff99f1db9947ce3089706945c8915695cb" +dependencies = [ + "minicbor-derive", +] + +[[package]] +name = "minicbor-derive" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8608fb1c805b5b6b3d5ab7bd95c40c396df622b64d77b2d621a5eae1eed050ee" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -512,6 +532,7 @@ version = "0.0.6" dependencies = [ "flat-rs", "hex", + "minicbor", "peg", "pretty", "proptest", diff --git a/crates/uplc/Cargo.toml b/crates/uplc/Cargo.toml index 5dbe082e..8c917ca3 100644 --- a/crates/uplc/Cargo.toml +++ b/crates/uplc/Cargo.toml @@ -15,6 +15,7 @@ exclude = ["test_data/*"] [dependencies] flat-rs = { path = "../flat", version = "0.0.6" } hex = "0.4.3" +minicbor = { version = "0.18.0", features = ["std"] } peg = "0.8.0" pretty = "0.11.3" thiserror = "1.0.31" diff --git a/crates/uplc/src/flat.rs b/crates/uplc/src/flat.rs index 2e7f9e77..497bccbf 100644 --- a/crates/uplc/src/flat.rs +++ b/crates/uplc/src/flat.rs @@ -33,8 +33,14 @@ where self.flat() } - pub fn flat_hex(&self) -> Result { - let bytes = self.flat()?; + pub fn to_cbor(&self) -> Result, en::Error> { + let flat_bytes = self.flat()?; + + minicbor::to_vec(&flat_bytes).map_err(|err| en::Error::Message(err.to_string())) + } + + pub fn to_hex(&self) -> Result { + let bytes = self.to_cbor()?; let hex = hex::encode(&bytes);