From 6e3025e1206902d19f1e33c7bb54686d24a0d06e Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Fri, 27 Oct 2023 14:13:44 -0400 Subject: [PATCH] test: add excluded-in-CI live tests --- .github/workflows/test-android.yaml | 11 +-- .github/workflows/test-jvm.yaml | 2 +- .github/workflows/test-python.yaml | 8 +-- .github/workflows/test-swift.yaml | 2 +- .../org/bitcoindevkit/LiveTxBuilderTest.kt | 28 ++++++++ .../org/bitcoindevkit/LiveWalletTest.kt | 19 ++++++ .../bitcoindevkit/OfflineDescriptorTest.kt | 18 +++++ ...AndroidLibTest.kt => OfflineWalletTest.kt} | 25 ++++--- bdk-jvm/lib/build.gradle.kts | 12 ++++ .../kotlin/org/bitcoindevkit/JvmLibTest.kt | 60 ---------------- bdk-python/tests/test_live_tx_builder.py | 38 +++++++++++ bdk-python/tests/test_live_wallet.py | 28 ++++++++ bdk-python/tests/test_offline_descriptor.py | 15 ++++ .../{test_bdk.py => test_offline_wallet.py} | 9 +-- .../BitcoinDevKitTests.swift | 68 ------------------- .../LiveTxBuilderTests.swift | 34 ++++++++++ .../BitcoinDevKitTests/LiveWalletTests.swift | 25 +++++++ .../OfflineDescriptorTests.swift | 20 ++++++ .../OfflineWalletTests.swift | 33 +++++++++ 19 files changed, 295 insertions(+), 160 deletions(-) create mode 100644 bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveTxBuilderTest.kt create mode 100644 bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt create mode 100644 bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineDescriptorTest.kt rename bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/{AndroidLibTest.kt => OfflineWalletTest.kt} (83%) delete mode 100644 bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/JvmLibTest.kt create mode 100644 bdk-python/tests/test_live_tx_builder.py create mode 100644 bdk-python/tests/test_live_wallet.py create mode 100644 bdk-python/tests/test_offline_descriptor.py rename bdk-python/tests/{test_bdk.py => test_offline_wallet.py} (69%) delete mode 100644 bdk-swift/Tests/BitcoinDevKitTests/BitcoinDevKitTests.swift create mode 100644 bdk-swift/Tests/BitcoinDevKitTests/LiveTxBuilderTests.swift create mode 100644 bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift create mode 100644 bdk-swift/Tests/BitcoinDevKitTests/OfflineDescriptorTests.swift create mode 100644 bdk-swift/Tests/BitcoinDevKitTests/OfflineWalletTests.swift diff --git a/.github/workflows/test-android.yaml b/.github/workflows/test-android.yaml index c5ef1bcc..48abe6b1 100644 --- a/.github/workflows/test-android.yaml +++ b/.github/workflows/test-android.yaml @@ -55,11 +55,12 @@ jobs: - name: "Build Android library" run: | cd bdk-android - ./gradlew buildAndroidLib + ./gradlew buildAndroidLib --console=plain -# There are currently no unit tests for bdk-android and the integration tests require the macOS image -# which is not working with the older NDK version we are using, so for now we just make sure that the library builds. -# - name: "Run Android unit tests" +# There are currently no unit tests for bdk-android (see the tests in bdk-jvm instead) and the +# integration tests require the macOS image which is not working with the older NDK version we +# are using, so for now we just make sure that the library builds and omit the connectedTest +# - name: "Run Android connected tests" # run: | # cd bdk-android -# ./gradlew test --console=rich +# ./gradlew connectedAndroidTest --console=plain diff --git a/.github/workflows/test-jvm.yaml b/.github/workflows/test-jvm.yaml index caf07a54..e2c0aa8c 100644 --- a/.github/workflows/test-jvm.yaml +++ b/.github/workflows/test-jvm.yaml @@ -38,4 +38,4 @@ jobs: - name: "Run JVM tests" run: | cd bdk-jvm - ./gradlew test + ./gradlew test -P excludeConnectedTests diff --git a/.github/workflows/test-python.yaml b/.github/workflows/test-python.yaml index 8d6b3691..99cc57fa 100644 --- a/.github/workflows/test-python.yaml +++ b/.github/workflows/test-python.yaml @@ -54,7 +54,7 @@ jobs: run: ${PYBIN}/pip install ./dist/*.whl - name: "Run tests" - run: ${PYBIN}/python -m unittest tests/test_bdk.py --verbose + run: ${PYBIN}/python --module unittest discover --start "./tests/" --pattern "test_offline_*.py" --verbose - name: "Upload artifact test" uses: actions/upload-artifact@v3 @@ -97,7 +97,7 @@ jobs: # - name: "Install wheel and run tests" # run: | # pip3 install ./dist/*.whl - # python3 -m unittest tests/test_bdk.py --verbose + # python3 --module unittest discover --start "./tests/" --pattern "test_offline_*.py" --verbose - name: "Upload artifact test" uses: actions/upload-artifact@v3 @@ -138,7 +138,7 @@ jobs: run: pip3 install ./dist/*.whl - name: "Run tests" - run: python3 -m unittest tests/test_bdk.py --verbose + run: python3 --module unittest discover --start "./tests/" --pattern "test_offline_*.py" --verbose - name: "Upload artifact test" uses: actions/upload-artifact@v3 @@ -186,4 +186,4 @@ jobs: shell: powershell - name: "Run tests" - run: python -m unittest tests/test_bdk.py --verbose + run: python --module unittest discover --start "./tests/" --pattern "test_offline_*.py" --verbose diff --git a/.github/workflows/test-swift.yaml b/.github/workflows/test-swift.yaml index fad668f7..d74728a2 100644 --- a/.github/workflows/test-swift.yaml +++ b/.github/workflows/test-swift.yaml @@ -23,4 +23,4 @@ jobs: - name: "Run Swift tests" working-directory: bdk-swift - run: swift test + run: swift test --skip LiveWalletTests --skip LiveTxBuilderTests diff --git a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveTxBuilderTest.kt b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveTxBuilderTest.kt new file mode 100644 index 00000000..0aa5a804 --- /dev/null +++ b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveTxBuilderTest.kt @@ -0,0 +1,28 @@ +package org.bitcoindevkit + +import org.junit.Test +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class LiveTxBuilderTest { + @Test + fun testTxBuilder() { + 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 update = esploraClient.scan(wallet, 10uL, 1uL) + wallet.applyUpdate(update) + println("Balance: ${wallet.getBalance().total()}") + + assert(wallet.getBalance().total() > 0uL) + + val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET) + val psbt: PartiallySignedTransaction = TxBuilder() + .addRecipient(recipient.scriptPubkey(), 4200uL) + .feeRate(2.0f) + .finish(wallet) + + println(psbt.serialize()) + } +} diff --git a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt new file mode 100644 index 00000000..37cdbcce --- /dev/null +++ b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt @@ -0,0 +1,19 @@ +package org.bitcoindevkit + +import org.junit.Test +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class LiveWalletTest { + @Test + fun testSyncedBalance() { + val descriptor: Descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET) + val wallet: Wallet = Wallet.newNoPersist(descriptor, null, Network.TESTNET) + val esploraClient: 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()}") + } +} diff --git a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineDescriptorTest.kt b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineDescriptorTest.kt new file mode 100644 index 00000000..79cb0efe --- /dev/null +++ b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineDescriptorTest.kt @@ -0,0 +1,18 @@ +package org.bitcoindevkit + +import kotlin.test.Test +import kotlin.test.assertTrue +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class OfflineDescriptorTest { + @Test + fun testDescriptorBip86() { + 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'") + } +} diff --git a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/AndroidLibTest.kt b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineWalletTest.kt similarity index 83% rename from bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/AndroidLibTest.kt rename to bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineWalletTest.kt index 394c60f1..5a3206a0 100644 --- a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/AndroidLibTest.kt +++ b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineWalletTest.kt @@ -1,19 +1,13 @@ package org.bitcoindevkit -import androidx.test.ext.junit.runners.AndroidJUnit4 -import org.junit.runner.RunWith import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.runner.RunWith -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ @RunWith(AndroidJUnit4::class) -class WalletTest { - +class OfflineWalletTest { @Test fun testDescriptorBip86() { val mnemonic: Mnemonic = Mnemonic(WordCount.WORDS12) @@ -36,7 +30,10 @@ class WalletTest { ) val addressInfo: AddressInfo = wallet.getAddress(AddressIndex.New) - assertEquals("tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e", addressInfo.address.asString()) + assertEquals( + expected = "tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e", + actual = addressInfo.address.asString() + ) } @Test @@ -51,7 +48,9 @@ class WalletTest { Network.TESTNET ) - assertEquals(0uL, wallet.getBalance().total()) + assertEquals( + expected = 0uL, + actual = wallet.getBalance().total() + ) } - -} \ No newline at end of file +} diff --git a/bdk-jvm/lib/build.gradle.kts b/bdk-jvm/lib/build.gradle.kts index 41b9bd0b..a3551e06 100644 --- a/bdk-jvm/lib/build.gradle.kts +++ b/bdk-jvm/lib/build.gradle.kts @@ -26,6 +26,18 @@ java { withJavadocJar() } +// This block ensures that the tests that require access to a blockchain are not +// run if the -P excludeConnectedTests flag is passed to gradle. +// This ensures our CI runs are not fickle by not requiring access to testnet. +// This is a workaround until we have a proper regtest setup for the CI. +// Note that the command in the CI is ./gradlew test -P excludeConnectedTests +tasks.test { + if (project.hasProperty("excludeConnectedTests")) { + exclude("**/LiveWalletTest.class") + exclude("**/LiveTxBuilderTest.class") + } +} + testing { suites { val test by getting(JvmTestSuite::class) { diff --git a/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/JvmLibTest.kt b/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/JvmLibTest.kt deleted file mode 100644 index 0d158679..00000000 --- a/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/JvmLibTest.kt +++ /dev/null @@ -1,60 +0,0 @@ -package org.bitcoindevkit - -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -class WalletTest { - - @Test - fun testDescriptorBip86() { - 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 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 = Descriptor( - "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)", - Network.TESTNET - ) - val wallet: Wallet = Wallet.newNoPersist( - descriptor, - null, - Network.TESTNET - ) - - 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()}") - // } - -} diff --git a/bdk-python/tests/test_live_tx_builder.py b/bdk-python/tests/test_live_tx_builder.py new file mode 100644 index 00000000..55bbe390 --- /dev/null +++ b/bdk-python/tests/test_live_tx_builder.py @@ -0,0 +1,38 @@ +import bdkpython as bdk +import unittest + +class TestLiveTxBuilder(unittest.TestCase): + + def test_tx_builder(self): + descriptor: bdk.Descriptor = bdk.Descriptor( + "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)", + bdk.Network.TESTNET + ) + wallet: bdk.Wallet = bdk.Wallet.new_no_persist( + descriptor, + None, + bdk.Network.TESTNET + ) + esploraClient: bdk.EsploraClient = bdk.EsploraClient(url = "https://mempool.space/testnet/api") + update = esploraClient.scan( + wallet = wallet, + stop_gap = 10, + parallel_requests = 1 + ) + wallet.apply_update(update) + + self.assertGreater(wallet.get_balance().total(), 0) + + recipient = bdk.Address( + address = "tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", + network = bdk.Network.TESTNET + ) + + psbt = bdk.TxBuilder().add_recipient(script=recipient.script_pubkey(), amount=4200).fee_rate(2.0).finish(wallet) + # print(psbt.serialize()) + + self.assertTrue(psbt.serialize().startswith("cHNi"), "The PSBT should start with cHNi") + + +if __name__ == '__main__': + unittest.main() diff --git a/bdk-python/tests/test_live_wallet.py b/bdk-python/tests/test_live_wallet.py new file mode 100644 index 00000000..16ee1e90 --- /dev/null +++ b/bdk-python/tests/test_live_wallet.py @@ -0,0 +1,28 @@ +import bdkpython as bdk +import unittest + +class TestLiveWallet(unittest.TestCase): + + def test_synced_balance(self): + descriptor: bdk.Descriptor = bdk.Descriptor( + "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)", + bdk.Network.TESTNET + ) + wallet: bdk.Wallet = bdk.Wallet.new_no_persist( + descriptor, + None, + bdk.Network.TESTNET + ) + esploraClient: bdk.EsploraClient = bdk.EsploraClient(url = "https://mempool.space/testnet/api") + update = esploraClient.scan( + wallet = wallet, + stop_gap = 10, + parallel_requests = 1 + ) + wallet.apply_update(update) + + self.assertGreater(wallet.get_balance().total(), 0) + + +if __name__ == '__main__': + unittest.main() diff --git a/bdk-python/tests/test_offline_descriptor.py b/bdk-python/tests/test_offline_descriptor.py new file mode 100644 index 00000000..a22d19fa --- /dev/null +++ b/bdk-python/tests/test_offline_descriptor.py @@ -0,0 +1,15 @@ +import bdkpython as bdk +import unittest + +class TestSimpleWallet(unittest.TestCase): + + 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) + + self.assertTrue(descriptor.as_string().startswith("tr"), "Bip86 Descriptor does not start with 'tr'") + + +if __name__ == '__main__': + unittest.main() diff --git a/bdk-python/tests/test_bdk.py b/bdk-python/tests/test_offline_wallet.py similarity index 69% rename from bdk-python/tests/test_bdk.py rename to bdk-python/tests/test_offline_wallet.py index d50b6354..49e3c909 100644 --- a/bdk-python/tests/test_bdk.py +++ b/bdk-python/tests/test_offline_wallet.py @@ -3,13 +3,6 @@ class TestSimpleWallet(unittest.TestCase): - 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) - - self.assertTrue(descriptor.as_string().startswith("tr"), "Bip86 Descriptor does not start with 'tr'") - def test_new_address(self): descriptor: bdk.Descriptor = bdk.Descriptor( "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)", @@ -29,7 +22,7 @@ def test_balance(self): "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)", bdk.Network.TESTNET ) - wallet: Wallet = bdk.Wallet.new_no_persist( + wallet: bdk.Wallet = bdk.Wallet.new_no_persist( descriptor, None, bdk.Network.TESTNET diff --git a/bdk-swift/Tests/BitcoinDevKitTests/BitcoinDevKitTests.swift b/bdk-swift/Tests/BitcoinDevKitTests/BitcoinDevKitTests.swift deleted file mode 100644 index 5fd1a4b7..00000000 --- a/bdk-swift/Tests/BitcoinDevKitTests/BitcoinDevKitTests.swift +++ /dev/null @@ -1,68 +0,0 @@ -import XCTest -@testable import BitcoinDevKit - -final class BitcoinDevKitTests: XCTestCase { - - 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.testnet - ) - let wallet: Wallet = try Wallet.newNoPersist( - descriptor: descriptor, - changeDescriptor: nil, - network: .testnet - ) - 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 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) -// -// XCTAssertEqual(wallet.getBalance().total(), 0) -// } - -} diff --git a/bdk-swift/Tests/BitcoinDevKitTests/LiveTxBuilderTests.swift b/bdk-swift/Tests/BitcoinDevKitTests/LiveTxBuilderTests.swift new file mode 100644 index 00000000..d0aee18e --- /dev/null +++ b/bdk-swift/Tests/BitcoinDevKitTests/LiveTxBuilderTests.swift @@ -0,0 +1,34 @@ +import XCTest +@testable import BitcoinDevKit + +final class LiveTxBuilderTests: XCTestCase { + func testTxBuilder() 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(url: "https://mempool.space/testnet/api") + let update = try esploraClient.scan( + wallet: wallet, + stopGap: 10, + parallelRequests: 1 + ) + try wallet.applyUpdate(update: update) + + XCTAssertGreaterThan(wallet.getBalance().total(), UInt64(0), "Wallet must have positive balance, please add funds") + + let recipient: Address = try Address(address: "tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", network: .testnet) + let psbt: PartiallySignedTransaction = try TxBuilder() + .addRecipient(script: recipient.scriptPubkey(), amount: 4200) + .feeRate(satPerVbyte: 2.0) + .finish(wallet: wallet) + + print(psbt.serialize()) + XCTAssertTrue(psbt.serialize().hasPrefix("cHNi"), "PSBT should start with cHNI") + } +} diff --git a/bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift b/bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift new file mode 100644 index 00000000..b8b6fabd --- /dev/null +++ b/bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift @@ -0,0 +1,25 @@ +import XCTest +@testable import BitcoinDevKit + +final class LiveWalletTests: XCTestCase { + 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(url: "https://mempool.space/testnet/api") + let update = try esploraClient.scan( + wallet: wallet, + stopGap: 10, + parallelRequests: 1 + ) + try wallet.applyUpdate(update: update) + + XCTAssertGreaterThan(wallet.getBalance().total(), UInt64(0)) + } +} diff --git a/bdk-swift/Tests/BitcoinDevKitTests/OfflineDescriptorTests.swift b/bdk-swift/Tests/BitcoinDevKitTests/OfflineDescriptorTests.swift new file mode 100644 index 00000000..ab4705cc --- /dev/null +++ b/bdk-swift/Tests/BitcoinDevKitTests/OfflineDescriptorTests.swift @@ -0,0 +1,20 @@ +import XCTest +@testable import BitcoinDevKit + +final class OfflineDescriptorTests: XCTestCase { + 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'") + } +} diff --git a/bdk-swift/Tests/BitcoinDevKitTests/OfflineWalletTests.swift b/bdk-swift/Tests/BitcoinDevKitTests/OfflineWalletTests.swift new file mode 100644 index 00000000..441f032f --- /dev/null +++ b/bdk-swift/Tests/BitcoinDevKitTests/OfflineWalletTests.swift @@ -0,0 +1,33 @@ +import XCTest +@testable import BitcoinDevKit + +final class OfflineWalletTests: XCTestCase { + func testNewAddress() 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 + ) + 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) + } +}