import { usage } from './usage' ; const PRIVATE_KEY_REGEX = /^0x[a-f\d]{64}$/i ; export function ensurePrivateKey ( value , message ) { ensureRegex ( PRIVATE_KEY_REGEX , value , message ) ; } const ADDRESS = /^0x[a-f\d]{40}$/i ; export function ensureAddress ( value , message ) { ensureRegex ( ADDRESS , value , message ) ; } const API_KEY_REGEX = /^[^\s]+$/ ; export function ensureApiKey ( value , message ) { ensureRegex ( API_KEY_REGEX , value , message ) ; } function ensureRegex ( regex , value , message ) { ensure ( ( value ) => typeof value === 'string' && regex . test ( value ) , value , message ) ; } export function ensureString ( value , message ) { ensure ( ( value ) => typeof value === 'string' , value , message ) ; } export function ensureNumber ( value , message ) { ensure ( ( value ) => typeof value === 'number' , value , message ) ; } export function ensureBoolean ( value , message ) { ensure ( ( value ) => typeof value === 'boolean' , value , message ) ; } const NETWORKS = [ 'development' , 'kovan' , 'ropsten' , 'goerli' , 'rinkeby' , 'mainnet' ] ; const URL_REGEX = /^https?:\/\/[^\s]+$/ ; function isProperNetwork ( value ) { return typeof value === 'string' && ( NETWORKS . includes ( value ) || URL_REGEX . test ( value ) ) ; } export function ensureNetwork ( value , message ) { ensure ( isProperNetwork , value , message ) ; } function ensure < T > ( check , value , message ) { if ( ! check ( value ) ) { exit ( message ) ; } } export function exit ( message ) { if ( process . env . NODE_ENV === 'test' ) { throw new Error ( message ) ; } console . error ( `Error: ${ message }` ) ; console . log ( usage ) ; console . error ( `Error: ${ message }` ) ; process . exit ( 1 ) ; }