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