-
Notifications
You must be signed in to change notification settings - Fork 65
feat: Add derivation path child and extend methods #935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ItoroD
wants to merge
1
commit into
bitcoindevkit:master
Choose a base branch
from
ItoroD:derivation-path
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/DerivationPathTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package org.bitcoindevkit | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertTrue | ||
| import kotlin.test.assertFalse | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import org.junit.runner.RunWith | ||
| import kotlin.test.assertFailsWith | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class DerivationPathTest { | ||
| @Test | ||
| fun derivationPathFromString() { | ||
| val pathString = "m/84h/0h/0h/0/0" | ||
| val expectedPath = "84'/0'/0'/0/0" | ||
| val derivationPath = DerivationPath(pathString) | ||
| assertEquals( | ||
| expected = expectedPath, | ||
| actual = derivationPath.toString() | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun derivationPathtoString() { | ||
| val pathString = "m/44h/1h/0h/1/5" | ||
| val expectedPath = "44'/1'/0'/1/5" | ||
| val derivationPath = DerivationPath(pathString) | ||
| assertEquals( | ||
| expected = expectedPath, | ||
| actual = derivationPath.toString() | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun correctlyIdentifiesMaster() { | ||
| val pathString = "m" | ||
| val derivationPath = DerivationPath(pathString) | ||
| assertTrue(derivationPath.isMaster()) | ||
|
|
||
| val nonMasterPathString = "m/0h/1/2" | ||
| val nonMasterDerivationPath = DerivationPath(nonMasterPathString) | ||
| assertFalse(nonMasterDerivationPath.isMaster()) | ||
|
|
||
| val masterPathString = DerivationPath.master() | ||
| assertTrue(masterPathString.isMaster()) | ||
| } | ||
|
|
||
| @Test | ||
| fun invalidDerivationPath(){ | ||
| val invalidPathString = "invalid/path/string" | ||
| assertFailsWith<Bip32Exception.InvalidChildNumberFormat> { DerivationPath(invalidPathString) } | ||
| } | ||
|
|
||
| @Test | ||
| fun createChildDerivationPath() { | ||
| val parentPathString = "m/44h/0h" | ||
| val childIndex = 5u | ||
| val expectedNormalChildPath = "44'/0'/5" | ||
| val parentDerivationPath = DerivationPath(parentPathString) | ||
| val childDerivationPath = parentDerivationPath.child(ChildNumber.Normal(childIndex)) | ||
|
|
||
| assertEquals( | ||
| expected = expectedNormalChildPath, | ||
| actual = childDerivationPath.toString() | ||
| ) | ||
|
|
||
| val expectedHardenedChildPath = "44'/0'/5'" | ||
| val hardenedChildDerivationPath = parentDerivationPath.child(ChildNumber.Hardened(childIndex)) | ||
|
|
||
| assertEquals( | ||
| expected = expectedHardenedChildPath, | ||
| actual = hardenedChildDerivationPath.toString() | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun checkInvalidNormalChildNumberFails(){ | ||
| val parentPathString = "m/44h/0h" | ||
| val aHardenedchildIndex = 2147483648u | ||
| val parentDerivationPath = DerivationPath(parentPathString) | ||
|
|
||
| assertFailsWith<Bip32Exception.InvalidChildNumber> { parentDerivationPath.child(ChildNumber.Normal(aHardenedchildIndex)) } | ||
| } | ||
|
|
||
| @Test | ||
| fun checkInvalidHardenedChildNumberFails(){ | ||
| val parentPathString = "m/44h/0h" | ||
| val alreadyHardenedIndex = 2147483649u | ||
| val parentDerivationPath = DerivationPath(parentPathString) | ||
|
|
||
| assertFailsWith<Bip32Exception.InvalidChildNumber> { parentDerivationPath.child(ChildNumber.Hardened(alreadyHardenedIndex)) } | ||
| } | ||
|
|
||
| @Test | ||
| fun extendDerivationPath(){ | ||
| val basePathString = "m/84h/0h" | ||
| val extensions = "0h/1/2" | ||
|
|
||
| val expectedExtendedPath = "84'/0'/0'/1/2" | ||
|
|
||
| val baseDerivationPath = DerivationPath(basePathString) | ||
| val extensionDerivationPath = DerivationPath(extensions) | ||
|
|
||
| val extendedDerivationPath = baseDerivationPath.extend(extensionDerivationPath) | ||
| assertEquals( | ||
| expected = expectedExtendedPath, | ||
| actual = extendedDerivationPath.toString() | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun conversionToList() { | ||
| val pathString = "m/49h/1h/0h/0/10" | ||
| val derivationPathFromString = DerivationPath(pathString) | ||
|
|
||
| val derivationList = derivationPathFromString.toU32Vec() | ||
|
|
||
| // BIP32 standard (0x80000000 or 2147483648) converted to decimal and added to each hardened index: | ||
|
|
||
| // 49h = 49 + 2147483648 = 2147483697 | ||
| // 1h = 1 + 2147483648 = 2147483649 | ||
| // 0h = 0 + 2147483648 = 2147483648 | ||
| val expectedListString = "[2147483697, 2147483649, 2147483648, 0, 10]" | ||
|
|
||
| assertEquals( | ||
| expected = expectedListString, | ||
| actual = derivationList.toString() | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you set childIndex to 2147483648u and then add
assertFailsWith<Exception> { DerivationPath(childDerivationPath.toString()) }it could fail because the path can be constructed but not parsed back because I think we’re bypassing the BIP32 index checks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh! that is a dangerous catch! Thanks! I will address this tomorrow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good, overall this pr is looking good though 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made some updates. Added new test in relation to these changes too
checkInvalidNormalChildNumberFailsandcheckInvalidHardenedChildNumberFails