use crate::*;
use candid::Principal;
use dmsg_types::*;
use icrc_ledger_types::icrc1::account::Account;
pub fn charge_terms_digest(ledger: Principal, payer: &Account, amount: u128, fee: u128) -> Hash {
digest(
"dmsg/handle-charge/v1",
&(
ledger,
dmsg_types::account::account_cbor::value(payer),
amount,
fee,
),
)
}
pub fn normalize_handle(handle: &str) -> Result<String> {
ensure_valid(
!handle.is_empty() && handle.len() <= 19 && !handle.starts_with('_'),
"handle length/prefix",
)?;
ensure_valid(
handle
.bytes()
.all(|c| c.is_ascii_alphanumeric() || c == b'_'),
"handle characters",
)?;
Ok(handle.to_ascii_lowercase())
}
pub const HANDLE_BUCKET_BITS: u32 = 20;
pub fn handle_bucket(handle: &str) -> u32 {
let d = digest("dmsg/handle-bucket/v1", &handle);
u32::from_be_bytes(d.as_slice()[..4].try_into().expect("four bytes"))
>> (32 - HANDLE_BUCKET_BITS)
}
const PANDA: u128 = 100_000_000;
pub const MIN_HANDLE_PRICE: u128 = 100 * PANDA;
pub fn price(handle: &str) -> u128 {
match handle.len() {
1 => 1_000_000 * PANDA,
2 => 200_000 * PANDA,
3 | 4 => 50_000 * PANDA,
5 | 6 => 20_000 * PANDA,
_ => MIN_HANDLE_PRICE,
}
}
#[cfg(test)]
mod tests;