mirror of
https://github.com/lil-org/tokenary.git
synced 2024-12-17 17:01:32 +03:00
46 lines
1.4 KiB
Swift
46 lines
1.4 KiB
Swift
// Copyright © 2021 Encrypted Ink. All rights reserved.
|
|
// Rewrite of Wallet.swift from Trust Wallet Core.
|
|
|
|
import Foundation
|
|
import WalletCore
|
|
|
|
final class InkWallet: Hashable, Equatable {
|
|
|
|
let id: String
|
|
var key: StoredKey
|
|
|
|
var accounts: [Account] {
|
|
return (0..<key.accountCount).compactMap({ key.account(index: $0) })
|
|
}
|
|
|
|
init(id: String, key: StoredKey) {
|
|
self.id = id
|
|
self.key = key
|
|
}
|
|
|
|
func getAccount(password: String, coin: CoinType) throws -> Account {
|
|
let wallet = key.wallet(password: Data(password.utf8))
|
|
guard let account = key.accountForCoin(coin: coin, wallet: wallet) else { throw KeyStore.Error.invalidPassword }
|
|
return account
|
|
}
|
|
|
|
func getAccounts(password: String, coins: [CoinType]) throws -> [Account] {
|
|
guard let wallet = key.wallet(password: Data(password.utf8)) else { throw KeyStore.Error.invalidPassword }
|
|
return coins.compactMap({ key.accountForCoin(coin: $0, wallet: wallet) })
|
|
}
|
|
|
|
func privateKey(password: String, coin: CoinType) throws -> PrivateKey {
|
|
guard let privateKey = key.privateKey(coin: coin, password: Data(password.utf8)) else { throw KeyStore.Error.invalidPassword }
|
|
return privateKey
|
|
}
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
hasher.combine(id)
|
|
}
|
|
|
|
static func == (lhs: InkWallet, rhs: InkWallet) -> Bool {
|
|
return lhs.id == rhs.id
|
|
}
|
|
|
|
}
|