Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs Update #307

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions chain/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ keywords = ["cardano"]
[lib]
crate-type = ["cdylib", "rlib"]

[features]
used_from_wasm = ["wasm-bindgen"]

[dependencies]
cml-core = { "path" = "../../core/rust", version = "5.3.1" }
cml-crypto = { "path" = "../../crypto/rust", version = "5.3.1" }
Expand All @@ -32,29 +35,17 @@ fraction = "0.10.0"
base64 = "0.21.5"
num-bigint = "0.4.0"
num-integer = "0.1.45"
#rand_os = "0.1"
thiserror = "1.0.37"
num = "0.4"
unicode-segmentation = "1.10.1"
# These can be removed if we make wasm bindings for ALL functionality here.
# This was not done right now as there is a lot of existing legacy code e.g.
# for Byron that might need to be used from WASM and might not.
# We can remove this dependency when that is decided.
#
# The other use-case here is enums. Without this two enums would need to be defined
# despite wasm_bindgen supporting C-style enums (with non-negative values) 100%
# This could possibly be resolved with macros but maybe not.
serde-aux = "4.5.0"
chrono = "0.4.38"

# non-wasm
#[target.'cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))'.dependencies]
#rand_os = "0.1"
#noop_proc_macro = "0.3.0"
noop_proc_macro = { version = "0.3.0", optional = false }

# wasm
#[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = { version = "=0.2.83", features = ["serde-serialize"] }
#rand_os = { version = "0.1", features = ["wasm-bindgen"] }
#js-sys = "=0.3.59"
wasm-bindgen = { version = "0.2.87", optional = true }


[dev-dependencies]
Expand Down
4 changes: 3 additions & 1 deletion chain/rust/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use schemars::JsonSchema;
use std::convert::{TryFrom, TryInto};
use std::io::{BufRead, Write};

// for enums
#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

use cml_crypto::{Ed25519KeyHash, ScriptHash};
Expand Down
5 changes: 1 addition & 4 deletions chain/rust/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cml_core::error::*;

use std::convert::TryFrom;

/// Use TryFrom<&str> / TryInto<&str> for utf8 text conversion and RawBytesEncoding for direct bytes access
#[derive(Clone, Debug, derivative::Derivative)]
#[derivative(Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct AssetName {
Expand All @@ -26,10 +27,6 @@ pub struct AssetName {
}

impl AssetName {
pub fn get(&self) -> &Vec<u8> {
&self.inner
}

pub fn new(inner: Vec<u8>) -> Result<Self, DeserializeError> {
if inner.len() > 32 {
return Err(DeserializeError::new(
Expand Down
14 changes: 12 additions & 2 deletions chain/rust/src/assets/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ impl<'a> TryInto<&'a str> for &'a AssetName {
type Error = std::str::Utf8Error;

fn try_into(self) -> Result<&'a str, Self::Error> {
std::str::from_utf8(self.get())
std::str::from_utf8(self.to_raw_bytes())
}
}

impl RawBytesEncoding for AssetName {
fn to_raw_bytes(&self) -> &[u8] {
self.inner.as_ref()
}

fn from_raw_bytes(bytes: &[u8]) -> Result<Self, DeserializeError> {
Self::new(bytes.to_vec())
}
}

Expand All @@ -71,7 +81,7 @@ impl<T: std::fmt::Debug> std::fmt::Debug for AssetBundle<T> {
for (pid, assets) in self.0.iter() {
let pid_hex = hex::encode(pid.to_raw_bytes());
for (an, val) in assets.iter() {
let an_hex = hex::encode(an.get());
let an_hex = hex::encode(an.to_raw_bytes());
let an_name = if an_hex.len() > 8 {
format!(
"{}..{}",
Expand Down
2 changes: 1 addition & 1 deletion chain/rust/src/auxdata/cbor_encodings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct ConwayFormatAuxDataEncoding {
}

#[derive(Clone, Debug, Default)]
pub struct ShelleyMaFormatAuxDataEncoding {
pub struct ShelleyMAFormatAuxDataEncoding {
pub len_encoding: LenEncoding,
pub auxiliary_scripts_encoding: LenEncoding,
}
14 changes: 7 additions & 7 deletions chain/rust/src/auxdata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ pub mod utils;

use crate::plutus::{PlutusV1Script, PlutusV2Script, PlutusV3Script};
use crate::transaction::NativeScript;
use cbor_encodings::{ConwayFormatAuxDataEncoding, ShelleyMaFormatAuxDataEncoding};
use cbor_encodings::{ConwayFormatAuxDataEncoding, ShelleyMAFormatAuxDataEncoding};

pub use metadata::*;

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub enum AuxiliaryData {
Shelley(ShelleyFormatAuxData),
ShelleyMA(ShelleyMaFormatAuxData),
ShelleyMA(ShelleyMAFormatAuxData),
Conway(ConwayFormatAuxData),
}

Expand All @@ -24,8 +24,8 @@ impl AuxiliaryData {
Self::Shelley(shelley)
}

pub fn new_shelley_m_a(shelley_m_a: ShelleyMaFormatAuxData) -> Self {
Self::ShelleyMA(shelley_m_a)
pub fn new_shelley_ma(shelley_ma: ShelleyMAFormatAuxData) -> Self {
Self::ShelleyMA(shelley_ma)
}

pub fn new_conway(conway: ConwayFormatAuxData) -> Self {
Expand Down Expand Up @@ -66,14 +66,14 @@ impl Default for ConwayFormatAuxData {
pub type ShelleyFormatAuxData = Metadata;

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct ShelleyMaFormatAuxData {
pub struct ShelleyMAFormatAuxData {
pub transaction_metadata: Metadata,
pub auxiliary_scripts: Vec<NativeScript>,
#[serde(skip)]
pub encodings: Option<ShelleyMaFormatAuxDataEncoding>,
pub encodings: Option<ShelleyMAFormatAuxDataEncoding>,
}

impl ShelleyMaFormatAuxData {
impl ShelleyMAFormatAuxData {
pub fn new(transaction_metadata: Metadata, auxiliary_scripts: Vec<NativeScript>) -> Self {
Self {
transaction_metadata,
Expand Down
18 changes: 9 additions & 9 deletions chain/rust/src/auxdata/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ impl Serialize for AuxiliaryData {
) -> cbor_event::Result<&'se mut Serializer<W>> {
match self {
AuxiliaryData::Shelley(shelley) => shelley.serialize(serializer, force_canonical),
AuxiliaryData::ShelleyMA(shelley_m_a) => {
shelley_m_a.serialize(serializer, force_canonical)
AuxiliaryData::ShelleyMA(shelley_ma) => {
shelley_ma.serialize(serializer, force_canonical)
}
AuxiliaryData::Conway(conway) => conway.serialize(serializer, force_canonical),
}
Expand All @@ -43,9 +43,9 @@ impl Deserialize for AuxiliaryData {
}
};
let deser_variant: Result<_, DeserializeError> =
ShelleyMaFormatAuxData::deserialize(raw);
ShelleyMAFormatAuxData::deserialize(raw);
match deser_variant {
Ok(shelley_m_a) => return Ok(Self::ShelleyMA(shelley_m_a)),
Ok(shelley_ma) => return Ok(Self::ShelleyMA(shelley_ma)),
Err(e) => {
errs.push(e.annotate("ShelleyMA"));
raw.as_mut_ref()
Expand Down Expand Up @@ -513,7 +513,7 @@ impl Deserialize for ConwayFormatAuxData {
}
}

impl Serialize for ShelleyMaFormatAuxData {
impl Serialize for ShelleyMAFormatAuxData {
fn serialize<'se, W: Write>(
&self,
serializer: &'se mut Serializer<W>,
Expand Down Expand Up @@ -551,7 +551,7 @@ impl Serialize for ShelleyMaFormatAuxData {
}
}

impl Deserialize for ShelleyMaFormatAuxData {
impl Deserialize for ShelleyMAFormatAuxData {
fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
let len = raw.array_sz()?;
let len_encoding: LenEncoding = len.into();
Expand Down Expand Up @@ -586,15 +586,15 @@ impl Deserialize for ShelleyMaFormatAuxData {
_ => return Err(DeserializeFailure::EndingBreakMissing.into()),
},
}
Ok(ShelleyMaFormatAuxData {
Ok(ShelleyMAFormatAuxData {
transaction_metadata,
auxiliary_scripts,
encodings: Some(ShelleyMaFormatAuxDataEncoding {
encodings: Some(ShelleyMAFormatAuxDataEncoding {
len_encoding,
auxiliary_scripts_encoding,
}),
})
})()
.map_err(|e| e.annotate("ShelleyMaFormatAuxData"))
.map_err(|e| e.annotate("ShelleyMAFormatAuxData"))
}
}
4 changes: 2 additions & 2 deletions chain/rust/src/auxdata/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
transaction::NativeScript,
};

use super::{AuxiliaryData, ConwayFormatAuxData, ShelleyMaFormatAuxData};
use super::{AuxiliaryData, ConwayFormatAuxData, ShelleyMAFormatAuxData};

impl AuxiliaryData {
pub fn new() -> Self {
Expand Down Expand Up @@ -81,7 +81,7 @@ impl AuxiliaryData {
pub fn add_native_scripts(&mut self, scripts: Vec<NativeScript>) {
match self {
Self::Shelley(shelley) => {
*self = Self::ShelleyMA(ShelleyMaFormatAuxData::new(shelley.clone(), scripts));
*self = Self::ShelleyMA(ShelleyMAFormatAuxData::new(shelley.clone(), scripts));
}
Self::ShelleyMA(shelley_ma) => {
shelley_ma.auxiliary_scripts.extend(scripts);
Expand Down
11 changes: 8 additions & 3 deletions chain/rust/src/builders/certificate_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,14 @@ pub fn add_cert_vkeys(
vkeys.insert(*hash);
}
},
Certificate::RegDrepCert(_cert) => {
// does not need a witness
}
Certificate::RegDrepCert(cert) => match &cert.drep_credential {
StakeCredential::Script { hash, .. } => {
return Err(CertBuilderError::ExpectedKeyHash(*hash))
}
StakeCredential::PubKey { hash, .. } => {
vkeys.insert(*hash);
}
},
Certificate::UnregDrepCert(cert) => match &cert.drep_credential {
StakeCredential::Script { hash, .. } => {
return Err(CertBuilderError::ExpectedKeyHash(*hash))
Expand Down
7 changes: 2 additions & 5 deletions chain/rust/src/builders/input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::builders::witness_builder::{InputAggregateWitnessData, PartialPlutusW

use super::{
tx_builder::TransactionUnspentOutput,
utils::required_wits_from_required_signers,
witness_builder::{NativeScriptWitnessInfo, RequiredWitnessSet},
};

Expand Down Expand Up @@ -138,11 +139,7 @@ impl SingleInputBuilder {
required_signers: RequiredSigners,
datum: Option<PlutusData>,
) -> Result<InputBuilderResult, InputBuilderError> {
let mut required_wits = RequiredWitnessSet::default();
required_signers
.as_ref()
.iter()
.for_each(|required_signer| required_wits.add_vkey_key_hash(*required_signer));
let mut required_wits = required_wits_from_required_signers(&required_signers);
input_required_wits(&self.utxo_info, &mut required_wits);
let mut required_wits_left = required_wits.clone();

Expand Down
3 changes: 3 additions & 0 deletions chain/rust/src/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ pub mod certificate_builder;
pub mod input_builder;
pub mod mint_builder;
pub mod output_builder;
pub mod proposal_builder;
pub mod redeemer_builder;
pub mod tx_builder;
mod utils;
pub mod vote_builder;
pub mod withdrawal_builder;
pub mod witness_builder;
Loading
Loading