From b9c283c89b4cd40e8a97ff2782a1802759e87a9a Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Thu, 22 Sep 2022 09:40:39 -0400 Subject: [PATCH] Add samples for AddressIndex and AddressInfo --- .../src/main/kotlin/org/bitcoindevkit/bdk.kt | 10 ++++-- .../test/kotlin/org/bitcoindevkit/Samples.kt | 36 +++++++++++++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/api-docs/src/main/kotlin/org/bitcoindevkit/bdk.kt b/api-docs/src/main/kotlin/org/bitcoindevkit/bdk.kt index e1b89c2..01759aa 100644 --- a/api-docs/src/main/kotlin/org/bitcoindevkit/bdk.kt +++ b/api-docs/src/main/kotlin/org/bitcoindevkit/bdk.kt @@ -22,6 +22,8 @@ enum class Network { * * @property index Child index of this address. * @property address Address. + * + * @sample org.bitcoindevkit.addressInfoSample */ data class AddressInfo ( var index: UInt, @@ -32,6 +34,8 @@ data class AddressInfo ( * The address index selection strategy to use to derive an address from the wallet’s external descriptor. * * If you’re unsure which one to use, use `AddressIndex.NEW`. + * + * @sample org.bitcoindevkit.addressIndexSample */ enum class AddressIndex { /** Return a new address after incrementing the current descriptor index. */ @@ -70,8 +74,8 @@ data class Balance ( /** * Type that can contain any of the database configurations defined by the library. * - * @sample org.bitcoindevkit.memoryDatabaseConfigExample - * @sample org.bitcoindevkit.sqliteDatabaseConfigExample + * @sample org.bitcoindevkit.memoryDatabaseConfigSample + * @sample org.bitcoindevkit.sqliteDatabaseConfigSample */ sealed class DatabaseConfig { /** Configuration for an in-memory database. */ @@ -113,7 +117,7 @@ data class SledDbConfiguration( * @property timeout Request timeout (seconds). * @property stopGap Stop searching addresses for transactions after finding an unused gap of this length. * - * @sample org.bitcoindevkit.electrumBlockchainConfigExample + * @sample org.bitcoindevkit.electrumBlockchainConfigSample */ data class ElectrumConfig ( var url: String, diff --git a/api-docs/src/test/kotlin/org/bitcoindevkit/Samples.kt b/api-docs/src/test/kotlin/org/bitcoindevkit/Samples.kt index e2f5fd8..b160a68 100644 --- a/api-docs/src/test/kotlin/org/bitcoindevkit/Samples.kt +++ b/api-docs/src/test/kotlin/org/bitcoindevkit/Samples.kt @@ -23,7 +23,7 @@ fun balanceSample() { println("Total wallet balance is ${balance.total}") } -fun electrumBlockchainConfigExample() { +fun electrumBlockchainConfigSample() { val blockchainConfig = BlockchainConfig.Electrum( ElectrumConfig( "ssl://electrum.blockstream.info:60002", @@ -35,10 +35,40 @@ fun electrumBlockchainConfigExample() { ) } -fun memoryDatabaseConfigExample() { +fun memoryDatabaseConfigSample() { val memoryDatabaseConfig = DatabaseConfig.Memory } -fun sqliteDatabaseConfigExample() { +fun sqliteDatabaseConfigSample() { val databaseConfig = DatabaseConfig.Sqlite(SqliteDbConfiguration("bdk-sqlite")) } + +fun addressIndexSample() { + val wallet: Wallet = Wallet( + descriptor = descriptor, + changeDescriptor = changeDescriptor, + network = Network.TESTNET, + databaseConfig = DatabaseConfig.Memory + ) + + fun getLastUnusedAddress(): AddressInfo { + return wallet.getAddress(AddressIndex.LAST_UNUSED) + } +} + +fun addressInfoSample() { + val wallet: Wallet = Wallet( + descriptor = descriptor, + changeDescriptor = changeDescriptor, + network = Network.TESTNET, + databaseConfig = DatabaseConfig.Memory + ) + + fun getLastUnusedAddress(): AddressInfo { + return wallet.getAddress(AddressIndex.NEW) + } + + val newAddress: AddressInfo = getLastUnusedAddress() + + println("New address at index ${newAddress.index} is ${newAddress.address}") +}