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%
14.0 KB · 315 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/// Hierarchical Deterministic (HD) Wallet13public final class HDWallet {1415    /// Computes the public key from an extended public key representation.16    ///17    /// - Parameter extended: extended public key18    /// - Parameter coin: a coin type19    /// - Parameter derivationPath: a derivation path20    /// - Note: Returned object needs to be deleted with \TWPublicKeyDelete21    /// - Returns: Nullable TWPublic key22    public static func getPublicKeyFromExtended(extended: String, coin: CoinType, derivationPath: String) -> PublicKey? {23        let extendedString = TWStringCreateWithNSString(extended)24        defer {25            TWStringDelete(extendedString)26        }27        let derivationPathString = TWStringCreateWithNSString(derivationPath)28        defer {29            TWStringDelete(derivationPathString)30        }31        guard let value = TWHDWalletGetPublicKeyFromExtended(extendedString, TWCoinType(rawValue: coin.rawValue), derivationPathString) else {32            return nil33        }34        return PublicKey(rawValue: value)35    }3637    /// Wallet seed.38    ///39    /// - Parameter wallet: non-null TWHDWallet40    /// - Returns: The wallet seed as a Non-null block of data.41    public var seed: Data {42        return TWDataNSData(TWHDWalletSeed(rawValue))43    }4445    /// Wallet Mnemonic46    ///47    /// - Parameter wallet: non-null TWHDWallet48    /// - Returns: The wallet mnemonic as a non-null TWString49    public var mnemonic: String {50        return TWStringNSString(TWHDWalletMnemonic(rawValue))51    }5253    /// Wallet entropy54    ///55    /// - Parameter wallet: non-null TWHDWallet56    /// - Returns: The wallet entropy as a non-null block of data.57    public var entropy: Data {58        return TWDataNSData(TWHDWalletEntropy(rawValue))59    }6061    let rawValue: OpaquePointer6263    init(rawValue: OpaquePointer) {64        self.rawValue = rawValue65    }6667    public init?(strength: Int32, passphrase: String) {68        let passphraseString = TWStringCreateWithNSString(passphrase)69        defer {70            TWStringDelete(passphraseString)71        }72        guard let rawValue = TWHDWalletCreate(Int32(strength), passphraseString) else {73            return nil74        }75        self.rawValue = rawValue76    }7778    public init?(mnemonic: String, passphrase: String) {79        let mnemonicString = TWStringCreateWithNSString(mnemonic)80        defer {81            TWStringDelete(mnemonicString)82        }83        let passphraseString = TWStringCreateWithNSString(passphrase)84        defer {85            TWStringDelete(passphraseString)86        }87        guard let rawValue = TWHDWalletCreateWithMnemonic(mnemonicString, passphraseString) else {88            return nil89        }90        self.rawValue = rawValue91    }9293    public init?(mnemonic: String, passphrase: String, check: Bool) {94        let mnemonicString = TWStringCreateWithNSString(mnemonic)95        defer {96            TWStringDelete(mnemonicString)97        }98        let passphraseString = TWStringCreateWithNSString(passphrase)99        defer {100            TWStringDelete(passphraseString)101        }102        guard let rawValue = TWHDWalletCreateWithMnemonicCheck(mnemonicString, passphraseString, check) else {103            return nil104        }105        self.rawValue = rawValue106    }107108    public init?(entropy: Data, passphrase: String) {109        let entropyData = TWDataCreateWithNSData(entropy)110        defer {111            TWDataDelete(entropyData)112        }113        let passphraseString = TWStringCreateWithNSString(passphrase)114        defer {115            TWStringDelete(passphraseString)116        }117        guard let rawValue = TWHDWalletCreateWithEntropy(entropyData, passphraseString) else {118            return nil119        }120        self.rawValue = rawValue121    }122123    deinit {124        TWHDWalletDelete(rawValue)125    }126127    /// Returns master key.128    ///129    /// - Parameter wallet: non-null TWHDWallet130    /// - Parameter curve:  a curve131    /// - Note: Returned object needs to be deleted with \TWPrivateKeyDelete132    /// - Returns: Non-null corresponding private key133    public func getMasterKey(curve: Curve) -> PrivateKey {134        return PrivateKey(rawValue: TWHDWalletGetMasterKey(rawValue, TWCurve(rawValue: curve.rawValue)))135    }136137    /// Generates the default private key for the specified coin, using default derivation.138    ///139    /// - SeeAlso: TWHDWalletGetKey140    /// - SeeAlso: TWHDWalletGetKeyDerivation141    /// - Parameter wallet: non-null TWHDWallet142    /// - Parameter coin:  a coin type143    /// - Note: Returned object needs to be deleted with \TWPrivateKeyDelete144    /// - Returns: return the default private key for the specified coin145    public func getKeyForCoin(coin: CoinType) -> PrivateKey {146        return PrivateKey(rawValue: TWHDWalletGetKeyForCoin(rawValue, TWCoinType(rawValue: coin.rawValue)))147    }148149    /// Generates the default address for the specified coin (without exposing intermediary private key), default derivation.150    ///151    /// - SeeAlso: TWHDWalletGetAddressDerivation152    /// - Parameter wallet: non-null TWHDWallet153    /// - Parameter coin:  a coin type154    /// - Returns: return the default address for the specified coin as a non-null TWString155    public func getAddressForCoin(coin: CoinType) -> String {156        return TWStringNSString(TWHDWalletGetAddressForCoin(rawValue, TWCoinType(rawValue: coin.rawValue)))157    }158159    /// Generates the default address for the specified coin and derivation (without exposing intermediary private key).160    ///161    /// - SeeAlso: TWHDWalletGetAddressForCoin162    /// - Parameter wallet: non-null TWHDWallet163    /// - Parameter coin:  a coin type164    /// - Parameter derivation:  a (custom) derivation to use165    /// - Returns: return the default address for the specified coin as a non-null TWString166    public func getAddressDerivation(coin: CoinType, derivation: Derivation) -> String {167        return TWStringNSString(TWHDWalletGetAddressDerivation(rawValue, TWCoinType(rawValue: coin.rawValue), TWDerivation(rawValue: derivation.rawValue)))168    }169170    /// Generates the private key for the specified derivation path.171    ///172    /// - SeeAlso: TWHDWalletGetKeyForCoin173    /// - SeeAlso: TWHDWalletGetKeyDerivation174    /// - Parameter wallet: non-null TWHDWallet175    /// - Parameter coin: a coin type176    /// - Parameter derivationPath:  a non-null derivation path177    /// - Note: Returned object needs to be deleted with \TWPrivateKeyDelete178    /// - Returns: The private key for the specified derivation path/coin, or null if the path is invalid179    public func getKey(coin: CoinType, derivationPath: String) -> PrivateKey? {180        let derivationPathString = TWStringCreateWithNSString(derivationPath)181        defer {182            TWStringDelete(derivationPathString)183        }184        guard let value = TWHDWalletGetKey(rawValue, TWCoinType(rawValue: coin.rawValue), derivationPathString) else {185            return nil186        }187        return PrivateKey(rawValue: value)188    }189190    /// Generates the private key for the specified derivation.191    ///192    /// - SeeAlso: TWHDWalletGetKey193    /// - SeeAlso: TWHDWalletGetKeyForCoin194    /// - Parameter wallet: non-null TWHDWallet195    /// - Parameter coin: a coin type196    /// - Parameter derivation: a (custom) derivation to use197    /// - Note: Returned object needs to be deleted with \TWPrivateKeyDelete198    /// - Returns: The private key for the specified derivation path/coin199    public func getKeyDerivation(coin: CoinType, derivation: Derivation) -> PrivateKey {200        return PrivateKey(rawValue: TWHDWalletGetKeyDerivation(rawValue, TWCoinType(rawValue: coin.rawValue), TWDerivation(rawValue: derivation.rawValue)))201    }202203    /// Generates the private key for the specified derivation path and curve.204    ///205    /// - Parameter wallet: non-null TWHDWallet206    /// - Parameter curve: a curve207    /// - Parameter derivationPath:  a non-null derivation path208    /// - Note: Returned object needs to be deleted with \TWPrivateKeyDelete209    /// - Returns: The private key for the specified derivation path/curve, or null if the path is invalid210    public func getKeyByCurve(curve: Curve, derivationPath: String) -> PrivateKey? {211        let derivationPathString = TWStringCreateWithNSString(derivationPath)212        defer {213            TWStringDelete(derivationPathString)214        }215        guard let value = TWHDWalletGetKeyByCurve(rawValue, TWCurve(rawValue: curve.rawValue), derivationPathString) else {216            return nil217        }218        return PrivateKey(rawValue: value)219    }220221    /// Shortcut method to generate private key with the specified account/change/address (bip44 standard).222    ///223    /// - SeeAlso: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki224    ///225    /// - Parameter wallet: non-null TWHDWallet226    /// - Parameter coin: a coin type227    /// - Parameter account: valid bip44 account228    /// - Parameter change: valid bip44 change229    /// - Parameter address: valid bip44 address230    /// - Note: Returned object needs to be deleted with \TWPrivateKeyDelete231    /// - Returns: The private key for the specified bip44 parameters232    public func getDerivedKey(coin: CoinType, account: UInt32, change: UInt32, address: UInt32) -> PrivateKey {233        return PrivateKey(rawValue: TWHDWalletGetDerivedKey(rawValue, TWCoinType(rawValue: coin.rawValue), account, change, address))234    }235236    /// Returns the extended private key (for default 0 account).237    ///238    /// - Parameter wallet: non-null TWHDWallet239    /// - Parameter purpose: a purpose240    /// - Parameter coin: a coin type241    /// - Parameter version: hd version242    /// - Note: Returned object needs to be deleted with \TWStringDelete243    /// - Returns:  Extended private key as a non-null TWString244    public func getExtendedPrivateKey(purpose: Purpose, coin: CoinType, version: HDVersion) -> String {245        return TWStringNSString(TWHDWalletGetExtendedPrivateKey(rawValue, TWPurpose(rawValue: purpose.rawValue), TWCoinType(rawValue: coin.rawValue), TWHDVersion(rawValue: version.rawValue)))246    }247248    /// Returns the extended public key (for default 0 account).249    ///250    /// - Parameter wallet: non-null TWHDWallet251    /// - Parameter purpose: a purpose252    /// - Parameter coin: a coin type253    /// - Parameter version: hd version254    /// - Note: Returned object needs to be deleted with \TWStringDelete255    /// - Returns:  Extended public key as a non-null TWString256    public func getExtendedPublicKey(purpose: Purpose, coin: CoinType, version: HDVersion) -> String {257        return TWStringNSString(TWHDWalletGetExtendedPublicKey(rawValue, TWPurpose(rawValue: purpose.rawValue), TWCoinType(rawValue: coin.rawValue), TWHDVersion(rawValue: version.rawValue)))258    }259260    /// Returns the extended private key, for custom account.261    ///262    /// - Parameter wallet: non-null TWHDWallet263    /// - Parameter purpose: a purpose264    /// - Parameter coin: a coin type265    /// - Parameter derivation: a derivation266    /// - Parameter version: an hd version267    /// - Parameter account: valid bip44 account268    /// - Note: Returned object needs to be deleted with \TWStringDelete269    /// - Returns:  Extended private key as a non-null TWString270    public func getExtendedPrivateKeyAccount(purpose: Purpose, coin: CoinType, derivation: Derivation, version: HDVersion, account: UInt32) -> String {271        return TWStringNSString(TWHDWalletGetExtendedPrivateKeyAccount(rawValue, TWPurpose(rawValue: purpose.rawValue), TWCoinType(rawValue: coin.rawValue), TWDerivation(rawValue: derivation.rawValue), TWHDVersion(rawValue: version.rawValue), account))272    }273274    /// Returns the extended public key, for custom account.275    ///276    /// - Parameter wallet: non-null TWHDWallet277    /// - Parameter purpose: a purpose278    /// - Parameter coin: a coin type279    /// - Parameter derivation: a derivation280    /// - Parameter version: an hd version281    /// - Parameter account: valid bip44 account282    /// - Note: Returned object needs to be deleted with \TWStringDelete283    /// - Returns: Extended public key as a non-null TWString284    public func getExtendedPublicKeyAccount(purpose: Purpose, coin: CoinType, derivation: Derivation, version: HDVersion, account: UInt32) -> String {285        return TWStringNSString(TWHDWalletGetExtendedPublicKeyAccount(rawValue, TWPurpose(rawValue: purpose.rawValue), TWCoinType(rawValue: coin.rawValue), TWDerivation(rawValue: derivation.rawValue), TWHDVersion(rawValue: version.rawValue), account))286    }287288    /// Returns the extended private key (for default 0 account with derivation).289    ///290    /// - Parameter wallet: non-null TWHDWallet291    /// - Parameter purpose: a purpose292    /// - Parameter coin: a coin type293    /// - Parameter derivation: a derivation294    /// - Parameter version: an hd version295    /// - Note: Returned object needs to be deleted with \TWStringDelete296    /// - Returns:  Extended private key as a non-null TWString297    public func getExtendedPrivateKeyDerivation(purpose: Purpose, coin: CoinType, derivation: Derivation, version: HDVersion) -> String {298        return TWStringNSString(TWHDWalletGetExtendedPrivateKeyDerivation(rawValue, TWPurpose(rawValue: purpose.rawValue), TWCoinType(rawValue: coin.rawValue), TWDerivation(rawValue: derivation.rawValue), TWHDVersion(rawValue: version.rawValue)))299    }300301    /// Returns the extended public key (for default 0 account with derivation).302    ///303    /// - Parameter wallet: non-null TWHDWallet304    /// - Parameter purpose: a purpose305    /// - Parameter coin: a coin type306    /// - Parameter derivation: a derivation307    /// - Parameter version: an hd version308    /// - Note: Returned object needs to be deleted with \TWStringDelete309    /// - Returns:  Extended public key as a non-null TWString310    public func getExtendedPublicKeyDerivation(purpose: Purpose, coin: CoinType, derivation: Derivation, version: HDVersion) -> String {311        return TWStringNSString(TWHDWalletGetExtendedPublicKeyDerivation(rawValue, TWPurpose(rawValue: purpose.rawValue), TWCoinType(rawValue: coin.rawValue), TWDerivation(rawValue: derivation.rawValue), TWHDVersion(rawValue: version.rawValue)))312    }313314}315