Create AccountHandlingApi, move inheritance from Wallet into HDWalletApi (#5627)

* Create AccountHandlingApi, move inheritance from Wallet.scala into HDWalletApi

* Fix get wallet accounts test
This commit is contained in:
Chris Stewart 2024-08-21 13:23:46 -05:00 committed by GitHub
parent 364df59ea2
commit 4212d6d616
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 47 additions and 15 deletions

View file

@ -650,6 +650,10 @@ class RoutesSpec extends AnyWordSpec with ScalatestRouteTest with MockFactory {
hdAccount = HDAccount(HDCoin(HDPurpose.Legacy, HDCoinType.Testnet), 0) hdAccount = HDAccount(HDCoin(HDPurpose.Legacy, HDCoinType.Testnet), 0)
) )
(() => mockWalletApi.accountHandling)
.expects()
.returning(mockWalletApi)
(() => mockWalletApi.listAccounts()) (() => mockWalletApi.listAccounts())
.expects() .expects()
.returning(Future.successful(Vector(accountDb))) .returning(Future.successful(Vector(accountDb)))

View file

@ -0,0 +1,12 @@
package org.bitcoins.core.api.wallet
import org.bitcoins.core.api.wallet.db.AccountDb
import org.bitcoins.core.hd.AddressType
import scala.concurrent.Future
trait AccountHandlingApi {
def getDefaultAccount(): Future[AccountDb]
def listAccounts(): Future[Vector[AccountDb]]
def getDefaultAccountForType(addressType: AddressType): Future[AccountDb]
}

View file

@ -27,9 +27,10 @@ import scala.concurrent.{ExecutionContext, Future}
* @see * @see
* [[https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki BIP44]] * [[https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki BIP44]]
*/ */
trait HDWalletApi extends WalletApi { trait HDWalletApi extends WalletApi with AccountHandlingApi {
override def keyManager: BIP39KeyManagerApi override def keyManager: BIP39KeyManagerApi
def accountHandling: AccountHandlingApi
/** Gets the balance of the given account */ /** Gets the balance of the given account */
def getBalance(account: HDAccount)(implicit def getBalance(account: HDAccount)(implicit
@ -67,7 +68,8 @@ trait HDWalletApi extends WalletApi {
* @return * @return
* Future[AccountDb] * Future[AccountDb]
*/ */
def getDefaultAccount(): Future[AccountDb] def getDefaultAccount(): Future[AccountDb] =
accountHandling.getDefaultAccount()
/** Fetches the default account for the given address/account kind /** Fetches the default account for the given address/account kind
* @param addressType * @param addressType
@ -475,7 +477,9 @@ trait HDWalletApi extends WalletApi {
chainType: HDChainType, chainType: HDChainType,
addressIndex: Int): Future[AddressDb] addressIndex: Int): Future[AddressDb]
def listAccounts(): Future[Vector[AccountDb]] def listAccounts(): Future[Vector[AccountDb]] = {
accountHandling.listAccounts()
}
/** Lists all wallet accounts with the given type /** Lists all wallet accounts with the given type
* @param purpose * @param purpose
@ -483,8 +487,9 @@ trait HDWalletApi extends WalletApi {
* [[Future[Vector[AccountDb]] * [[Future[Vector[AccountDb]]
*/ */
def listAccounts(purpose: HDPurpose)(implicit def listAccounts(purpose: HDPurpose)(implicit
ec: ExecutionContext): Future[Vector[AccountDb]] = ec: ExecutionContext): Future[Vector[AccountDb]] = {
listAccounts().map(_.filter(_.hdAccount.purpose == purpose)) accountHandling.listAccounts().map(_.filter(_.hdAccount.purpose == purpose))
}
/** Creates a new account with the given purpose */ /** Creates a new account with the given purpose */
def createNewAccount(purpose: HDPurpose): Future[HDWalletApi] def createNewAccount(purpose: HDPurpose): Future[HDWalletApi]

View file

@ -49,7 +49,6 @@ import scala.util.{Failure, Random, Success}
abstract class Wallet abstract class Wallet
extends NeutrinoHDWalletApi extends NeutrinoHDWalletApi
with AddressHandling with AddressHandling
with AccountHandling
with FundTransactionHandling with FundTransactionHandling
with TransactionProcessing with TransactionProcessing
with RescanHandling with RescanHandling
@ -96,6 +95,7 @@ abstract class Wallet
def utxoHandling: UtxoHandling = def utxoHandling: UtxoHandling =
UtxoHandling(spendingInfoDAO, transactionDAO, chainQueryApi) UtxoHandling(spendingInfoDAO, transactionDAO, chainQueryApi)
def accountHandling: AccountHandlingApi = AccountHandling(accountDAO)
def walletCallbacks: WalletCallbacks = walletConfig.callBacks def walletCallbacks: WalletCallbacks = walletConfig.callBacks
@ -1033,6 +1033,11 @@ abstract class Wallet
tx: Transaction): Future[Vector[SpendingInfoDb]] = { tx: Transaction): Future[Vector[SpendingInfoDb]] = {
utxoHandling.unmarkUTXOsAsReserved(tx) utxoHandling.unmarkUTXOsAsReserved(tx)
} }
override def getDefaultAccountForType(
addressType: AddressType): Future[AccountDb] = {
accountHandling.getDefaultAccountForType(addressType)
}
} }
// todo: create multiple wallets, need to maintain multiple databases // todo: create multiple wallets, need to maintain multiple databases

View file

@ -67,6 +67,8 @@ class WalletHolder(initWalletOpt: Option[DLCNeutrinoHDWalletApi])(implicit
} }
} }
override def accountHandling: AccountHandlingApi = wallet.accountHandling
def isInitialized: Boolean = synchronized { def isInitialized: Boolean = synchronized {
walletOpt.isDefined walletOpt.isDefined
} }
@ -653,10 +655,6 @@ class WalletHolder(initWalletOpt: Option[DLCNeutrinoHDWalletApi])(implicit
_.getAddress(account, chainType, addressIndex) _.getAddress(account, chainType, addressIndex)
) )
override def listAccounts(): Future[Vector[AccountDb]] = delegate(
_.listAccounts()
)
override def createNewAccount( override def createNewAccount(
purpose: HDPurpose purpose: HDPurpose
): Future[HDWalletApi] = delegate(_.createNewAccount(purpose)) ): Future[HDWalletApi] = delegate(_.createNewAccount(purpose))

View file

@ -1,22 +1,30 @@
package org.bitcoins.wallet.internal package org.bitcoins.wallet.internal
import org.bitcoins.core.api.wallet.AccountHandlingApi
import org.bitcoins.core.api.wallet.db.AccountDb import org.bitcoins.core.api.wallet.db.AccountDb
import org.bitcoins.core.hd.AddressType._ import org.bitcoins.core.hd.AddressType.*
import org.bitcoins.core.hd._ import org.bitcoins.core.hd.*
import org.bitcoins.core.protocol.blockchain.{ import org.bitcoins.core.protocol.blockchain.{
ChainParams,
MainNetChainParams, MainNetChainParams,
RegTestNetChainParams, RegTestNetChainParams,
SigNetChainParams, SigNetChainParams,
TestNetChainParams TestNetChainParams
} }
import org.bitcoins.wallet.Wallet import org.bitcoins.wallet.config.WalletAppConfig
import org.bitcoins.wallet.models.AccountDAO
import scala.concurrent.Future import scala.concurrent.{ExecutionContext, Future}
/** Provides functionality related enumerating accounts. Account creation does /** Provides functionality related enumerating accounts. Account creation does
* not happen here, as that requires an unlocked wallet. * not happen here, as that requires an unlocked wallet.
*/ */
private[wallet] trait AccountHandling { self: Wallet => case class AccountHandling(accountDAO: AccountDAO)(implicit
walletConfig: WalletAppConfig,
ec: ExecutionContext)
extends AccountHandlingApi {
private val chainParams: ChainParams = walletConfig.chain
/** @inheritdoc */ /** @inheritdoc */
override def listAccounts(): Future[Vector[AccountDb]] = override def listAccounts(): Future[Vector[AccountDb]] =