NotReeceHarris/torv3

Add vanity support

Closed this issue · 3 comments

Incorporating support for onion v3 vanity addresses would enhance the package by allowing users to create custom, memorable onion addresses for their hidden services.

/**
 * Function generates a vanity onion address with the prefix specified by the targetPrefix parameter
 * @param {String} targetPrefix - The targetPrefix for the vanity url
 * @returns {Object} An object containing the public and private key buffers, the Onion v3 address and a verification flag
 */
const vanityOnionV3 = (targetPrefix) => {
	// Record the start time to calculate the total time taken to generate the onion address
	const start = performance.now();
  
	let count = 0; // Keep track of the number of attempts it takes to generate the onion address
	let v3; // The onion address generated by generateOnionV3()
  
	// Loop until an onion address is generated with the desired prefix
	do {
		try {
			v3 = generateOnionV3();
		} catch (error) {
		// Handle any errors that may occur during address generation, e.g. log them or throw a custom error
			console.error(error);
			continue; // Continue the loop if an error occurs
		}
		count++; // Increment the count of attempts made
	} while (!v3.address.startsWith(targetPrefix)); // Keep looping until an onion address is generated with the desired prefix
  
	// Record the number of attempts made and the time taken to generate the onion address
	v3.attempts = count;
	v3.time = (performance.now() - start) / 1000;
  
	return v3; // Return the generated onion address with the number of attempts and time taken to generate it
};

This doesnt seem to work rn, but after some tweaks it should

const { Worker, isMainThread } = require('worker_threads');

// This function generates a vanity onion address with the prefix specified by the targetPrefix parameter
// The function uses the generateOnionV3() function to generate the onion address and keeps trying until the generated address starts with the targetPrefix
// The function uses multiple threads to speed up the process
const vanityOnionV3 = (targetPrefix, numThreads = 3) => {
  if (isMainThread) {
    // If the function is called from the main thread, create numThreads worker threads to generate onion addresses
    return new Promise((resolve, reject) => {
      const results = [];
      let completed = 0;
      const handleMessage = (result) => {
        if (result && result.address.startsWith(targetPrefix)) {
          // If an onion address is found with the desired prefix, terminate all worker threads and resolve the promise with the result
          for (const worker of workers) {
            worker.terminate();
          }
          resolve(result);
        } else {
          // Otherwise, add the result to the list of results
          results.push(result);
          if (++completed === numThreads) {
            // If all worker threads have completed, reject the promise with the list of results
            reject(results);
          }
        }
      };
      const workers = [];
      for (let i = 0; i < numThreads; i++) {
        const worker = new Worker(__filename);
        worker.on('message', handleMessage);
        workers.push(worker);
      }
    });
  } else {
    // If the function is called from a worker thread, generate onion addresses until an address with the desired prefix is found
    const start = performance.now();
    let count = 0;
    let v3;
    do {
      try {
        v3 = generateOnionV3();
      } catch (error) {
        console.error(error);
        continue;
      }
      count++;
    } while (!v3.address.startsWith(targetPrefix));
    v3.attempts = count;
    v3.time = (performance.now() - start) / 1000;
    // Send the result back to the main thread
    parentPort.postMessage(v3);
  }
};

// Usage:
vanityOnionV3('myPrefix', 4)
  .then((result) => console.log(`Address found after ${result.attempts} attempts in ${result.time} seconds.`))
  .catch((results) => console.log(`${results.length} addresses generated, none with desired prefix.`));

Added in 1.2.0 however a multi thread function could be added in the future