mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-13 19:37:30 +01:00
Create KeyManagerAppConfig (#2268)
* Create KeyManagerAppConfig * Fix name * Use module name
This commit is contained in:
parent
0897ea5da1
commit
79b3d959d3
7 changed files with 92 additions and 37 deletions
|
@ -6,6 +6,7 @@ import com.typesafe.config.{Config, ConfigFactory}
|
|||
import org.bitcoins.chain.config.ChainAppConfig
|
||||
import org.bitcoins.core.util.StartStopAsync
|
||||
import org.bitcoins.db.AppConfig
|
||||
import org.bitcoins.keymanager.config.KeyManagerAppConfig
|
||||
import org.bitcoins.node.config.NodeAppConfig
|
||||
import org.bitcoins.wallet.config.WalletAppConfig
|
||||
|
||||
|
@ -29,12 +30,16 @@ case class BitcoinSAppConfig(
|
|||
lazy val nodeConf: NodeAppConfig = NodeAppConfig(directory, confs: _*)
|
||||
lazy val chainConf: ChainAppConfig = ChainAppConfig(directory, confs: _*)
|
||||
|
||||
lazy val kmConf: KeyManagerAppConfig =
|
||||
KeyManagerAppConfig(directory, confs: _*)
|
||||
|
||||
lazy val bitcoindRpcConf: BitcoindRpcAppConfig =
|
||||
BitcoindRpcAppConfig(directory, confs: _*)
|
||||
|
||||
/** Initializes the wallet, node and chain projects */
|
||||
override def start(): Future[Unit] = {
|
||||
val futures = List(walletConf.start(),
|
||||
val futures = List(kmConf.start(),
|
||||
walletConf.start(),
|
||||
nodeConf.start(),
|
||||
chainConf.start(),
|
||||
bitcoindRpcConf.start())
|
||||
|
|
|
@ -365,11 +365,11 @@ object DLCOracle {
|
|||
case None => decryptedMnemonic
|
||||
}
|
||||
if (!conf.seedExists()) {
|
||||
WalletStorage.writeMnemonicToDisk(conf.seedPath, toWrite)
|
||||
WalletStorage.writeMnemonicToDisk(conf.kmConf.seedPath, toWrite)
|
||||
}
|
||||
|
||||
val key =
|
||||
WalletStorage.getPrivateKeyFromDisk(conf.seedPath,
|
||||
WalletStorage.getPrivateKeyFromDisk(conf.kmConf.seedPath,
|
||||
SegWitMainNetPriv,
|
||||
passwordOpt,
|
||||
bip39PasswordOpt)
|
||||
|
|
|
@ -16,6 +16,7 @@ import org.bitcoins.dlc.oracle.DLCOracle
|
|||
import org.bitcoins.dlc.oracle.storage._
|
||||
import org.bitcoins.keymanager.WalletStorage
|
||||
import org.bitcoins.keymanager.bip39.BIP39KeyManager
|
||||
import org.bitcoins.keymanager.config.KeyManagerAppConfig
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
|
||||
|
@ -42,12 +43,13 @@ case class DLCOracleAppConfig(
|
|||
|
||||
override def baseDatadir: Path = directory
|
||||
|
||||
lazy val kmConf: KeyManagerAppConfig =
|
||||
KeyManagerAppConfig(directory, confs: _*)
|
||||
|
||||
lazy val networkParameters: NetworkParameters = chain.network
|
||||
|
||||
/** The path to our encrypted mnemonic seed */
|
||||
lazy val seedPath: Path = {
|
||||
baseDatadir.resolve(WalletStorage.ENCRYPTED_SEED_FILE_NAME)
|
||||
}
|
||||
lazy val seedPath: Path = kmConf.seedPath
|
||||
|
||||
override def start(): Future[Unit] = {
|
||||
logger.debug(s"Initializing dlc oracle setup")
|
||||
|
@ -99,21 +101,15 @@ case class DLCOracleAppConfig(
|
|||
}
|
||||
|
||||
lazy val kmParams: KeyManagerParams =
|
||||
KeyManagerParams(seedPath, HDPurpose(DLCOracle.R_VALUE_PURPOSE), network)
|
||||
KeyManagerParams(kmConf.seedPath,
|
||||
HDPurpose(DLCOracle.R_VALUE_PURPOSE),
|
||||
network)
|
||||
|
||||
lazy val aesPasswordOpt: Option[AesPassword] = {
|
||||
val passOpt = config.getStringOrNone("bitcoin-s.key-manager.aesPassword")
|
||||
passOpt.flatMap(AesPassword.fromStringOpt)
|
||||
}
|
||||
|
||||
lazy val bip39PasswordOpt: Option[String] = {
|
||||
config.getStringOrNone("bitcoin-s.key-manager.bip39password")
|
||||
}
|
||||
lazy val aesPasswordOpt: Option[AesPassword] = kmConf.aesPasswordOpt
|
||||
lazy val bip39PasswordOpt: Option[String] = kmConf.bip39PasswordOpt
|
||||
|
||||
/** Checks if our oracle as a mnemonic seed associated with it */
|
||||
def seedExists(): Boolean = {
|
||||
WalletStorage.seedExists(seedPath)
|
||||
}
|
||||
def seedExists(): Boolean = kmConf.seedExists()
|
||||
|
||||
def exists(): Boolean = {
|
||||
lazy val hasDb = this.driver match {
|
||||
|
@ -136,7 +132,7 @@ case class DLCOracleAppConfig(
|
|||
}
|
||||
|
||||
val key =
|
||||
WalletStorage.getPrivateKeyFromDisk(seedPath,
|
||||
WalletStorage.getPrivateKeyFromDisk(kmConf.seedPath,
|
||||
SegWitMainNetPriv,
|
||||
aesPasswordOpt,
|
||||
bip39PasswordOpt)
|
||||
|
|
|
@ -162,7 +162,7 @@ bitcoin-s {
|
|||
addressQueueTimeout = 5 seconds
|
||||
}
|
||||
|
||||
key-manager {
|
||||
keymanager {
|
||||
# You can optionally set a BIP 39 password
|
||||
# bip39password = "changeMe"
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ class WalletStorageTest extends BitcoinSWalletTest with BeforeAndAfterEach {
|
|||
withWalletConfig(test)
|
||||
|
||||
def getSeedPath(config: WalletAppConfig): Path = {
|
||||
config.seedPath
|
||||
config.kmConf.seedPath
|
||||
}
|
||||
|
||||
behavior of "WalletStorage"
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
package org.bitcoins.keymanager.config
|
||||
|
||||
import java.nio.file.Path
|
||||
|
||||
import com.typesafe.config.Config
|
||||
import org.bitcoins.core.config.NetworkParameters
|
||||
import org.bitcoins.core.util.FutureUtil
|
||||
import org.bitcoins.crypto.AesPassword
|
||||
import org.bitcoins.db._
|
||||
import org.bitcoins.keymanager.WalletStorage
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
|
||||
case class KeyManagerAppConfig(
|
||||
private val directory: Path,
|
||||
private val confs: Config*)(implicit val ec: ExecutionContext)
|
||||
extends AppConfig {
|
||||
|
||||
override def configOverrides: List[Config] = confs.toList
|
||||
|
||||
override type ConfigType = KeyManagerAppConfig
|
||||
|
||||
override def newConfigOfType(
|
||||
configOverrides: Seq[Config]): KeyManagerAppConfig =
|
||||
KeyManagerAppConfig(directory, configOverrides: _*)
|
||||
|
||||
override def moduleName: String = "keymanager"
|
||||
|
||||
override def baseDatadir: Path = directory
|
||||
|
||||
lazy val networkParameters: NetworkParameters = chain.network
|
||||
|
||||
/** The path to our encrypted mnemonic seed */
|
||||
lazy val seedPath: Path = {
|
||||
baseDatadir.resolve(WalletStorage.ENCRYPTED_SEED_FILE_NAME)
|
||||
}
|
||||
|
||||
override def start(): Future[Unit] = FutureUtil.unit
|
||||
|
||||
lazy val aesPasswordOpt: Option[AesPassword] = {
|
||||
val passOpt = config.getStringOrNone(s"bitcoin-s.$moduleName.aesPassword")
|
||||
passOpt.flatMap(AesPassword.fromStringOpt)
|
||||
}
|
||||
|
||||
lazy val bip39PasswordOpt: Option[String] = {
|
||||
config.getStringOrNone(s"bitcoin-s.$moduleName.bip39password")
|
||||
}
|
||||
|
||||
/** Checks if our key manager as a mnemonic seed associated with it */
|
||||
def seedExists(): Boolean = {
|
||||
WalletStorage.seedExists(seedPath)
|
||||
}
|
||||
}
|
||||
|
||||
object KeyManagerAppConfig extends AppConfigFactory[KeyManagerAppConfig] {
|
||||
|
||||
override def fromDatadir(datadir: Path, confs: Vector[Config])(implicit
|
||||
ec: ExecutionContext): KeyManagerAppConfig =
|
||||
KeyManagerAppConfig(datadir, confs: _*)
|
||||
}
|
|
@ -16,8 +16,8 @@ import org.bitcoins.core.wallet.keymanagement.{
|
|||
import org.bitcoins.crypto.AesPassword
|
||||
import org.bitcoins.db.DatabaseDriver.{PostgreSQL, SQLite}
|
||||
import org.bitcoins.db._
|
||||
import org.bitcoins.keymanager.WalletStorage
|
||||
import org.bitcoins.keymanager.bip39.{BIP39KeyManager, BIP39LockedKeyManager}
|
||||
import org.bitcoins.keymanager.config.KeyManagerAppConfig
|
||||
import org.bitcoins.wallet.db.WalletDbManagement
|
||||
import org.bitcoins.wallet.models.AccountDAO
|
||||
import org.bitcoins.wallet.{Wallet, WalletCallbacks, WalletLogger}
|
||||
|
@ -55,6 +55,9 @@ case class WalletAppConfig(
|
|||
callbacks.atomicUpdate(newCallbacks)(_ + _)
|
||||
}
|
||||
|
||||
lazy val kmConf: KeyManagerAppConfig =
|
||||
KeyManagerAppConfig(directory, conf: _*)
|
||||
|
||||
lazy val defaultAccountKind: HDPurpose =
|
||||
config.getString("bitcoin-s.wallet.defaultAccountType") match {
|
||||
case "legacy" => HDPurposes.Legacy
|
||||
|
@ -105,14 +108,9 @@ case class WalletAppConfig(
|
|||
lazy val feeProviderTargetOpt: Option[Int] =
|
||||
config.getIntOpt("bitcoin-s.fee-provider.target")
|
||||
|
||||
lazy val bip39PasswordOpt: Option[String] = {
|
||||
config.getStringOrNone("bitcoin-s.key-manager.bip39password")
|
||||
}
|
||||
lazy val bip39PasswordOpt: Option[String] = kmConf.bip39PasswordOpt
|
||||
|
||||
lazy val aesPasswordOpt: Option[AesPassword] = {
|
||||
val passOpt = config.getStringOrNone("bitcoin-s.key-manager.aesPassword")
|
||||
passOpt.flatMap(AesPassword.fromStringOpt)
|
||||
}
|
||||
lazy val aesPasswordOpt: Option[AesPassword] = kmConf.aesPasswordOpt
|
||||
|
||||
override def start(): Future[Unit] = {
|
||||
for {
|
||||
|
@ -133,17 +131,13 @@ case class WalletAppConfig(
|
|||
}
|
||||
|
||||
/** The path to our encrypted mnemonic seed */
|
||||
private[bitcoins] def seedPath: Path = {
|
||||
baseDatadir.resolve(WalletStorage.ENCRYPTED_SEED_FILE_NAME)
|
||||
}
|
||||
private[bitcoins] lazy val seedPath: Path = kmConf.seedPath
|
||||
|
||||
/** Checks if our wallet as a mnemonic seed associated with it */
|
||||
def seedExists(): Boolean = {
|
||||
Files.exists(seedPath)
|
||||
}
|
||||
def seedExists(): Boolean = kmConf.seedExists()
|
||||
|
||||
def kmParams: KeyManagerParams =
|
||||
KeyManagerParams(seedPath, defaultAccountKind, network)
|
||||
KeyManagerParams(kmConf.seedPath, defaultAccountKind, network)
|
||||
|
||||
/** How much elements we can have in [[org.bitcoins.wallet.internal.AddressHandling.addressRequestQueue]]
|
||||
* before we throw an exception
|
||||
|
@ -177,7 +171,7 @@ case class WalletAppConfig(
|
|||
private def hasWallet()(implicit
|
||||
walletConf: WalletAppConfig,
|
||||
ec: ExecutionContext): Future[Boolean] = {
|
||||
if (walletConf.seedExists()) {
|
||||
if (kmConf.seedExists()) {
|
||||
val hdCoin = walletConf.defaultAccount.coin
|
||||
val walletDB = walletConf.dbPath resolve walletConf.dbName
|
||||
walletConf.driver match {
|
||||
|
|
Loading…
Add table
Reference in a new issue