Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Wallet tests' keymanager to use changed config #1173

Merged
merged 1 commit into from Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand All @@ -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 =
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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)
Expand Down
@@ -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
Expand All @@ -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}

Expand Down Expand Up @@ -95,7 +96,7 @@ trait FundWalletUtil {
s"got balance=${hdAccount1} expected=${expectedAccount1Amt}"
)

} yield FundedWallet(fundedWallet.asInstanceOf[LockedWallet])
} yield FundedWallet(fundedWallet)
}
}

Expand All @@ -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
}
Expand Down