|
| 1 | +package com.example.bip39.api; |
| 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.assertThrows; |
| 6 | + |
| 7 | +import com.example.bip39.error.Bip39ErrorCode; |
| 8 | +import com.example.bip39.error.Bip39Exception; |
| 9 | +import com.fasterxml.jackson.databind.JsonNode; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.util.HexFormat; |
| 14 | +import java.util.List; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +class MnemonicToSeedTest { |
| 18 | + |
| 19 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 20 | + |
| 21 | + private static final String ZERO_ENTROPY_MNEMONIC = |
| 22 | + "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon" |
| 23 | + + " about"; |
| 24 | + |
| 25 | + @Test |
| 26 | + void matchesAllOfficialEnglishSeedVectorsWithTrezorPassphrase() throws IOException { |
| 27 | + DefaultBip39Service service = new DefaultBip39Service(); |
| 28 | + JsonNode vectors = loadEnglishVectors(); |
| 29 | + |
| 30 | + for (JsonNode vector : vectors) { |
| 31 | + String mnemonic = vector.get(1).textValue(); |
| 32 | + byte[] expectedSeed = HexFormat.of().parseHex(vector.get(2).textValue()); |
| 33 | + |
| 34 | + assertArrayEquals(expectedSeed, service.mnemonicToSeed(mnemonic, "TREZOR")); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void derivesKnownSeedWithEmptyPassphrase() { |
| 40 | + DefaultBip39Service service = new DefaultBip39Service(); |
| 41 | + |
| 42 | + assertEquals( |
| 43 | + "5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc1" |
| 44 | + + "9a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4", |
| 45 | + HexFormat.of().formatHex(service.mnemonicToSeed(ZERO_ENTROPY_MNEMONIC, ""))); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void derivesSeedFromWordListInput() { |
| 50 | + DefaultBip39Service service = new DefaultBip39Service(); |
| 51 | + |
| 52 | + assertArrayEquals( |
| 53 | + service.mnemonicToSeed(ZERO_ENTROPY_MNEMONIC, "TREZOR"), |
| 54 | + service.mnemonicToSeed( |
| 55 | + List.of( |
| 56 | + "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", |
| 57 | + "abandon", "abandon", "abandon", "abandon", "about"), |
| 58 | + "TREZOR")); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void doesNotValidateMnemonicContentForStringInput() { |
| 63 | + DefaultBip39Service service = new DefaultBip39Service(); |
| 64 | + |
| 65 | + assertEquals(64, service.mnemonicToSeed("abandon\tabandon abandon", "").length); |
| 66 | + assertEquals(64, service.mnemonicToSeed("abandon abandon abandon", "TREZOR").length); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void rejectsInvalidListStructureOrNullInputs() { |
| 71 | + DefaultBip39Service service = new DefaultBip39Service(); |
| 72 | + |
| 73 | + assertInvalidFormat(() -> service.mnemonicToSeed((String) null, "")); |
| 74 | + assertInvalidFormat(() -> service.mnemonicToSeed(ZERO_ENTROPY_MNEMONIC, null)); |
| 75 | + assertInvalidFormat(() -> service.mnemonicToSeed((List<String>) null, "")); |
| 76 | + assertInvalidFormat(() -> service.mnemonicToSeed(List.of("abandon", "", "about"), "")); |
| 77 | + assertInvalidFormat(() -> service.mnemonicToSeed(List.of("abandon", "ab andon", "about"), "")); |
| 78 | + } |
| 79 | + |
| 80 | + private static void assertInvalidFormat(ThrowingRunnable action) { |
| 81 | + Bip39Exception exception = assertThrows(Bip39Exception.class, action::run); |
| 82 | + assertEquals(Bip39ErrorCode.ERR_INVALID_MNEMONIC_FORMAT, exception.getErrorCode()); |
| 83 | + } |
| 84 | + |
| 85 | + private static JsonNode loadEnglishVectors() throws IOException { |
| 86 | + try (InputStream inputStream = |
| 87 | + MnemonicToSeedTest.class.getResourceAsStream("/bip39/vectors.json")) { |
| 88 | + if (inputStream == null) { |
| 89 | + throw new IllegalStateException("Missing resource: /bip39/vectors.json"); |
| 90 | + } |
| 91 | + return OBJECT_MAPPER.readTree(inputStream).get("english"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @FunctionalInterface |
| 96 | + private interface ThrowingRunnable { |
| 97 | + void run(); |
| 98 | + } |
| 99 | +} |
0 commit comments