Functional Weave
Code in TypeScript

auth.recovery-codes@1.0.0

impl/rust/normalise_recovery_code.rs

1,148 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::auth_recovery_codes_generate_recovery_codes::RECOVERY_ALPHABET;  ← generateRecoveryCodes, another function of this group · built into the same file, even by a slim install

/// A typed code in its one stored form: ASCII lower case, spaces and hyphens
/// dropped, i/l read as 1 and o as 0 (Crockford). None when what is left is
/// not exactly 16 characters of the alphabet.
pub fn normalise_recovery_code(code: &str) -> Option<String> {
    let mut out = String::with_capacity(16);
    for raw in code.chars() {
        if raw == ' ' || raw == '-' {
            continue;
        }
        let mut ch = raw.to_ascii_lowercase();
        if ch == 'i' || ch == 'l' {
            ch = '1';
        } else if ch == 'o' {
            ch = '0';
        }
        if !RECOVERY_ALPHABET.contains(ch) {
            return None;
        }
        out.push(ch);
    }
    if out.len() == 16 {
        Some(out)
    } else {
        None
    }
}

pub fn fune_vector(args: &[Value]) -> Value {
    let code = match &args[0] {
        Value::Str(s) => s.clone(),
        _ => panic!("code must be a string"),
    };
    match normalise_recovery_code(&code) {
        Some(s) => Value::Str(s),
        None => Value::Null,
    }
}