1+ package org.bitcoindevkit
2+
3+ import kotlin.test.Test
4+ import kotlin.test.assertEquals
5+ import kotlin.test.assertTrue
6+ import kotlin.test.assertFalse
7+ import androidx.test.ext.junit.runners.AndroidJUnit4
8+ import org.junit.runner.RunWith
9+ import kotlin.test.assertFailsWith
10+
11+ @RunWith(AndroidJUnit4 ::class )
12+ class DerivationPathTest {
13+ @Test
14+ fun derivationPathFromString () {
15+ val pathString = " m/84h/0h/0h/0/0"
16+ val expectedPath = " 84'/0'/0'/0/0"
17+ val derivationPath = DerivationPath (pathString)
18+ assertEquals(
19+ expected = expectedPath,
20+ actual = derivationPath.toString()
21+ )
22+ }
23+
24+ @Test
25+ fun derivationPathtoString () {
26+ val pathString = " m/44h/1h/0h/1/5"
27+ val expectedPath = " 44'/1'/0'/1/5"
28+ val derivationPath = DerivationPath (pathString)
29+ assertEquals(
30+ expected = expectedPath,
31+ actual = derivationPath.toString()
32+ )
33+ }
34+
35+ @Test
36+ fun correctlyIdentifiesMaster () {
37+ val pathString = " m"
38+ val derivationPath = DerivationPath (pathString)
39+ assertTrue(derivationPath.isMaster())
40+
41+ val nonMasterPathString = " m/0h/1/2"
42+ val nonMasterDerivationPath = DerivationPath (nonMasterPathString)
43+ assertFalse(nonMasterDerivationPath.isMaster())
44+
45+ val masterPathString = DerivationPath .master()
46+ assertTrue(masterPathString.isMaster())
47+ }
48+
49+ @Test
50+ fun invalidDerivationPath (){
51+ val invalidPathString = " invalid/path/string"
52+ assertFailsWith<Bip32Exception .InvalidChildNumberFormat > { DerivationPath (invalidPathString) }
53+ }
54+
55+ @Test
56+ fun createChildDerivationPath () {
57+ val parentPathString = " m/44h/0h"
58+ val childIndex = 5u
59+ val expectedNormalChildPath = " 44'/0'/5"
60+ val parentDerivationPath = DerivationPath (parentPathString)
61+ val childDerivationPath = parentDerivationPath.child(ChildNumber .Normal (childIndex))
62+
63+ assertEquals(
64+ expected = expectedNormalChildPath,
65+ actual = childDerivationPath.toString()
66+ )
67+
68+ val expectedHardenedChildPath = " 44'/0'/5'"
69+ val hardenedChildDerivationPath = parentDerivationPath.child(ChildNumber .Hardened (childIndex))
70+
71+ assertEquals(
72+ expected = expectedHardenedChildPath,
73+ actual = hardenedChildDerivationPath.toString()
74+ )
75+ }
76+
77+ @Test
78+ fun checkInvalidNormalChildNumberFails (){
79+ val parentPathString = " m/44h/0h"
80+ val aHardenedchildIndex = 2147483648u
81+ val parentDerivationPath = DerivationPath (parentPathString)
82+
83+ assertFailsWith<Bip32Exception .InvalidChildNumber > { parentDerivationPath.child(ChildNumber .Normal (aHardenedchildIndex)) }
84+ }
85+
86+ @Test
87+ fun checkInvalidHardenedChildNumberFails (){
88+ val parentPathString = " m/44h/0h"
89+ val alreadyHardenedIndex = 2147483649u
90+ val parentDerivationPath = DerivationPath (parentPathString)
91+
92+ assertFailsWith<Bip32Exception .InvalidChildNumber > { parentDerivationPath.child(ChildNumber .Hardened (alreadyHardenedIndex)) }
93+ }
94+
95+ @Test
96+ fun extendDerivationPath (){
97+ val basePathString = " m/84h/0h"
98+ val extensions = " 0h/1/2"
99+
100+ val expectedExtendedPath = " 84'/0'/0'/1/2"
101+
102+ val baseDerivationPath = DerivationPath (basePathString)
103+ val extensionDerivationPath = DerivationPath (extensions)
104+
105+ val extendedDerivationPath = baseDerivationPath.extend(extensionDerivationPath)
106+ assertEquals(
107+ expected = expectedExtendedPath,
108+ actual = extendedDerivationPath.toString()
109+ )
110+ }
111+
112+ @Test
113+ fun conversionToList () {
114+ val pathString = " m/49h/1h/0h/0/10"
115+ val derivationPathFromString = DerivationPath (pathString)
116+
117+ val derivationList = derivationPathFromString.toU32Vec()
118+
119+ // BIP32 standard (0x80000000 or 2147483648) converted to decimal and added to each hardened index:
120+
121+ // 49h = 49 + 2147483648 = 2147483697
122+ // 1h = 1 + 2147483648 = 2147483649
123+ // 0h = 0 + 2147483648 = 2147483648
124+ val expectedListString = " [2147483697, 2147483649, 2147483648, 0, 10]"
125+
126+ assertEquals(
127+ expected = expectedListString,
128+ actual = derivationList.toString()
129+ )
130+ }
131+ }
0 commit comments