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.6 KB · 85 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/// Bech32 encode / decode functions13public struct Bech32 {1415    /// Encodes data as a Bech32 string.16    ///17    /// - Parameter hrp: The human-readable part.18    /// - Parameter data: The data part.19    /// - Returns: the encoded Bech32 string.20    public static func encode(hrp: String, data: Data) -> String {21        let hrpString = TWStringCreateWithNSString(hrp)22        defer {23            TWStringDelete(hrpString)24        }25        let dataData = TWDataCreateWithNSData(data)26        defer {27            TWDataDelete(dataData)28        }29        return TWStringNSString(TWBech32Encode(hrpString, dataData))30    }3132    /// Decodes a Bech32 string. Returns null if the string is not a valid Bech32 string.33    ///34    /// - Parameter string: The Bech32 string to decode.35    /// - Returns: the decoded data, null if the string is not a valid Bech32 string. Note that the human-readable part is not returned.36    public static func decode(string: String) -> Data? {37        let stringString = TWStringCreateWithNSString(string)38        defer {39            TWStringDelete(stringString)40        }41        guard let result = TWBech32Decode(stringString) else {42            return nil43        }44        return TWDataNSData(result)45    }4647    /// Encodes data as a Bech32m string.48    ///49    /// - Parameter hrp: The human-readable part.50    /// - Parameter data: The data part.51    /// - Returns: the encoded Bech32m string.52    public static func encodeM(hrp: String, data: Data) -> String {53        let hrpString = TWStringCreateWithNSString(hrp)54        defer {55            TWStringDelete(hrpString)56        }57        let dataData = TWDataCreateWithNSData(data)58        defer {59            TWDataDelete(dataData)60        }61        return TWStringNSString(TWBech32EncodeM(hrpString, dataData))62    }6364    /// Decodes a Bech32m string. Returns null if the string is not a valid Bech32m string.65    ///66    /// - Parameter string: The Bech32m string to decode.67    /// - Returns: the decoded data, null if the string is not a valid Bech32m string. Note that the human-readable part is not returned.68    public static func decodeM(string: String) -> Data? {69        let stringString = TWStringCreateWithNSString(string)70        defer {71            TWStringDelete(stringString)72        }73        guard let result = TWBech32DecodeM(stringString) else {74            return nil75        }76        return TWDataNSData(result)77    }787980    init() {81    }828384}85