Skip to content

Commit

Permalink
fix: Join Market was broken. Added a "configure JM" button.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fonta1n3 committed Nov 5, 2024
1 parent 54e6379 commit 16404d4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
9 changes: 9 additions & 0 deletions FullyNoded-Server/Helpers/InstallBtcCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class CreateFNDirConfigureCore {
class func checkForExistingConf(completion: @escaping (Bool) -> Void) {
BitcoinConf.getBitcoinConf { (conf, error) in
if let existingBitcoinConf = conf {
var createBdbExists = false
// check if deprecatedrpc=create_bdb exists, if not add it.
for item in existingBitcoinConf {
let arr = item.split(separator: "=")
if arr.count > 1 {
Expand All @@ -22,6 +24,9 @@ class CreateFNDirConfigureCore {
if item.hasPrefix("txindex=") {
UserDefaults.standard.setValue(value, forKey: "txindex")
}
if item.contains("deprecatedrpc=create_bdb") {
createBdbExists = true
}
}
}
}
Expand Down Expand Up @@ -51,6 +56,10 @@ class CreateFNDirConfigureCore {
}

var updatedBitcoinConf = existingBitcoinConf.joined(separator: "\n")
if !createBdbExists {
// For Join Market to work...
updatedBitcoinConf = "deprecatedrpc=create_bdb" + "\n" + updatedBitcoinConf
}
updatedBitcoinConf = rpcauth + "\n" + updatedBitcoinConf
setBitcoinConf(updatedBitcoinConf, completion: completion)
}
Expand Down
74 changes: 74 additions & 0 deletions FullyNoded-Server/Views/JoinMarket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ struct JoinMarket: View {
} label: {
Text("joinmarket.cfg")
}
Button {
configureJm()
} label: {
Text("Configure JM")
}
}
.padding([.leading, .trailing])
.frame(maxWidth: .infinity, alignment: .leading)
Expand Down Expand Up @@ -160,6 +165,75 @@ struct JoinMarket: View {
}
}

private func configureJm() {
var chain = UserDefaults.standard.object(forKey: "chain") as? String ?? "signet"
let port = UserDefaults.standard.object(forKey: "port") as? String ?? "38332"
switch chain {
case "main": chain = "mainnet"
case "regtest": chain = "testnet"
case "test": chain = "testnet"
default:
break
}
updateConf(key: "network", value: chain)
updateConf(key: "rpc_port", value: port)
updateConf(key: "rpc_wallet_file", value: "jm_wallet")

DataManager.retrieve(entityName: "BitcoinRPCCreds") { rpcCreds in
guard let rpcCreds = rpcCreds,
let encryptedPassword = rpcCreds["password"] as? Data,
let decryptedPass = Crypto.decrypt(encryptedPassword),
let stringPass = String(data: decryptedPass, encoding: .utf8) else {
showMessage(message: "Unable to get rpc creds to congifure JM.")
return
}

updateConf(key: "rpc_password", value: stringPass)
updateConf(key: "rpc_user", value: "FullyNoded-Server")

BitcoinRPC.shared.command(method: "createwallet", params: ["wallet_name": "jm_wallet", "descriptors": false]) { (result, error) in
guard error == nil else {
if !error!.contains("Database already exists.") {
showMessage(message: error!)
}
isAnimating = false
return
}

showMessage(message: "Join Market configured ✓")
isAnimating = false
}
}
}

private func fileExists(path: String) -> Bool {
return FileManager.default.fileExists(atPath: path)
}

private func updateConf(key: String, value: String) {
let jmConfPath = "/Users/\(NSUserName())/Library/Application Support/joinmarket/joinmarket.cfg"
guard fileExists(path: jmConfPath) else { return }
guard let conf = try? Data(contentsOf: URL(fileURLWithPath: jmConfPath)) else {
print("no jm conf")
return
}
guard let string = String(data: conf, encoding: .utf8) else {
print("cant get string")
return
}
let arr = string.split(separator: "\n")
for item in arr {
if item.hasPrefix("\(key) =") {
let newConf = string.replacingOccurrences(of: item, with: key + " = " + value)
if (try? newConf.write(to: URL(fileURLWithPath: jmConfPath), atomically: false, encoding: .utf8)) == nil {
print("failed writing to jm config")
} else {
print("wrote to joinmarket.cfg")
}
}
}
}

private func showConnectUrls() {
guard let hiddenServices = TorClient.sharedInstance.hostnames() else {
showMessage(message: "No hostnames.")
Expand Down
8 changes: 7 additions & 1 deletion FullyNoded-Server/Views/JoinMarketTaggedReleases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ struct JoinMarketTaggedReleasesView: View {
updateConf(key: "rpc_wallet_file", value: "jm_wallet")

DataManager.retrieve(entityName: "BitcoinRPCCreds") { rpcCreds in
guard let rpcCreds = rpcCreds, let encryptedPassword = rpcCreds["password"] as? Data, let decryptedPass = Crypto.decrypt(encryptedPassword), let stringPass = String(data: decryptedPass, encoding: .utf8) else { return }
guard let rpcCreds = rpcCreds,
let encryptedPassword = rpcCreds["password"] as? Data,
let decryptedPass = Crypto.decrypt(encryptedPassword),
let stringPass = String(data: decryptedPass, encoding: .utf8) else {
showMessage(message: "Unable to get rpc creds to congifure JM.")
return
}

updateConf(key: "rpc_password", value: stringPass)
updateConf(key: "rpc_user", value: "FullyNoded-Server")
Expand Down

0 comments on commit 16404d4

Please sign in to comment.