From 52b8ac68eca7ced8b88d8a32030c9ef6c0e27a1b Mon Sep 17 00:00:00 2001 From: chyngyz Date: Mon, 18 Dec 2023 15:59:22 +0600 Subject: [PATCH] Add method for getting used addresses --- .../io/horizontalsystems/bitcoincore/AbstractKit.kt | 4 ++++ .../io/horizontalsystems/bitcoincore/BitcoinCore.kt | 10 ++++++++++ .../bitcoincore/WatchAddressPublicKeyManager.kt | 2 ++ .../horizontalsystems/bitcoincore/core/Interfaces.kt | 1 + .../bitcoincore/managers/AccountPublicKeyManager.kt | 4 ++++ .../bitcoincore/managers/PublicKeyManager.kt | 4 ++++ .../bitcoincore/models/UsedAddress.kt | 6 ++++++ 7 files changed, 31 insertions(+) create mode 100644 bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/models/UsedAddress.kt diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/AbstractKit.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/AbstractKit.kt index 82ba4574a..e585e0465 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/AbstractKit.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/AbstractKit.kt @@ -89,6 +89,10 @@ abstract class AbstractKit { return bitcoinCore.receiveAddress() } + fun usedAddresses(): List { + return bitcoinCore.usedAddresses() + } + fun receivePublicKey(): PublicKey { return bitcoinCore.receivePublicKey() } diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/BitcoinCore.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/BitcoinCore.kt index 8953cd243..bc2f3b0e7 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/BitcoinCore.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/BitcoinCore.kt @@ -24,6 +24,7 @@ import io.horizontalsystems.bitcoincore.models.PublicKey import io.horizontalsystems.bitcoincore.models.TransactionDataSortType import io.horizontalsystems.bitcoincore.models.TransactionFilterType import io.horizontalsystems.bitcoincore.models.TransactionInfo +import io.horizontalsystems.bitcoincore.models.UsedAddress import io.horizontalsystems.bitcoincore.network.messages.IMessageParser import io.horizontalsystems.bitcoincore.network.messages.IMessageSerializer import io.horizontalsystems.bitcoincore.network.messages.NetworkMessageParser @@ -203,6 +204,15 @@ class BitcoinCore( return addressConverter.convert(publicKeyManager.receivePublicKey(), purpose.scriptType).stringValue } + fun usedAddresses(): List { + return publicKeyManager.usedPublicKeys().map { + UsedAddress( + index = it.index, + address = addressConverter.convert(it, purpose.scriptType).stringValue + ) + }.sortedBy { it.index } + } + fun receivePublicKey(): PublicKey { return publicKeyManager.receivePublicKey() } diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/WatchAddressPublicKeyManager.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/WatchAddressPublicKeyManager.kt index 9ffe26c54..15dbd2867 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/WatchAddressPublicKeyManager.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/WatchAddressPublicKeyManager.kt @@ -20,6 +20,8 @@ class WatchAddressPublicKeyManager( override fun receivePublicKey() = publicKey + override fun usedPublicKeys(): List = listOf(publicKey) + override fun fillGap() { bloomFilterManager?.regenerateBloomFilter() } diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/core/Interfaces.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/core/Interfaces.kt index 8f83be332..59a0842ac 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/core/Interfaces.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/core/Interfaces.kt @@ -252,6 +252,7 @@ interface IAccountWallet { interface IPublicKeyManager { fun changePublicKey(): PublicKey fun receivePublicKey(): PublicKey + fun usedPublicKeys(): List fun fillGap() fun addKeys(keys: List) fun gapShifts(): Boolean diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/AccountPublicKeyManager.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/AccountPublicKeyManager.kt index f0269af23..9b68712e5 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/AccountPublicKeyManager.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/AccountPublicKeyManager.kt @@ -29,6 +29,10 @@ class AccountPublicKeyManager( return getPublicKey(external = true) } + override fun usedPublicKeys(): List { + return storage.getPublicKeysWithUsedState().filter { it.publicKey.external && it.used }.map { it.publicKey } + } + @Throws override fun changePublicKey(): PublicKey { return getPublicKey(external = false) diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/PublicKeyManager.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/PublicKeyManager.kt index dbd2acf74..dbd0204cc 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/PublicKeyManager.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/PublicKeyManager.kt @@ -36,6 +36,10 @@ class PublicKeyManager( return getPublicKey(external = false) } + override fun usedPublicKeys(): List { + return storage.getPublicKeysWithUsedState().filter { it.publicKey.external && it.used }.map { it.publicKey } + } + override fun getPublicKeyByPath(path: String): PublicKey { val parts = path.split("/").map { it.toInt() } diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/models/UsedAddress.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/models/UsedAddress.kt new file mode 100644 index 000000000..b694f7819 --- /dev/null +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/models/UsedAddress.kt @@ -0,0 +1,6 @@ +package io.horizontalsystems.bitcoincore.models + +data class UsedAddress( + val index: Int, + val address: String +)