mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-19 05:43:51 +01:00
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:
parent
56d0ae68ad
commit
c3300aec52
@ -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)
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user