Make KeyManagerAppConfig.walletName return String rather than Option[String] (#4507)

This commit is contained in:
Chris Stewart 2022-07-16 09:33:10 -05:00 committed by GitHub
parent 6f42f83146
commit 948d2b424e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 41 deletions

View file

@ -230,7 +230,7 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
chainConf.addCallbacks(chainCallbacks)
val walletCallbacks =
WebsocketUtil.buildWalletCallbacks(wsQueue, walletConf.walletNameOpt)
WebsocketUtil.buildWalletCallbacks(wsQueue, walletConf.walletName)
walletConf.addCallbacks(walletCallbacks)
val dlcWalletCallbacks = WebsocketUtil.buildDLCWalletCallbacks(wsQueue)
@ -341,7 +341,7 @@ class BitcoinSServerMain(override val serverArgParser: ServerArgParser)(implicit
walletCallbacks = WebsocketUtil.buildWalletCallbacks(
wsQueue,
walletConf.walletNameOpt)
walletConf.walletName)
_ = walletConf.addCallbacks(walletCallbacks)
(wallet, chainCallbacks) <- walletF

View file

@ -1011,7 +1011,7 @@ case class WalletRoutes(wallet: AnyDLCHDWalletApi)(implicit
KeyManagerAppConfig.moduleName -> Obj(
"rootXpub" -> Str(wallet.keyManager.getRootXPub.toString)
),
"walletName" -> Str(walletConf.walletNameOpt.getOrElse("")),
"walletName" -> Str(walletConf.walletName),
"xpub" -> Str(accountDb.xpub.toString),
"hdPath" -> Str(accountDb.hdAccount.toString),
"height" -> Num(walletState.height),

View file

@ -84,8 +84,7 @@ object WebsocketUtil extends Logging {
/** Builds websocket callbacks for the wallet */
def buildWalletCallbacks(
walletQueue: SourceQueueWithComplete[Message],
walletNameOpt: Option[String])(implicit
ec: ExecutionContext): WalletCallbacks = {
walletName: String)(implicit ec: ExecutionContext): WalletCallbacks = {
val onAddressCreated: OnNewAddressGenerated = { addr =>
val notification = WalletNotification.NewAddressNotification(addr)
val json =
@ -118,9 +117,7 @@ object WebsocketUtil extends Logging {
}
val onRescanComplete: OnRescanComplete = { _ =>
val name =
walletNameOpt.getOrElse("") // default name empty string on the wallet
val notification = WalletNotification.RescanComplete(name)
val notification = WalletNotification.RescanComplete(walletName)
val notificationJson =
upickle.default.writeJs(notification)(WsPicklers.rescanPickler)
val msg = TextMessage.Strict(notificationJson.toString())

View file

@ -0,0 +1,14 @@
package org.bitcoins.db
import org.bitcoins.keymanager.config.KeyManagerAppConfig
object PostgresUtil {
def getSchemaName(moduleName: String, walletName: String): String = {
if (walletName == KeyManagerAppConfig.DEFAULT_WALLET_NAME) {
moduleName
} else {
s"${moduleName}_$walletName"
}
}
}

View file

@ -17,6 +17,7 @@ import org.bitcoins.dlc.wallet.models.{
OfferedDbState,
SetupCompleteDLCDbState
}
import org.bitcoins.keymanager.config.KeyManagerAppConfig
import org.bitcoins.wallet.config.WalletAppConfig
import org.bitcoins.wallet.models.TransactionDAO
import org.bitcoins.wallet.{Wallet, WalletLogger}
@ -75,28 +76,30 @@ case class DLCAppConfig(baseDatadir: Path, configOverrides: Vector[Config])(
lazy val walletConf: WalletAppConfig =
WalletAppConfig(baseDatadir, configOverrides)
lazy val walletNameOpt: Option[String] = walletConf.walletNameOpt
lazy val walletName: String = walletConf.walletName
override lazy val dbPath: Path = {
val pathStrOpt =
config.getStringOrNone(s"bitcoin-s.$moduleName.db.path")
(pathStrOpt, walletNameOpt) match {
case (Some(pathStr), Some(walletName)) =>
Paths.get(pathStr).resolve(walletName)
case (Some(pathStr), None) =>
Paths.get(pathStr)
case (None, Some(_)) | (None, None) =>
pathStrOpt match {
case Some(pathStr) =>
if (walletName == KeyManagerAppConfig.DEFAULT_WALLET_NAME) {
Paths.get(pathStr)
} else {
Paths.get(pathStr).resolve(walletName)
}
case None =>
sys.error(s"Could not find dbPath for $moduleName.db.path")
}
}
override lazy val schemaName: Option[String] = {
(driver, walletNameOpt) match {
case (PostgreSQL, Some(walletName)) =>
Some(s"${moduleName}_$walletName")
case (PostgreSQL, None) =>
Some(moduleName)
case (SQLite, None) | (SQLite, Some(_)) =>
driver match {
case PostgreSQL =>
val schema = PostgresUtil.getSchemaName(moduleName = moduleName,
walletName = walletName)
Some(schema)
case SQLite =>
None
}
}

View file

@ -670,7 +670,7 @@ class WalletStorageTest extends BitcoinSWalletTest with BeforeAndAfterEach {
assert(walletConfA.kmConf.seedExists())
val otherWalletName = StringGenerators.genNonEmptyString
.suchThat(_ != walletConfA.walletNameOpt.getOrElse(""))
.suchThat(_ != walletConfA.walletName)
.sampleSome
val walletConfB = walletConfA.withOverrides(

View file

@ -9,6 +9,7 @@ import org.bitcoins.core.wallet.keymanagement.KeyManagerParams
import org.bitcoins.crypto.{AesPassword, CryptoUtil}
import org.bitcoins.keymanager.{ReadMnemonicError, WalletStorage}
import org.bitcoins.keymanager.bip39.BIP39KeyManager
import org.bitcoins.keymanager.config.KeyManagerAppConfig.DEFAULT_WALLET_NAME
import scodec.bits.BitVector
import java.nio.file.{Files, Path}
@ -29,11 +30,11 @@ case class KeyManagerAppConfig(
lazy val networkParameters: NetworkParameters = chain.network
lazy val walletNameOpt: Option[String] = {
lazy val walletName: String = {
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
nameOpt.getOrElse(KeyManagerAppConfig.DEFAULT_WALLET_NAME)
}
lazy val seedFolder: Path = baseDatadir
@ -45,10 +46,10 @@ case class KeyManagerAppConfig(
}
private val seedFileName: String = {
val prefix = walletNameOpt match {
case Some(walletName) =>
s"$walletName-"
case None => ""
val prefix = if (walletName == DEFAULT_WALLET_NAME) {
DEFAULT_WALLET_NAME
} else {
s"$walletName-"
}
s"$prefix${WalletStorage.ENCRYPTED_SEED_FILE_NAME}"
}
@ -214,6 +215,10 @@ case class KeyManagerAppConfig(
}
object KeyManagerAppConfig extends AppConfigFactory[KeyManagerAppConfig] {
/** Default wallet name is the empty string for now */
final val DEFAULT_WALLET_NAME: String = ""
override val moduleName: String = "keymanager"
override def fromDatadir(datadir: Path, confs: Vector[Config])(implicit

View file

@ -155,28 +155,26 @@ case class WalletAppConfig(baseDatadir: Path, configOverrides: Vector[Config])(
lazy val aesPasswordOpt: Option[AesPassword] = kmConf.aesPasswordOpt
lazy val walletNameOpt: Option[String] = kmConf.walletNameOpt
lazy val walletName: String = kmConf.walletName
override lazy val dbPath: Path = {
val pathStrOpt =
config.getStringOrNone(s"bitcoin-s.$moduleName.db.path")
(pathStrOpt, walletNameOpt) match {
case (Some(pathStr), Some(walletName)) =>
pathStrOpt match {
case Some(pathStr) =>
Paths.get(pathStr).resolve(walletName)
case (Some(pathStr), None) =>
Paths.get(pathStr)
case (None, Some(_)) | (None, None) =>
case None =>
sys.error(s"Could not find dbPath for $moduleName.db.path")
}
}
override lazy val schemaName: Option[String] = {
(driver, walletNameOpt) match {
case (PostgreSQL, Some(walletName)) =>
Some(s"${moduleName}_$walletName")
case (PostgreSQL, None) =>
Some(moduleName)
case (SQLite, None) | (SQLite, Some(_)) =>
driver match {
case PostgreSQL =>
val schema = PostgresUtil.getSchemaName(moduleName = moduleName,
walletName = walletName)
Some(schema)
case SQLite =>
None
}
}
@ -195,7 +193,7 @@ case class WalletAppConfig(baseDatadir: Path, configOverrides: Vector[Config])(
isExists <- seedExists()
_ <- {
logger.info(
s"Starting wallet with xpub=${masterXpub} walletName=${walletNameOpt}")
s"Starting wallet with xpub=${masterXpub} walletName=${walletName}")
if (!isExists) {
masterXPubDAO
.create(masterXpub)