Skip to content

Commit

Permalink
Use blockchair as transactions provider for ecash restore
Browse files Browse the repository at this point in the history
  • Loading branch information
abdrasulov committed Oct 21, 2024
1 parent 47db768 commit 13ea2b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
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

0 comments on commit 13ea2b1

Please sign in to comment.