whisky_pallas/utils/
staking.rs

1use whisky_common::WError;
2
3use crate::wrapper::transaction_body::RewardAccount;
4
5pub fn script_hash_to_stake_address(script_hash: &str, network_id: u8) -> Result<String, WError> {
6    let script_hash_bytes = hex::decode(script_hash).map_err(|e| {
7        WError::new(
8            "script_hash_to_stake_address - invalid script hash",
9            &format!("Hex decode error: {}", e.to_string()),
10        )
11    })?;
12    let header_byte: u8 = if network_id == 1 {
13        0b1111_0001 // Mainnet
14    } else {
15        0b1111_0000 // Testnet
16    };
17    // concat header byte and script hash bytes
18    let mut address_bytes = vec![header_byte];
19    address_bytes.extend(script_hash_bytes);
20    RewardAccount::from_bytes(&address_bytes)?.to_bech32()
21}