use near_sdk::bs58; use thiserror::Error as ThisError; #[derive(Debug, ThisError, PartialEq, Eq)] pub enum ParseCurveError { #[error("wrong curve type")] WrongCurveType, #[error("base58: {0}")] Base58(#[from] bs58::decode::Error), #[error("invalid length")] InvalidLength, } /// Decodes input as base58 into array and checks for its length pub fn checked_base58_decode_array( input: impl AsRef<[u8]>, ) -> Result<[u8; N], ParseCurveError> { let mut output = [0u8; N]; let n = bs58::decode(input.as_ref()) // NOTE: `.into_array_const()` doesn't return an error on insufficient // input length and pads the array with zeros .onto(&mut output)?; (n == N) .then_some(output) .ok_or(ParseCurveError::InvalidLength) }