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%
6.0 KB · 166 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/// Represents an address in C++ for almost any blockchain.13public final class AnyAddress: Address {1415    /// Compares two addresses for equality.16    ///17    /// - Parameter lhs: The first address to compare.18    /// - Parameter rhs: The second address to compare.19    /// - Returns: bool indicating the addresses are equal.20    public static func == (lhs: AnyAddress, rhs: AnyAddress) -> Bool {21        return TWAnyAddressEqual(lhs.rawValue, rhs.rawValue)22    }2324    /// Determines if the string is a valid Any address.25    ///26    /// - Parameter string: address to validate.27    /// - Parameter coin: coin type of the address.28    /// - Returns: bool indicating if the address is valid.29    public static func isValid(string: String, coin: CoinType) -> Bool {30        let stringString = TWStringCreateWithNSString(string)31        defer {32            TWStringDelete(stringString)33        }34        return TWAnyAddressIsValid(stringString, TWCoinType(rawValue: coin.rawValue))35    }3637    /// Determines if the string is a valid Any address with the given hrp.38    ///39    /// - Parameter string: address to validate.40    /// - Parameter coin: coin type of the address.41    /// - Parameter hrp: explicit given hrp of the given address.42    /// - Returns: bool indicating if the address is valid.43    public static func isValidBech32(string: String, coin: CoinType, hrp: String) -> Bool {44        let stringString = TWStringCreateWithNSString(string)45        defer {46            TWStringDelete(stringString)47        }48        let hrpString = TWStringCreateWithNSString(hrp)49        defer {50            TWStringDelete(hrpString)51        }52        return TWAnyAddressIsValidBech32(stringString, TWCoinType(rawValue: coin.rawValue), hrpString)53    }5455    /// Determines if the string is a valid Any address with the given SS58 network prefix.56    ///57    /// - Parameter string: address to validate.58    /// - Parameter coin: coin type of the address.59    /// - Parameter ss58Prefix: ss58Prefix of the given address.60    /// - Returns: bool indicating if the address is valid.61    public static func isValidSS58(string: String, coin: CoinType, ss58Prefix: UInt32) -> Bool {62        let stringString = TWStringCreateWithNSString(string)63        defer {64            TWStringDelete(stringString)65        }66        return TWAnyAddressIsValidSS58(stringString, TWCoinType(rawValue: coin.rawValue), ss58Prefix)67    }6869    /// Returns the address string representation.70    ///71    /// - Parameter address: address to get the string representation of.72    public var description: String {73        return TWStringNSString(TWAnyAddressDescription(rawValue))74    }7576    /// Returns coin type of address.77    ///78    /// - Parameter address: address to get the coin type of.79    public var coin: CoinType {80        return CoinType(rawValue: TWAnyAddressCoin(rawValue).rawValue)!81    }8283    /// Returns underlaying data (public key or key hash)84    ///85    /// - Parameter address: address to get the data of.86    public var data: Data {87        return TWDataNSData(TWAnyAddressData(rawValue))88    }8990    let rawValue: OpaquePointer9192    init(rawValue: OpaquePointer) {93        self.rawValue = rawValue94    }9596    public init?(string: String, coin: CoinType) {97        let stringString = TWStringCreateWithNSString(string)98        defer {99            TWStringDelete(stringString)100        }101        guard let rawValue = TWAnyAddressCreateWithString(stringString, TWCoinType(rawValue: coin.rawValue)) else {102            return nil103        }104        self.rawValue = rawValue105    }106107    public init?(string: String, coin: CoinType, hrp: String) {108        let stringString = TWStringCreateWithNSString(string)109        defer {110            TWStringDelete(stringString)111        }112        let hrpString = TWStringCreateWithNSString(hrp)113        defer {114            TWStringDelete(hrpString)115        }116        guard let rawValue = TWAnyAddressCreateBech32(stringString, TWCoinType(rawValue: coin.rawValue), hrpString) else {117            return nil118        }119        self.rawValue = rawValue120    }121122    public init?(string: String, coin: CoinType, ss58Prefix: UInt32) {123        let stringString = TWStringCreateWithNSString(string)124        defer {125            TWStringDelete(stringString)126        }127        guard let rawValue = TWAnyAddressCreateSS58(stringString, TWCoinType(rawValue: coin.rawValue), ss58Prefix) else {128            return nil129        }130        self.rawValue = rawValue131    }132133    public init(publicKey: PublicKey, coin: CoinType) {134        rawValue = TWAnyAddressCreateWithPublicKey(publicKey.rawValue, TWCoinType(rawValue: coin.rawValue))135    }136137    public init(publicKey: PublicKey, coin: CoinType, derivation: Derivation) {138        rawValue = TWAnyAddressCreateWithPublicKeyDerivation(publicKey.rawValue, TWCoinType(rawValue: coin.rawValue), TWDerivation(rawValue: derivation.rawValue))139    }140141    public init(publicKey: PublicKey, coin: CoinType, hrp: String) {142        let hrpString = TWStringCreateWithNSString(hrp)143        defer {144            TWStringDelete(hrpString)145        }146        rawValue = TWAnyAddressCreateBech32WithPublicKey(publicKey.rawValue, TWCoinType(rawValue: coin.rawValue), hrpString)147    }148149    public init(publicKey: PublicKey, coin: CoinType, ss58Prefix: UInt32) {150        rawValue = TWAnyAddressCreateSS58WithPublicKey(publicKey.rawValue, TWCoinType(rawValue: coin.rawValue), ss58Prefix)151    }152153    public init(publicKey: PublicKey, filecoinAddressType: FilecoinAddressType) {154        rawValue = TWAnyAddressCreateWithPublicKeyFilecoinAddressType(publicKey.rawValue, TWFilecoinAddressType(rawValue: filecoinAddressType.rawValue))155    }156157    public init(publicKey: PublicKey, firoAddressType: FiroAddressType) {158        rawValue = TWAnyAddressCreateWithPublicKeyFiroAddressType(publicKey.rawValue, TWFiroAddressType(rawValue: firoAddressType.rawValue))159    }160161    deinit {162        TWAnyAddressDelete(rawValue)163    }164165}166