Skip to content

Commit

Permalink
Merge pull request #115 from sidan-lab/fix/asset-name-decode
Browse files Browse the repository at this point in the history
return js error for asset name decoding
  • Loading branch information
twwu123 authored Jan 17, 2025
2 parents b846430 + e433f0f commit f7219b1
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
6 changes: 3 additions & 3 deletions packages/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
version = "0.9.16"
version = "0.9.17"
resolver = "2"
members = [
"sidan-csl-rs",
Expand Down
2 changes: 1 addition & 1 deletion packages/sidan-csl-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sidan-csl-rs"
version = "0.9.16"
version = "0.9.17"
edition = "2021"
license = "Apache-2.0"
description = "Wrapper around the cardano-serialization-lib for easier transaction building, heavily inspired by cardano-cli APIs"
Expand Down
10 changes: 7 additions & 3 deletions packages/sidan-csl-rs/src/core/common/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cardano_serialization_lib::JsError;
use hex;

pub fn bytes_to_hex(bytes: &[u8]) -> String {
Expand All @@ -12,7 +13,10 @@ pub fn string_to_hex(s: &str) -> String {
hex::encode(s)
}

pub fn hex_to_string(hex: &str) -> Result<String, std::str::Utf8Error> {
let bytes = hex::decode(hex).unwrap();
Ok(std::str::from_utf8(&bytes)?.to_string())
pub fn hex_to_string(hex: &str) -> Result<String, JsError> {
let bytes = hex::decode(hex)
.map_err(|err| JsError::from_str(&format!("Invalid hex string found: {}", err)))?;
Ok(std::str::from_utf8(&bytes)
.map_err(|err| JsError::from_str(&format!("Invalid bytes for utf-8 found: {}", err)))?
.to_string())
}
24 changes: 15 additions & 9 deletions packages/sidan-csl-rs/src/core/core_csl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl MeshCSL {
&csl::TransactionHash::from_hex(&input.tx_in.tx_hash)?,
input.tx_in.tx_index,
),
&to_value(&input.tx_in.amount.unwrap()),
&to_value(&input.tx_in.amount.unwrap())?,
)?;
Ok(())
}
Expand All @@ -53,7 +53,7 @@ impl MeshCSL {
&csl::TransactionHash::from_hex(&input.tx_in.tx_hash)?,
input.tx_in.tx_index,
),
&to_value(&input.tx_in.amount.unwrap()),
&to_value(&input.tx_in.amount.unwrap())?,
);
Ok(())
}
Expand All @@ -71,7 +71,7 @@ impl MeshCSL {
&csl::TransactionHash::from_hex(&input.tx_in.tx_hash)?,
input.tx_in.tx_index,
),
&to_value(&input.tx_in.amount.unwrap()),
&to_value(&input.tx_in.amount.unwrap())?,
);
Ok(())
}
Expand Down Expand Up @@ -122,7 +122,7 @@ impl MeshCSL {
&csl::TransactionHash::from_hex(&input.tx_in.tx_hash)?,
input.tx_in.tx_index,
),
&to_value(&input.tx_in.amount.unwrap()),
&to_value(&input.tx_in.amount.unwrap())?,
);
Ok(())
}
Expand Down Expand Up @@ -185,7 +185,7 @@ impl MeshCSL {
}
}

let tx_value = to_value(&output.amount);
let tx_value = to_value(&output.amount)?;
let amount_builder = output_builder.next()?;
let mut built_output: csl::TransactionOutput = if tx_value.multiasset().is_some() {
if tx_value.coin().is_zero() {
Expand Down Expand Up @@ -238,7 +238,7 @@ impl MeshCSL {
&csl::TransactionHash::from_hex(&collateral.tx_in.tx_hash)?,
collateral.tx_in.tx_index,
),
&to_value(&collateral.tx_in.amount.unwrap()),
&to_value(&collateral.tx_in.amount.unwrap())?,
)?;
Ok(())
}
Expand Down Expand Up @@ -353,7 +353,9 @@ impl MeshCSL {
let mint_script = to_csl_script_source(script_source_info)?;
mint_builder.add_asset(
&csl::MintWitness::new_plutus_script(&mint_script, &mint_redeemer),
&csl::AssetName::new(hex::decode(script_mint.mint.asset_name).unwrap())?,
&csl::AssetName::new(hex::decode(script_mint.mint.asset_name).map_err(|err| {
JsError::from_str(&format!("Invalid asset name found: {}", err))
})?)?,
&csl::Int::from_str(&script_mint.mint.amount.to_string()).unwrap(),
)?;
Ok(())
Expand All @@ -370,7 +372,9 @@ impl MeshCSL {
&csl::MintWitness::new_native_script(&csl::NativeScriptSource::new(
&csl::NativeScript::from_hex(&script.script_cbor)?,
)),
&csl::AssetName::new(hex::decode(native_mint.mint.asset_name).unwrap())?,
&csl::AssetName::new(hex::decode(native_mint.mint.asset_name).map_err(|err| {
JsError::from_str(&format!("Invalid asset name found: {}", err))
})?)?,
&csl::Int::from_str(&native_mint.mint.amount.to_string()).unwrap(),
)?,
SimpleScriptSource::InlineSimpleScriptSource(script) => mint_builder.add_asset(
Expand All @@ -382,7 +386,9 @@ impl MeshCSL {
),
script.script_size,
)),
&csl::AssetName::new(hex::decode(native_mint.mint.asset_name).unwrap())?,
&csl::AssetName::new(hex::decode(native_mint.mint.asset_name).map_err(|err| {
JsError::from_str(&format!("Invalid asset name found: {}", err))
})?)?,
&csl::Int::from_str(&native_mint.mint.amount.to_string()).unwrap(),
)?,
};
Expand Down
9 changes: 5 additions & 4 deletions packages/sidan-csl-rs/src/core/utils/ungroup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::csl;
use cardano_serialization_lib::JsError;
use hex::FromHex;

use crate::model::*;
Expand Down Expand Up @@ -40,16 +41,16 @@ pub fn build_tx_builder(params: Option<Protocol>) -> csl::TransactionBuilder {
csl::TransactionBuilder::new(&cfg)
}

pub fn to_value(assets: &Vec<Asset>) -> csl::Value {
pub fn to_value(assets: &Vec<Asset>) -> Result<csl::Value, JsError> {
let lovelace = assets.iter().find(|asset| asset.unit() == "lovelace");
let mut multi_asset = csl::MultiAsset::new();

for asset in assets {
if asset.unit() == "lovelace" {
continue;
}
let name_bytes =
Vec::<u8>::from_hex(&asset.unit()[56..]).expect("Failed to parse hex asset name");
let name_bytes = Vec::<u8>::from_hex(&asset.unit()[56..])
.map_err(|err| JsError::from_str(&format!("Invalid hex string found: {}", err)))?;

multi_asset.set_asset(
&csl::ScriptHash::from_hex(&asset.unit()[0..56]).unwrap(),
Expand All @@ -68,5 +69,5 @@ pub fn to_value(assets: &Vec<Asset>) -> csl::Value {
if assets.len() > 1 || lovelace.is_none() {
value.set_multiasset(&multi_asset);
}
value
Ok(value)
}
2 changes: 1 addition & 1 deletion packages/sidan-csl-rs/src/core/utils/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn get_min_utxo_value(output: &Output, coins_per_utxo_size: &u64) -> Result<
}
}
}
let multi_asset = match to_value(&output.amount).multiasset() {
let multi_asset = match to_value(&output.amount)?.multiasset() {
Some(multi_asset) => multi_asset,
None => csl::MultiAsset::new(),
};
Expand Down
4 changes: 2 additions & 2 deletions packages/whisky-examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "whisky-examples"
version = "0.9.16"
version = "0.9.17"
edition = "2021"
license = "Apache-2.0"
description = "The Cardano Rust SDK, inspired by MeshJS"
Expand All @@ -13,4 +13,4 @@ path = "src/server.rs"
actix-cors = "0.7.0"
actix-web = "4.9.0"
serde = "1.0.209"
whisky = { version = "=0.9.16", path = "../whisky" }
whisky = { version = "=0.9.17", path = "../whisky" }
4 changes: 2 additions & 2 deletions packages/whisky/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "whisky"
version = "0.9.16"
version = "0.9.17"
edition = "2021"
license = "Apache-2.0"
description = "The Cardano Rust SDK, inspired by MeshJS"
Expand All @@ -24,7 +24,7 @@ pallas-codec = { version = "0.30.2", features = ["num-bigint"] }
pallas-primitives = "0.31.0"
pallas-traverse = "0.31.0"
maestro-rust-sdk = "1.1.3"
sidan-csl-rs = { version = "=0.9.16", path = "../sidan-csl-rs" }
sidan-csl-rs = { version = "0.9.17", path = "../sidan-csl-rs" }
reqwest = "0.12.5"
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }

Expand Down

0 comments on commit f7219b1

Please sign in to comment.