Add samples for AddressIndex and AddressInfo

This commit is contained in:
thunderbiscuit 2022-09-22 09:40:39 -04:00
parent 3e96aad10e
commit b9c283c89b
No known key found for this signature in database
GPG Key ID: 88253696EB836462
2 changed files with 40 additions and 6 deletions

View File

@ -22,6 +22,8 @@ enum class Network {
* *
* @property index Child index of this address. * @property index Child index of this address.
* @property address Address. * @property address Address.
*
* @sample org.bitcoindevkit.addressInfoSample
*/ */
data class AddressInfo ( data class AddressInfo (
var index: UInt, var index: UInt,
@ -32,6 +34,8 @@ data class AddressInfo (
* The address index selection strategy to use to derive an address from the wallets external descriptor. * The address index selection strategy to use to derive an address from the wallets external descriptor.
* *
* If youre unsure which one to use, use `AddressIndex.NEW`. * If youre unsure which one to use, use `AddressIndex.NEW`.
*
* @sample org.bitcoindevkit.addressIndexSample
*/ */
enum class AddressIndex { enum class AddressIndex {
/** Return a new address after incrementing the current descriptor index. */ /** 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. * Type that can contain any of the database configurations defined by the library.
* *
* @sample org.bitcoindevkit.memoryDatabaseConfigExample * @sample org.bitcoindevkit.memoryDatabaseConfigSample
* @sample org.bitcoindevkit.sqliteDatabaseConfigExample * @sample org.bitcoindevkit.sqliteDatabaseConfigSample
*/ */
sealed class DatabaseConfig { sealed class DatabaseConfig {
/** Configuration for an in-memory database. */ /** Configuration for an in-memory database. */
@ -113,7 +117,7 @@ data class SledDbConfiguration(
* @property timeout Request timeout (seconds). * @property timeout Request timeout (seconds).
* @property stopGap Stop searching addresses for transactions after finding an unused gap of this length. * @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 ( data class ElectrumConfig (
var url: String, var url: String,

View File

@ -23,7 +23,7 @@ fun balanceSample() {
println("Total wallet balance is ${balance.total}") println("Total wallet balance is ${balance.total}")
} }
fun electrumBlockchainConfigExample() { fun electrumBlockchainConfigSample() {
val blockchainConfig = BlockchainConfig.Electrum( val blockchainConfig = BlockchainConfig.Electrum(
ElectrumConfig( ElectrumConfig(
"ssl://electrum.blockstream.info:60002", "ssl://electrum.blockstream.info:60002",
@ -35,10 +35,40 @@ fun electrumBlockchainConfigExample() {
) )
} }
fun memoryDatabaseConfigExample() { fun memoryDatabaseConfigSample() {
val memoryDatabaseConfig = DatabaseConfig.Memory val memoryDatabaseConfig = DatabaseConfig.Memory
} }
fun sqliteDatabaseConfigExample() { fun sqliteDatabaseConfigSample() {
val databaseConfig = DatabaseConfig.Sqlite(SqliteDbConfiguration("bdk-sqlite")) 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}")
}