Skip to content

Commit

Permalink
Merge branch 'develop' into blockfrost-crate-initial
Browse files Browse the repository at this point in the history
  • Loading branch information
rooooooooob authored Jul 17, 2024
2 parents 41bc64c + bf34fe4 commit b93b1da
Show file tree
Hide file tree
Showing 154 changed files with 7,461 additions and 4,286 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ members = [
# exclude old crate structure to avoid error in it
exclude = [
"rust",
"rust/json-gen"
"rust/json-gen",
"tools/metadata-cddl-checker",
"tools/plutus-datum-codegen"
]

[profile.release]
Expand Down
1 change: 1 addition & 0 deletions build-and-test.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
set -eu
. test.sh && npm run rust:build-nodejs
29 changes: 10 additions & 19 deletions chain/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cml-chain"
version = "5.2.0"
version = "5.3.1"
edition = "2018"
authors = ["dcSpark"]
license = "MIT"
Expand All @@ -13,9 +13,12 @@ keywords = ["cardano"]
[lib]
crate-type = ["cdylib", "rlib"]

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

[dependencies]
cml-core = { "path" = "../../core/rust", version = "5.2.0" }
cml-crypto = { "path" = "../../crypto/rust", version = "5.2.0" }
cml-core = { "path" = "../../core/rust", version = "5.3.1" }
cml-crypto = { "path" = "../../crypto/rust", version = "5.3.1" }
cbor_event = "2.2.0"
linked-hash-map = "0.5.3"
derivative = "2.2.0"
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
14 changes: 7 additions & 7 deletions chain/rust/src/block/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ impl Deserialize for HeaderBody {
(|| -> Result<_, DeserializeError> {
let (block_number, block_number_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("block_number"))?;
let (slot, slot_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("slot"))?;
let (prev_hash, prev_hash_encoding) = (|| -> Result<_, DeserializeError> {
Ok(match raw.cbor_type()? != cbor_event::Type::Special {
Expand Down Expand Up @@ -444,8 +444,8 @@ impl Deserialize for HeaderBody {
.map_err(|e: DeserializeError| e.annotate("vrf_result"))?;
let (block_body_size, block_body_size_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("block_body_size"))?;
let (block_body_hash, block_body_hash_encoding) = raw
.bytes_sz()
Expand Down Expand Up @@ -600,13 +600,13 @@ impl DeserializeEmbeddedGroup for OperationalCert {
.map_err(|e: DeserializeError| e.annotate("hot_vkey"))?;
let (sequence_number, sequence_number_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("sequence_number"))?;
let (kes_period, kes_period_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("kes_period"))?;
let (sigma, sigma_encoding) = raw
.bytes_sz()
Expand Down Expand Up @@ -716,13 +716,13 @@ impl DeserializeEmbeddedGroup for ProtocolVersion {
(|| -> Result<_, DeserializeError> {
let (major, major_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("major"))?;
let (minor, minor_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("minor"))?;
Ok(ProtocolVersion {
major,
Expand Down
17 changes: 11 additions & 6 deletions chain/rust/src/builders/certificate_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::witness_builder::{NativeScriptWitnessInfo, RequiredWitnessSet};

use crate::{
certs::{Certificate, StakeCredential},
transaction::RequiredSigners,
RequiredSigners,
};

use cml_crypto::{Ed25519KeyHash, ScriptHash};
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn cert_required_wits(cert: &Certificate, required_witnesses: &mut RequiredW
required_witnesses.add_from_credential(cert.stake_credential.clone());
}
Certificate::PoolRegistration(cert) => {
for owner in &cert.pool_params.pool_owners {
for owner in cert.pool_params.pool_owners.as_ref() {
required_witnesses.add_vkey_key_hash(*owner);
}
required_witnesses.add_vkey_key_hash(cert.pool_params.operator);
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn add_cert_vkeys(
}
},
Certificate::PoolRegistration(cert) => {
for owner in &cert.pool_params.pool_owners {
for owner in cert.pool_params.pool_owners.as_ref() {
vkeys.insert(*owner);
}
vkeys.insert(cert.pool_params.operator);
Expand Down 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
10 changes: 4 additions & 6 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 All @@ -10,8 +11,8 @@ use crate::{
certs::StakeCredential,
crypto::hash::hash_plutus_data,
plutus::PlutusData,
transaction::{RequiredSigners, TransactionInput, TransactionOutput},
NativeScript,
transaction::{TransactionInput, TransactionOutput},
NativeScript, RequiredSigners,
};

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -138,10 +139,7 @@ impl SingleInputBuilder {
required_signers: RequiredSigners,
datum: Option<PlutusData>,
) -> Result<InputBuilderResult, InputBuilderError> {
let mut required_wits = RequiredWitnessSet::default();
required_signers
.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
Loading

0 comments on commit b93b1da

Please sign in to comment.