From eb2731e0d8b2f965ad8925e8487e35469dbaa215 Mon Sep 17 00:00:00 2001 From: PawelGorny Date: Thu, 22 Sep 2022 10:44:46 +0200 Subject: [PATCH] 0.17.0 printing seed with 2 words swapped (worker: SWAP) --- pom.xml | 4 +- .../pawelgorny/lostword/Configuration.java | 2 +- .../java/com/pawelgorny/lostword/Main.java | 2 +- .../java/com/pawelgorny/lostword/WORK.java | 2 +- .../java/com/pawelgorny/lostword/Worker.java | 5 +- .../com/pawelgorny/lostword/WorkerSwap.java | 87 +++++++++++++++++++ 6 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/pawelgorny/lostword/WorkerSwap.java diff --git a/pom.xml b/pom.xml index 4458614..7182c08 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.pawelgorny lostword - 0.16.0 + 0.17.0 jar @@ -40,7 +40,7 @@ junit junit 4.12 - test + diff --git a/src/main/java/com/pawelgorny/lostword/Configuration.java b/src/main/java/com/pawelgorny/lostword/Configuration.java index 7d08c4e..cf2dc4c 100644 --- a/src/main/java/com/pawelgorny/lostword/Configuration.java +++ b/src/main/java/com/pawelgorny/lostword/Configuration.java @@ -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: diff --git a/src/main/java/com/pawelgorny/lostword/Main.java b/src/main/java/com/pawelgorny/lostword/Main.java index 478d2e9..b589610 100644 --- a/src/main/java/com/pawelgorny/lostword/Main.java +++ b/src/main/java/com/pawelgorny/lostword/Main.java @@ -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) { diff --git a/src/main/java/com/pawelgorny/lostword/WORK.java b/src/main/java/com/pawelgorny/lostword/WORK.java index 6a9699d..a30d52b 100644 --- a/src/main/java/com/pawelgorny/lostword/WORK.java +++ b/src/main/java/com/pawelgorny/lostword/WORK.java @@ -2,5 +2,5 @@ public enum WORK { ONE_UNKNOWN, KNOWN_POSITION, ONE_UNKNOWN_CHECK_ALL, POOL, PERMUTATION, PERMUTATION_CHECK, - PRINT_SEEDS + PRINT_SEEDS, SWAP } diff --git a/src/main/java/com/pawelgorny/lostword/Worker.java b/src/main/java/com/pawelgorny/lostword/Worker.java index 0abe843..cb7d5b3 100644 --- a/src/main/java/com/pawelgorny/lostword/Worker.java +++ b/src/main/java/com/pawelgorny/lostword/Worker.java @@ -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; diff --git a/src/main/java/com/pawelgorny/lostword/WorkerSwap.java b/src/main/java/com/pawelgorny/lostword/WorkerSwap.java new file mode 100644 index 0000000..a2282dd --- /dev/null +++ b/src/main/java/com/pawelgorny/lostword/WorkerSwap.java @@ -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 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 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