diff --git a/FullyNoded-Server/Helpers/BitcoinRPC.swift b/FullyNoded-Server/Helpers/BitcoinRPC.swift index bef3e7cc..ba1dae02 100644 --- a/FullyNoded-Server/Helpers/BitcoinRPC.swift +++ b/FullyNoded-Server/Helpers/BitcoinRPC.swift @@ -17,11 +17,7 @@ class BitcoinRPC { let port = UserDefaults.standard.string(forKey: "port") ?? "8332" let nodeIp = "127.0.0.1:\(port)" - - guard let user = UserDefaults.standard.string(forKey: "rpcuser") else { - completion((nil, "No rpc user saved.")) - return - } + let user = UserDefaults.standard.string(forKey: "rpcuser") ?? "FullyNoded-Server" DataManager.retrieve(entityName: "BitcoinRPCCreds") { [weak self] creds in guard let self = self else { return } diff --git a/FullyNoded-Server/Helpers/InstallBtcCore.swift b/FullyNoded-Server/Helpers/InstallBtcCore.swift index 0cb26529..30292e5b 100644 --- a/FullyNoded-Server/Helpers/InstallBtcCore.swift +++ b/FullyNoded-Server/Helpers/InstallBtcCore.swift @@ -14,13 +14,15 @@ class CreateFNDirConfigureCore { if let existingBitcoinConf = conf { for item in existingBitcoinConf { let arr = item.split(separator: "=") - guard arr.count > 1 else { return } - guard let value = Int(arr[1]) else { return } - if item.hasPrefix("prune=") { - UserDefaults.standard.setValue(value, forKey: "prune") - } - if item.hasPrefix("txindex=") { - UserDefaults.standard.setValue(value, forKey: "txindex") + if arr.count > 1 { + if let value = Int(arr[1]) { + if item.hasPrefix("prune=") { + UserDefaults.standard.setValue(value, forKey: "prune") + } + if item.hasPrefix("txindex=") { + UserDefaults.standard.setValue(value, forKey: "txindex") + } + } } } let rpcuser = "FullyNoded-Server" @@ -121,10 +123,12 @@ class CreateFNDirConfigureCore { DataManager.retrieve(entityName: "BitcoinRPCCreds") { existingCreds in if let _ = existingCreds { DataManager.update(keyToUpdate: "password", newValue: encryptedPass, entity: "BitcoinRPCCreds") { updated in + UserDefaults.standard.set("FullyNoded-Server", forKey: "rpcuser") completion(updated) } } else { DataManager.saveEntity(entityName: "BitcoinRPCCreds", dict: ["password": encryptedPass]) { saved in + UserDefaults.standard.set("FullyNoded-Server", forKey: "rpcuser") completion(saved) } } diff --git a/FullyNoded-Server/Views/BitcoinCore.swift b/FullyNoded-Server/Views/BitcoinCore.swift index 2105806c..c785cbcc 100644 --- a/FullyNoded-Server/Views/BitcoinCore.swift +++ b/FullyNoded-Server/Views/BitcoinCore.swift @@ -164,10 +164,12 @@ struct BitcoinCore: View { } label: { Text("bitcoin.conf") } - Button { - openFile(file: "debug.log") - } label: { - Text("Log") + if let debugPath = debugLogPath() { + Button { + openFile(file: debugPath) + } label: { + Text("debug.log") + } } Button { refreshRPCAuth() @@ -364,7 +366,7 @@ struct BitcoinCore: View { } private func initialLoad() { - selectedChain = UserDefaults.standard.string(forKey: "chain") ?? "main" + selectedChain = UserDefaults.standard.string(forKey: "chain") ?? "signet" DataManager.retrieve(entityName: "BitcoinEnv") { env in guard let env = env else { return } let envValues = BitcoinEnvValues(dictionary: env) @@ -442,6 +444,7 @@ struct BitcoinCore: View { UserDefaults.standard.setValue(port, forKey: "port") UserDefaults.standard.setValue(chain.lowercased(), forKey: "chain") self.env["CHAIN"] = chain + self.blockchainInfo = nil DataManager.update(keyToUpdate: "chain", newValue: chain, entity: "BitcoinEnv") { updated in guard updated else { showMessage(message: "There was an issue updating your network...") @@ -556,28 +559,9 @@ struct BitcoinCore: View { private func showBitcoinLog() { - let chain = Defaults.shared.chain - var path: URL? + guard let debugPath = debugLogPath() else { return } - print(Defaults.shared.dataDir) - - switch chain { - case "main": - path = URL(fileURLWithPath: "\(Defaults.shared.dataDir)/debug.log") - case "test": - path = URL(fileURLWithPath: "\(Defaults.shared.dataDir)/testnet3/debug.log") - case "regtest": - path = URL(fileURLWithPath: "\(Defaults.shared.dataDir)/regtest/debug.log") - case "signet": - path = URL(fileURLWithPath: "\(Defaults.shared.dataDir)/signet/debug.log") - default: - break - } - - guard let path = path else { - print("can not get path") - return - } + let path = URL(fileURLWithPath: debugPath) guard let log = try? String(contentsOf: path, encoding: .utf8) else { print("can't get log, path: \(path)") @@ -593,13 +577,28 @@ struct BitcoinCore: View { if lastLogItem.contains("Shutdown: done") { isRunning = false } -// if lastLogItem.contains("ThreadRPCServer incorrect password") { -// showMessage(message: lastLogItem) -// } } } } + private func debugLogPath() -> String? { + let chain = Defaults.shared.chain + var debugLogPath: String? + switch chain { + case "main": + debugLogPath = "\(Defaults.shared.dataDir)/debug.log" + case "test": + debugLogPath = "\(Defaults.shared.dataDir)/testnet3/debug.log" + case "regtest": + debugLogPath = "\(Defaults.shared.dataDir)/regtest/debug.log" + case "signet": + debugLogPath = "\(Defaults.shared.dataDir)/signet/debug.log" + default: + break + } + return debugLogPath + } + private func isBitcoinCoreRunning() { isAnimating = true BitcoinRPC.shared.command(method: "getblockchaininfo", params: [:]) { (result, error) in diff --git a/FullyNoded-Server/Views/ContentView.swift b/FullyNoded-Server/Views/ContentView.swift index fcf34e38..a34ac927 100644 --- a/FullyNoded-Server/Views/ContentView.swift +++ b/FullyNoded-Server/Views/ContentView.swift @@ -242,6 +242,8 @@ struct ContentView: View { .onAppear(perform: { if TorClient.sharedInstance.state == .connected { torProgress = 100.0 + } else { + TorClient.sharedInstance.start(delegate: nil) } TorClient.sharedInstance.showProgress = { progress in torProgress = Double(progress) diff --git a/FullyNoded-Server/Views/JoinMarketTaggedReleases.swift b/FullyNoded-Server/Views/JoinMarketTaggedReleases.swift index 28cab3ea..7dea0147 100644 --- a/FullyNoded-Server/Views/JoinMarketTaggedReleases.swift +++ b/FullyNoded-Server/Views/JoinMarketTaggedReleases.swift @@ -23,8 +23,8 @@ struct JoinMarketTaggedReleasesView: View { init(taggedReleases: TaggedReleases, existingVersion: String) { self.taggedReleases = taggedReleases - self.taggedRelease = taggedReleases[0] self.existingVersion = existingVersion + self.taggedRelease = taggedReleases[0] } var body: some View { diff --git a/FullyNoded-Server/Views/TaggedReleasesView.swift b/FullyNoded-Server/Views/TaggedReleasesView.swift index 8bbb6a47..ce441656 100644 --- a/FullyNoded-Server/Views/TaggedReleasesView.swift +++ b/FullyNoded-Server/Views/TaggedReleasesView.swift @@ -10,6 +10,7 @@ import SwiftUI struct TaggedReleasesView: View { let timerForBitcoinInstall = Timer.publish(every: 3, on: .main, in: .common).autoconnect() + @State private var prune = false @State private var bitcoinCoreInstallComplete = false @State private var startCheckingForBitcoinInstall = false @State private var description = "" @@ -25,8 +26,8 @@ struct TaggedReleasesView: View { init(taggedReleases: TaggedReleases, existingVersion: String) { self.taggedReleases = taggedReleases - self.taggedRelease = taggedReleases[0] self.existingVersion = existingVersion + self.taggedRelease = taggedReleases[0] } var body: some View { @@ -40,42 +41,7 @@ struct TaggedReleasesView: View { } .padding([.top, .leading, .trailing]) - HStack() { - Text("Data Directory:") - Label(dataDir, systemImage: "") - Button("Update") { - chooseDataDir() - } - } - .padding([.leading, .trailing]) - .frame(maxWidth: .infinity, alignment: .leading) - Text("Do not update the data directory unless you want to save your Bitcoin Core data in a custom location like an external hard drive.") - .padding([.leading]) - .foregroundStyle(.secondary) - - HStack() { - if txIndex == 0 { - Label("Pruned node", systemImage: "") - - - Button("Do not prune") { - UserDefaults.standard.setValue(1, forKey: "txindex") - UserDefaults.standard.setValue(0, forKey: "prune") - txIndex = 1 - } - } else { - Label("Full node", systemImage: "") - - Button("Prune") { - UserDefaults.standard.setValue(0, forKey: "txindex") - UserDefaults.standard.setValue(1000, forKey: "prune") - txIndex = 0 - } - } - } - .padding([.leading, .trailing]) - .frame(maxWidth: .infinity, alignment: .leading) if let author = taggedRelease.author, let login = author.login, let tagName = taggedRelease.tagName { let processedVersion = tagName.replacingOccurrences(of: "v", with: "") @@ -100,6 +66,45 @@ struct TaggedReleasesView: View { .padding([.leading, .bottom]) .frame(maxWidth: .infinity, alignment: .leading) + Label("Configuration options", systemImage: "gear") + .padding(.leading) + .frame(maxWidth: .infinity, alignment: .leading) + + HStack() { + Text("Data Directory:") + Label(dataDir, systemImage: "") + Button("Update") { + chooseDataDir() + } + } + .padding([.leading, .trailing]) + .frame(maxWidth: .infinity, alignment: .leading) + + Text("Do not update the data directory unless you want to save your Bitcoin Core data in a custom location like an external hard drive.") + .padding([.leading]) + .foregroundStyle(.secondary) + + HStack() { + Toggle("Prune", isOn: $prune) + .onChange(of: prune) { oldValue, newValue in + if newValue { + UserDefaults.standard.setValue(0, forKey: "txindex") + UserDefaults.standard.setValue(1000, forKey: "prune") + txIndex = 0 + } else { + UserDefaults.standard.setValue(1, forKey: "txindex") + UserDefaults.standard.setValue(0, forKey: "prune") + } + } + } + .padding([.leading, .trailing]) + .frame(maxWidth: .infinity, alignment: .leading) + + Text("Pruning your node reduces the amount of disc space Bitcoin Core will use.") + .padding([.leading]) + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, alignment: .leading) + HStack() { if isAnimating { ProgressView() @@ -160,6 +165,7 @@ struct TaggedReleasesView: View { } Spacer() .onAppear { + prune = !((Defaults.shared.prune) == 0) taggedRelease = .init(url: nil, assetsURL: nil, uploadURL: nil, htmlURL: nil, id: nil, author: nil, nodeID: nil, tagName: "", targetCommitish: nil, name: nil, draft: nil, prerelease: nil, createdAt: nil, publishedAt: nil, tarballURL: "", zipballURL: nil, body: nil) } .alert(message, isPresented: $showError) { @@ -264,6 +270,7 @@ struct TaggedReleasesView: View { } description = "Downloading Bitcoin Core tarball from \(macOSUrl)" CreateFNDirConfigureCore.checkForExistingConf { startDownload in + print("startDownload") if startDownload { isAnimating = true downloadTask(url: URL(string: macOSUrl)!) { data in