From 501530d85f3e2fae98a18a766a6bfda4a99fc645 Mon Sep 17 00:00:00 2001 From: woodser <13068859+woodser@users.noreply.github.com> Date: Mon, 2 Jun 2025 08:48:03 -0400 Subject: [PATCH] read bip39 words once and synchronized --- .../java/haveno/core/trade/HavenoUtils.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/haveno/core/trade/HavenoUtils.java b/core/src/main/java/haveno/core/trade/HavenoUtils.java index 74caf5d7f2..32a6d76824 100644 --- a/core/src/main/java/haveno/core/trade/HavenoUtils.java +++ b/core/src/main/java/haveno/core/trade/HavenoUtils.java @@ -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 bip39Words = new ArrayList(); // 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 bip39Words = Files.readAllLines(bip39File.toPath(), StandardCharsets.UTF_8); + loadBip39Words(); // select words randomly List passphraseWords = new ArrayList(); @@ -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 prefixes = new ArrayList(); for (String word : words) prefixes.add(word.substring(0, Math.min(word.length(), 4)));