Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.test.assertFails

class CreatingWalletTest {
class CreateWalletTest {
private val conn: Persister = Persister.newInMemory()

@Nested
Expand Down
60 changes: 60 additions & 0 deletions lib/src/test/kotlin/org/bitcoindevkit/LoadWalletTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.bitcoindevkit

import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails

class LoadWalletTest {
private val PERSISTENCE_FILE_PATH = run {
val currentDirectory = System.getProperty("user.dir")
val dbFileName = "awesome_wallet_1.sqlite3"
"$currentDirectory/src/test/resources/$dbFileName"
}
val sqlitePersister: Persister = Persister.newSqlite(PERSISTENCE_FILE_PATH)

@Nested
inner class Success {
@Test
fun `Wallet can be loaded from persistence`() {
Wallet.load(
descriptor = TEST_BIP86_DESCRIPTOR,
changeDescriptor = TEST_BIP86_CHANGE_DESCRIPTOR,
persister = sqlitePersister
)
}

@Test
fun `Loaded wallet is at the right revealed index`() {
val wallet = Wallet.load(
descriptor = TEST_BIP86_DESCRIPTOR,
changeDescriptor = TEST_BIP86_CHANGE_DESCRIPTOR,
persister = sqlitePersister
)
val addressInfo: AddressInfo = wallet.revealNextAddress(KeychainKind.EXTERNAL)

assertEquals(
expected = 26u,
actual = addressInfo.index,
)
assertEquals(
expected = "bcrt1pqhqlr5hxya35pmr0en5s3w9jyy9cmj90qwev0d36d6787jm27jzqg9kxse",
actual = addressInfo.address.toString(),
)
}
}

@Nested
inner class Failure {
@Test
fun `Loading a wallet with mismatched descriptors should fail`() {
assertFails {
Wallet.load(
descriptor = TEST_BIP84_DESCRIPTOR,
changeDescriptor = TEST_BIP84_CHANGE_DESCRIPTOR,
persister = sqlitePersister
)
}
}
}
}
41 changes: 0 additions & 41 deletions lib/src/test/kotlin/org/bitcoindevkit/PersistenceTest.kt

This file was deleted.

102 changes: 53 additions & 49 deletions lib/src/test/kotlin/org/bitcoindevkit/WalletTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bitcoindevkit

import org.junit.jupiter.api.Nested
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
Expand All @@ -8,56 +9,59 @@ import kotlin.test.assertFalse
class WalletTest {
val conn: Persister = Persister.newInMemory()

@Test
fun `Wallet produces valid addresses for its network`() {
val wallet: Wallet = Wallet(
descriptor = TEST_BIP84_DESCRIPTOR,
changeDescriptor = TEST_BIP84_CHANGE_DESCRIPTOR,
network = Network.TESTNET4,
persister = conn
)
val addressInfo: AddressInfo = wallet.revealNextAddress(KeychainKind.EXTERNAL)

assertTrue(addressInfo.address.isValidForNetwork(Network.TESTNET), "Address is not valid for Testnet 3 network but it should be")
assertTrue(addressInfo.address.isValidForNetwork(Network.TESTNET4), "Address is not valid for Testnet 4 network but it should be")
assertTrue(addressInfo.address.isValidForNetwork(Network.SIGNET), "Address is not valid for Signet network but it should be")

assertFalse(addressInfo.address.isValidForNetwork(Network.REGTEST), "Address is valid for Regtest network, but it should not be")
assertFalse(addressInfo.address.isValidForNetwork(Network.BITCOIN), "Address is valid for Mainnet network, but it should not be")
}
@Nested
inner class Success {
@Test
fun `Wallet produces valid addresses for its network`() {
val wallet: Wallet = Wallet(
descriptor = TEST_BIP84_DESCRIPTOR,
changeDescriptor = TEST_BIP84_CHANGE_DESCRIPTOR,
network = Network.TESTNET4,
persister = conn
)
val addressInfo: AddressInfo = wallet.revealNextAddress(KeychainKind.EXTERNAL)

@Test
fun `Wallet has 0 balance prior to sync`() {
val wallet: Wallet = Wallet(
descriptor = TEST_BIP84_DESCRIPTOR,
changeDescriptor = TEST_BIP84_CHANGE_DESCRIPTOR,
network = Network.TESTNET4,
persister = conn
)

assertEquals(
expected = 0uL,
actual = wallet.balance().total.toSat()
)
}
assertTrue(addressInfo.address.isValidForNetwork(Network.TESTNET), "Address is not valid for Testnet 3 network but it should be")
assertTrue(addressInfo.address.isValidForNetwork(Network.TESTNET4), "Address is not valid for Testnet 4 network but it should be")
assertTrue(addressInfo.address.isValidForNetwork(Network.SIGNET), "Address is not valid for Signet network but it should be")

assertFalse(addressInfo.address.isValidForNetwork(Network.REGTEST), "Address is valid for Regtest network, but it should not be")
assertFalse(addressInfo.address.isValidForNetwork(Network.BITCOIN), "Address is valid for Mainnet network, but it should not be")
}

@Test
fun `Wallet has 0 balance prior to sync`() {
val wallet: Wallet = Wallet(
descriptor = TEST_BIP84_DESCRIPTOR,
changeDescriptor = TEST_BIP84_CHANGE_DESCRIPTOR,
network = Network.TESTNET4,
persister = conn
)

assertEquals(
expected = 0uL,
actual = wallet.balance().total.toSat()
)
}

// Single-descriptor wallets return an address on the external keychain even if a change descriptor is not provided
// and the wallet.revealNextAddress(KeychainKind.INTERNAL) or wallet.peekAddress(KeychainKind.EXTERNAL, 0u) APIs are
// used.
@Test
fun `Single-descriptor wallets can create addresses`() {
val wallet: Wallet = Wallet.createSingle(
descriptor = TEST_BIP84_DESCRIPTOR,
network = Network.TESTNET4,
persister = conn
)
val address1 = wallet.peekAddress(KeychainKind.EXTERNAL, 0u)
val address2 = wallet.peekAddress(KeychainKind.INTERNAL, 0u)

// Single-descriptor wallets return an address on the external keychain even if a change descriptor is not provided
// and the wallet.revealNextAddress(KeychainKind.INTERNAL) or wallet.peekAddress(KeychainKind.EXTERNAL, 0u) APIs are
// used.
@Test
fun `Single-descriptor wallets can create addresses`() {
val wallet: Wallet = Wallet.createSingle(
descriptor = TEST_BIP84_DESCRIPTOR,
network = Network.TESTNET4,
persister = conn
)
val address1 = wallet.peekAddress(KeychainKind.EXTERNAL, 0u)
val address2 = wallet.peekAddress(KeychainKind.INTERNAL, 0u)

assertEquals(
expected = address1.address,
actual = address2.address,
message = "Addresses should be the same"
)
assertEquals(
expected = address1.address,
actual = address2.address,
message = "Addresses should be the same"
)
}
}
}
10 changes: 10 additions & 0 deletions lib/src/test/resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Readme

The wallet persistence file `awesome_wallet_1.sqlite3` is used for testing. It has:

- A transaction on address index 0
- A transaction on address index 21
- A transaction that spends from these 2 to address index 25
- The wallet has 3 unspent UTXOs
- A total balance of 24,691,201 satoshis
- Its mnemonic is the MNEMONIC_AWESOME constant
Binary file not shown.
Loading