|
| 1 | +package com.example.bip39.integration; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +import com.example.bip39.api.Bip39Service; |
| 11 | +import com.example.bip39.api.DefaultBip39Service; |
| 12 | +import com.example.bip39.error.Bip39ErrorCode; |
| 13 | +import com.example.bip39.error.Bip39Exception; |
| 14 | +import com.example.bip39.model.ValidationResult; |
| 15 | +import java.util.HexFormat; |
| 16 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 17 | +import java.util.concurrent.atomic.AtomicReference; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +class MnemonicFlowTest { |
| 21 | + |
| 22 | + private static final String RAW_ZERO_ENTROPY_MNEMONIC = |
| 23 | + " ABANDON\tabandon\nABANDON\rabandon abandon\tabandon\nabandon\rabandon" |
| 24 | + + " abandon\tabandon\nABANDON\rABOUT "; |
| 25 | + |
| 26 | + private static final String NORMALIZED_ZERO_ENTROPY_MNEMONIC = |
| 27 | + "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon" |
| 28 | + + " about"; |
| 29 | + |
| 30 | + private static final String ZERO_ENTROPY_SEED_WITH_TREZOR = |
| 31 | + "c55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e5349553" |
| 32 | + + "1f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04"; |
| 33 | + |
| 34 | + @Test |
| 35 | + void preparesUiInputWhilePreservingOriginalText() { |
| 36 | + MnemonicFlow mnemonicFlow = new MnemonicFlow(new DefaultBip39Service()); |
| 37 | + |
| 38 | + PreparedMnemonicInput preparedMnemonicInput = |
| 39 | + mnemonicFlow.prepareInput(RAW_ZERO_ENTROPY_MNEMONIC); |
| 40 | + |
| 41 | + assertEquals(RAW_ZERO_ENTROPY_MNEMONIC, preparedMnemonicInput.originalInput()); |
| 42 | + assertEquals(NORMALIZED_ZERO_ENTROPY_MNEMONIC, preparedMnemonicInput.normalizedMnemonic()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void validatesNormalizedInputSeparatelyFromDerivation() { |
| 47 | + MnemonicFlow mnemonicFlow = new MnemonicFlow(new DefaultBip39Service()); |
| 48 | + |
| 49 | + ValidationResult validationResult = |
| 50 | + mnemonicFlow.validatePreparedInput(mnemonicFlow.prepareInput(RAW_ZERO_ENTROPY_MNEMONIC)); |
| 51 | + |
| 52 | + assertTrue(validationResult.ok()); |
| 53 | + assertEquals(NORMALIZED_ZERO_ENTROPY_MNEMONIC, validationResult.normalizedMnemonic()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void validatesBeforeDerivingSeedWhenRequested() { |
| 58 | + RecordingBip39Service bip39Service = new RecordingBip39Service(); |
| 59 | + MnemonicFlow mnemonicFlow = new MnemonicFlow(bip39Service); |
| 60 | + PreparedMnemonicInput preparedMnemonicInput = |
| 61 | + new PreparedMnemonicInput("abandon typo", "abandon typo"); |
| 62 | + |
| 63 | + Bip39Exception exception = |
| 64 | + assertThrows( |
| 65 | + Bip39Exception.class, |
| 66 | + () -> mnemonicFlow.validateThenDeriveSeed(preparedMnemonicInput, "TREZOR")); |
| 67 | + |
| 68 | + assertEquals(Bip39ErrorCode.ERR_INVALID_WORD_COUNT, exception.getErrorCode()); |
| 69 | + assertFalse(bip39Service.mnemonicToSeedCalled.get()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void forwardsValidatedSeedToBip32Consumer() { |
| 74 | + MnemonicFlow mnemonicFlow = new MnemonicFlow(new DefaultBip39Service()); |
| 75 | + AtomicReference<byte[]> capturedSeed = new AtomicReference<>(); |
| 76 | + |
| 77 | + byte[] returnedSeed = |
| 78 | + mnemonicFlow.validateThenSendSeedToBip32( |
| 79 | + RAW_ZERO_ENTROPY_MNEMONIC, "TREZOR", capturedSeed::set); |
| 80 | + |
| 81 | + assertEquals(ZERO_ENTROPY_SEED_WITH_TREZOR, HexFormat.of().formatHex(returnedSeed)); |
| 82 | + assertArrayEquals(returnedSeed, capturedSeed.get()); |
| 83 | + assertNotSame(returnedSeed, capturedSeed.get()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void mapsErrorCodesToUiMessages() { |
| 88 | + MnemonicFlow mnemonicFlow = new MnemonicFlow(new DefaultBip39Service()); |
| 89 | + |
| 90 | + assertEquals( |
| 91 | + "Mnemonic contains a word outside the English BIP39 list.", |
| 92 | + mnemonicFlow.uiMessage(Bip39ErrorCode.ERR_WORD_NOT_IN_LIST)); |
| 93 | + assertEquals( |
| 94 | + "Mnemonic checksum does not match the words provided.", |
| 95 | + mnemonicFlow.uiMessage(Bip39ErrorCode.ERR_CHECKSUM_MISMATCH)); |
| 96 | + } |
| 97 | + |
| 98 | + private static final class RecordingBip39Service implements Bip39Service { |
| 99 | + |
| 100 | + private final AtomicBoolean mnemonicToSeedCalled = new AtomicBoolean(false); |
| 101 | + |
| 102 | + @Override |
| 103 | + public String entropyToMnemonic(byte[] entropy) { |
| 104 | + throw new UnsupportedOperationException(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public byte[] mnemonicToEntropy(String mnemonic) { |
| 109 | + throw new UnsupportedOperationException(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public byte[] mnemonicToEntropy(java.util.List<String> words) { |
| 114 | + throw new UnsupportedOperationException(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public ValidationResult validateMnemonic(String mnemonic) { |
| 119 | + return ValidationResult.failure(Bip39ErrorCode.ERR_INVALID_WORD_COUNT, mnemonic, 2, null); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public ValidationResult validateMnemonic(java.util.List<String> words) { |
| 124 | + throw new UnsupportedOperationException(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public byte[] mnemonicToSeed(String mnemonic, String passphrase) { |
| 129 | + mnemonicToSeedCalled.set(true); |
| 130 | + return new byte[64]; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public byte[] mnemonicToSeed(java.util.List<String> words, String passphrase) { |
| 135 | + throw new UnsupportedOperationException(); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments