mirror of
https://github.com/lil-org/tokenary.git
synced 2024-12-13 17:44:07 +03:00
Support importing mnemonic
This commit is contained in:
parent
ca52a484b5
commit
fe831129a2
@ -736,7 +736,7 @@
|
|||||||
<constraint firstAttribute="width" constant="160" id="Pi7-Hn-MhX"/>
|
<constraint firstAttribute="width" constant="160" id="Pi7-Hn-MhX"/>
|
||||||
<constraint firstAttribute="height" constant="120" id="WsU-g6-Y2a"/>
|
<constraint firstAttribute="height" constant="120" id="WsU-g6-Y2a"/>
|
||||||
</constraints>
|
</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"/>
|
<font key="font" metaFont="system"/>
|
||||||
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
|
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
|
||||||
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
|
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
|
||||||
|
@ -18,7 +18,7 @@ class ImportViewController: NSViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func actionButtonTapped(_ sender: Any) {
|
@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 {
|
if let account = account, AccountsService.getAccounts().count == 1, let onSelectedAccount = onSelectedAccount {
|
||||||
onSelectedAccount(account)
|
onSelectedAccount(account)
|
||||||
} else {
|
} else {
|
||||||
@ -41,7 +41,7 @@ class ImportViewController: NSViewController {
|
|||||||
extension ImportViewController: NSTextFieldDelegate {
|
extension ImportViewController: NSTextFieldDelegate {
|
||||||
|
|
||||||
func controlTextDidChange(_ obj: Notification) {
|
func controlTextDidChange(_ obj: Notification) {
|
||||||
okButton.isEnabled = AccountsService.validateAccountKey(textField.stringValue)
|
okButton.isEnabled = AccountsService.validateAccountInput(textField.stringValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,20 +5,29 @@ import WalletCore
|
|||||||
|
|
||||||
struct AccountsService {
|
struct AccountsService {
|
||||||
|
|
||||||
static func validateAccountKey(_ key: String) -> Bool {
|
static func validateAccountInput(_ input: String) -> Bool {
|
||||||
if let data = Data(hexString: key) {
|
if Mnemonic.isValid(mnemonic: input) {
|
||||||
|
return true
|
||||||
|
} else if let data = Data(hexString: input) {
|
||||||
return PrivateKey.isValid(data: data, curve: CoinType.ethereum.curve)
|
return PrivateKey.isValid(data: data, curve: CoinType.ethereum.curve)
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func addAccount(privateKey: String) -> Account? {
|
static func addAccount(input: String) -> Account? {
|
||||||
guard let data = Data(hexString: privateKey),
|
let key: PrivateKey
|
||||||
let key = PrivateKey(data: data) else { return nil }
|
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()
|
let address = CoinType.ethereum.deriveAddress(privateKey: key).lowercased()
|
||||||
// TODO: use checksum address
|
// TODO: use checksum address
|
||||||
let account = Account(privateKey: privateKey, address: address)
|
let account = Account(privateKey: key.data.hexString, address: address)
|
||||||
var accounts = getAccounts()
|
var accounts = getAccounts()
|
||||||
guard !accounts.contains(where: { $0.address == address }) else { return nil }
|
guard !accounts.contains(where: { $0.address == address }) else { return nil }
|
||||||
accounts.append(account)
|
accounts.append(account)
|
||||||
|
Loading…
Reference in New Issue
Block a user