Support importing mnemonic

This commit is contained in:
Ivan Grachev 2021-07-13 21:35:50 +03:00
parent ca52a484b5
commit fe831129a2
3 changed files with 18 additions and 9 deletions

View File

@ -736,7 +736,7 @@
<constraint firstAttribute="width" constant="160" id="Pi7-Hn-MhX"/>
<constraint firstAttribute="height" constant="120" id="WsU-g6-Y2a"/>
</constraints>
<textFieldCell key="cell" controlSize="large" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="border" focusRingType="none" alignment="left" placeholderString="Ethereum Private Key" drawsBackground="YES" id="qLb-bZ-IWI">
<textFieldCell key="cell" controlSize="large" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="border" focusRingType="none" alignment="left" placeholderString="Ethereum Private Key or Secret Words" drawsBackground="YES" id="qLb-bZ-IWI">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>

View File

@ -18,7 +18,7 @@ class ImportViewController: NSViewController {
}
@IBAction func actionButtonTapped(_ sender: Any) {
let account = AccountsService.addAccount(privateKey: textField.stringValue)
let account = AccountsService.addAccount(input: textField.stringValue)
if let account = account, AccountsService.getAccounts().count == 1, let onSelectedAccount = onSelectedAccount {
onSelectedAccount(account)
} else {
@ -41,7 +41,7 @@ class ImportViewController: NSViewController {
extension ImportViewController: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
okButton.isEnabled = AccountsService.validateAccountKey(textField.stringValue)
okButton.isEnabled = AccountsService.validateAccountInput(textField.stringValue)
}
}

View File

@ -5,20 +5,29 @@ import WalletCore
struct AccountsService {
static func validateAccountKey(_ key: String) -> Bool {
if let data = Data(hexString: key) {
static func validateAccountInput(_ input: String) -> Bool {
if Mnemonic.isValid(mnemonic: input) {
return true
} else if let data = Data(hexString: input) {
return PrivateKey.isValid(data: data, curve: CoinType.ethereum.curve)
} else {
return false
}
}
static func addAccount(privateKey: String) -> Account? {
guard let data = Data(hexString: privateKey),
let key = PrivateKey(data: data) else { return nil }
static func addAccount(input: String) -> Account? {
let key: PrivateKey
if Mnemonic.isValid(mnemonic: input) {
key = HDWallet(mnemonic: input, passphrase: "").getKeyForCoin(coin: .ethereum)
} else if let data = Data(hexString: input), let privateKey = PrivateKey(data: data) {
key = privateKey
} else {
return nil
}
let address = CoinType.ethereum.deriveAddress(privateKey: key).lowercased()
// TODO: use checksum address
let account = Account(privateKey: privateKey, address: address)
let account = Account(privateKey: key.data.hexString, address: address)
var accounts = getAccounts()
guard !accounts.contains(where: { $0.address == address }) else { return nil }
accounts.append(account)