74 lines
2.6 KiB
Java
74 lines
2.6 KiB
Java
package jmaa.pbc;
|
|
|
|
import java.util.List;
|
|
import java.util.ArrayList;
|
|
import com.partisiablockchain.BlockchainAddress;
|
|
import com.partisiablockchain.crypto.BlockchainPublicKey;
|
|
import com.partisiablockchain.crypto.KeyPair;
|
|
|
|
/** Search for PBC vanity addresses.
|
|
*/
|
|
public final class PbcVanityAddressSearcher {
|
|
|
|
private static final int NUM_THREADS = 8;
|
|
private static final long MAX_ITERATIONS_PER_THREAD = 100_000_000;
|
|
private static final long INITIAL_PING_INTERVAL = 10_000;
|
|
|
|
public static void main(String[] args) throws InterruptedException {
|
|
searchForPrivateKeyWithVanityAddress(args[0], args[1]);
|
|
}
|
|
|
|
/**
|
|
* @param vanityPrefix Desired address prefix
|
|
*/
|
|
private static void searchForPrivateKeyWithVanityAddress(String vanityPrefix, String vanitySuffix) throws InterruptedException {
|
|
if (!vanityPrefix.startsWith("00")) {
|
|
throw new IllegalArgumentException("Vanity prefix must start with 00.");
|
|
}
|
|
|
|
System.out.println("Searching for private key for address prefix \"%s\" and suffix \"%s\" ".formatted(vanityPrefix, vanitySuffix));
|
|
|
|
List<Thread> searchers = new ArrayList<>();
|
|
for(int threadIdx=0;threadIdx<NUM_THREADS ;threadIdx++) {
|
|
final Thread searcher = new Thread(new KeySearcher(threadIdx, vanityPrefix, vanitySuffix));
|
|
searcher.start();
|
|
searchers.add(searcher);
|
|
}
|
|
|
|
for (Thread searcher : searchers) {
|
|
searcher.join();
|
|
}
|
|
}
|
|
|
|
private record KeySearcher(int threadIdx, String vanityPrefix, String vanitySuffix) implements Runnable {
|
|
|
|
@Override
|
|
public void run() {
|
|
long start = System.nanoTime();
|
|
long pingInterval = INITIAL_PING_INTERVAL;
|
|
for(int iterationIdx=1;iterationIdx<MAX_ITERATIONS_PER_THREAD;iterationIdx++) {
|
|
// Generate random keypair
|
|
final KeyPair keyPair = new KeyPair();
|
|
final BlockchainAddress address = keyPair.getPublic().createAddress();
|
|
|
|
// Check vanity
|
|
final String addressAsString = address.writeAsString();
|
|
final boolean satisfiesVanity = addressAsString.startsWith(vanityPrefix) && addressAsString.endsWith(vanitySuffix);
|
|
|
|
// Output if vain enough
|
|
if(satisfiesVanity) {
|
|
System.out.println("[%2d:%10d] match: addr=%s - pk=%s".formatted(threadIdx, iterationIdx, addressAsString, keyPair.getPrivateKey().toString(16)));
|
|
}
|
|
|
|
// Ping
|
|
if (iterationIdx % pingInterval == 0) {
|
|
final long now = System.nanoTime();
|
|
final long duration = (now - start) / 1_000_000_000;
|
|
System.out.println("[%2d:%10d] ping at %d sec".formatted(threadIdx, iterationIdx, duration));
|
|
pingInterval = pingInterval * 2;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|