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%
2.2 KB · 75 lines swift
Raw Blame History
1import WalletCoreC2import Foundation3// SPDX-License-Identifier: Apache-2.04//5// Copyright © 2017 Trust Wallet.6//7// This is a GENERATED FILE, changes made here WILL BE LOST.8//910import Foundation1112/// Base64 encode / decode functions13public struct Base64 {1415    /// Decode a Base64 input with the default alphabet (RFC4648 with '+', '/')16    ///17    /// - Parameter string: Encoded input to be decoded18    /// - Returns: The decoded data, empty if decoding failed.19    public static func decode(string: String) -> Data? {20        let stringString = TWStringCreateWithNSString(string)21        defer {22            TWStringDelete(stringString)23        }24        guard let result = TWBase64Decode(stringString) else {25            return nil26        }27        return TWDataNSData(result)28    }2930    /// Decode a Base64 input with the alphabet safe for URL-s and filenames (RFC4648 with '-', '_')31    ///32    /// - Parameter string: Encoded base64 input to be decoded33    /// - Returns: The decoded data, empty if decoding failed.34    public static func decodeUrl(string: String) -> Data? {35        let stringString = TWStringCreateWithNSString(string)36        defer {37            TWStringDelete(stringString)38        }39        guard let result = TWBase64DecodeUrl(stringString) else {40            return nil41        }42        return TWDataNSData(result)43    }4445    /// Encode an input to Base64 with the default alphabet (RFC4648 with '+', '/')46    ///47    /// - Parameter data: Data to be encoded (raw bytes)48    /// - Returns: The encoded data49    public static func encode(data: Data) -> String {50        let dataData = TWDataCreateWithNSData(data)51        defer {52            TWDataDelete(dataData)53        }54        return TWStringNSString(TWBase64Encode(dataData))55    }5657    /// Encode an input to Base64 with the alphabet safe for URL-s and filenames (RFC4648 with '-', '_')58    ///59    /// - Parameter data: Data to be encoded (raw bytes)60    /// - Returns: The encoded data61    public static func encodeUrl(data: Data) -> String {62        let dataData = TWDataCreateWithNSData(data)63        defer {64            TWDataDelete(dataData)65        }66        return TWStringNSString(TWBase64EncodeUrl(dataData))67    }686970    init() {71    }727374}75