Code formatting
This commit is contained in:
parent
8ff84c66db
commit
b066738593
|
@ -1,14 +1,12 @@
|
||||||
package jmaa.pbc;
|
package jmaa.pbc;
|
||||||
|
|
||||||
import java.util.List;
|
import com.partisiablockchain.BlockchainAddress;
|
||||||
|
import com.partisiablockchain.crypto.KeyPair;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import com.partisiablockchain.BlockchainAddress;
|
import java.util.List;
|
||||||
import com.partisiablockchain.crypto.BlockchainPublicKey;
|
|
||||||
import com.partisiablockchain.crypto.KeyPair;
|
|
||||||
|
|
||||||
/** Search for PBC vanity addresses.
|
/** Search for PBC vanity addresses. */
|
||||||
*/
|
|
||||||
public final class PbcVanityAddressSearcher {
|
public final class PbcVanityAddressSearcher {
|
||||||
|
|
||||||
private static final int NUM_THREADS = 8;
|
private static final int NUM_THREADS = 8;
|
||||||
|
@ -20,12 +18,13 @@ public final class PbcVanityAddressSearcher {
|
||||||
searchForPrivateKeyWithVanityAddress(requirements);
|
searchForPrivateKeyWithVanityAddress(requirements);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void searchForPrivateKeyWithVanityAddress(AddressRequirements addressRequirements) throws InterruptedException {
|
private static void searchForPrivateKeyWithVanityAddress(AddressRequirements addressRequirements)
|
||||||
|
throws InterruptedException {
|
||||||
System.out.println("Searching for private key: %s".formatted(addressRequirements));
|
System.out.println("Searching for private key: %s".formatted(addressRequirements));
|
||||||
System.out.println("Difficulty: 1/%s".formatted(addressRequirements.difficulty()));
|
System.out.println("Difficulty: 1/%s".formatted(addressRequirements.difficulty()));
|
||||||
|
|
||||||
List<Thread> searchers = new ArrayList<>();
|
List<Thread> searchers = new ArrayList<>();
|
||||||
for(int threadIdx=0;threadIdx<NUM_THREADS ;threadIdx++) {
|
for (int threadIdx = 0; threadIdx < NUM_THREADS; threadIdx++) {
|
||||||
final Thread searcher = new Thread(new AddressSearcherThread(threadIdx, addressRequirements));
|
final Thread searcher = new Thread(new AddressSearcherThread(threadIdx, addressRequirements));
|
||||||
searcher.start();
|
searcher.start();
|
||||||
searchers.add(searcher);
|
searchers.add(searcher);
|
||||||
|
@ -42,27 +41,35 @@ public final class PbcVanityAddressSearcher {
|
||||||
* @param threadIx Id of thread
|
* @param threadIx Id of thread
|
||||||
* @param addressRequirements Requirements for the current search.
|
* @param addressRequirements Requirements for the current search.
|
||||||
*/
|
*/
|
||||||
private record AddressSearcherThread(int threadIdx, AddressRequirements addressRequirements) implements Runnable {
|
private record AddressSearcherThread(int threadIdx, AddressRequirements addressRequirements)
|
||||||
|
implements Runnable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
long start = System.nanoTime();
|
long start = System.nanoTime();
|
||||||
long pingInterval = INITIAL_PING_INTERVAL;
|
long pingInterval = INITIAL_PING_INTERVAL;
|
||||||
for(int iterationIdx=1;iterationIdx<MAX_ITERATIONS_PER_THREAD;iterationIdx++) {
|
for (int iterationIdx = 1; iterationIdx < MAX_ITERATIONS_PER_THREAD; iterationIdx++) {
|
||||||
// Generate random keypair
|
// Generate random keypair
|
||||||
final KeyPair keyPair = new KeyPair();
|
final KeyPair keyPair = new KeyPair();
|
||||||
final BlockchainAddress address = keyPair.getPublic().createAddress();
|
final BlockchainAddress address = keyPair.getPublic().createAddress();
|
||||||
|
|
||||||
// Output if vain enough
|
// Output if vain enough
|
||||||
if(addressRequirements.satisfiedBy(address)) {
|
if (addressRequirements.satisfiedBy(address)) {
|
||||||
System.out.println("[%2d:%10d] match: addr=%s - pk=%s".formatted(threadIdx, iterationIdx, address.writeAsString(), keyPair.getPrivateKey().toString(16)));
|
System.out.println(
|
||||||
|
"[%2d:%10d] match: addr=%s - pk=%s"
|
||||||
|
.formatted(
|
||||||
|
threadIdx,
|
||||||
|
iterationIdx,
|
||||||
|
address.writeAsString(),
|
||||||
|
keyPair.getPrivateKey().toString(16)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ping
|
// Ping
|
||||||
if (iterationIdx % pingInterval == 0) {
|
if (iterationIdx % pingInterval == 0) {
|
||||||
final long now = System.nanoTime();
|
final long now = System.nanoTime();
|
||||||
final long duration = (now - start) / 1_000_000_000;
|
final long duration = (now - start) / 1_000_000_000;
|
||||||
System.out.println("[%2d:%10d] ping at %d sec".formatted(threadIdx, iterationIdx, duration));
|
System.out.println(
|
||||||
|
"[%2d:%10d] ping at %d sec".formatted(threadIdx, iterationIdx, duration));
|
||||||
pingInterval = pingInterval * 2;
|
pingInterval = pingInterval * 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,7 +83,8 @@ public final class PbcVanityAddressSearcher {
|
||||||
final String word = args[idx];
|
final String word = args[idx];
|
||||||
System.out.println("%s : %s".formatted(idx, word));
|
System.out.println("%s : %s".formatted(idx, word));
|
||||||
if ("--prefix".equals(word)) {
|
if ("--prefix".equals(word)) {
|
||||||
prefix = args[++idx];;
|
prefix = args[++idx];
|
||||||
|
;
|
||||||
} else if ("--suffix".equals(word)) {
|
} else if ("--suffix".equals(word)) {
|
||||||
suffix = args[++idx];
|
suffix = args[++idx];
|
||||||
} else {
|
} else {
|
||||||
|
@ -101,19 +109,15 @@ public final class PbcVanityAddressSearcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Whether the given address satisfies the requirements. */
|
||||||
* Whether the given address satisfies the requirements.
|
|
||||||
*/
|
|
||||||
public boolean satisfiedBy(BlockchainAddress address) {
|
public boolean satisfiedBy(BlockchainAddress address) {
|
||||||
final String addressAsString = address.writeAsString();
|
final String addressAsString = address.writeAsString();
|
||||||
return addressAsString.startsWith(prefix) && addressAsString.endsWith(suffix);
|
return addressAsString.startsWith(prefix) && addressAsString.endsWith(suffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Attempts to measure the "one-in-X" factor. */
|
||||||
* Attempts to measure the "one-in-X" factor.
|
|
||||||
*/
|
|
||||||
public BigInteger difficulty() {
|
public BigInteger difficulty() {
|
||||||
return BigInteger.ONE.shiftLeft(4*(prefix.length()-2 + suffix.length()));
|
return BigInteger.ONE.shiftLeft(4 * (prefix.length() - 2 + suffix.length()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user