Skip to content

Commit

Permalink
0.17.0 printing seed with 2 words swapped (worker: SWAP)
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGorny committed Sep 22, 2022
1 parent 3722618 commit eb2731e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.pawelgorny</groupId>
<artifactId>lostword</artifactId>
<version>0.16.0</version>
<version>0.17.0</version>
<packaging>jar</packaging>

<dependencies>
Expand Down Expand Up @@ -40,7 +40,7 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>

</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/pawelgorny/lostword/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void parseScript(String targetAddress) {
this.coin = Configuration.ETHEREUM;
}
}
if (!WORK.ONE_UNKNOWN_CHECK_ALL.equals(work) && !WORK.PERMUTATION.equals(work) && !WORK.PRINT_SEEDS.equals(work)){
if (!WORK.ONE_UNKNOWN_CHECK_ALL.equals(work) && !WORK.PERMUTATION.equals(work) && !WORK.PRINT_SEEDS.equals(work) && !WORK.SWAP.equals(work)){
if (this.ethereumAddress==null){
switch (getDBscriptType()){
case P2PKH:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/pawelgorny/lostword/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private static Configuration readConfiguration(String file) {
}
words.add(line);
}
}else if (WORK.KNOWN_POSITION.equals(work) || WORK.PERMUTATION.equals(work) || WORK.PERMUTATION_CHECK.equals(work)|| WORK.PRINT_SEEDS.equals(work)){
}else if (WORK.KNOWN_POSITION.equals(work) || WORK.PERMUTATION.equals(work) || WORK.SWAP.equals(work) || WORK.PERMUTATION_CHECK.equals(work)|| WORK.PRINT_SEEDS.equals(work)){
if (words.size() == size) {
path = line;
}else if (words.size() < size) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/pawelgorny/lostword/WORK.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public enum WORK {
ONE_UNKNOWN, KNOWN_POSITION, ONE_UNKNOWN_CHECK_ALL, POOL, PERMUTATION, PERMUTATION_CHECK,
PRINT_SEEDS
PRINT_SEEDS, SWAP
}
5 changes: 4 additions & 1 deletion src/main/java/com/pawelgorny/lostword/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ public void run() throws InterruptedException, MnemonicException {
case PRINT_SEEDS:
worker = new WorkerPrintSeeds(configuration);
break;
case SWAP:
worker = new WorkerSwap(configuration);
break;
}
System.out.println("--- Starting worker --- "+ SDTF.format(new Date())+" ---");
if (WORK.PERMUTATION.equals(configuration.getWork()) || WORK.PRINT_SEEDS.equals(configuration.getWork())){
if (WORK.PERMUTATION.equals(configuration.getWork()) || WORK.PRINT_SEEDS.equals(configuration.getWork())|| WORK.SWAP.equals(configuration.getWork())){
worker.run();
System.out.println();
return;
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/com/pawelgorny/lostword/WorkerSwap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.pawelgorny.lostword;

import org.bitcoinj.core.Utils;
import org.bitcoinj.crypto.MnemonicException;

import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class WorkerSwap extends Worker {

private MessageDigest SHA_256_DIGEST;
private FileWriter fileWriter = null;
private final String fileName;
private int counter = 0;
private final Set<String> result = new HashSet<>();

public WorkerSwap(Configuration configuration){
super(configuration);
try {
SHA_256_DIGEST = MessageDigest.getInstance("SHA-256");
}catch (Exception e){
System.out.println(e.getLocalizedMessage());
}
fileName = "SWAPS_"+(System.currentTimeMillis())+".txt";
System.out.println("Saving to file: "+fileName);
}

@Override
public void run() throws InterruptedException, MnemonicException {
System.out.println("Input: " + Utils.SPACE_JOINER.join(configuration.getWORDS()));
String[] words = new String[0];
words = configuration.getWORDS().toArray(words);
printAllSwaps(configuration.getSIZE(), words);
if (!result.isEmpty()){
for (String r : result){
System.out.println(r);
}
}
if (fileName!=null){
if (fileWriter!=null) {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
}
System.out.println("File " + fileName + " created, "+counter+" results");
}
}
}

private void printAllSwaps(int size, String[] words) {

List<String> mnemonic = Arrays.asList(words);
if (checksumCheck(mnemonic, SHA_256_DIGEST)) {
result.add(Utils.SPACE_JOINER.join(mnemonic));
}
String[] WORDS = new String[size];
for (int i=0; i<size; i++){
for (int w=0; w<size; w++){
WORDS[w] = words[w];
}
for (int j=0; j<size; j++){
swap(WORDS, i, j);
mnemonic = Arrays.asList(WORDS);
if (checksumCheck(mnemonic, SHA_256_DIGEST)) {
result.add(Utils.SPACE_JOINER.join(mnemonic));
}
swap(WORDS, j, i);
}
}

}

private void swap(String[] words, int i, int j) {
String t = words[i];
words[i] = words[j];
words[j] = t;
}


}

0 comments on commit eb2731e

Please sign in to comment.