Skip to content

Commit

Permalink
feat: adding parser utils
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Jan 20, 2024
1 parent 23b0b30 commit 07f8d91
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/utils/csl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn build_tx_builder() -> csl::tx_builder::TransactionBuilder {
))
.build()
.unwrap();
return csl::tx_builder::TransactionBuilder::new(&cfg);
csl::tx_builder::TransactionBuilder::new(&cfg)
}

pub fn to_value(assets: &Vec<Asset>) -> csl::utils::Value {
Expand All @@ -36,15 +36,15 @@ pub fn to_value(assets: &Vec<Asset>) -> csl::utils::Value {
continue;
}
let mut policy_assets = csl::Assets::new();
let name_bytes = Vec::<u8>::from_hex(asset.unit[56..].to_string())
.expect("Failed to parse hex asset name");
let name_bytes =
Vec::<u8>::from_hex(&asset.unit[56..]).expect("Failed to parse hex asset name");
policy_assets.insert(
&csl::AssetName::new(name_bytes).unwrap(),
&csl::utils::BigNum::from_str(&asset.quantity.to_string()).unwrap(),
);

multi_asset.insert(
&csl::crypto::ScriptHash::from_hex(&asset.unit[0..56].to_string()).unwrap(),
&csl::crypto::ScriptHash::from_hex(&asset.unit[0..56]).unwrap(),
&policy_assets,
);
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod aiken;
pub mod csl;
pub mod parser;
40 changes: 40 additions & 0 deletions src/utils/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use hex;

pub fn bytes_to_hex(bytes: &[u8]) -> String {
hex::encode(bytes)
}

#[test]
fn test_bytes_to_hex() {
let bytes = vec![0, 1, 2, 3, 4, 5];
assert_eq!(bytes_to_hex(&bytes), "000102030405");
}

pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, hex::FromHexError> {
hex::decode(hex)
}

#[test]
fn test_hex_to_bytes() {
let bytes = vec![0, 1, 2, 3, 4, 255];
assert_eq!(hex_to_bytes("0001020304ff").unwrap(), bytes);
}

pub fn string_to_hex(s: &str) -> String {
hex::encode(s)
}

#[test]
fn test_string_to_hex() {
assert_eq!(string_to_hex("DELTA"), "44454c5441");
}

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())
}

#[test]
fn test_hex_to_string() {
assert_eq!(hex_to_string("44454c5441").unwrap(), "DELTA");
}

0 comments on commit 07f8d91

Please sign in to comment.