Skip to content

Commit

Permalink
tests: update all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Oct 27, 2023
1 parent 00cd55b commit 372f79a
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 90 deletions.
3 changes: 2 additions & 1 deletion bdk-android/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ dependencies {
androidTestImplementation("com.github.tony19:logback-android:2.0.0")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
androidTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1")
androidTestImplementation("org.jetbrains.kotlin:kotlin-test:1.6.10")
androidTestImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.6.10")
}

afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,57 @@
package org.bitcoindevkit

import org.junit.Assert.*
import org.junit.Test
import android.app.Application
import android.content.Context.MODE_PRIVATE
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.runner.RunWith
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class AndroidLibTest {
@Test
fun testNetwork() {
val signetNetwork = Network.SIGNET
}
class WalletTest {

@Test
fun testDescriptorBip86() {
val mnemonic = Mnemonic(WordCount.WORDS12)
val descriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null)
val descriptor = Descriptor.newBip86(descriptorSecretKey, KeychainKind.EXTERNAL, Network.TESTNET)
val mnemonic: Mnemonic = Mnemonic(WordCount.WORDS12)
val descriptorSecretKey: DescriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null)
val descriptor: Descriptor = Descriptor.newBip86(descriptorSecretKey, KeychainKind.EXTERNAL, Network.TESTNET)

assertTrue(descriptor.asString().startsWith("tr"), "Bip86 Descriptor does not start with 'tr'")
}

@Test
fun testUsedWallet() {
val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
val wallet = Wallet.newNoPersist(descriptor, null, Network.TESTNET)
val (index, address, keychain) = wallet.getAddress(AddressIndex.LastUnused)
println("Address ${address.asString()} at index $index")
@Test
fun testNewAddress() {
val descriptor: Descriptor = Descriptor(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
Network.TESTNET
)
val wallet: Wallet = Wallet.newNoPersist(
descriptor,
null,
Network.TESTNET
)
val addressInfo: AddressInfo = wallet.getAddress(AddressIndex.New)

assertEquals("tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e", addressInfo.address.asString())
}

@Test
fun testBalance() {
val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
val wallet = Wallet.newNoPersist(descriptor, null, Network.TESTNET)
val descriptor: Descriptor = Descriptor(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
Network.TESTNET
)
val wallet: Wallet = Wallet.newNoPersist(
descriptor,
null,
Network.TESTNET
)

assert(wallet.getBalance().total() == 0uL)
assertEquals(0uL, wallet.getBalance().total())
}

// @Test
// fun testSyncedBalance() {
// val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
// val wallet = Wallet.newNoPersist(descriptor, null, Network.TESTNET)
// val esploraClient = EsploraClient("https://mempool.space/testnet/api")
// // val esploraClient = EsploraClient("https://blockstream.info/testnet/api")
// val update = esploraClient.scan(wallet, 10uL, 1uL)
// wallet.applyUpdate(update)
// println("Balance: ${wallet.getBalance().total()}")
// }
}
}
12 changes: 9 additions & 3 deletions bdk-jvm/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@ java {
withJavadocJar()
}

tasks.withType<Test> {
useJUnitPlatform()
testing {
suites {
val test by getting(JvmTestSuite::class) {
useKotlinTest("1.6.10")
}
}
}

tasks.withType<Test> {
testLogging {
events(PASSED, SKIPPED, FAILED, STANDARD_OUT, STANDARD_ERROR)
exceptionFormat = FULL
showExceptions = true
showCauses = true
showStackTraces = true
showCauses = true
}
}

Expand Down
50 changes: 33 additions & 17 deletions bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/JvmLibTest.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
package org.bitcoindevkit

import org.junit.Test
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class WalletTest {
@Test
fun testNetwork() {
val signetNetwork = Network.SIGNET
}

@Test
fun testDescriptorBip86() {
val mnemonic = Mnemonic(WordCount.WORDS12)
val descriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null)
val descriptor = Descriptor.newBip86(descriptorSecretKey, KeychainKind.EXTERNAL, Network.TESTNET)
val mnemonic: Mnemonic = Mnemonic(WordCount.WORDS12)
val descriptorSecretKey: DescriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null)
val descriptor: Descriptor = Descriptor.newBip86(descriptorSecretKey, KeychainKind.EXTERNAL, Network.TESTNET)

assertTrue(descriptor.asString().startsWith("tr"), "Bip86 Descriptor does not start with 'tr'")
}

@Test
fun testUsedWallet() {
val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
val wallet = Wallet.newNoPersist(descriptor, null, Network.TESTNET)
val (index, address, keychain) = wallet.getAddress(AddressIndex.LastUnused)
println("Address ${address.asString()} at index $index")
@Test
fun testNewAddress() {
val descriptor: Descriptor = Descriptor(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
Network.TESTNET
)
val wallet: Wallet = Wallet.newNoPersist(
descriptor,
null,
Network.TESTNET
)
val addressInfo: AddressInfo = wallet.getAddress(AddressIndex.New)

assertEquals("tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e", addressInfo.address.asString())
}

@Test
fun testBalance() {
val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
val wallet = Wallet.newNoPersist(descriptor, null, Network.TESTNET)
val descriptor: Descriptor = Descriptor(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
Network.TESTNET
)
val wallet: Wallet = Wallet.newNoPersist(
descriptor,
null,
Network.TESTNET
)

assert(wallet.getBalance().total() == 0uL)
assertEquals(0uL, wallet.getBalance().total())
}

// @Test
Expand All @@ -41,4 +56,5 @@ class WalletTest {
// wallet.applyUpdate(update)
// println("Balance: ${wallet.getBalance().total()}")
// }

}
42 changes: 31 additions & 11 deletions bdk-python/tests/test_bdk.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import bdkpython as bdk
import unittest

class TestSimpleWallet(unittest.TestCase):

descriptor = bdk.Descriptor("wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)", bdk.Network.TESTNET)
def test_descriptor_bip86(self):
mnemonic: bdk.Mnemonic = bdk.Mnemonic(bdk.WordCount.WORDS12)
descriptor_secret_key: bdk.DescriptorSecretKey = bdk.DescriptorSecretKey(bdk.Network.TESTNET, mnemonic, None)
descriptor: bdk.Descriptor = bdk.Descriptor.new_bip86(descriptor_secret_key, bdk.KeychainKind.EXTERNAL, bdk.Network.TESTNET)

class TestSimpleBip84Wallet(unittest.TestCase):
self.assertTrue(descriptor.as_string().startswith("tr"), "Bip86 Descriptor does not start with 'tr'")

def test_address_bip84_testnet(self):
wallet = bdk.Wallet.new_no_persist(
descriptor=descriptor,
change_descriptor=None,
network=bdk.Network.TESTNET,
def test_new_address(self):
descriptor: bdk.Descriptor = bdk.Descriptor(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
bdk.Network.TESTNET
)
address_info = wallet.get_address(bdk.AddressIndex.LAST_UNUSED())
address = address_info.address.as_string()
# print(f"New address is {address}")
assert address == "tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e", f"Wrong address {address}, should be tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e"
wallet: Wallet = bdk.Wallet.new_no_persist(
descriptor,
None,
bdk.Network.TESTNET
)
address_info: bdk.AddressInfo = wallet.get_address(bdk.AddressIndex.NEW())

self.assertEqual("tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e", address_info.address.as_string())

def test_balance(self):
descriptor: bdk.Descriptor = bdk.Descriptor(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
bdk.Network.TESTNET
)
wallet: Wallet = bdk.Wallet.new_no_persist(
descriptor,
None,
bdk.Network.TESTNET
)

self.assertEqual(wallet.get_balance().total(), 0)

if __name__ == '__main__':
unittest.main()
79 changes: 57 additions & 22 deletions bdk-swift/Tests/BitcoinDevKitTests/BitcoinDevKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,67 @@ import XCTest
@testable import BitcoinDevKit

final class BitcoinDevKitTests: XCTestCase {
func testMemoryWalletNewAddress() throws {
let desc = try Descriptor(

func testDescriptorBip86() {
let mnemonic: Mnemonic = Mnemonic(wordCount: WordCount.words12)
let descriptorSecretKey: DescriptorSecretKey = DescriptorSecretKey(
network: Network.testnet,
mnemonic: mnemonic,
password: nil
)
let descriptor: Descriptor = Descriptor.newBip86(
secretKey: descriptorSecretKey,
keychain: KeychainKind.external,
network: Network.testnet
)

XCTAssertTrue(descriptor.asString().hasPrefix("tr"), "Bip86 Descriptor does not start with 'tr'")
}

func testNewAddress() throws {
let descriptor: Descriptor = try Descriptor(
descriptor: "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
network: Network.regtest
network: Network.testnet
)
let wallet: Wallet = try Wallet.newNoPersist(
descriptor: descriptor,
changeDescriptor: nil,
network: .testnet
)
let wallet = try Wallet.newNoPersist(descriptor: desc, changeDescriptor: nil, network: .testnet)
let addressInfo = wallet.getAddress(addressIndex: AddressIndex.lastUnused)
let addressInfo: AddressInfo = wallet.getAddress(addressIndex: AddressIndex.new)

XCTAssertEqual(addressInfo.address.asString(), "tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e")
}

func testBalance() throws {
let descriptor: Descriptor = try Descriptor(
descriptor: "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
network: Network.testnet
)
let wallet: Wallet = try Wallet.newNoPersist(
descriptor: descriptor,
changeDescriptor: nil,
network: .testnet
)

XCTAssertEqual(wallet.getBalance().total(), 0)
}

// func testConnectedWalletBalance() throws {
// let descriptor = try Descriptor(
// descriptor: "wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)",
// network: Network.testnet
// )
// let wallet = try Wallet.newNoPersist(
// descriptor: descriptor,
// changeDescriptor: nil,
// network: .testnet
// )
//
// let esploraClient = EsploraClient(url: "https://mempool.space/testnet/api")
// // val esploraClient = EsploraClient("https://blockstream.info/testnet/api")
// let update = try esploraClient.scan(wallet: wallet, stopGap: 10, parallelRequests: 1)
// try wallet.applyUpdate(update: update)
// func testSyncedBalance() throws {
// let descriptor = try Descriptor(
// descriptor: "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
// network: Network.testnet
// )
// let wallet = try Wallet.newNoPersist(
// descriptor: descriptor,
// changeDescriptor: nil,
// network: .testnet
// )
// let esploraClient = EsploraClient("https://mempool.space/testnet/api")
// let update = esploraClient.scan(wallet, 10, 1)
// wallet.applyUpdate(update)
//
// print("Balance: \(wallet.getBalance().total())")
// }
// XCTAssertEqual(wallet.getBalance().total(), 0)
// }

}

0 comments on commit 372f79a

Please sign in to comment.