BitFoundation module to centralize shared components (#1089)

* Run local packages’ tests as well on CI

* BitFoundation module to centralize shared components
This commit is contained in:
Islam
2026-04-14 14:10:03 -05:00
committed by GitHub
parent 3e137d784c
commit 4cfcefcda6
81 changed files with 290 additions and 141 deletions
+1 -56
View File
@@ -6,62 +6,7 @@
//
import Foundation
import CryptoKit
// MARK: - Hex Encoding/Decoding
extension Data {
func hexEncodedString() -> String {
if self.isEmpty {
return ""
}
return self.map { String(format: "%02x", $0) }.joined()
}
func sha256Hex() -> String {
let digest = SHA256.hash(data: self)
return digest.map { String(format: "%02x", $0) }.joined()
}
/// Initialize Data from a hex string.
/// - Parameter hexString: A hex string, optionally prefixed with "0x" or "0X".
/// Whitespace is trimmed. Must have even length after prefix removal.
/// - Returns: nil if the string has odd length or contains invalid hex characters.
init?(hexString: String) {
var hex = hexString.trimmed
// Remove optional 0x prefix
if hex.hasPrefix("0x") || hex.hasPrefix("0X") {
hex = String(hex.dropFirst(2))
}
// Reject odd-length strings
guard hex.count % 2 == 0 else {
return nil
}
// Reject empty strings
guard !hex.isEmpty else {
self = Data()
return
}
let len = hex.count / 2
var data = Data(capacity: len)
var index = hex.startIndex
for _ in 0..<len {
let nextIndex = hex.index(index, offsetBy: 2)
guard let byte = UInt8(String(hex[index..<nextIndex]), radix: 16) else {
return nil
}
data.append(byte)
index = nextIndex
}
self = data
}
}
import BitFoundation
// MARK: - Binary Encoding Utilities
+1
View File
@@ -60,6 +60,7 @@
import Foundation
import CoreBluetooth
import BitFoundation
// MARK: - Message Types