mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-23 14:50:42 +01:00
Fix Wallet tests' keymanager to use changed config (#1173)
This commit is contained in:
parent
6a5700ffef
commit
694e727dfc
2 changed files with 57 additions and 21 deletions
|
@ -44,6 +44,12 @@ trait BitcoinSWalletTest extends BitcoinSFixture with WalletLogger {
|
|||
|
||||
def nodeApi: NodeApi = MockNodeApi
|
||||
|
||||
val legacyWalletConf: Config =
|
||||
ConfigFactory.parseString("bitcoin-s.wallet.defaultAccountType = legacy")
|
||||
|
||||
val segwitWalletConf: Config =
|
||||
ConfigFactory.parseString("bitcoin-s.wallet.defaultAccountType = segwit")
|
||||
|
||||
// This is a random block on testnet
|
||||
val testBlockHash = DoubleSha256DigestBE.fromHex(
|
||||
"00000000496dcc754fabd97f3e2df0a7337eab417d75537fecf97a7ebb0e7c75")
|
||||
|
@ -120,7 +126,8 @@ trait BitcoinSWalletTest extends BitcoinSFixture with WalletLogger {
|
|||
/** Lets you customize the parameters for the created wallet */
|
||||
val withNewConfiguredWallet: Config => OneArgAsyncTest => FutureOutcome = {
|
||||
walletConfig =>
|
||||
val km = createNewKeyManager()
|
||||
val newWalletConf = walletAppConfig.withOverrides(walletConfig)
|
||||
val km = createNewKeyManager()(newWalletConf)
|
||||
val bip39PasswordOpt = KeyManagerTestUtil.bip39PasswordOpt
|
||||
makeDependentFixture(
|
||||
build = createNewWallet(keyManager = km,
|
||||
|
@ -144,18 +151,26 @@ trait BitcoinSWalletTest extends BitcoinSFixture with WalletLogger {
|
|||
)(test)
|
||||
}
|
||||
|
||||
def withFundedSegwitWallet(test: OneArgAsyncTest): FutureOutcome = {
|
||||
makeDependentFixture(
|
||||
build = () =>
|
||||
FundWalletUtil.createFundedWallet(nodeApi,
|
||||
chainQueryApi,
|
||||
Some(segwitWalletConf)),
|
||||
destroy = { funded: FundedWallet =>
|
||||
destroyWallet(funded.wallet)
|
||||
}
|
||||
)(test)
|
||||
}
|
||||
|
||||
/** Fixture for an initialized wallet which produce legacy addresses */
|
||||
def withLegacyWallet(test: OneArgAsyncTest): FutureOutcome = {
|
||||
val confOverride =
|
||||
ConfigFactory.parseString("bitcoin-s.wallet.defaultAccountType = legacy")
|
||||
withNewConfiguredWallet(confOverride)(test)
|
||||
withNewConfiguredWallet(legacyWalletConf)(test)
|
||||
}
|
||||
|
||||
/** Fixture for an initialized wallet which produce segwit addresses */
|
||||
def withSegwitWallet(test: OneArgAsyncTest): FutureOutcome = {
|
||||
val confOverride =
|
||||
ConfigFactory.parseString("bitcoin-s.wallet.defaultAccountType = segwit")
|
||||
withNewConfiguredWallet(confOverride)(test)
|
||||
withNewConfiguredWallet(segwitWalletConf)(test)
|
||||
}
|
||||
|
||||
def withNewWallet(test: OneArgAsyncTest): FutureOutcome =
|
||||
|
@ -257,10 +272,10 @@ object BitcoinSWalletTest extends WalletLogger {
|
|||
|
||||
private def createNewKeyManager(
|
||||
bip39PasswordOpt: Option[String] = KeyManagerTestUtil.bip39PasswordOpt)(
|
||||
implicit config: BitcoinSAppConfig): BIP39KeyManager = {
|
||||
val keyManagerE = BIP39KeyManager.initialize(
|
||||
kmParams = config.walletConf.kmParams,
|
||||
bip39PasswordOpt = bip39PasswordOpt)
|
||||
implicit config: WalletAppConfig): BIP39KeyManager = {
|
||||
val keyManagerE = BIP39KeyManager.initialize(kmParams = config.kmParams,
|
||||
bip39PasswordOpt =
|
||||
bip39PasswordOpt)
|
||||
keyManagerE match {
|
||||
case Right(keyManager) => keyManager
|
||||
case Left(err) =>
|
||||
|
@ -301,23 +316,38 @@ object BitcoinSWalletTest extends WalletLogger {
|
|||
}
|
||||
|
||||
/** Creates a wallet with the default configuration */
|
||||
def createDefaultWallet(nodeApi: NodeApi, chainQueryApi: ChainQueryApi)(
|
||||
def createDefaultWallet(
|
||||
nodeApi: NodeApi,
|
||||
chainQueryApi: ChainQueryApi,
|
||||
extraConfig: Option[Config] = None)(
|
||||
implicit config: BitcoinSAppConfig,
|
||||
ec: ExecutionContext): Future[Wallet] = {
|
||||
val bip39PasswordOpt = KeyManagerTestUtil.bip39PasswordOpt
|
||||
val km = createNewKeyManager(bip39PasswordOpt = bip39PasswordOpt)
|
||||
|
||||
val newWalletConf = extraConfig match {
|
||||
case None =>
|
||||
config.walletConf
|
||||
case Some(walletConf) =>
|
||||
config.walletConf.withOverrides(walletConf)
|
||||
}
|
||||
val km =
|
||||
createNewKeyManager(bip39PasswordOpt = bip39PasswordOpt)(newWalletConf)
|
||||
createNewWallet(
|
||||
keyManager = km,
|
||||
bip39PasswordOpt = bip39PasswordOpt,
|
||||
extraConfig = None,
|
||||
extraConfig = extraConfig,
|
||||
nodeApi = nodeApi,
|
||||
chainQueryApi = chainQueryApi)(config, ec)() // get the standard config
|
||||
}
|
||||
|
||||
def createWallet2Accounts(nodeApi: NodeApi, chainQueryApi: ChainQueryApi)(
|
||||
def createWallet2Accounts(
|
||||
nodeApi: NodeApi,
|
||||
chainQueryApi: ChainQueryApi,
|
||||
extraConfig: Option[Config] = None)(
|
||||
implicit config: BitcoinSAppConfig,
|
||||
ec: ExecutionContext): Future[Wallet] = {
|
||||
val defaultWalletF = createDefaultWallet(nodeApi, chainQueryApi)
|
||||
val defaultWalletF =
|
||||
createDefaultWallet(nodeApi, chainQueryApi, extraConfig)
|
||||
for {
|
||||
wallet <- defaultWalletF
|
||||
account1 = WalletTestUtil.getHdAccount1(wallet.walletConfig)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package org.bitcoins.testkit.wallet
|
||||
|
||||
import akka.actor.ActorSystem
|
||||
import com.typesafe.config.Config
|
||||
import org.bitcoins.core.api.{ChainQueryApi, NodeApi}
|
||||
import org.bitcoins.core.currency.{Bitcoins, CurrencyUnit, CurrencyUnits, _}
|
||||
import org.bitcoins.core.hd.HDAccount
|
||||
|
@ -9,7 +10,7 @@ import org.bitcoins.core.protocol.transaction.TransactionOutput
|
|||
import org.bitcoins.server.BitcoinSAppConfig
|
||||
import org.bitcoins.testkit.util.TransactionTestUtil
|
||||
import org.bitcoins.testkit.wallet.FundWalletUtil.FundedWallet
|
||||
import org.bitcoins.wallet.{LockedWallet, Wallet}
|
||||
import org.bitcoins.wallet.Wallet
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
|
||||
|
@ -95,7 +96,7 @@ trait FundWalletUtil {
|
|||
s"got balance=${hdAccount1} expected=${expectedAccount1Amt}"
|
||||
)
|
||||
|
||||
} yield FundedWallet(fundedWallet.asInstanceOf[LockedWallet])
|
||||
} yield FundedWallet(fundedWallet)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,19 +106,24 @@ object FundWalletUtil extends FundWalletUtil {
|
|||
* Account 0 (default account) has utxos of 1,2,3 bitcoin in it (6 btc total)
|
||||
* Account 1 has a utxos of 0.2,0.3,0.5 bitcoin in it (0.6 total)
|
||||
* */
|
||||
case class FundedWallet(wallet: LockedWallet)
|
||||
case class FundedWallet(wallet: Wallet)
|
||||
|
||||
/** This creates a wallet that was two funded accounts
|
||||
* Account 0 (default account) has utxos of 1,2,3 bitcoin in it (6 btc total)
|
||||
* Account 1 has a utxos of 0.2,0.3,0.5 bitcoin in it (1 btc total)
|
||||
* */
|
||||
def createFundedWallet(nodeApi: NodeApi, chainQueryApi: ChainQueryApi)(
|
||||
def createFundedWallet(
|
||||
nodeApi: NodeApi,
|
||||
chainQueryApi: ChainQueryApi,
|
||||
extraConfig: Option[Config] = None)(
|
||||
implicit config: BitcoinSAppConfig,
|
||||
system: ActorSystem): Future[FundedWallet] = {
|
||||
|
||||
import system.dispatcher
|
||||
for {
|
||||
wallet <- BitcoinSWalletTest.createWallet2Accounts(nodeApi, chainQueryApi)
|
||||
wallet <- BitcoinSWalletTest.createWallet2Accounts(nodeApi,
|
||||
chainQueryApi,
|
||||
extraConfig)
|
||||
funded <- FundWalletUtil.fundWallet(wallet)
|
||||
} yield funded
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue