2022 03 03 hdpath fromstring factory exn (#4159)

* Add explicit excpetion when we are unaware of the purpose in a hd path

* Remove test

* Cleanup
This commit is contained in:
Chris Stewart 2022-03-04 06:05:49 -06:00 committed by GitHub
parent 56d0ae68ad
commit c3300aec52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 23 deletions

View File

@ -113,22 +113,6 @@ class HDPathTest extends BitcoinSUnitTest {
}
}
it must "fail to generate a HD path with an invalid purpose field" in {
val badPaths = HDGenerators.bip32Path.suchThat { bip32 =>
bip32.path.nonEmpty &&
!HDPurposes.all.exists(_.constant == bip32.path.head.index)
}
forAll(badPaths) { badPath =>
val attempt = HDPath.fromStringOpt(badPath.toString)
attempt match {
case None =>
succeed
case Some(_) => fail()
}
}
}
it must "fail to generate HD paths with an invalid length" in {
forAll(HDGenerators.hdPathWithConstructor) { case (hd, hdApply) =>
val tooShortPath = hd.path.dropRight(1)

View File

@ -57,11 +57,26 @@ trait HDPath extends BIP32Path {
object HDPath extends StringFactory[HDPath] {
/** Attempts to parse a string into a valid HD path */
override def fromStringT(string: String): Try[HDPath] =
LegacyHDPath
.fromStringT(string)
.orElse(SegWitHDPath.fromStringT(string))
.orElse(NestedSegWitHDPath.fromStringT(string))
override def fromStringT(string: String): Try[HDPath] = {
val path: BIP32Path = BIP32Path.fromString(string)
if (path.path.isEmpty) {
Failure(
new IllegalArgumentException(
s"Cannot parse an empty HDPath, got str=$string"))
} else {
val purpose = path.path.head.index
if (purpose == LegacyHDPath.PURPOSE) {
LegacyHDPath
.fromStringT(string)
} else if (purpose == SegWitHDPath.PURPOSE) {
SegWitHDPath.fromStringT(string)
} else if (purpose == NestedSegWitHDPath.PURPOSE) {
NestedSegWitHDPath.fromStringT(string)
} else {
Failure(new IllegalArgumentException(s"Unknown purpose=$purpose"))
}
}
}
override def fromString(string: String): HDPath = {
fromStringT(string) match {

View File

@ -162,8 +162,10 @@ private[wallet] trait AddressHandling extends WalletLogger {
s"Found previous address at path=${addr.path}, next=$next")
next
case None =>
val chain = account.hdAccount.toChain(chainType)
val address = HDAddress(chain, 0)
val address = account.hdAccount
.toChain(chainType)
.toHDAddress(0)
val path = address.toPath
logger.debug(s"Did not find previous address, next=$path")
path