Skip to content

Commit

Permalink
Add method for getting used addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
omurovch committed Dec 19, 2023
1 parent 59b8e4e commit 52b8ac6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ abstract class AbstractKit {
return bitcoinCore.receiveAddress()
}

fun usedAddresses(): List<UsedAddress> {
return bitcoinCore.usedAddresses()
}

fun receivePublicKey(): PublicKey {
return bitcoinCore.receivePublicKey()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -203,6 +204,15 @@ class BitcoinCore(
return addressConverter.convert(publicKeyManager.receivePublicKey(), purpose.scriptType).stringValue
}

fun usedAddresses(): List<UsedAddress> {
return publicKeyManager.usedPublicKeys().map {
UsedAddress(
index = it.index,
address = addressConverter.convert(it, purpose.scriptType).stringValue
)
}.sortedBy { it.index }
}

fun receivePublicKey(): PublicKey {
return publicKeyManager.receivePublicKey()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class WatchAddressPublicKeyManager(

override fun receivePublicKey() = publicKey

override fun usedPublicKeys(): List<PublicKey> = listOf(publicKey)

override fun fillGap() {
bloomFilterManager?.regenerateBloomFilter()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ interface IAccountWallet {
interface IPublicKeyManager {
fun changePublicKey(): PublicKey
fun receivePublicKey(): PublicKey
fun usedPublicKeys(): List<PublicKey>
fun fillGap()
fun addKeys(keys: List<PublicKey>)
fun gapShifts(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class AccountPublicKeyManager(
return getPublicKey(external = true)
}

override fun usedPublicKeys(): List<PublicKey> {
return storage.getPublicKeysWithUsedState().filter { it.publicKey.external && it.used }.map { it.publicKey }
}

@Throws
override fun changePublicKey(): PublicKey {
return getPublicKey(external = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class PublicKeyManager(
return getPublicKey(external = false)
}

override fun usedPublicKeys(): List<PublicKey> {
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() }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.horizontalsystems.bitcoincore.models

data class UsedAddress(
val index: Int,
val address: String
)

0 comments on commit 52b8ac6

Please sign in to comment.