diff --git a/packages/sidan-csl-rs/src/core/utils/address.rs b/packages/sidan-csl-rs/src/core/utils/address.rs index ec09a80..cceb387 100644 --- a/packages/sidan-csl-rs/src/core/utils/address.rs +++ b/packages/sidan-csl-rs/src/core/utils/address.rs @@ -1,3 +1,5 @@ +use serde_json::{from_str, Value}; + use crate::{csl, model::*, *}; #[wasm_bindgen] @@ -9,12 +11,8 @@ pub fn script_to_address( 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(), - ), + &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) @@ -22,9 +20,7 @@ pub fn script_to_address( None => csl::EnterpriseAddress::new( network_id, - &csl::Credential::from_scripthash( - &csl::ScriptHash::from_hex(&script_hash).unwrap(), - ), + &csl::Credential::from_scripthash(&csl::ScriptHash::from_hex(&script_hash).unwrap()), ) .to_address() .to_bech32(None) @@ -34,9 +30,8 @@ pub fn script_to_address( #[wasm_bindgen] pub fn serialize_bech32_address(bech32_addr: String) -> SerializedAddress { - let csl_address = csl::BaseAddress::from_address( - &csl::Address::from_bech32(&bech32_addr).unwrap(), - ); + let csl_address = + csl::BaseAddress::from_address(&csl::Address::from_bech32(&bech32_addr).unwrap()); match csl_address { Some(address) => { let csl_key_hash = address @@ -85,11 +80,61 @@ pub fn serialize_bech32_address(bech32_addr: String) -> SerializedAddress { } } -pub fn address_bech32_to_obj(_bech32: &str) {} +pub fn address_bech32_to_obj(bech32: &str) -> String { + let csl_address = csl::Address::from_bech32(bech32).unwrap(); + csl::PlutusData::from_address(&csl_address) + .unwrap() + .to_json(csl::PlutusDatumSchema::DetailedSchema) + .unwrap() +} + +pub fn parse_plutus_address_obj_to_bech32(plutus_data_address_obj: &str, network_id: u8) -> String { + let plutus_data_address: Value = + from_str(&plutus_data_address_obj).expect("Invalid json string"); + let plutus_data_key_obj = plutus_data_address.get("fields").unwrap(); + let plutus_data_key_list = plutus_data_key_obj.as_array().unwrap(); + + let plutus_data_payment_key_obj = &plutus_data_key_list[0]; + let plutus_data_stake_key_obj = &plutus_data_key_list[1]; + + let payment_key_hash = plutus_data_payment_key_obj["fields"][0]["bytes"] + .as_str() + .unwrap(); + + let csl_payment_credential = + if plutus_data_payment_key_obj["constructor"].as_u64().unwrap() == 0 { + csl::Credential::from_keyhash(&csl::Ed25519KeyHash::from_hex(payment_key_hash).unwrap()) + } else { + csl::Credential::from_scripthash(&csl::ScriptHash::from_hex(payment_key_hash).unwrap()) + }; + + if plutus_data_stake_key_obj["constructor"].as_u64().unwrap() == 0 { + let stake_key_hash = plutus_data_stake_key_obj["fields"][0]["fields"][0]["fields"][0] + ["bytes"] + .as_str() + .unwrap(); + csl::BaseAddress::new( + network_id, + &csl_payment_credential, + &csl::Credential::from_keyhash(&csl::Ed25519KeyHash::from_hex(stake_key_hash).unwrap()), + ) + .to_address() + .to_bech32(None) + .unwrap() + } else { + csl::EnterpriseAddress::new(network_id, &csl_payment_credential) + .to_address() + .to_bech32(None) + .unwrap() + } +} -// export const addrBech32ToObj = (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; -// }; +pub fn parse_plutus_address_to_bech32(plutus_hex: &str, network_id: u8) -> String { + let csl_plutus_data_address = csl::PlutusData::from_hex(plutus_hex).unwrap(); + parse_plutus_address_obj_to_bech32( + &csl_plutus_data_address + .to_json(csl::PlutusDatumSchema::DetailedSchema) + .unwrap(), + network_id, + ) +} \ No newline at end of file