Skip to content

Commit 01d0b55

Browse files
committed
refactor: Remove hardcoded file path
Since we should be able to run examples from cmd, when in a different directory from the examples, file path issues might occur if hardcoded.
1 parent 594d174 commit 01d0b55

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

bdk-jvm/examples/src/main/kotlin/MultisigTransaction.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,16 @@ fun getNewWallet(script: ActiveWalletScriptType, network: Network): Wallet {
152152
}
153153

154154
fun generateUniquePersistenceFilePath(): String {
155-
val randomSuffix = (1..100000).random() // Generate a unique random number for the path
156-
val currentDirectory = Paths.get("").toAbsolutePath().toString() + "/bdk-jvm/examples/src/main/kotlin/bdk_persistence_$randomSuffix.sqlite"
157-
return currentDirectory
155+
// Resolve the absolute path to the fixed `examples/data` directory
156+
val projectRoot = Paths.get("").toAbsolutePath().parent.resolve("examples")
157+
val persistenceDirectory = projectRoot.resolve("data").toFile()
158+
159+
persistenceDirectory.apply {
160+
if (!exists()) mkdirs()
161+
}
162+
163+
// Generate a file path with a unique random suffix
164+
val randomSuffix = (1..100000).random() // Generate a random number
165+
val persistenceFilePath = projectRoot.resolve("data/bdk_persistence_$randomSuffix.sqlite").toString()
166+
return persistenceFilePath
158167
}

bdk-jvm/examples/src/main/kotlin/WalletSetupBip32.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,20 @@ fun createScriptAppropriateDescriptor(
6464
)
6565
}
6666
}
67-
6867
fun getPersistenceFilePath(): String {
69-
val currentDirectory = Paths.get("").toAbsolutePath().toString() + "/bdk-jvm/examples/src/main/kotlin/bdk_persistence.sqlite"
70-
File(currentDirectory).apply {
68+
// Resolve absolute path to the fixed `examples/data` directory
69+
val projectRoot = Paths.get("").toAbsolutePath().parent.resolve("examples")
70+
val persistenceDirectory = projectRoot.resolve("data").toFile()
71+
val persistenceFilePath = projectRoot.resolve("data/bdk_persistence.sqlite").toString()
72+
73+
persistenceDirectory.apply {
74+
if (!exists()) mkdirs()
75+
}
76+
77+
File(persistenceFilePath).apply {
7178
if (exists()) delete()
7279
}
73-
return currentDirectory
80+
return persistenceFilePath
7481
}
7582

7683
enum class ActiveWalletScriptType {

0 commit comments

Comments
 (0)