seedelf_cli/
address.rs

1use crate::constants::{
2    COLLATERAL_HASH, Config, MAINNET_STAKE_HASH, PREPROD_STAKE_HASH, get_config,
3};
4use pallas_addresses::{
5    Address, Network, PaymentKeyHash, ScriptHash, ShelleyAddress, ShelleyDelegationPart,
6    ShelleyPaymentPart, StakeKeyHash,
7};
8
9/// Returns the [LOGIC] stake used during address generation
10///
11/// /// # Arguments
12///
13/// * `network_flag` - A boolean flag indicating the expected network:
14///    - `true` checks for Testnet.
15///    - `false` checks for Mainnet.
16///
17/// /// # Returns
18///
19/// * `str` - The stake key in hex.
20pub fn stake_key(network_flag: bool) -> &'static str {
21    if network_flag {
22        PREPROD_STAKE_HASH
23    } else {
24        MAINNET_STAKE_HASH
25    }
26}
27
28/// Determines whether the given address belongs to the correct Cardano network.
29///
30/// Checks if the provided address matches the expected network based on the `network_flag`.
31///
32/// # Arguments
33///
34/// * `addr` - A Cardano address to verify.
35/// * `network_flag` - A boolean flag indicating the expected network:
36///    - `true` checks for Testnet.
37///    - `false` checks for Mainnet.
38///
39/// # Returns
40///
41/// * `true` if the address matches the expected network.
42/// * `false` otherwise.
43pub fn is_on_correct_network(addr: Address, network_flag: bool) -> bool {
44    if network_flag {
45        Address::network(&addr) == Some(Network::Testnet)
46    } else {
47        Address::network(&addr) == Some(Network::Mainnet)
48    }
49}
50
51/// Determines whether the given address is not a script address.
52///
53/// This function checks if the provided Cardano address does not contain a script.
54///
55/// # Arguments
56///
57/// * `addr` - A Cardano address to verify.
58///
59/// # Returns
60///
61/// * `true` if the address does not contain a script.
62/// * `false` if the address contains a script.
63pub fn is_not_a_script(addr: Address) -> bool {
64    !Address::has_script(&addr)
65}
66
67/// Generates the wallet contract address for the specified Cardano network.
68///
69/// This function constructs a Shelley address for the wallet contract based on the given `network_flag`.
70/// If the flag indicates Testnet, the Testnet network and pre-production stake hash are used.
71/// Otherwise, the Mainnet network and stake hash are used.
72///
73/// # Arguments
74///
75/// * `network_flag` - A boolean flag specifying the network:
76///    - `true` for Testnet.
77///    - `false` for Mainnet.
78///
79/// # Returns
80///
81/// * `Address` - The wallet contract address for the specified network.
82pub fn wallet_contract(network_flag: bool, variant: u64) -> Address {
83    let config: Config = get_config(variant, network_flag).unwrap_or_else(|| {
84        eprintln!("Error: Invalid Variant");
85        std::process::exit(1);
86    });
87
88    // Construct the Shelley wallet address based on the network flag.
89    let shelly_wallet_address: ShelleyAddress = if network_flag {
90        ShelleyAddress::new(
91            Network::Testnet,
92            ShelleyPaymentPart::Script(ScriptHash::new(
93                hex::decode(config.contract.wallet_contract_hash)
94                    .unwrap()
95                    .try_into()
96                    .expect("Incorrect Length"),
97            )),
98            ShelleyDelegationPart::Key(StakeKeyHash::new(
99                hex::decode(PREPROD_STAKE_HASH)
100                    .unwrap()
101                    .try_into()
102                    .expect("Incorrect Length"),
103            )),
104        )
105    } else {
106        ShelleyAddress::new(
107            Network::Mainnet,
108            ShelleyPaymentPart::Script(ScriptHash::new(
109                hex::decode(config.contract.wallet_contract_hash)
110                    .unwrap()
111                    .try_into()
112                    .expect("Incorrect Length"),
113            )),
114            ShelleyDelegationPart::Key(StakeKeyHash::new(
115                hex::decode(MAINNET_STAKE_HASH)
116                    .unwrap()
117                    .try_into()
118                    .expect("Incorrect Length"),
119            )),
120        )
121    };
122    // we need this as the address type and not the shelley
123    Address::from(shelly_wallet_address.clone())
124}
125
126/// Generates a collateral address for the specified Cardano network.
127///
128/// This function creates a Shelley address for collateral purposes. The address is not staked,
129/// meaning it has a `Null` delegation part. The `network_flag` determines whether the address
130/// is for the Testnet or Mainnet.
131///
132/// # Arguments
133///
134/// * `network_flag` - A boolean flag specifying the network:
135///    - `true` for Testnet.
136///    - `false` for Mainnet.
137///
138/// # Returns
139///
140/// * `Address` - The collateral address for the specified network.
141pub fn collateral_address(network_flag: bool) -> Address {
142    // Construct the Shelley wallet address based on the network flag.
143    let shelly_wallet_address: ShelleyAddress = if network_flag {
144        ShelleyAddress::new(
145            Network::Testnet,
146            ShelleyPaymentPart::Key(PaymentKeyHash::new(
147                hex::decode(COLLATERAL_HASH)
148                    .unwrap()
149                    .try_into()
150                    .expect("Not Correct Length"),
151            )),
152            ShelleyDelegationPart::Null,
153        )
154    } else {
155        ShelleyAddress::new(
156            Network::Mainnet,
157            ShelleyPaymentPart::Key(PaymentKeyHash::new(
158                hex::decode(COLLATERAL_HASH)
159                    .unwrap()
160                    .try_into()
161                    .expect("Not Correct Length"),
162            )),
163            ShelleyDelegationPart::Null,
164        )
165    };
166    // Convert the Shelley address to the generic Address type.
167    Address::from(shelly_wallet_address.clone())
168}
169
170pub fn dapp_address(public_key: String, network_flag: bool) -> Address {
171    // Construct the Shelley wallet address based on the network flag.
172    let shelly_wallet_address: ShelleyAddress = if network_flag {
173        ShelleyAddress::new(
174            Network::Testnet,
175            ShelleyPaymentPart::Key(PaymentKeyHash::new(
176                hex::decode(public_key)
177                    .unwrap()
178                    .try_into()
179                    .expect("Incorrect Length"),
180            )),
181            ShelleyDelegationPart::Key(StakeKeyHash::new(
182                hex::decode(PREPROD_STAKE_HASH)
183                    .unwrap()
184                    .try_into()
185                    .expect("Incorrect Length"),
186            )),
187        )
188    } else {
189        ShelleyAddress::new(
190            Network::Mainnet,
191            ShelleyPaymentPart::Key(PaymentKeyHash::new(
192                hex::decode(public_key)
193                    .unwrap()
194                    .try_into()
195                    .expect("Incorrect Length"),
196            )),
197            ShelleyDelegationPart::Key(StakeKeyHash::new(
198                hex::decode(MAINNET_STAKE_HASH)
199                    .unwrap()
200                    .try_into()
201                    .expect("Incorrect Length"),
202            )),
203        )
204    };
205    // we need this as the address type and not the shelley
206    Address::from(shelly_wallet_address.clone())
207}