3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 14:54:15 +00:00

added test for HDUtils.formatPath and changed HDUtils.parsePath to handle paths that start with the letter M

This commit is contained in:
Giannis Dzegoutanis 2014-07-09 20:15:34 +02:00 committed by Mike Hearn
parent fec6cbc7df
commit 9446b1b625
2 changed files with 41 additions and 10 deletions

View File

@ -32,8 +32,6 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* Static utilities used in BIP 32 Hierarchical Deterministic Wallets (HDW). * Static utilities used in BIP 32 Hierarchical Deterministic Wallets (HDW).
*/ */
@ -85,7 +83,7 @@ public final class HDUtils {
* Where a single quote (') means hardened key. Spaces are ignored. * Where a single quote (') means hardened key. Spaces are ignored.
*/ */
public static List<ChildNumber> parsePath(@Nonnull String path) { public static List<ChildNumber> parsePath(@Nonnull String path) {
String[] parsedNodes = path.split("/"); String[] parsedNodes = path.replace("M", "").split("/");
List<ChildNumber> nodes = new ArrayList<ChildNumber>(); List<ChildNumber> nodes = new ArrayList<ChildNumber>();
for (String n : parsedNodes) { for (String n : parsedNodes) {

View File

@ -124,28 +124,61 @@ public class HDUtilsTest {
Assert.assertEquals("00000402", HEX.encode(bytes)); Assert.assertEquals("00000402", HEX.encode(bytes));
} }
@Test @Test
public void testParsePath() { public void testFormatPath() {
Object tv[] = { Object tv[] = {
"44' / 0' / 0' / 1 / 1", "M/44'/0'/0'/1/1",
ImmutableList.of(new ChildNumber(44, true), new ChildNumber(0, true), new ChildNumber(0, true), ImmutableList.of(new ChildNumber(44, true), new ChildNumber(0, true), new ChildNumber(0, true),
new ChildNumber(1, false), new ChildNumber(1, false)), new ChildNumber(1, false), new ChildNumber(1, false)),
"/7'/3/3/1'/", "M/7'/3/3/1'",
ImmutableList.of(new ChildNumber(7, true), new ChildNumber(3, false), new ChildNumber(3, false),
new ChildNumber(1, true)),
"M/1'/2'/3'",
ImmutableList.of(new ChildNumber(1, true), new ChildNumber(2, true), new ChildNumber(3, true)),
"M/1/2/3",
ImmutableList.of(new ChildNumber(1, false), new ChildNumber(2, false), new ChildNumber(3, false))
};
for (int i = 0; i < tv.length; i += 2) {
String expectedStrPath = (String) tv[i];
List<ChildNumber> path = (List<ChildNumber>) tv[i+1];
String generatedStrPath = HDUtils.formatPath(path);
Assert.assertEquals(generatedStrPath, expectedStrPath);
}
}
@Test
public void testParsePath() {
Object tv[] = {
"M / 44' / 0' / 0' / 1 / 1",
ImmutableList.of(new ChildNumber(44, true), new ChildNumber(0, true), new ChildNumber(0, true),
new ChildNumber(1, false), new ChildNumber(1, false)),
"M/7'/3/3/1'/",
ImmutableList.of(new ChildNumber(7, true), new ChildNumber(3, false), new ChildNumber(3, false), ImmutableList.of(new ChildNumber(7, true), new ChildNumber(3, false), new ChildNumber(3, false),
new ChildNumber(1, true)), new ChildNumber(1, true)),
"1 ' / 2 ' / 3 ' /", "1 ' / 2 ' / 3 ' /",
ImmutableList.of(new ChildNumber(1, true), new ChildNumber(2, true), new ChildNumber(3, true)), ImmutableList.of(new ChildNumber(1, true), new ChildNumber(2, true), new ChildNumber(3, true)),
"1 ' / 2 ' / 3 ' /", "1 / 2 / 3 /",
ImmutableList.of(new ChildNumber(1, true), new ChildNumber(2, true), new ChildNumber(3, true)) ImmutableList.of(new ChildNumber(1, false), new ChildNumber(2, false), new ChildNumber(3, false))
}; };
for (int i = 0; i < tv.length; i += 2) { for (int i = 0; i < tv.length; i += 2) {
List<ChildNumber> path = HDUtils.parsePath((String) tv[i]); String strPath = (String) tv[i];
List<ChildNumber> expectedPath = (List<ChildNumber>) tv[i+1];
Assert.assertEquals(path, tv[i+1]); List<ChildNumber> path = HDUtils.parsePath(strPath);
Assert.assertEquals(path, expectedPath);
} }
} }
} }