Skip to main content

polyester/chain/
destination.rs

1//! Withdraw destination encoding matching TypeScript polyester-features.
2
3/// UTF-8 bytes of the normalized address (lowercase when not case-sensitive).
4///
5/// Matches TS `encodeWithdrawDestination` / `evmUtf8ToHex` (hex of UTF-8),
6/// not a 20-byte pubkey decode.
7pub fn encode_withdraw_destination(address: &str, is_case_sensitive: bool) -> Vec<u8> {
8    if is_case_sensitive {
9        address.as_bytes().to_vec()
10    } else {
11        address.to_ascii_lowercase().into_bytes()
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[test]
20    fn preserves_case_when_sensitive() {
21        let address = "Tb1QCaseSensitiveAddress";
22        assert_eq!(
23            encode_withdraw_destination(address, true),
24            address.as_bytes()
25        );
26    }
27
28    #[test]
29    fn lowercases_when_insensitive() {
30        let address = "0xABCDEFabcdefABCDEFabcdefABCDEFabcdefABCD";
31        assert_eq!(
32            encode_withdraw_destination(address, false),
33            b"0xabcdefabcdefabcdefabcdefabcdefabcdefabcd"
34        );
35    }
36}