spb/os-vault Public
Self-custody, multi-chain crypto wallet for macOS. One recovery phrase, six chain families, zero API keys — nothing leaves your Mac.
Swift 96%
Shell 3.4%
Makefile 0.6%
1import Foundation23protocol HexToData {4 func ask_hexToData() -> Data5}67extension UInt32: HexToData {8 func ask_hexToData() -> Data {9 var v = byteSwapped10 let data = Data(bytes: &v, count: MemoryLayout<UInt32>.size)11 return data12 }13}1415extension UInt8: HexToData {16 func ask_hexToData() -> Data {17 var v = self18 let data = Data(bytes: &v, count: MemoryLayout<UInt8>.size)19 return data20 }21}2223extension String {24 /// Create `Data` from hexadecimal string representation25 ///26 /// This takes a hexadecimal representation and creates a `Data` object. Note, if the string has any spaces or non-hex characters (e.g. starts with '<' and with a '>'), those are ignored and only hex characters are processed.27 ///28 /// - returns: Data represented by this hexadecimal string.2930 func ask_hexadecimal() -> Data? {31 var data = Data(capacity: count / 2)3233 let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)34 regex.enumerateMatches(in: self, range: NSMakeRange(0, utf16.count)) { match, _, _ in35 let byteString = (self as NSString).substring(with: match!.range)36 var num = UInt8(byteString, radix: 16)!37 data.append(&num, count: 1)38 }3940 guard !data.isEmpty else { return nil }4142 return data43 }44}4546extension Data {47 /// Create hexadecimal string representation of `Data` object.48 ///49 /// - returns: `String` representation of this `Data` object.5051 func ask_hexadecimal() -> String {52 map { String(format: "%02x", $0) }53 .joined(separator: "")54 }55}5657extension Data {58 func ask_BTCHash256() -> Data {59 sha256().sha256()60 }6162 public func ask_BTCHash160() -> Data {63 RIPEMD.digest(sha256())64 }6566 func ask_BTCHash160String() -> String {67 ask_BTCHash160().toHexString()68 }6970 func ck_reversedData() -> Data {71 Data(reversed())72 }7374 func ask_base58Check() -> String {75 Base58.encode([UInt8](self))76 }7778 static func dataFromHexString(hexString _: String) -> Data {79 let data = Data()8081 return data82 }83}8485// Copy from CryptoSwift/UInt32+Extension.swift86extension UInt32 {87 init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {88 self = UInt32(bytes: bytes, fromIndex: bytes.startIndex)89 }9091 init<T: Collection>(bytes: T, fromIndex index: T.Index) where T.Iterator.Element == UInt8, T.Index == Int {92 let val0 = UInt32(bytes[index.advanced(by: 0)]) << 2493 let val1 = UInt32(bytes[index.advanced(by: 1)]) << 1694 let val2 = UInt32(bytes[index.advanced(by: 2)]) << 895 let val3 = UInt32(bytes[index.advanced(by: 3)])9697 self = val0 | val1 | val2 | val398 }99}100