use crate::constants::{
COLLATERAL_HASH, Config, MAINNET_STAKE_HASH, PREPROD_STAKE_HASH, get_config,
};
use pallas_addresses::{
Address, Network, PaymentKeyHash, ScriptHash, ShelleyAddress, ShelleyDelegationPart,
ShelleyPaymentPart, StakeKeyHash,
};
pub fn stake_key(network_flag: bool) -> &'static str {
if network_flag {
PREPROD_STAKE_HASH
} else {
MAINNET_STAKE_HASH
}
}
pub fn is_on_correct_network(addr: Address, network_flag: bool) -> bool {
if network_flag {
Address::network(&addr) == Some(Network::Testnet)
} else {
Address::network(&addr) == Some(Network::Mainnet)
}
}
pub fn is_not_a_script(addr: Address) -> bool {
!Address::has_script(&addr)
}
pub fn wallet_contract(network_flag: bool, variant: u64) -> Address {
let config: Config = get_config(variant, network_flag).unwrap_or_else(|| {
eprintln!("Error: Invalid Variant");
std::process::exit(1);
});
let shelly_wallet_address: ShelleyAddress = if network_flag {
ShelleyAddress::new(
Network::Testnet,
ShelleyPaymentPart::Script(ScriptHash::new(
hex::decode(config.contract.wallet_contract_hash)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
ShelleyDelegationPart::Key(StakeKeyHash::new(
hex::decode(PREPROD_STAKE_HASH)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
)
} else {
ShelleyAddress::new(
Network::Mainnet,
ShelleyPaymentPart::Script(ScriptHash::new(
hex::decode(config.contract.wallet_contract_hash)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
ShelleyDelegationPart::Key(StakeKeyHash::new(
hex::decode(MAINNET_STAKE_HASH)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
)
};
Address::from(shelly_wallet_address.clone())
}
pub fn collateral_address(network_flag: bool) -> Address {
let shelly_wallet_address: ShelleyAddress = if network_flag {
ShelleyAddress::new(
Network::Testnet,
ShelleyPaymentPart::Key(PaymentKeyHash::new(
hex::decode(COLLATERAL_HASH)
.unwrap()
.try_into()
.expect("Not Correct Length"),
)),
ShelleyDelegationPart::Null,
)
} else {
ShelleyAddress::new(
Network::Mainnet,
ShelleyPaymentPart::Key(PaymentKeyHash::new(
hex::decode(COLLATERAL_HASH)
.unwrap()
.try_into()
.expect("Not Correct Length"),
)),
ShelleyDelegationPart::Null,
)
};
Address::from(shelly_wallet_address.clone())
}
pub fn dapp_address(public_key: String, network_flag: bool) -> Address {
let shelly_wallet_address: ShelleyAddress = if network_flag {
ShelleyAddress::new(
Network::Testnet,
ShelleyPaymentPart::Key(PaymentKeyHash::new(
hex::decode(public_key)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
ShelleyDelegationPart::Key(StakeKeyHash::new(
hex::decode(PREPROD_STAKE_HASH)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
)
} else {
ShelleyAddress::new(
Network::Mainnet,
ShelleyPaymentPart::Key(PaymentKeyHash::new(
hex::decode(public_key)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
ShelleyDelegationPart::Key(StakeKeyHash::new(
hex::decode(MAINNET_STAKE_HASH)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
)
};
Address::from(shelly_wallet_address.clone())
}