]> zoso.dev Git - nano-pow.git/commitdiff
Add support for multiple hashes.
authorChris Duncan <chris@zoso.dev>
Mon, 10 Mar 2025 23:29:44 +0000 (16:29 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 10 Mar 2025 23:29:44 +0000 (16:29 -0700)
cli.js

diff --git a/cli.js b/cli.js
index 90aec93f3adbfeb27aa12effca3e7a72db4749ad..930c57dddd55a5ae94f19d6adccc9ee8cee09bc1 100755 (executable)
--- a/cli.js
+++ b/cli.js
@@ -6,74 +6,92 @@ import * as puppeteer from 'puppeteer'
 import * as fs from 'node:fs/promises'
 
 const args = process.argv.slice(2)
-const hash = args.pop()
-if (!/^[0-9A-Fa-f]{64}$/.test(hash)) throw new Error('Invalid block hash')
+const hashes = []
+let hash = args.pop()
+while (/^[0-9A-Fa-f]{64}$/.test(hash)) {
+       hashes.unshift(hash)
+       hash = args.pop()
+}
+if (hashes.length === 0) {
+       console.error('Invalid block hash input')
+       process.exit(1)
+}
 
 const options = {}
 for (let i = 0; i < args.length; i++) {
-  switch (args[i]) {
-    case ('--threshold'):
-    case ('-t'): {
-      if (args[i + 1] == null) throw new Error('Missing argument for threshold')
-      if (/^[0-9A-Fa-f]{,8}$/.test(args[i + 1])) throw new Error('Invalid threshold')
-      options['threshold'] = parseInt(args[i + 1], 16)
-      break
-    }
-    case ('--effort'):
-    case ('-e'): {
-      if (args[i + 1] == null) throw new Error('Missing argument for effort')
-      if (/^[0-9]{,2}$/.test(args[i + 1])) throw new Error('Invalid effort')
-      options['effort'] = parseInt(args[i + 1], 10)
-      break
-    }
-    case ('--debug'):
-    case ('-d'): {
-      options['debug'] = true
-      break
-    }
-  }
+       switch (args[i]) {
+               case ('--threshold'):
+               case ('-t'): {
+                       if (args[i + 1] == null) throw new Error('Missing argument for threshold')
+                       if (/^[0-9A-Fa-f]{,8}$/.test(args[i + 1])) throw new Error('Invalid threshold')
+                       options['threshold'] = parseInt(args[i + 1], 16)
+                       break
+               }
+               case ('--effort'):
+               case ('-e'): {
+                       if (args[i + 1] == null) throw new Error('Missing argument for effort')
+                       if (/^[0-9]{,2}$/.test(args[i + 1])) throw new Error('Invalid effort')
+                       options['effort'] = parseInt(args[i + 1], 10)
+                       break
+               }
+               case ('--debug'):
+               case ('-d'): {
+                       options['debug'] = true
+                       break
+               }
+       }
 }
 
 (async () => {
-  const NanoPow = await fs.readFile(`${import.meta.dirname}/main.min.js`, 'utf-8')
-  const browser = await puppeteer.launch({
-    headless: true,
-    args: [
-      '--no-sandbox',
-      '--headless=new',
-      '--use-angle=vulkan',
-      '--enable-features=Vulkan',
-      '--disable-vulkan-surface',
-      '--enable-unsafe-webgpu',
-      '--enable-vulkan'
-    ]
-  })
-  const page = await browser.newPage()
-  console.log('Starting search...')
-  page.on('console', async (msg) => {
-    if (msg.type() === 'info' && /^[A-Fa-f0-9]{16}$/.test(msg.text())) {
-      console.log(msg.text())
-      process.exit()
-    }
-  })
-  await page.setContent(`
-    <!DOCTYPE html>
-    <html>
-      <head>
-        <script type="module">
-          ${NanoPow}
-          try {
-            const work = await NanoPow.search('${hash}')
-            console.info(work)
-          } catch (err) {
-            console.error(err)
-          }
-        </script>
-      </head>
-      <body></body>
-    </html>
-  `)
-  await new Promise(r => setTimeout(r, 90000))
-  console.log('Not found')
-  await browser.close()
+       const NanoPow = await fs.readFile(`${import.meta.dirname}/main.min.js`, 'utf-8')
+       const browser = await puppeteer.launch({
+               headless: true,
+               args: [
+                       '--no-sandbox',
+                       '--headless=new',
+                       '--use-angle=vulkan',
+                       '--enable-features=Vulkan',
+                       '--disable-vulkan-surface',
+                       '--enable-unsafe-webgpu',
+                       '--enable-vulkan'
+               ]
+       })
+       const page = await browser.newPage()
+       page.on('console', async (msg) => {
+               const output = msg.text().split(' ')
+               if (output[0] === 'cli') {
+                       if (output[1] === 'exit') {
+                               process.exit()
+                       } else {
+                               console.log(output[1])
+                       }
+               } else if (options['debug']) {
+                       console.log(msg.text())
+               }
+       })
+       await page.setContent(`
+               <!DOCTYPE html>
+               <html>
+                       <head>
+                               <script type="module">
+                                       ${NanoPow}
+                                       const hashes = ["${hashes.join('","')}"]
+                                       for (const hash of hashes) {
+                                               try {
+                                                       console.log(\`cli \${hash}\`)
+                                                       const work = await NanoPow.search(hash)
+                                                       console.log(\`cli \${work}\`)
+                                               } catch (err) {
+                                                       console.error(\`cli \${err}\`)
+                                               }
+                                       }
+                                       console.log('cli exit')
+                               </script>
+                       </head>
+                       <body></body>
+               </html>
+       `)
+       await new Promise(r => setTimeout(r, 90000))
+       console.log('Not found')
+       await browser.close()
 })()