import fs from "fs" ; import { stripOffNewlines } from "./stripOffNewlines" ; import { writeFile600Perm } from "./fs" ; import { getValidatorPassphrasePath } from "../validatorDir/paths" ; /**
 * Utility to read file as UTF8 and strip any trailing new lines.
 * All passphrase files must be read with this function
 */ export function readPassphraseFile ( passphraseFile ) { const data = fs . readFileSync ( passphraseFile , "utf8" ) ; const passphrase = stripOffNewlines ( data ) ; try { if ( passphrase . includes ( "\n" ) ) throw Error ( "contains multiple lines" ) ; if ( passphrase . length > 512 ) throw Error ( "is really long" ) ; } catch ( e ) { throw new Error ( `passphraseFile ${ passphraseFile } ${ ( e as Error ) . message }. Is this a well-formated passphraseFile?` ) ; } return passphrase ; } export function readValidatorPassphrase ( { secretsDir , pubkey } ) { const notPrefixedPath = getValidatorPassphrasePath ( { secretsDir , pubkey } ) ; const prefixedPath = getValidatorPassphrasePath ( { secretsDir , pubkey , prefixed : true } ) ; if ( fs . existsSync ( notPrefixedPath ) ) { return readPassphraseFile ( notPrefixedPath ) ; } else { return readPassphraseFile ( prefixedPath ) ; } } export function writeValidatorPassphrase ( { secretsDir , pubkey , passphrase , } ) { writeFile600Perm ( getValidatorPassphrasePath ( { secretsDir , pubkey , prefixed : true } ) , passphrase ) ; }