|
| 1 | +package com.example.bip39.cli; |
| 2 | + |
| 3 | +import com.example.bip39.api.Bip39Service; |
| 4 | +import com.example.bip39.api.DefaultBip39Service; |
| 5 | +import com.example.bip39.entropy.SecureEntropyGenerator; |
| 6 | +import com.example.bip39.error.Bip39Exception; |
| 7 | +import com.example.bip39.model.ValidationResult; |
| 8 | +import com.example.bip39.normalize.MnemonicInputNormalizer; |
| 9 | +import com.example.bip39.util.Bip39Constants; |
| 10 | +import java.io.PrintStream; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.HexFormat; |
| 13 | + |
| 14 | +public final class Bip39Cli { |
| 15 | + |
| 16 | + private static final String MAIN_CLASS = "com.example.bip39.cli.Bip39Cli"; |
| 17 | + |
| 18 | + private final Bip39Service bip39Service; |
| 19 | + private final SecureEntropyGenerator entropyGenerator; |
| 20 | + |
| 21 | + public Bip39Cli() { |
| 22 | + this(new DefaultBip39Service(), new SecureEntropyGenerator()); |
| 23 | + } |
| 24 | + |
| 25 | + Bip39Cli(Bip39Service bip39Service, SecureEntropyGenerator entropyGenerator) { |
| 26 | + this.bip39Service = bip39Service; |
| 27 | + this.entropyGenerator = entropyGenerator; |
| 28 | + } |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + int exitCode = new Bip39Cli().run(args, System.out, System.err); |
| 32 | + if (exitCode != 0) { |
| 33 | + System.exit(exitCode); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + int run(String[] args, PrintStream out, PrintStream err) { |
| 38 | + if (args.length == 0 || isHelpCommand(args[0])) { |
| 39 | + printUsage(out); |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + return switch (args[0]) { |
| 45 | + case "generate" -> runGenerate(Arrays.copyOfRange(args, 1, args.length), out); |
| 46 | + case "to-entropy" -> runToEntropy(Arrays.copyOfRange(args, 1, args.length), out); |
| 47 | + case "to-seed" -> runToSeed(Arrays.copyOfRange(args, 1, args.length), out, err); |
| 48 | + case "validate" -> runValidate(Arrays.copyOfRange(args, 1, args.length), out); |
| 49 | + case "normalize" -> runNormalize(Arrays.copyOfRange(args, 1, args.length), out); |
| 50 | + default -> { |
| 51 | + err.println("Unknown command: " + args[0]); |
| 52 | + printUsage(err); |
| 53 | + yield 2; |
| 54 | + } |
| 55 | + }; |
| 56 | + } catch (IllegalArgumentException exception) { |
| 57 | + err.println(exception.getMessage()); |
| 58 | + return 2; |
| 59 | + } catch (Bip39Exception exception) { |
| 60 | + err.println("errorCode=" + exception.getErrorCode()); |
| 61 | + err.println("message=" + exception.getMessage()); |
| 62 | + return 1; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private int runGenerate(String[] args, PrintStream out) { |
| 67 | + int mnemonicWordCount = 12; |
| 68 | + boolean showEntropy = false; |
| 69 | + |
| 70 | + for (int index = 0; index < args.length; index++) { |
| 71 | + String argument = args[index]; |
| 72 | + if ("--words".equals(argument)) { |
| 73 | + if (index + 1 >= args.length) { |
| 74 | + throw new IllegalArgumentException("Missing value for --words"); |
| 75 | + } |
| 76 | + mnemonicWordCount = Integer.parseInt(args[++index]); |
| 77 | + } else if ("--show-entropy".equals(argument)) { |
| 78 | + showEntropy = true; |
| 79 | + } else { |
| 80 | + throw new IllegalArgumentException("Unknown option for generate: " + argument); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + int numBytes = Bip39Constants.entropyLengthBytesForMnemonicWordCount(mnemonicWordCount); |
| 85 | + byte[] entropy = entropyGenerator.generateEntropy(numBytes); |
| 86 | + String mnemonic = bip39Service.entropyToMnemonic(entropy); |
| 87 | + if (showEntropy) { |
| 88 | + out.println("entropy=" + HexFormat.of().formatHex(entropy)); |
| 89 | + } |
| 90 | + out.println("mnemonic=" + mnemonic); |
| 91 | + return 0; |
| 92 | + } |
| 93 | + |
| 94 | + private int runToEntropy(String[] args, PrintStream out) { |
| 95 | + String mnemonic = requireMnemonicArgument(args, "to-entropy"); |
| 96 | + out.println(HexFormat.of().formatHex(bip39Service.mnemonicToEntropy(mnemonic))); |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + private int runToSeed(String[] args, PrintStream out, PrintStream err) { |
| 101 | + String passphrase = ""; |
| 102 | + int mnemonicStart = 0; |
| 103 | + |
| 104 | + while (mnemonicStart < args.length && args[mnemonicStart].startsWith("--")) { |
| 105 | + String option = args[mnemonicStart]; |
| 106 | + if (!"--passphrase".equals(option)) { |
| 107 | + throw new IllegalArgumentException("Unknown option for to-seed: " + option); |
| 108 | + } |
| 109 | + if (mnemonicStart + 1 >= args.length) { |
| 110 | + throw new IllegalArgumentException("Missing value for --passphrase"); |
| 111 | + } |
| 112 | + passphrase = args[mnemonicStart + 1]; |
| 113 | + mnemonicStart += 2; |
| 114 | + } |
| 115 | + |
| 116 | + if (mnemonicStart >= args.length) { |
| 117 | + printUsage(err); |
| 118 | + throw new IllegalArgumentException("Missing mnemonic for to-seed"); |
| 119 | + } |
| 120 | + |
| 121 | + String mnemonic = joinArgs(Arrays.copyOfRange(args, mnemonicStart, args.length)); |
| 122 | + out.println(HexFormat.of().formatHex(bip39Service.mnemonicToSeed(mnemonic, passphrase))); |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + private int runValidate(String[] args, PrintStream out) { |
| 127 | + ValidationResult validationResult = |
| 128 | + bip39Service.validateMnemonic(requireMnemonicArgument(args, "validate")); |
| 129 | + |
| 130 | + out.println("ok=" + validationResult.ok()); |
| 131 | + out.println("errorCode=" + nullableValue(validationResult.errorCode())); |
| 132 | + out.println("normalizedMnemonic=" + nullableValue(validationResult.normalizedMnemonic())); |
| 133 | + out.println("wordCount=" + nullableValue(validationResult.wordCount())); |
| 134 | + out.println("invalidWord=" + nullableValue(validationResult.invalidWord())); |
| 135 | + return validationResult.ok() ? 0 : 1; |
| 136 | + } |
| 137 | + |
| 138 | + private int runNormalize(String[] args, PrintStream out) { |
| 139 | + out.println( |
| 140 | + MnemonicInputNormalizer.normalizeMnemonicInput(requireMnemonicArgument(args, "normalize"))); |
| 141 | + return 0; |
| 142 | + } |
| 143 | + |
| 144 | + private static String requireMnemonicArgument(String[] args, String command) { |
| 145 | + if (args.length == 0) { |
| 146 | + throw new IllegalArgumentException("Missing mnemonic for " + command); |
| 147 | + } |
| 148 | + return joinArgs(args); |
| 149 | + } |
| 150 | + |
| 151 | + private static String joinArgs(String[] args) { |
| 152 | + return String.join(" ", args); |
| 153 | + } |
| 154 | + |
| 155 | + private static String nullableValue(Object value) { |
| 156 | + return value == null ? "null" : value.toString(); |
| 157 | + } |
| 158 | + |
| 159 | + private static boolean isHelpCommand(String command) { |
| 160 | + return "help".equals(command) || "--help".equals(command) || "-h".equals(command); |
| 161 | + } |
| 162 | + |
| 163 | + private static void printUsage(PrintStream out) { |
| 164 | + out.println("Usage: java -jar target/bip39-java-0.1.0-SNAPSHOT.jar <command> [options]"); |
| 165 | + out.println( |
| 166 | + " or: java -cp target/bip39-java-0.1.0-SNAPSHOT.jar " + MAIN_CLASS + " <command>"); |
| 167 | + out.println(); |
| 168 | + out.println("Commands:"); |
| 169 | + out.println(" generate [--words N] [--show-entropy]"); |
| 170 | + out.println(" to-entropy <mnemonic>"); |
| 171 | + out.println(" to-seed [--passphrase TEXT] <mnemonic>"); |
| 172 | + out.println(" validate <mnemonic>"); |
| 173 | + out.println(" normalize <mnemonic>"); |
| 174 | + out.println(" help"); |
| 175 | + } |
| 176 | +} |
0 commit comments