Use WalletCore in AccountsService

This commit is contained in:
Ivan Grachev 2021-07-12 21:25:36 +03:00
parent 279befddb4
commit 7e9ca15b1f

View File

@ -2,6 +2,7 @@
import Foundation
import Web3Swift
import WalletCore
struct AccountsService {
@ -11,13 +12,10 @@ struct AccountsService {
}
static func addAccount(privateKey: String) -> Account? {
guard
let addressBytes = try? EthPrivateKey(hex: privateKey).address().value()
else {
return nil
}
// TODO: checksum address
let address = addressBytes.toPrefixedHexString()
guard let data = Data(hexString: privateKey),
let key = PrivateKey(data: data) else { return nil }
let address = CoinType.ethereum.deriveAddress(privateKey: key).lowercased()
// TODO: use checksum address
let account = Account(privateKey: privateKey, address: address)
var accounts = getAccounts()
guard !accounts.contains(where: { $0.address == address }) else { return nil }