Compare commits
5 Commits
65b5c0713c
...
f167b704bf
Author | SHA1 | Date | |
---|---|---|---|
f167b704bf | |||
c3d2c68860 | |||
785cd3f713 | |||
8dffe41150 | |||
21660b6c58 |
|
@ -49,6 +49,7 @@ dependencies {
|
||||||
compile 'io.rudefox:vixen:0.0.3'
|
compile 'io.rudefox:vixen:0.0.3'
|
||||||
compile 'info.picocli:picocli:4.5.1'
|
compile 'info.picocli:picocli:4.5.1'
|
||||||
compile 'com.google.zxing:core:3.4.0'
|
compile 'com.google.zxing:core:3.4.0'
|
||||||
|
compile 'com.diogonunes:JColor:5.0.0'
|
||||||
testCompile 'com.bjdweck.test:commons-test:0.0.1'
|
testCompile 'com.bjdweck.test:commons-test:0.0.1'
|
||||||
testCompile "org.junit.jupiter:junit-jupiter-params:$junitVersion"
|
testCompile "org.junit.jupiter:junit-jupiter-params:$junitVersion"
|
||||||
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
|
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
|
||||||
|
|
|
@ -1,23 +1,32 @@
|
||||||
package io.rudefox.burrow;
|
package io.rudefox.burrow;
|
||||||
|
|
||||||
import com.bjdweck.bitcoin.mnemonic.Entropy;
|
import com.bjdweck.bitcoin.mnemonic.Entropy;
|
||||||
|
import com.diogonunes.jcolor.AnsiFormat;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import static com.diogonunes.jcolor.Attribute.*;
|
||||||
|
|
||||||
public class Dice8EntropyGenerator {
|
public class Dice8EntropyGenerator {
|
||||||
|
|
||||||
|
public static final AnsiFormat RED_STYLE = new AnsiFormat(WHITE_TEXT(), RED_BACK(), BOLD());
|
||||||
|
public static final AnsiFormat GREEN_STYLE = new AnsiFormat(BLACK_TEXT(), GREEN_BACK(), BOLD());
|
||||||
|
public static final AnsiFormat BLUE_STYLE = new AnsiFormat(WHITE_TEXT(), BLUE_BACK(), BOLD());
|
||||||
|
|
||||||
public static final int DICE_PER_ROLL = 11;
|
public static final int DICE_PER_ROLL = 11;
|
||||||
|
|
||||||
private final int targetBitsOfEntropy;
|
private final int targetBitsOfEntropy;
|
||||||
private final Scanner inputScanner;
|
private final Scanner inputScanner;
|
||||||
|
private final boolean ansiColorOutput;
|
||||||
|
|
||||||
private Entropy entropy = new Entropy();
|
private Entropy entropy = new Entropy();
|
||||||
|
|
||||||
public Dice8EntropyGenerator(int targetBitsOfEntropy, Scanner inputScanner) {
|
public Dice8EntropyGenerator(int targetBitsOfEntropy, Scanner inputScanner, boolean ansiColorOutput) {
|
||||||
|
|
||||||
this.targetBitsOfEntropy = targetBitsOfEntropy;
|
this.targetBitsOfEntropy = targetBitsOfEntropy;
|
||||||
this.inputScanner = inputScanner;
|
this.inputScanner = inputScanner;
|
||||||
|
this.ansiColorOutput = ansiColorOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
Entropy generate() {
|
Entropy generate() {
|
||||||
|
@ -81,18 +90,42 @@ public class Dice8EntropyGenerator {
|
||||||
int entropyBitIndex = entropyBitString.length() - currentRollSetBitString.length() + rollSetBitIndex;
|
int entropyBitIndex = entropyBitString.length() - currentRollSetBitString.length() + rollSetBitIndex;
|
||||||
|
|
||||||
if (entropyBitIndex < targetBitsOfEntropy)
|
if (entropyBitIndex < targetBitsOfEntropy)
|
||||||
rollSetBitsLine.append(currentRollSetBitString.charAt(rollSetBitIndex));
|
rollSetBitsLine.append(styleBit("" + currentRollSetBitString.charAt(rollSetBitIndex), entropyBitIndex, rollSetBitIndex));
|
||||||
else
|
else
|
||||||
rollSetBitsLine.append("-");
|
rollSetBitsLine.append("-");
|
||||||
|
|
||||||
if (rollSetBitIndex == 32) rollSetBitsLine.append("|");
|
if (rollSetBitIndex == 32)
|
||||||
else if (rollSetBitIndex % 3 == 2) rollSetBitsLine.append(" ");
|
rollSetBitsLine.append("|");
|
||||||
else if (rollSetBitIndex % 11 == 10) rollSetBitsLine.append(" | ");
|
else if (rollSetBitIndex % 3 == 2)
|
||||||
|
rollSetBitsLine.append(styleBit(" ", entropyBitIndex, rollSetBitIndex));
|
||||||
|
else if (rollSetBitIndex % 11 == 10)
|
||||||
|
rollSetBitsLine.append(" | ");
|
||||||
}
|
}
|
||||||
|
|
||||||
return rollSetBitsLine;
|
return rollSetBitsLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String styleBit(String bit, int globalEntropyBitIndex, int currentRollSetBitIndex) {
|
||||||
|
|
||||||
|
if (!ansiColorOutput || globalEntropyBitIndex >= targetBitsOfEntropy)
|
||||||
|
return bit;
|
||||||
|
|
||||||
|
return getBitFormat(currentRollSetBitIndex).format(bit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private AnsiFormat getBitFormat(int rollSetBitIndex) {
|
||||||
|
|
||||||
|
int wordBitIndex = rollSetBitIndex % 11;
|
||||||
|
|
||||||
|
if (wordBitIndex == 0)
|
||||||
|
return RED_STYLE;
|
||||||
|
|
||||||
|
if (wordBitIndex <= 6)
|
||||||
|
return GREEN_STYLE;
|
||||||
|
|
||||||
|
return BLUE_STYLE;
|
||||||
|
}
|
||||||
|
|
||||||
private String getSeedWordsLine(int rollSet) {
|
private String getSeedWordsLine(int rollSet) {
|
||||||
|
|
||||||
int baseIndex = rollSet * 3;
|
int baseIndex = rollSet * 3;
|
||||||
|
|
|
@ -85,7 +85,7 @@ public class MnemonicCommand implements Runnable {
|
||||||
return getDice6EntropyInteractive(diceEventBuffer);
|
return getDice6EntropyInteractive(diceEventBuffer);
|
||||||
|
|
||||||
if (isDice8InteractiveMode())
|
if (isDice8InteractiveMode())
|
||||||
return new Dice8EntropyGenerator(targetBitsOfEntropy, new Scanner(System.in)).generate();
|
return new Dice8EntropyGenerator(targetBitsOfEntropy, new Scanner(System.in), CommandLine.Help.Ansi.AUTO.enabled()).generate();
|
||||||
|
|
||||||
diceEventBuffer.appendEvents(entropyOptions.eventMethod.getEventString);
|
diceEventBuffer.appendEvents(entropyOptions.eventMethod.getEventString);
|
||||||
return diceEventBuffer.toEntropy();
|
return diceEventBuffer.toEntropy();
|
||||||
|
|
|
@ -10,6 +10,8 @@ class mnemonic_8_sided_dice_tests extends CliTestFixture {
|
||||||
@Test
|
@Test
|
||||||
void with_arguments_interactive_8_sided_dice_should_generate_mnemonic_sentence() throws UnsupportedEncodingException {
|
void with_arguments_interactive_8_sided_dice_should_generate_mnemonic_sentence() throws UnsupportedEncodingException {
|
||||||
|
|
||||||
|
System.setProperty("picocli.ansi", "false");
|
||||||
|
|
||||||
withArgs("mnemonic -i8 --bits 128");
|
withArgs("mnemonic -i8 --bits 128");
|
||||||
|
|
||||||
expectedOutput("Input 11 x 8-sided dice rolls [1-8]: ");
|
expectedOutput("Input 11 x 8-sided dice rolls [1-8]: ");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user