Skip to content

Commit

Permalink
Merge pull request #5 from Fonta1n3/v0.0.1-beta
Browse files Browse the repository at this point in the history
v0.0.1 beta
  • Loading branch information
Fonta1n3 authored Dec 4, 2024
2 parents 54e6379 + a1bd48a commit 7c375ee
Show file tree
Hide file tree
Showing 40 changed files with 1,865 additions and 1,191 deletions.
48 changes: 42 additions & 6 deletions FullyNoded-Server.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 13 additions & 2 deletions FullyNoded-Server/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,67 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"filename" : "1024-mac.png",
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
},
{
"filename" : "16-mac.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"filename" : "32-mac.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"filename" : "32-mac.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"filename" : "64-mac.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"filename" : "128-mac.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"filename" : "256-mac.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"filename" : "256-mac.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"filename" : "512-mac.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"filename" : "512-mac.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"filename" : "1024-mac.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
Expand Down
40 changes: 20 additions & 20 deletions FullyNoded-Server/Core Data/DataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ class DataManager: NSObject {
container.loadPersistentStores { _, _ in }
}

class func retrieve(entityName: String, completion: @escaping (([String:Any]?)) -> Void) {
print("retrieve: \(entityName)")
class func retrieve(entityName: Entity, completion: @escaping (([String:Any]?)) -> Void) {
let context = DataManager.shared.container.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName.rawValue)
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.resultType = .dictionaryResultType

Expand All @@ -45,9 +44,9 @@ class DataManager: NSObject {
}


class func deleteAllData(entityName: String, completion: @escaping ((Bool)) -> Void) {
class func deleteAllData(entityName: Entity, completion: @escaping ((Bool)) -> Void) {
let context = DataManager.shared.container.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName.rawValue)
fetchRequest.returnsObjectsAsFaults = false

do {
Expand All @@ -66,11 +65,10 @@ class DataManager: NSObject {
}
}

class func saveEntity(entityName: String, dict: [String:Any], completion: @escaping ((Bool)) -> Void) {
print("saveEntity")
class func saveEntity(entityName: Entity, dict: [String:Any], completion: @escaping ((Bool)) -> Void) {
let context = DataManager.shared.container.viewContext

guard let entity = NSEntityDescription.entity(forEntityName: entityName, in: context) else {
guard let entity = NSEntityDescription.entity(forEntityName: entityName.rawValue, in: context) else {
completion(false)
return
}
Expand All @@ -91,21 +89,23 @@ class DataManager: NSObject {
completion(success)
}

class func update(keyToUpdate: String, newValue: Any, entity: String, completion: @escaping ((Bool)) -> Void) {
class func update(keyToUpdate: String, newValue: Any, entity: Entity, completion: @escaping ((Bool)) -> Void) {
let context = DataManager.shared.container.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: entity)
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: entity.rawValue)
fetchRequest.returnsObjectsAsFaults = false

guard let results = try? context.fetch(fetchRequest), results.count > 0 else { completion(false); return }
guard let results = try? context.fetch(fetchRequest), results.count > 0 else {
completion(false)
return
}

//for data in results[0] {
results[0].setValue(newValue, forKey: keyToUpdate)
do {
try context.save()
completion(true)
} catch {
completion(false)
}
//}
results[0].setValue(newValue, forKey: keyToUpdate)

do {
try context.save()
completion(true)
} catch {
completion(false)
}
}
}
7 changes: 7 additions & 0 deletions FullyNoded-Server/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ extension Double {
let exahashesPerSecond = self / 1000000000000000000.0
return "\(Int(exahashesPerSecond)) EX/s"
}

var avoidNotation: String {
let numberFormatter = NumberFormatter()
numberFormatter.maximumFractionDigits = 8
numberFormatter.numberStyle = .decimal
return numberFormatter.string(for: self) ?? ""
}
}

extension Int {
Expand Down
6 changes: 3 additions & 3 deletions FullyNoded-Server/Helpers/BitcoinConf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class BitcoinConf {
}

static func saveCreds(rpcuser: String, encryptedPass: Data) {
DataManager.retrieve(entityName: "BitcoinRPCCreds") { existingCreds in
DataManager.retrieve(entityName: .rpcCreds) { existingCreds in
if let _ = existingCreds {
DataManager.update(keyToUpdate: "password", newValue: encryptedPass, entity: "BitcoinRPCCreds") { updated in
DataManager.update(keyToUpdate: "password", newValue: encryptedPass, entity: .rpcCreds) { updated in
guard updated else { return }
UserDefaults.standard.setValue(rpcuser, forKey: "rpcuser")
}
} else {
DataManager.saveEntity(entityName: "BitcoinRPCCreds", dict: ["password": encryptedPass]) { saved in
DataManager.saveEntity(entityName: .rpcCreds, dict: ["password": encryptedPass]) { saved in
guard saved else { return }
UserDefaults.standard.setValue(rpcuser, forKey: "rpcuser")
}
Expand Down
7 changes: 5 additions & 2 deletions FullyNoded-Server/Helpers/BitcoinRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BitcoinRPC {
let nodeIp = "127.0.0.1:\(port)"
let user = UserDefaults.standard.string(forKey: "rpcuser") ?? "FullyNoded-Server"

DataManager.retrieve(entityName: "BitcoinRPCCreds") { [weak self] creds in
DataManager.retrieve(entityName: .rpcCreds) { [weak self] creds in
guard let self = self else { return }

guard let creds = creds else {
Expand All @@ -42,7 +42,10 @@ class BitcoinRPC {
return
}

let stringUrl = "http://\(user):\(rpcPassword)@\(nodeIp)"
var stringUrl = "http://\(user):\(rpcPassword)@\(nodeIp)"
if method == "rescanblockchain" {
stringUrl += "/wallet/jm_wallet"
}
guard let url = URL(string: stringUrl) else {
completion((nil, "Error converting the url."))
return
Expand Down
103 changes: 65 additions & 38 deletions FullyNoded-Server/Helpers/InstallBtcCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,78 @@ import Foundation

class CreateFNDirConfigureCore {

class func checkForExistingConf(completion: @escaping (Bool) -> Void) {
class func checkForExistingConf(updatedPruneValue: Int?, completion: @escaping (Bool) -> Void) {
var existingPruneValue: Int?
BitcoinConf.getBitcoinConf { (conf, error) in
if let existingBitcoinConf = conf {
for item in existingBitcoinConf {
var createBdbExists = false

func setNow() {
let rpcuser = "FullyNoded-Server"
guard let rpcAuthCreds = RPCAuth().generateCreds(username: rpcuser, password: nil) else {
completion(false)
print("unable to generate rpc auth creds.")
return
}
let rpcauth = rpcAuthCreds.rpcAuth
let data = Data(rpcAuthCreds.rpcPassword.utf8)

guard let encryptedPass = Crypto.encrypt(data) else {
#if DEBUG
print("Unable to encrypt rpc pass.")
#endif
completion(false)
return
}
updateRpcCreds(encryptedPass: encryptedPass, rpcUser: rpcuser) { updated in
guard updated else {
#if DEBUG
print("Unable to save new password.")
#endif
completion(false)
return
}

var updatedBitcoinConf = existingBitcoinConf.joined(separator: "\n")
if !createBdbExists {
// For Join Market to work...
updatedBitcoinConf = "deprecatedrpc=create_bdb" + "\n" + updatedBitcoinConf
if let updatedPruneValue = updatedPruneValue, let existingPruneValue = existingPruneValue {
updatedBitcoinConf = updatedBitcoinConf.replacingOccurrences(of: "prune=\(existingPruneValue)", with: "prune=\(updatedPruneValue)")
}
}
updatedBitcoinConf = rpcauth + "\n" + updatedBitcoinConf
setBitcoinConf(updatedBitcoinConf, completion: completion)
}
}
// check if deprecatedrpc=create_bdb exists, if not add it.
for (i, item) in existingBitcoinConf.enumerated() {
let arr = item.split(separator: "=")
if arr.count > 1 {
if let value = Int(arr[1]) {
if item.hasPrefix("prune=") {
UserDefaults.standard.setValue(value, forKey: "prune")
if let updatedPruneValue = updatedPruneValue {
UserDefaults.standard.setValue(updatedPruneValue, forKey: "prune")
existingPruneValue = value
}
}
if item.hasPrefix("txindex=") {
UserDefaults.standard.setValue(value, forKey: "txindex")
}

} else {
// deprecatedrpc=create_bdb
if item.contains("deprecatedrpc=create_bdb") {
createBdbExists = true
}
}
}
}
let rpcuser = "FullyNoded-Server"
guard let rpcAuthCreds = RPCAuth().generateCreds(username: rpcuser, password: nil) else {
completion(false)
print("unable to generate rpc auth creds.")
return
}
let rpcauth = rpcAuthCreds.rpcAuth
let data = Data(rpcAuthCreds.rpcPassword.utf8)

guard let encryptedPass = Crypto.encrypt(data) else {
#if DEBUG
print("Unable to encrypt rpc pass.")
#endif
completion(false)
return
}
updateRpcCreds(encryptedPass: encryptedPass, rpcUser: rpcuser) { updated in
guard updated else {
#if DEBUG
print("Unable to save new password.")
#endif
completion(false)
return
if i + 1 == existingBitcoinConf.count {
setNow()
}

var updatedBitcoinConf = existingBitcoinConf.joined(separator: "\n")
updatedBitcoinConf = rpcauth + "\n" + updatedBitcoinConf
setBitcoinConf(updatedBitcoinConf, completion: completion)
}


} else {
if let defaultConf = BitcoinConf.newBitcoinConf() {
self.setBitcoinConf(defaultConf, completion: completion)
Expand Down Expand Up @@ -94,18 +120,19 @@ class CreateFNDirConfigureCore {
}
}

static let fnServerDir = "/Users/\(NSUserName())/.fullynoded"

class func setFullyNodedDirectory(completion: @escaping (Bool) -> Void) {
createDirectory("/Users/\(NSUserName())/.fullynoded", completion: completion)

if writeFile("/Users/\(NSUserName())/.fullynoded/fullynoded.log", "") {
createDirectory(fnServerDir, completion: completion)
if writeFile("\(fnServerDir)/fullynoded.log", "") {
createBitcoinCoreDirectory(completion: completion)
} else {
completion((false))
}
}

class func createBitcoinCoreDirectory(completion: @escaping (Bool) -> Void) {
let path = "/Users/\(NSUserName())/.fullynoded/BitcoinCore"
let path = "\(fnServerDir)/BitcoinCore"
do {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: path) {
Expand All @@ -120,14 +147,14 @@ class CreateFNDirConfigureCore {
}

class func updateRpcCreds(encryptedPass: Data, rpcUser: String, completion: @escaping (Bool) -> Void) {
DataManager.retrieve(entityName: "BitcoinRPCCreds") { existingCreds in
DataManager.retrieve(entityName: .rpcCreds) { existingCreds in
if let _ = existingCreds {
DataManager.update(keyToUpdate: "password", newValue: encryptedPass, entity: "BitcoinRPCCreds") { updated in
DataManager.update(keyToUpdate: "password", newValue: encryptedPass, entity: .rpcCreds) { updated in
UserDefaults.standard.set("FullyNoded-Server", forKey: "rpcuser")
completion(updated)
}
} else {
DataManager.saveEntity(entityName: "BitcoinRPCCreds", dict: ["password": encryptedPass]) { saved in
DataManager.saveEntity(entityName: .rpcCreds, dict: ["password": encryptedPass]) { saved in
UserDefaults.standard.set("FullyNoded-Server", forKey: "rpcuser")
completion(saved)
}
Expand Down
Loading

0 comments on commit 7c375ee

Please sign in to comment.