Validate bitcoin-s.wallet.walletName config option (#4336)

This commit is contained in:
Chris Stewart 2022-05-17 10:29:17 -05:00 committed by GitHub
parent d60d984a6b
commit 341c712563
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View File

@ -214,4 +214,18 @@ class KeyManagerAppConfigTest extends BitcoinSAsyncTest {
appConfig1.toBip39KeyManager appConfig1.toBip39KeyManager
} }
} }
it must "validate a wallet name" in {
assert(KeyManagerAppConfig.validateWalletName(""))
assert(KeyManagerAppConfig.validateWalletName("old-wallet"))
//weird whitespace
assert(!KeyManagerAppConfig.validateWalletName(" "))
assert(!KeyManagerAppConfig.validateWalletName(" old-wallet"))
//no non alpha-numeric
assert(!KeyManagerAppConfig.validateWalletName("@@@"))
assert(!KeyManagerAppConfig.validateWalletName("old-wallet."))
}
} }

View File

@ -30,7 +30,10 @@ case class KeyManagerAppConfig(
lazy val networkParameters: NetworkParameters = chain.network lazy val networkParameters: NetworkParameters = chain.network
lazy val walletNameOpt: Option[String] = { lazy val walletNameOpt: Option[String] = {
config.getStringOrNone(s"bitcoin-s.wallet.walletName") val nameOpt = config.getStringOrNone(s"bitcoin-s.wallet.walletName")
require(nameOpt.map(KeyManagerAppConfig.validateWalletName).getOrElse(true),
s"Invalid wallet name, only alphanumeric with _, got=$nameOpt")
nameOpt
} }
lazy val seedFolder: Path = baseDatadir lazy val seedFolder: Path = baseDatadir
@ -215,4 +218,10 @@ object KeyManagerAppConfig extends AppConfigFactory[KeyManagerAppConfig] {
override def fromDatadir(datadir: Path, confs: Vector[Config])(implicit override def fromDatadir(datadir: Path, confs: Vector[Config])(implicit
ec: ExecutionContext): KeyManagerAppConfig = ec: ExecutionContext): KeyManagerAppConfig =
KeyManagerAppConfig(datadir, confs) KeyManagerAppConfig(datadir, confs)
def validateWalletName(walletName: String): Boolean = {
walletName.forall { char =>
char.isLetterOrDigit || char == '-' || char == '_'
}
}
} }

View File

@ -404,5 +404,4 @@ object WalletAppConfig
() ()
} }
} }
} }