impl/rust/hash_recovery_code.rs
976 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_normalise_recovery_code::normalise_recovery_code; ← normaliseRecoveryCode, another function of this group · built into the same file, even by a slim install
use super::crypto_sha256::sha256; ← from crypto.sha256 ^1.0.0 · built alongside by fune
use super::encoding_hex_hex_encode::hex_encode;
use super::encoding_utf8_utf8_encode::utf8_encode;
/// The hex SHA-256 of "fune.recovery-code:" and the normalised code: what to
/// store. A fast unsalted hash is enough because each code carries 80 random
/// bits.
///
/// # Panics
/// Panics with "not a recovery code" when the code cannot be normalised.
pub fn hash_recovery_code(code: &str) -> String {
let normal = match normalise_recovery_code(code) {
Some(n) => n,
None => panic!("not a recovery code"),
};
hex_encode(&sha256(&utf8_encode(&format!("fune.recovery-code:{}", normal))))
}
pub fn fune_vector(args: &[Value]) -> Value {
let code = match &args[0] {
Value::Str(s) => s.clone(),
_ => panic!("code must be a string"),
};
Value::Str(hash_recovery_code(&code))
}