Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
twwu123 committed Jan 29, 2024
2 parents d2072c0 + 736e8c7 commit cd8e4af
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 39 deletions.
2 changes: 1 addition & 1 deletion 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sidan-csl-rs"
version = "0.1.5"
version = "0.1.8"
edition = "2021"
license = "MIT"
description = "Wrapper around the cardano-serialization-lib for easier transaction building, heavily inspired by cardano-cli APIs"
Expand Down
2 changes: 1 addition & 1 deletion src/builder/core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cardano_serialization_lib as csl;

use crate::{
builder::models::*,
model::builder::*,
utils::csl::{build_tx_builder, to_bignum, to_value},
};

Expand Down
1 change: 0 additions & 1 deletion src/builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod core;
pub mod models;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod builder;
pub mod model;
pub mod utils;
File renamed without changes.
1 change: 1 addition & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod builder;
43 changes: 31 additions & 12 deletions src/utils/csl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cardano_serialization_lib as csl;

use crate::builder::models::*;
use crate::model::builder::*;
use hex::FromHex;

pub fn to_bignum(val: u64) -> csl::utils::BigNum {
Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn script_to_address(
}
}

pub fn serialize_bech32_script_address(bech32_addr: String) -> SerializedAddress {
pub fn serialize_bech32_address(bech32_addr: String) -> SerializedAddress {
let csl_address = csl::address::BaseAddress::from_address(
&csl::address::Address::from_bech32(&bech32_addr).unwrap(),
);
Expand All @@ -102,22 +102,22 @@ pub fn serialize_bech32_script_address(bech32_addr: String) -> SerializedAddress
let csl_key_hash = address
.payment_cred()
.to_keyhash()
.map_or("".to_string(), |key_hash| key_hash.to_hex());
.map(|key_hash| key_hash.to_hex());

let csl_script_hash = address
.payment_cred()
.to_scripthash()
.map_or("".to_string(), |script_hash| script_hash.to_hex());
.map(|script_hash| script_hash.to_hex());

let csl_stake_key_hash = address
.stake_cred()
.to_keyhash()
.map_or("".to_string(), |stake_key_hash| stake_key_hash.to_hex());
.map(|stake_key_hash| stake_key_hash.to_hex());

SerializedAddress {
pub_key_hash: csl_key_hash,
script_hash: csl_script_hash,
stake_key_hash: csl_stake_key_hash,
pub_key_hash: csl_key_hash.unwrap_or("".to_string()),
script_hash: csl_script_hash.unwrap_or("".to_string()),
stake_key_hash: csl_stake_key_hash.unwrap_or("".to_string()),
}
}
None => {
Expand All @@ -129,18 +129,37 @@ pub fn serialize_bech32_script_address(bech32_addr: String) -> SerializedAddress
let csl_key_hash = csl_enterprize_address
.payment_cred()
.to_keyhash()
.map_or("".to_string(), |key_hash| key_hash.to_hex());
.map(|key_hash| key_hash.to_hex());

let csl_script_hash = csl_enterprize_address
.payment_cred()
.to_scripthash()
.map_or("".to_string(), |script_hash| script_hash.to_hex());
.map(|script_hash| script_hash.to_hex());

SerializedAddress {
pub_key_hash: csl_key_hash,
script_hash: csl_script_hash,
pub_key_hash: csl_key_hash.unwrap_or("".to_string()),
script_hash: csl_script_hash.unwrap_or("".to_string()),
stake_key_hash: "".to_string(),
}
}
}
}

pub fn get_v2_script_hash(script: &str) -> String {
csl::plutus::PlutusScript::from_hex_with_version(
script,
&csl::plutus::Language::new_plutus_v2(),
)
.unwrap()
.hash()
.to_hex()
}

pub fn address_bech32_to_obj(bech32: &str) {}

// export const addrBech32ToObj = <T>(bech32: string): T => {
// const hexAddress = csl.Address.from_bech32(bech32).to_hex();
// const cslAddress = csl.Address.from_hex(hexAddress);
// const json = JSON.parse(csl.PlutusData.from_address(cslAddress).to_json(1));
// return json;
// };
3 changes: 2 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod aiken;
pub mod csl;
pub mod parser;
pub mod utxo_selection;
pub mod utxo_selection;
pub mod plutus_types;
240 changes: 240 additions & 0 deletions src/utils/plutus_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
use serde_json::{json, Value};

pub fn con_str<N: Into<Value>, T: Into<Value>>(constructor: N, fields: T) -> Value {
json!({ "constructor": constructor.into(), "fields": fields.into() })
}

pub fn con_str0<T: Into<Value>>(fields: T) -> Value {
con_str(0, fields)
}

pub fn con_str1<T: Into<Value>>(fields: T) -> Value {
con_str(1, fields)
}

pub fn con_str2<T: Into<Value>>(fields: T) -> Value {
con_str(2, fields)
}

pub fn bool(b: bool) -> Value {
if b {
con_str1(json!([]))
} else {
con_str0(json!([]))
}
}

pub fn builtin_byte_string(bytes: &str) -> Value {
json!({ "bytes": bytes })
}

pub fn integer(int: i32) -> Value {
json!({ "int": int })
}

pub fn list<T: Into<Value>>(p_list: Vec<T>) -> Value {
let list: Vec<Value> = p_list.into_iter().map(|item| item.into()).collect();
json!({ "list": list })
}

// Other functions like currencySymbol, tokenName, etc., would create JSON objects
pub fn currency_symbol(policy_id: &str) -> Value {
builtin_byte_string(policy_id)
}

pub fn token_name(token_name: &str) -> Value {
builtin_byte_string(token_name)
}

pub fn maybe_staking_hash(stake_credential: &str) -> Value {
if stake_credential.is_empty() {
con_str1(json!([]))
} else {
con_str0(vec![con_str0(vec![con_str0(vec![builtin_byte_string(
stake_credential,
)])])])
}
}

pub fn pub_key_address(bytes: &str, stake_credential: Option<&str>) -> Value {
con_str0(vec![
con_str0(vec![builtin_byte_string(bytes)]),
maybe_staking_hash(stake_credential.unwrap_or("")),
])
}

pub fn script_address(bytes: &str, stake_credential: Option<&str>) -> Value {
con_str0(vec![
con_str1(vec![builtin_byte_string(bytes)]),
maybe_staking_hash(stake_credential.unwrap_or("")),
])
}

pub fn asset_class(policy_id: &str, asset_name: &str) -> Value {
con_str0(vec![currency_symbol(policy_id), token_name(asset_name)])
}

pub fn tx_out_ref(tx_hash: &str, index: i32) -> Value {
con_str0(vec![
con_str0(vec![builtin_byte_string(tx_hash)]),
integer(index),
])
}

pub fn assoc_map<K: Into<Value>, V: Into<Value>>(items_map: Vec<(K, V)>) -> Value {
let map: Vec<Value> = items_map
.into_iter()
.map(|(k, v)| json!({"k": k.into(), "v": v.into()}))
.collect();
json!({ "map": map })
}

pub fn tuple<K: Into<Value>, V: Into<Value>>(key: K, value: V) -> Value {
con_str0(vec![key.into(), value.into()])
}

pub fn payment_pub_key_hash(pub_key_hash: &str) -> Value {
builtin_byte_string(pub_key_hash)
}

pub fn pub_key_hash(pub_key_hash: &str) -> Value {
builtin_byte_string(pub_key_hash)
}

pub fn posix_time(posix_time: i32) -> Value {
integer(posix_time)
}

#[test]
fn test_con_str() {
let correct_con_str = "{\"constructor\":10,\"fields\":[{\"bytes\":\"hello\"}]}";
assert_eq!(
con_str(10, json!([builtin_byte_string("hello")])).to_string(),
correct_con_str
);
}

#[test]
fn test_con_str0() {
let correct_con_str0 = "{\"constructor\":0,\"fields\":{\"bytes\":\"hello\"}}";
assert_eq!(
con_str0(builtin_byte_string("hello")).to_string(),
correct_con_str0
);
}

#[test]
fn test_con_str1() {
let correct_con_str1 = "{\"constructor\":1,\"fields\":{\"bytes\":\"hello\"}}";
assert_eq!(
con_str1(builtin_byte_string("hello")).to_string(),
correct_con_str1
);
}

#[test]
fn test_con_str2() {
let correct_con_str2 = "{\"constructor\":2,\"fields\":{\"bytes\":\"hello\"}}";
assert_eq!(
con_str2(builtin_byte_string("hello")).to_string(),
correct_con_str2
);
}

#[test]
fn test_bool() {
let correct_bool = "{\"constructor\":1,\"fields\":[]}";
assert_eq!(bool(true).to_string(), correct_bool);
}

#[test]
fn test_builtin_byte_string() {
let correct_builtin_byte_string = "{\"bytes\":\"hello\"}";
assert_eq!(
builtin_byte_string("hello").to_string(),
correct_builtin_byte_string
);
}

#[test]
fn test_integer() {
let correct_integer = "{\"int\":1}";
assert_eq!(integer(1).to_string(), correct_integer);
}

#[test]
fn test_list() {
let correct_list = "{\"list\":[1,2,3]}";
assert_eq!(list(vec![1, 2, 3]).to_string(), correct_list);
}

#[test]
fn test_maybe_staking_hash() {
let correct_maybe_staking_hash = "{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"bytes\":\"hello\"}]}]}]}";
assert_eq!(
maybe_staking_hash("hello").to_string(),
correct_maybe_staking_hash
);
}

#[test]
fn test_pub_key_address() {
let correct_pub_key_address = "{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"bytes\":\"8f2ac4b2a57a90feb7717c7361c7043af6c3646e9db2b0e616482f73\"}]},{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"bytes\":\"039506b8e57e150bb66f6134f3264d50c3b70ce44d052f4485cf388f\"}]}]}]}]}";
assert_eq!(
pub_key_address(
"8f2ac4b2a57a90feb7717c7361c7043af6c3646e9db2b0e616482f73",
Some("039506b8e57e150bb66f6134f3264d50c3b70ce44d052f4485cf388f")
)
.to_string(),
correct_pub_key_address
);
}

#[test]
fn test_script_address() {
let correct_script_address = "{\"constructor\":0,\"fields\":[{\"constructor\":1,\"fields\":[{\"bytes\":\"hello\"}]},{\"constructor\":1,\"fields\":[]}]}";
assert_eq!(
script_address("hello", None).to_string(),
correct_script_address
);
}

#[test]
fn test_asset_class() {
let correct_asset_class =
"{\"constructor\":0,\"fields\":[{\"bytes\":\"hello\"},{\"bytes\":\"world\"}]}";
assert_eq!(
asset_class("hello", "world").to_string(),
correct_asset_class
);
}

#[test]
fn test_tx_out_ref() {
let correct_tx_out_ref = "{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"bytes\":\"hello\"}]},{\"int\":12}]}";
assert_eq!(tx_out_ref("hello", 12).to_string(), correct_tx_out_ref);
}

#[test]
fn test_assoc_map() {
let correct_assoc_map =
"{\"map\":[{\"k\":{\"bytes\":\"hello\"},\"v\":{\"bytes\":\"world\"}},{\"k\":{\"bytes\":\"123\"},\"v\":{\"bytes\":\"456\"}}]}";
assert_eq!(
assoc_map(vec![
(builtin_byte_string("hello"), builtin_byte_string("world")),
(builtin_byte_string("123"), builtin_byte_string("456"))
])
.to_string(),
correct_assoc_map
);
}

#[test]
fn test_tuple() {
let correct_tuple =
"{\"constructor\":0,\"fields\":[{\"bytes\":\"hello\"},{\"bytes\":\"world\"}]}";
assert_eq!(
tuple(builtin_byte_string("hello"), builtin_byte_string("world")).to_string(),
correct_tuple
);
}
2 changes: 1 addition & 1 deletion src/utils/utxo_selection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::builder::models::*;
use crate::model::builder::*;
use std::collections::{HashMap, HashSet};

pub fn select_utxos(
Expand Down
Loading

0 comments on commit cd8e4af

Please sign in to comment.