Skip to content

Commit

Permalink
Rename and improve to secrets file. Support STRING_DO_SHA256, BIG_INT…
Browse files Browse the repository at this point in the history
…EGER, SHA256, DUMPED_RIVATE_KEY.
  • Loading branch information
bernardladenthin committed Mar 11, 2024
1 parent d0cebde commit b27db55
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public abstract class AbstractPlaintextFile {
@Nonnull
protected final AtomicBoolean shouldRun;

public AbstractPlaintextFile(File file, ReadStatistic readStatistic, AtomicBoolean shouldRun) {
public AbstractPlaintextFile(@Nonnull File file, @Nonnull ReadStatistic readStatistic, @Nonnull AtomicBoolean shouldRun) {
this.file = file;
this.readStatistic = readStatistic;
this.shouldRun = shouldRun;
}

protected double calculateFileProgress(RandomAccessFile raf) throws IOException {
protected double calculateFileProgress(@Nonnull RandomAccessFile raf) throws IOException {
return ((double)(Math.max(raf.getFilePointer(),1)) / (double)raf.length()) * 100.0d;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public class AddressFilesToLMDB implements Runnable, Interruptable {

private final CAddressFilesToLMDB addressFilesToLMDB;

private NetworkParameters networkParameters;

private LMDBPersistence persistence;

private final AtomicLong addressCounter = new AtomicLong();
Expand All @@ -59,7 +57,7 @@ public AddressFilesToLMDB(CAddressFilesToLMDB addressFilesToLMDB, AtomicBoolean

@Override
public void run() {
createNetworkParameter();
final NetworkParameters networkParameters = new NetworkParameterFactory().getOrCreate();

PersistenceUtils persistenceUtils = new PersistenceUtils(networkParameters);
persistence = new LMDBPersistence(addressFilesToLMDB.lmdbConfigurationWrite, persistenceUtils);
Expand Down Expand Up @@ -122,11 +120,6 @@ private void logProgress() {
logger.info("Progress: " + addressCounter.get() + " addresses. Unsupported: " + readStatistic.unsupported + ". Errors: " + readStatistic.errors.size() + ". Current File progress: " + String.format("%.2f", readStatistic.currentFileProgress) + "%.");
}

private void createNetworkParameter() {
networkParameters = MainNetParams.get();
Context.getOrCreate(networkParameters);
}

@Override
public void interrupt() {
}
Expand Down

This file was deleted.

16 changes: 8 additions & 8 deletions src/main/java/net/ladenthin/bitcoinaddressfinder/Finder.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import net.ladenthin.bitcoinaddressfinder.configuration.CProducerJava;
import net.ladenthin.bitcoinaddressfinder.configuration.CProducerOpenCL;
import net.ladenthin.bitcoinaddressfinder.configuration.CFinder;
import net.ladenthin.bitcoinaddressfinder.configuration.CProducerJavaBrainwallet;
import net.ladenthin.bitcoinaddressfinder.configuration.CProducerJavaSecretsFiles;
import net.ladenthin.bitcoinaddressfinder.persistence.PersistenceUtils;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.params.MainNetParams;
Expand All @@ -47,7 +47,7 @@ public class Finder implements Interruptable {

private final List<ProducerOpenCL> openCLProducers = new ArrayList<>();
private final List<ProducerJava> javaProducers = new ArrayList<>();
private final List<ProducerJavaBrainwallet> javaProducersBrainwallet = new ArrayList<>();
private final List<ProducerJavaSecretsFiles> javaProducersSecretsFiles = new ArrayList<>();

/**
* It is already thread local, no need for {@link java.util.concurrent.ThreadLocalRandom}.
Expand Down Expand Up @@ -91,11 +91,11 @@ public void configureProducer() {
}
}

if (finder.producerJavaBrainwallet != null) {
for (CProducerJavaBrainwallet cProducerJavaBrainwallet : finder.producerJavaBrainwallet) {
cProducerJavaBrainwallet.assertGridNumBitsCorrect();
ProducerJavaBrainwallet producerJavaBrainwallet = new ProducerJavaBrainwallet(cProducerJavaBrainwallet, shouldRun, consumerJava, keyUtility, random);
javaProducersBrainwallet.add(producerJavaBrainwallet);
if (finder.producerJavaSecretsFiles != null) {
for (CProducerJavaSecretsFiles cProducerJavaSecretsFiles : finder.producerJavaSecretsFiles) {
cProducerJavaSecretsFiles.assertGridNumBitsCorrect();
ProducerJavaSecretsFiles producerJavaSecretsFiles = new ProducerJavaSecretsFiles(cProducerJavaSecretsFiles, shouldRun, consumerJava, keyUtility, random);
javaProducersSecretsFiles.add(producerJavaSecretsFiles);
}
}

Expand Down Expand Up @@ -137,7 +137,7 @@ public void interrupt() {
public List<Producer> getAllProducers() {
List<Producer> producers = new ArrayList<>();
producers.addAll(javaProducers);
producers.addAll(javaProducersBrainwallet);
producers.addAll(javaProducersSecretsFiles);
producers.addAll(openCLProducers);
return producers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public class LMDBToAddressFile implements Runnable, Interruptable {

private final AtomicBoolean shouldRun;

private NetworkParameters networkParameters;

private LMDBPersistence persistence;

public LMDBToAddressFile(CLMDBToAddressFile lmdbToAddressFile, AtomicBoolean shouldRun) {
Expand All @@ -50,7 +48,8 @@ public LMDBToAddressFile(CLMDBToAddressFile lmdbToAddressFile, AtomicBoolean sho

@Override
public void run() {
createNetworkParameter();
final NetworkParameters networkParameters = new NetworkParameterFactory().getOrCreate();

PersistenceUtils persistenceUtils = new PersistenceUtils(networkParameters);
persistence = new LMDBPersistence(lmdbToAddressFile.lmdbConfigurationReadOnly, persistenceUtils);
persistence.init();
Expand All @@ -68,11 +67,6 @@ public void run() {
}
}

private void createNetworkParameter() {
networkParameters = MainNetParams.get();
Context.getOrCreate(networkParameters);
}

@Override
public void interrupt() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @formatter:off
/**
* Copyright 2024 Bernard Ladenthin bernard.ladenthin@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// @formatter:on
package net.ladenthin.bitcoinaddressfinder;

import org.bitcoinj.core.Context;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.params.MainNetParams;

public class NetworkParameterFactory {

public NetworkParameters getOrCreate() {
NetworkParameters networkParameters = MainNetParams.get();
Context.getOrCreate(networkParameters);
return networkParameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import net.ladenthin.bitcoinaddressfinder.configuration.CProducerJavaBrainwallet;
import net.ladenthin.bitcoinaddressfinder.configuration.CProducerJavaSecretsFiles;
import org.bitcoinj.core.NetworkParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProducerJavaBrainwallet extends AbstractProducer {
public class ProducerJavaSecretsFiles extends AbstractProducer {

private final Logger logger = LoggerFactory.getLogger(ProducerJavaBrainwallet.class);
private final Logger logger = LoggerFactory.getLogger(ProducerJavaSecretsFiles.class);

private final CProducerJavaBrainwallet producerJavaBrainwallet;
private final CProducerJavaSecretsFiles producerJavaSecretsFiles;

private final ReadStatistic readStatistic = new ReadStatistic();

public ProducerJavaBrainwallet(CProducerJavaBrainwallet producerJavaBrainwallet, AtomicBoolean shouldRun, Consumer consumer, KeyUtility keyUtility, Random random) {
public ProducerJavaSecretsFiles(CProducerJavaSecretsFiles producerJavaSecretsFiles, AtomicBoolean shouldRun, Consumer consumer, KeyUtility keyUtility, Random random) {
super(shouldRun, consumer, keyUtility, random);
this.producerJavaBrainwallet = producerJavaBrainwallet;
this.producerJavaSecretsFiles = producerJavaSecretsFiles;
}

@Override
Expand All @@ -47,27 +48,31 @@ public void initProducer() {

@Override
public void produceKeys() {
final NetworkParameters networkParameters = new NetworkParameterFactory().getOrCreate();

FileHelper fileHelper = new FileHelper();
List<File> files = fileHelper.stringsToFiles(producerJavaBrainwallet.brainwalletStringsFiles);
List<File> files = fileHelper.stringsToFiles(producerJavaSecretsFiles.files);
fileHelper.assertFilesExists(files);

logger.info("writeAllAmounts ...");
logger.info("Iterate brainwallet files ...");
logger.info("Iterate secrets files ...");
try {
for (File file : files) {
BrainwalletFile brainwalletFile = new BrainwalletFile(
SecretsFile secretsFile = new SecretsFile(
networkParameters,
file,
producerJavaSecretsFiles.secretFormat,
readStatistic,
this::processSecret,
this.shouldRun
);

logger.info("process " + file.getAbsolutePath());
brainwalletFile.readFile();
logger.info("process: " + file.getAbsolutePath());
secretsFile.readFile();
logger.info("finished: " + file.getAbsolutePath());

logProgress();
logger.info("... iterate brainwallet files done.");
logger.info("... iterate secrets files done.");
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/net/ladenthin/bitcoinaddressfinder/SecretsFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// @formatter:off
/**
* Copyright 2021 Bernard Ladenthin bernard.ladenthin@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// @formatter:on
package net.ladenthin.bitcoinaddressfinder;

import com.google.common.hash.Hashing;
import java.io.File;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
import net.ladenthin.bitcoinaddressfinder.configuration.CSecretFormat;
import static net.ladenthin.bitcoinaddressfinder.configuration.CSecretFormat.BIG_INTEGER;
import static net.ladenthin.bitcoinaddressfinder.configuration.CSecretFormat.SHA256;
import net.ladenthin.bitcoinaddressfinder.configuration.UnknownSecretFormatException;
import org.apache.commons.codec.binary.Hex;
import org.bitcoinj.core.DumpedPrivateKey;
import org.bitcoinj.core.NetworkParameters;

public class SecretsFile extends AbstractPlaintextFile {

private final CSecretFormat secretFormat;
private final Consumer<BigInteger> secretConsumer;
private final NetworkParameters networkParameters;

public SecretsFile(@Nonnull NetworkParameters networkParameters, @Nonnull File file, @Nonnull CSecretFormat secretFormat, @Nonnull ReadStatistic readStatistic, @Nonnull Consumer<BigInteger> secretConsumer, @Nonnull AtomicBoolean shouldRun) {
super(file, readStatistic, shouldRun);
this.networkParameters = networkParameters;
this.secretFormat = secretFormat;
this.secretConsumer = secretConsumer;
}

@Override
public void processLine(String line) {
final BigInteger secret;
switch (secretFormat) {
case STRING_DO_SHA256:
byte[] sha256 = Hashing.sha256().hashString(line, StandardCharsets.UTF_8).asBytes();
String hexOfHash = Hex.encodeHexString( sha256 );
secret = new BigInteger(hexOfHash, 16);
break;
case BIG_INTEGER:
secret = new BigInteger(line);
break;
case SHA256:
secret = new BigInteger(line, 16);
break;
case DUMPED_RIVATE_KEY:
DumpedPrivateKey dpk = DumpedPrivateKey.fromBase58(networkParameters, line);
secret = dpk.getKey().getPrivKey();
break;
default:
throw new UnknownSecretFormatException(secretFormat);
}
secretConsumer.accept(secret);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public class CFinder {

public CConsumerJava consumerJava;
public List<CProducerJava> producerJava = new ArrayList<>();
public List<CProducerJavaBrainwallet> producerJavaBrainwallet = new ArrayList<>();
public List<CProducerJavaSecretsFiles> producerJavaSecretsFiles = new ArrayList<>();
public List<CProducerOpenCL> producerOpenCL = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
import java.util.ArrayList;
import java.util.List;

public class CProducerJavaBrainwallet extends CProducer {
public class CProducerJavaSecretsFiles extends CProducer {
/**
* The list of strings files which should be read.
*/
public List<String> brainwalletStringsFiles = new ArrayList<>();
public List<String> files = new ArrayList<>();

/**
* The format of each line in the files.
*/
public CSecretFormat secretFormat = CSecretFormat.STRING_DO_SHA256;
}
Loading

0 comments on commit b27db55

Please sign in to comment.