read bip39 words once and synchronized

This commit is contained in:
woodser 2025-06-02 08:48:03 -04:00 committed by woodser
parent fa375b3cbd
commit 501530d85f

View file

@ -123,6 +123,7 @@ public class HavenoUtils {
private static final BigInteger XMR_AU_MULTIPLIER = new BigInteger("1000000000000");
public static final DecimalFormat XMR_FORMATTER = new DecimalFormat("##############0.000000000000", DECIMAL_FORMAT_SYMBOLS);
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
private static List<String> bip39Words = new ArrayList<String>();
// shared references TODO: better way to share references?
public static HavenoSetup havenoSetup;
@ -298,10 +299,7 @@ public class HavenoUtils {
try {
// load bip39 words
String fileName = "bip39_english.txt";
File bip39File = new File(havenoSetup.getConfig().appDataDir, fileName);
if (!bip39File.exists()) FileUtil.resourceToFile(fileName, bip39File);
List<String> bip39Words = Files.readAllLines(bip39File.toPath(), StandardCharsets.UTF_8);
loadBip39Words();
// select words randomly
List<String> passphraseWords = new ArrayList<String>();
@ -315,13 +313,26 @@ public class HavenoUtils {
}
}
private static synchronized void loadBip39Words() {
if (bip39Words.isEmpty()) {
try {
String fileName = "bip39_english.txt";
File bip39File = new File(havenoSetup.getConfig().appDataDir, fileName);
if (!bip39File.exists()) FileUtil.resourceToFile(fileName, bip39File);
bip39Words = Files.readAllLines(bip39File.toPath(), StandardCharsets.UTF_8);
} catch (Exception e) {
throw new IllegalStateException("Failed to load BIP39 words", e);
}
}
}
public static String getChallengeHash(String challenge) {
if (challenge == null) return null;
// tokenize passphrase
String[] words = challenge.toLowerCase().split(" ");
// collect first 4 letters of each word, which are unique in bip39
// collect up to first 4 letters of each word, which are unique in bip39
List<String> prefixes = new ArrayList<String>();
for (String word : words) prefixes.add(word.substring(0, Math.min(word.length(), 4)));