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

add more address utils for plutus scripts #24

Merged
merged 2 commits into from
May 23, 2024
Merged
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
83 changes: 64 additions & 19 deletions packages/sidan-csl-rs/src/core/utils/address.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use serde_json::{from_str, Value};

use crate::{csl, model::*, *};

#[wasm_bindgen]
Expand All @@ -9,22 +11,16 @@ 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)
.unwrap(),

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)
Expand All @@ -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
Expand Down Expand Up @@ -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 = <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;
// };
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,
)
}
Loading