Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions lib/src/test/kotlin/org/bitcoindevkit/Constants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.bitcoindevkit

val BIP84_DESCRIPTOR: Descriptor = Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/1h/0/*)",
Network.TESTNET
)
val BIP84_CHANGE_DESCRIPTOR: Descriptor = Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/1h/1/*)",
Network.TESTNET
)
val BIP86_DESCRIPTOR: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/*)",
Network.TESTNET
)
val BIP86_CHANGE_DESCRIPTOR: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/1/*)",
Network.TESTNET
)
val NON_EXTENDED_DESCRIPTOR_0: Descriptor = Descriptor(
descriptor = "wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/1h/0/0)",
network = Network.TESTNET
)
val NON_EXTENDED_DESCRIPTOR_1: Descriptor = Descriptor(
descriptor = "wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/1h/0/1)",
network = Network.TESTNET
)
58 changes: 58 additions & 0 deletions lib/src/test/kotlin/org/bitcoindevkit/CreatingWalletsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.bitcoindevkit

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

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

@Nested
inner class Success {
@Test
fun `Create WPKH wallet`() {
val wallet: Wallet = Wallet(
descriptor = BIP84_DESCRIPTOR,
changeDescriptor = BIP84_CHANGE_DESCRIPTOR,
network = Network.TESTNET,
persister = conn
)
}

@Test
fun `Create TR wallet`() {
val wallet: Wallet = Wallet(
descriptor = BIP86_DESCRIPTOR,
changeDescriptor = BIP86_CHANGE_DESCRIPTOR,
network = Network.TESTNET,
persister = conn
)
}

@Test
fun `Create a wallet with a non-extended descriptor`() {
val wallet: Wallet = Wallet(
descriptor = NON_EXTENDED_DESCRIPTOR_0,
changeDescriptor = NON_EXTENDED_DESCRIPTOR_1,
network = Network.TESTNET,
persister = conn
)
}
}

@Nested
inner class Failure {
@Test
fun `Descriptors do not match provided network`() {
// The descriptors provided are for Testnet 3, but the wallet attempts to build for mainnet
assertFails {
val wallet: Wallet = Wallet(
descriptor = NON_EXTENDED_DESCRIPTOR_0,
changeDescriptor = NON_EXTENDED_DESCRIPTOR_1,
network = Network.BITCOIN,
persister = conn
)
}
}
}
}
95 changes: 95 additions & 0 deletions lib/src/test/kotlin/org/bitcoindevkit/DescriptorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.bitcoindevkit

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

class DescriptorTest {
@Nested
inner class Success {
@Test
fun `Create extended WPKH descriptors for all networks`() {
val descriptor1: Descriptor = Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/*)",
Network.REGTEST
)
val descriptor2: Descriptor = Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/*)",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is for WPKH I would think the path should be m/84 not m/86.

I am a bit surprised this worked.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the rest as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops yep.

The reason why it works is that these bips are simply conventions. You can use any path you want. And this is what makes those strings dangerous to build manually; we see mistakes like this everywhere, even with very advanced bitcoin devs. This is why BDK developed the descriptor templates.

Good catch!

Network.TESTNET
)
val descriptor3: Descriptor = Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/*)",
Network.TESTNET4
)
val descriptor4: Descriptor = Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/*)",
Network.SIGNET
)
val descriptor5: Descriptor = Descriptor(
"wpkh(xprv9s21ZrQH143K3LRcTnWpaCSYb75ic2rGuSgicmJhSVQSbfaKgPXfa8PhnYszgdcyWLoc8n1E2iHUnskjgGTAyCEpJYv7fqKxUcRNaVngA1V/86h/1h/1h/0/*)",
Network.BITCOIN
)
}

@Test
fun `Create extended TR descriptors for all networks`() {
val descriptor1: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/*)",
Network.REGTEST
)
val descriptor2: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/*)",
Network.TESTNET
)
val descriptor3: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/*)",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make the actual string of the paths constants too. I imagine we will be repeating them every where maybe in new tests too. (Thinking out loud)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I actually like that idea. Let me push some new stuff to potentially clean this up.

Network.TESTNET4
)
val descriptor4: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/*)",
Network.SIGNET
)
val descriptor5: Descriptor = Descriptor(
"tr(xprv9s21ZrQH143K3LRcTnWpaCSYb75ic2rGuSgicmJhSVQSbfaKgPXfa8PhnYszgdcyWLoc8n1E2iHUnskjgGTAyCEpJYv7fqKxUcRNaVngA1V/86h/1h/1h/0/*)",
Network.BITCOIN
)
}

@Test
fun `Create non-extended descriptors for all networks`() {
val descriptor1: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/0)",
Network.REGTEST
)
val descriptor2: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/0)",
Network.TESTNET
)
val descriptor3: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/0)",
Network.TESTNET4
)
val descriptor4: Descriptor = Descriptor(
"tr(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/86h/1h/1h/0/0)",
Network.SIGNET
)
val descriptor5: Descriptor = Descriptor(
"tr(xprv9s21ZrQH143K3LRcTnWpaCSYb75ic2rGuSgicmJhSVQSbfaKgPXfa8PhnYszgdcyWLoc8n1E2iHUnskjgGTAyCEpJYv7fqKxUcRNaVngA1V/86h/1h/1h/0/0)",
Network.BITCOIN
)
}
}

@Nested
inner class Failure {
@Test
fun `Cannot create addr() descriptor`() {
assertFails {
val descriptor: Descriptor = Descriptor(
"addr(tb1qhjys9wxlfykmte7ftryptx975uqgd6kcm6a7z4)",
Network.TESTNET
)
}
}
}
}