import { Serializable, spawn } from 'node:child_process'
import { getRandomValues } from 'node:crypto'
import { createInterface } from 'node:readline/promises'
-import { stats, Logger } from '#utils'
+import { isHex8, isHex32, Logger, stats } from '#utils'
import { WorkErrorResponse, WorkGenerateResponse, WorkValidateResponse } from '#types'
process.title = 'NanoPow CLI'
let i = 0
for await (const line of stdin) {
i++
- if (/^[A-Fa-f\d]{64}$/.test(line)) {
+ if (isHex32(line)) {
hashes.push(line)
} else {
stdinErrors.push(`Skipping invalid stdin input line ${i}`)
const inArgs: string[] = []
let isParsingHash: boolean = true
while (isParsingHash) {
- if (!/^[A-Fa-f\d]{16}$/.test(args[args.length - 1])) break
+ if (!isHex8(args[args.length - 1])) break
try {
inArgs.unshift(args.pop() as string)
} catch {
case ('-d'): {
const d = args[i + 1]
if (d == null) throw new Error('Missing argument for difficulty')
- if (!/^[A-Fa-f\d]{16}$/.test(d)) throw new Error('Invalid argument for difficulty')
+ if (!isHex8(d)) throw new Error('Invalid argument for difficulty')
body.difficulty = d
break
}
case ('-v'): {
const v = args[i + 1]
if (v == null) throw new Error('Missing argument for work validation')
- if (!/^[A-Fa-f\d]{16}$/.test(v)) throw new Error('Invalid argument for work validation')
+ if (!isHex8(v)) throw new Error('Invalid argument for work validation')
if (hashes.length !== 1) throw new Error('Validate accepts exactly one hash')
body.action = 'work_validate'
body.work = v
import { join } from 'node:path'
import { launch } from 'puppeteer'
import { WorkErrorResponse, WorkGenerateResponse, WorkValidateResponse } from '#types'
-import { Logger } from '#utils'
+import { isHex8, isHex32, Logger } from '#utils'
/**
* Used to define NanoPow server configuration.
}
resBody = `${action} failed`
- if (typeof hash !== 'string' || !/[A-Fa-f\d]{64}/.test(hash)) {
+ if (!isHex32(hash)) {
LOG: logger.log('Hash must be hex char(64)')
throw new Error(resBody)
}
- if (difficulty !== undefined
- && (typeof difficulty !== 'string'
- || !/^[A-Fa-f\d]{16}$/.test(difficulty))
- ) {
+ if (difficulty !== undefined && !isHex8(difficulty)) {
LOG: logger.log('Difficulty must be hex char(16).')
throw new Error(resBody)
}
if (action === 'work_validate') {
- if (typeof work !== 'string' || !/^[A-Fa-f\d]{16}$/.test(work)) {
+ if (!isHex8(work)) {
LOG: logger.log('Work must be hex char(16).')
throw new Error(resBody)
}