#![allow(
dead_code,
reason = "each integration-test binary uses a different subset of these helpers"
)]
use bech32::{ByteIterExt, Fe32, Fe32IterExt, Hrp};
pub(crate) fn pq_recipients() -> Vec<&'static str> {
include_str!("../data/age-pq-recipients.txt")
.lines()
.filter(|l| !l.is_empty())
.collect()
}
pub(crate) fn pq(n: usize) -> &'static str {
pq_recipients()[n]
}
pub(crate) const CLASSICAL_RECIPIENT: &str =
"age1t7rxyev2z3rw82stdlrrepyc39nvn86l5078zqkf5uasdy86jp6svpy7pa";
enum Bech32Long {}
impl bech32::Checksum for Bech32Long {
type MidstateRepr = u32;
type CorrectionField = <bech32::Bech32 as bech32::Checksum>::CorrectionField;
const ROOT_GENERATOR: Self::CorrectionField = bech32::Bech32::ROOT_GENERATOR;
const ROOT_EXPONENTS: core::ops::RangeInclusive<usize> = bech32::Bech32::ROOT_EXPONENTS;
const CODE_LENGTH: usize = usize::MAX;
const CHECKSUM_LENGTH: usize = bech32::Bech32::CHECKSUM_LENGTH;
const GENERATOR_SH: [u32; 5] = bech32::Bech32::GENERATOR_SH;
const TARGET_RESIDUE: u32 = bech32::Bech32::TARGET_RESIDUE;
}
pub(crate) fn payload_of(recipient: &str) -> Vec<u8> {
bech32::primitives::decode::CheckedHrpstring::new::<Bech32Long>(recipient)
.expect("fixture is valid bech32")
.byte_iter()
.collect()
}
pub(crate) fn encode(hrp: &str, payload: &[u8]) -> String {
let hrp = Hrp::parse(hrp).expect("test hrp is valid");
bech32::encode_lower::<Bech32Long>(hrp, payload).expect("no length limit")
}
pub(crate) fn encode_with_nonzero_padding(payload: &[u8]) -> String {
let hrp = Hrp::parse("age1pq").expect("valid hrp");
let mut fes: Vec<Fe32> = payload.iter().copied().bytes_to_fes().collect();
let last = fes.pop().expect("payload is not empty");
let flipped = Fe32::try_from(last.to_u8() | 0b1).expect("value below 32");
assert_ne!(flipped, last, "padding bit must change");
fes.push(flipped);
fes.into_iter()
.with_checksum::<Bech32Long>(&hrp)
.chars()
.collect()
}