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

Use blockchair as transactions provider for ecash restore #664

Merged
merged 1 commit into from
Oct 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions ecashkit/src/main/kotlin/io/horizontalsystems/ecash/ECashKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.horizontalsystems.bitcoincore.AbstractKit
import io.horizontalsystems.bitcoincore.BitcoinCore
import io.horizontalsystems.bitcoincore.BitcoinCore.SyncMode
import io.horizontalsystems.bitcoincore.BitcoinCoreBuilder
import io.horizontalsystems.bitcoincore.apisync.BiApiTransactionProvider
import io.horizontalsystems.bitcoincore.apisync.blockchair.BlockchairApi
import io.horizontalsystems.bitcoincore.apisync.blockchair.BlockchairBlockHashFetcher
import io.horizontalsystems.bitcoincore.apisync.blockchair.BlockchairTransactionProvider
Expand Down Expand Up @@ -161,15 +160,18 @@ class ECashKit : AbstractKit {

val checkpoint = Checkpoint.resolveCheckpoint(syncMode, network, storage)
val apiSyncStateManager = ApiSyncStateManager(storage, network.syncableFromApi && syncMode !is SyncMode.Full)
val apiTransactionProvider = apiTransactionProvider(networkType, syncMode, apiSyncStateManager)
val apiTransactionProvider = apiTransactionProvider(networkType)
val paymentAddressParser = PaymentAddressParser("bitcoincash", removeScheme = false)
val blockValidatorSet = blockValidatorSet(networkType, storage)

val bitcoinCore = BitcoinCoreBuilder()
val purpose = Purpose.BIP44
val bitcoinCoreBuilder = BitcoinCoreBuilder()

val bitcoinCore = bitcoinCoreBuilder
.setContext(context)
.setExtendedKey(extendedKey)
.setWatchAddressPublicKey(watchAddressPublicKey)
.setPurpose(Purpose.BIP44)
.setPurpose(purpose)
.setNetwork(network)
.setCheckpoint(checkpoint)
.setPaymentAddressParser(paymentAddressParser)
Expand All @@ -185,7 +187,9 @@ class ECashKit : AbstractKit {
// extending bitcoinCore

bitcoinCore.prependAddressConverter(CashAddressConverter(network.addressSegwitHrp))
bitcoinCore.addRestoreKeyConverter(ECashRestoreKeyConverter())
bitcoinCore.addRestoreKeyConverter(
ECashRestoreKeyConverter(bitcoinCoreBuilder.addressConverter, purpose)
)

return bitcoinCore
}
Expand Down Expand Up @@ -231,26 +235,12 @@ class ECashKit : AbstractKit {
return blockValidatorSet
}

private fun apiTransactionProvider(
networkType: NetworkType,
syncMode: SyncMode,
apiSyncStateManager: ApiSyncStateManager
) = when (networkType) {
private fun apiTransactionProvider(networkType: NetworkType) = when (networkType) {
NetworkType.MainNet -> {
val chronikApiProvider = ChronikApi()
if (syncMode is SyncMode.Blockchair) {
val blockchairApi = BlockchairApi(network.blockchairChainId)
val blockchairBlockHashFetcher = BlockchairBlockHashFetcher(blockchairApi)
val blockchairProvider = BlockchairTransactionProvider(blockchairApi, blockchairBlockHashFetcher)

BiApiTransactionProvider(
restoreProvider = chronikApiProvider,
syncProvider = blockchairProvider,
syncStateManager = apiSyncStateManager
)
} else {
chronikApiProvider
}
val blockchairApi = BlockchairApi(network.blockchairChainId)
val blockchairBlockHashFetcher = BlockchairBlockHashFetcher(blockchairApi)

BlockchairTransactionProvider(blockchairApi, blockchairBlockHashFetcher)
}

NetworkType.TestNet -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package io.horizontalsystems.ecash

import io.horizontalsystems.bitcoincore.core.scriptType
import io.horizontalsystems.bitcoincore.extensions.toHexString
import io.horizontalsystems.bitcoincore.managers.IRestoreKeyConverter
import io.horizontalsystems.bitcoincore.models.PublicKey
import io.horizontalsystems.bitcoincore.utils.AddressConverterChain
import io.horizontalsystems.hdwalletkit.HDWallet

class ECashRestoreKeyConverter: IRestoreKeyConverter {
class ECashRestoreKeyConverter(
private val addressConverter: AddressConverterChain,
private val purpose: HDWallet.Purpose
) : IRestoreKeyConverter {
override fun keysForApiRestore(publicKey: PublicKey): List<String> {
return listOf(publicKey.publicKeyHash.toHexString())
return listOf(
publicKey.publicKeyHash.toHexString(),
addressConverter.convert(publicKey, purpose.scriptType).stringValue
)
}

override fun bloomFilterElements(publicKey: PublicKey): List<ByteArray> {
Expand Down
Loading