Skip to main content

generate_key_file

Function generate_key_file 

Source
pub fn generate_key_file(path: &Path) -> Result<NostrSecretKey, KeyFileError>
Expand description

Creates a new operator key file holding a freshly generated key.

The key is 32 bytes from the operating system’s random source (getrandom), written as one NIP-19 nsec1… line followed by a newline. The file is created exclusively with mode 0600 set at creation, so an existing file, or a symbolic link at path, is never overwritten or followed. The returned key’s public principal is the operator’s DID; the secret is never returned in text form.

§Examples

use stratum_node_cli::{generate_key_file, load_key_file};

let dir = std::env::temp_dir().join(format!("stratum-doc-{}", std::process::id()));
std::fs::create_dir_all(&dir)?;
let path = dir.join("operator.nsec");
let key = generate_key_file(&path)?;
assert_eq!(load_key_file(&path)?.public(), key.public());
assert!(generate_key_file(&path).is_err(), "never overwrites");

§Errors

Returns KeyFileError::File with PrivateFileError::AlreadyExists when anything exists at path, another KeyFileError::File for an operating-system failure (nothing is left behind), and KeyFileError::RandomUnavailable when the random source fails.