SPB Git

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%
1.3 KB · 46 lines swift
Raw Blame History
1import Foundation23public extension UInt32 {4    var bytes: [UInt8] {5        var littleEndian = self.littleEndian6        return withUnsafeBytes(of: &littleEndian) { Array($0) }7    }8}910public extension UInt64 {11    var bytes: [UInt8] {12        var littleEndian = self.littleEndian13        return withUnsafeBytes(of: &littleEndian) { Array($0) }14    }1516    func convertToBalance(decimals: Int?) -> Double {17        guard let decimals = decimals else { return 0 }18        return convertToBalance(decimals: UInt8(decimals))19    }2021    func convertToBalance(decimals: UInt8?) -> Double {22        guard let decimals = decimals else { return 0 }23        return (Double(self) * pow(10, -Double(decimals))).rounded(toPlaces: decimals)24    }25}2627extension Double {28    public func toLamport(decimals: Int) -> UInt64 {29        UInt64((self * pow(10, Double(decimals))).rounded())30    }3132    public func toLamport(decimals: UInt8) -> UInt64 {33        toLamport(decimals: Int(decimals))34    }3536    /// Rounds the double to decimal places value37    func rounded(toPlaces places: Int) -> Double {38        rounded(toPlaces: UInt8(places))39    }4041    func rounded(toPlaces places: UInt8) -> Double {42        let divisor = pow(10.0, Double(places))43        return (self * divisor).rounded() / divisor44    }45}46