seedelf_cli/address.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
use crate::constants::{
COLLATERAL_HASH, MAINNET_STAKE_HASH, PREPROD_STAKE_HASH, WALLET_CONTRACT_HASH,
};
use pallas_addresses::{
Address, Network, PaymentKeyHash, ScriptHash, ShelleyAddress, ShelleyDelegationPart,
ShelleyPaymentPart, StakeKeyHash,
};
/// Determines whether the given address belongs to the correct Cardano network.
///
/// Checks if the provided address matches the expected network based on the `network_flag`.
///
/// # Arguments
///
/// * `addr` - A Cardano address to verify.
/// * `network_flag` - A boolean flag indicating the expected network:
/// - `true` checks for Testnet.
/// - `false` checks for Mainnet.
///
/// # Returns
///
/// * `true` if the address matches the expected network.
/// * `false` otherwise.
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)
}
}
/// Determines whether the given address is not a script address.
///
/// This function checks if the provided Cardano address does not contain a script.
///
/// # Arguments
///
/// * `addr` - A Cardano address to verify.
///
/// # Returns
///
/// * `true` if the address does not contain a script.
/// * `false` if the address contains a script.
pub fn is_not_a_script(addr: Address) -> bool {
!Address::has_script(&addr)
}
/// Generates the wallet contract address for the specified Cardano network.
///
/// This function constructs a Shelley address for the wallet contract based on the given `network_flag`.
/// If the flag indicates Testnet, the Testnet network and pre-production stake hash are used.
/// Otherwise, the Mainnet network and stake hash are used.
///
/// # Arguments
///
/// * `network_flag` - A boolean flag specifying the network:
/// - `true` for Testnet.
/// - `false` for Mainnet.
///
/// # Returns
///
/// * `Address` - The wallet contract address for the specified network.
pub fn wallet_contract(network_flag: bool) -> Address {
// Construct the Shelley wallet address based on the network flag.
let shelly_wallet_address: ShelleyAddress = if network_flag {
ShelleyAddress::new(
Network::Testnet,
ShelleyPaymentPart::Script(ScriptHash::new(
hex::decode(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(WALLET_CONTRACT_HASH)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
ShelleyDelegationPart::Key(StakeKeyHash::new(
hex::decode(MAINNET_STAKE_HASH)
.unwrap()
.try_into()
.expect("Incorrect Length"),
)),
)
};
// we need this as the address type and not the shelley
Address::from(shelly_wallet_address.clone())
}
/// Generates a collateral address for the specified Cardano network.
///
/// This function creates a Shelley address for collateral purposes. The address is not staked,
/// meaning it has a `Null` delegation part. The `network_flag` determines whether the address
/// is for the Testnet or Mainnet.
///
/// # Arguments
///
/// * `network_flag` - A boolean flag specifying the network:
/// - `true` for Testnet.
/// - `false` for Mainnet.
///
/// # Returns
///
/// * `Address` - The collateral address for the specified network.
pub fn collateral_address(network_flag: bool) -> Address {
// Construct the Shelley wallet address based on the network flag.
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,
)
};
// Convert the Shelley address to the generic Address type.
Address::from(shelly_wallet_address.clone())
}