-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtensions.swift
More file actions
149 lines (122 loc) · 4.35 KB
/
Copy pathExtensions.swift
File metadata and controls
149 lines (122 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Extensions.swift
// FullyNoded-Server
//
// Created by Peter Denton on 9/5/24.
//
import Foundation
import SwiftUI
extension Double {
func rounded(toPlaces places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
var bitcoinCoreSyncStatus: String {
if self >= 0.9999 {
return "100%"
} else {
return "\(Int(self*100))%"
}
}
var uptime: String {
return "\(Int(self) / 86400) d \((Int(self) % 86400) / 3600) h"
}
var hashrate: String {
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 {
var size: String {
return "\((Double(self)/1000000000.0).rounded(toPlaces: 1)) gb"
}
var diffString: String {
return "\(Int(self / 1000000000000)) trillion"
}
var withCommas: String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal
return numberFormatter.string(from: NSNumber(value:self))!
}
}
extension Data {
/// A hexadecimal string representation of the bytes.
var hexString: String {
let hexDigits = Array("0123456789abcdef".utf16)
var hexChars = [UTF16.CodeUnit]()
hexChars.reserveCapacity(count * 2)
for byte in self {
let (index1, index2) = Int(byte).quotientAndRemainder(dividingBy: 16)
hexChars.append(hexDigits[index1])
hexChars.append(hexDigits[index2])
}
return String(utf16CodeUnits: hexChars, count: hexChars.count)
}
var urlSafeB64String: String {
return self.base64EncodedString().replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "").replacingOccurrences(of: "+", with: "-")
}
var prettyPrintedJSONString: NSString? { /// NSString gives us a nice sanitized debugDescription
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }
return prettyPrintedString
}
}
extension String {
var port: String {
var port:String!
switch self {
case "main":
port = "8332"
case "test":
port = "18332"
case "regtest":
port = "18443"
case "signet":
port = "38332"
default:
break
}
return port
}
var qrQode: NSImage {
let data = self.data(using: .ascii)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter!.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 10, y: 10)
let output = filter?.outputImage?.transformed(by: transform)
let colorParameters = [
"inputColor0": CIColor(color: NSColor.black), // Foreground
"inputColor1": CIColor(color: NSColor.white) // Background
]
let colored = (output!.applyingFilter("CIFalseColor", parameters: colorParameters as [String : Any]))
let rep = NSCIImageRep(ciImage: colored)
let nsImage = NSImage(size: rep.size)
nsImage.addRepresentation(rep)
return nsImage
}
var urlSafeB64String: String {
return self.replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "").replacingOccurrences(of: "+", with: "-")
}
}
public extension BlockchainInfo {
var size: String {
return "\(self.size_on_disk/1000000000) gb"
}
var progressString: String {
if self.verificationprogress > 0.9999 {
return "Fully verified"
} else {
return "\(Int(self.verificationprogress*100))% verified"
}
}
var diffString: String {
return "Difficulty \(Int(self.difficulty / 1000000000000).withCommas) trillion"
}
}