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%
1import WalletCoreC2import Foundation3// Copyright © 2017-2018 Trust.4//5// This file is part of Trust. The full Trust copyright notice, including6// terms governing use, modification, and redistribution, is contained in the7// file LICENSE at the root of the source code distribution tree.89import Foundation1011/// Coin wallet.12public final class Wallet: Hashable, Equatable {13 /// Unique wallet identifier.14 public let identifier: String1516 /// URL for the key file on disk.17 public var keyURL: URL1819 /// Encrypted wallet key20 public var key: StoredKey2122 public var accounts: [Account] {23 return (0..<key.accountCount).compactMap({ key.account(index: $0) })24 }2526 /// Creates a `Wallet` from an encrypted key.27 public init(keyURL: URL, key: StoredKey) {28 identifier = keyURL.lastPathComponent29 self.keyURL = keyURL30 self.key = key31 }3233 /// Returns the account for a specific coin.34 ///35 /// - Parameters:36 /// - password: wallet encryption password37 /// - coin: coin type38 /// - Returns: the account39 /// - Throws: `KeyStore.Error.invalidPassword` if the password is incorrect.40 public func getAccount(password: String, coin: CoinType) throws -> Account {41 let wallet = key.wallet(password: Data(password.utf8))42 guard let account = key.accountForCoin(coin: coin, wallet: wallet) else {43 throw KeyStore.Error.invalidPassword44 }45 return account46 }4748 /// Returns the account for a specific coin and derivation.49 ///50 /// - Parameters:51 /// - password: wallet encryption password52 /// - coin: coin type53 /// - derivation: derivation, a specific or default54 /// - Returns: the account55 /// - Throws: `KeyStore.Error.invalidPassword` if the password is incorrect.56 public func getAccount(password: String, coin: CoinType, derivation: Derivation) throws -> Account {57 let wallet = key.wallet(password: Data(password.utf8))58 guard let account = key.accountForCoinDerivation(coin: coin, derivation: derivation, wallet: wallet) else {59 throw KeyStore.Error.invalidPassword60 }61 return account62 }6364 /// Returns the accounts for a specific coins.65 ///66 /// - Parameters:67 /// - password: wallet encryption password68 /// - coins: coins to add accounts for69 /// - Returns: the added accounts70 /// - Throws: `KeyStore.Error.invalidPassword` if the password is incorrect.71 public func getAccounts(password: String, coins: [CoinType]) throws -> [Account] {72 guard let wallet = key.wallet(password: Data(password.utf8)) else {73 throw KeyStore.Error.invalidPassword74 }75 return coins.compactMap({ key.accountForCoin(coin: $0, wallet: wallet) })76 }7778 /// Returns the private key for a specific coin.79 ///80 /// - Parameters:81 /// - password: wallet encryption password82 /// - coin: coin type83 /// - Returns: the private key84 /// - Throws: `KeyStore.Error.invalidPassword` if the password is incorrect.85 public func privateKey(password: String, coin: CoinType) throws -> PrivateKey {86 guard let pk = key.privateKey(coin: coin, password: Data(password.utf8)) else {87 throw KeyStore.Error.invalidPassword88 }89 return pk90 }9192 public func hash(into hasher: inout Hasher) {93 hasher.combine(identifier)94 }9596 public static func == (lhs: Wallet, rhs: Wallet) -> Bool {97 return lhs.identifier == rhs.identifier98 }99}100101/// Support account types.102public enum WalletType {103 case encryptedKey104 case hierarchicalDeterministicWallet105}106107public enum WalletError: LocalizedError {108 case invalidKeyType109}110