From bb3eae1f17a5a1390a68c41e33f0d87e6a334135 Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Thu, 13 Jun 2024 11:55:12 +0800 Subject: [PATCH] feat: building script to addr --- packages/Cargo.lock | 4 +- packages/sidan-csl-rs/Cargo.toml | 2 +- .../sidan-csl-rs/src/core/utils/address.rs | 42 +++++++++++++++---- packages/sidan-csl-rs/tests/address_tests.rs | 19 ++++++++- packages/whisky/Cargo.toml | 4 +- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/packages/Cargo.lock b/packages/Cargo.lock index 52e0cd4..5c00af2 100644 --- a/packages/Cargo.lock +++ b/packages/Cargo.lock @@ -1833,7 +1833,7 @@ dependencies = [ [[package]] name = "sidan-csl-rs" -version = "0.5.3" +version = "0.5.4" dependencies = [ "cardano-serialization-lib", "cryptoxide", @@ -2387,7 +2387,7 @@ dependencies = [ [[package]] name = "whisky" -version = "0.5.3" +version = "0.5.4" dependencies = [ "async-trait", "cardano-serialization-lib", diff --git a/packages/sidan-csl-rs/Cargo.toml b/packages/sidan-csl-rs/Cargo.toml index 8e4273e..6d29c89 100644 --- a/packages/sidan-csl-rs/Cargo.toml +++ b/packages/sidan-csl-rs/Cargo.toml @@ -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" diff --git a/packages/sidan-csl-rs/src/core/utils/address.rs b/packages/sidan-csl-rs/src/core/utils/address.rs index a99b12e..2091566 100644 --- a/packages/sidan-csl-rs/src/core/utils/address.rs +++ b/packages/sidan-csl-rs/src/core/utils/address.rs @@ -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, + 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, diff --git a/packages/sidan-csl-rs/tests/address_tests.rs b/packages/sidan-csl-rs/tests/address_tests.rs index c1def53..d0fc8ab 100644 --- a/packages/sidan-csl-rs/tests/address_tests.rs +++ b/packages/sidan-csl-rs/tests/address_tests.rs @@ -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"; diff --git a/packages/whisky/Cargo.toml b/packages/whisky/Cargo.toml index 41db6d8..372e22b 100644 --- a/packages/whisky/Cargo.toml +++ b/packages/whisky/Cargo.toml @@ -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" @@ -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.