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

feat: building script to addr #36

Merged
merged 1 commit into from
Jun 13, 2024
Merged
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
4 changes: 2 additions & 2 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/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.5.3"
version = "0.5.4"
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
42 changes: 33 additions & 9 deletions packages/sidan-csl-rs/src/core/utils/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,44 @@ use serde_json::{from_str, Value};
use crate::{csl, model::*, *};

#[wasm_bindgen]
pub fn script_to_address(
pub fn wasm_script_to_address(
network_id: u8,
script_hash: String,
stake_hash: Option<String>,
is_script_stake_key: bool,
) -> String {
match stake_hash {
Some(stake) => csl::BaseAddress::new(
network_id,
&csl::Credential::from_scripthash(&csl::ScriptHash::from_hex(&script_hash).unwrap()),
&csl::Credential::from_keyhash(&csl::Ed25519KeyHash::from_hex(&stake).unwrap()),
)
.to_address()
.to_bech32(None)
.unwrap(),
Some(stake) => {
script_to_address(network_id, script_hash, Some((stake, is_script_stake_key)))
}
None => script_to_address(network_id, script_hash, None),
}
}

pub fn script_to_address(
network_id: u8,
script_hash: String,
stake_hash: Option<(String, bool)>,
) -> String {
match stake_hash {
Some((stake, is_script)) => {
let stake_cred = if is_script {
csl::Credential::from_scripthash(&csl::ScriptHash::from_hex(&stake).unwrap())
} else {
csl::Credential::from_keyhash(&csl::Ed25519KeyHash::from_hex(&stake).unwrap())
};

csl::BaseAddress::new(
network_id,
&csl::Credential::from_scripthash(
&csl::ScriptHash::from_hex(&script_hash).unwrap(),
),
&stake_cred,
)
.to_address()
.to_bech32(None)
.unwrap()
}

None => csl::EnterpriseAddress::new(
network_id,
Expand Down
19 changes: 18 additions & 1 deletion packages/sidan-csl-rs/tests/address_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@ mod address_tests {
let base_addr = script_to_address(
0,
"e55a6e7c9f4e96692a3c23a56f126911cc70a29d2e2ac967dc644432".to_string(),
Some("6d913965402b012050e09f12012c533e6c33678d1c5ed2154b328d25".to_string()),
Some((
"6d913965402b012050e09f12012c533e6c33678d1c5ed2154b328d25".to_string(),
false,
)),
);

assert!(base_addr == "addr_test1zrj45mnuna8fv6f28s362mcjdygucu9zn5hz4jt8m3jygvndjyuk2sptqys9pcylzgqjc5e7dsek0rgutmfp2jej35jseqau4y");
}

#[test]
fn test_script_to_address_script_stake_key() {
let base_addr = script_to_address(
0,
"c12e891c8e995cfa5d1547ace30413cad298827a19fbb8ea49b46469".to_string(),
Some((
"867c8b572e5ac8f0c14aa7417cb9caec9d1ff50e994f772eab2d69f4".to_string(),
true,
)),
);

assert!(base_addr == "addr_test1xrqjazgu36v4e7jaz4r6eccyz09d9xyz0gvlhw82fx6xg6vx0j94wtj6ercvzj48g97tnjhvn50l2r5efamja2edd86ql04h5v");
}

#[test]
fn test_serialize_address() {
let addr1 = "addr_test1qz8j439j54afpl4hw978xcw8qsa0dsmyd6wm9v8xzeyz7ucrj5rt3et7z59mvmmpxnejvn2scwmseezdq5h5fpw08z8s8d93my";
Expand Down
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.5.3"
version = "0.5.4"
edition = "2021"
license = "Apache-2.0"
description = "The Cardano Rust SDK, inspired by MeshJS"
Expand All @@ -24,7 +24,7 @@ noop_proc_macro = "0.3.0"
pallas-codec = "0.22.0"
pallas-primitives = "0.22.0"
pallas-traverse = "0.22.0"
sidan-csl-rs = { version = "=0.5.3", path = "../sidan-csl-rs" }
sidan-csl-rs = { version = "=0.5.4", path = "../sidan-csl-rs" }

[profile.release]
# Tell `rustc` to optimize for small code size.
Expand Down
Loading